Skip to content

Commit

Permalink
Added Unscoped Delete functionality; Test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Kayastha committed Aug 19, 2018
1 parent fe93da8 commit fdee4c1
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 65 deletions.
44 changes: 26 additions & 18 deletions examples/comparison/gorm4/autogenerated_gorm4.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 32 additions & 8 deletions queryset/methods/queryset.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,26 +270,50 @@ func NewDeleteMethod(qsTypeName, structTypeName string) DeleteMethod {
}
}

// DeleteNumMethod creates Delete Num method
// DeleteNumMethod creates DeleteNum method
type DeleteNumMethod struct {
namedMethod
baseQuerySetMethod

noArgsMethod
constBodyMethod
constRetMethod
}

// NewDeleteNumMethod delete row count
func NewDeleteNumMethod(qsTypeName, structTypeName string) DeleteNumMethod {
return DeleteNumMethod{
namedMethod: newNamedMethod("DeleteNum"),
baseQuerySetMethod: newBaseQuerySetMethod(qsTypeName),
constRetMethod: newConstRetMethod("(int64, error)"),
constBodyMethod: newConstBodyMethod(
strings.Join([]string{
"db := qs.db.Delete(" + structTypeName + "{}" + ")",
"return db.RowsAffected, db.Error",
}, "\n"),
),
}
}

// DeleteNumUnscopedMethod creates DeleteNumUnscoped method for performing hard deletes
type DeleteNumUnscopedMethod struct {
namedMethod
baseQuerySetMethod

dbArgMethod
noArgsMethod
constBodyMethod
constRetMethod
}

// NewDeleteNumMethod delete row count
func NewDeleteNumMethod(qsTypeName, structTypeName string) DeleteNumMethod {
return DeleteNumMethod{
namedMethod: newNamedMethod("DeleteNum"),
dbArgMethod: newDbArgMethod(),
// NewDeleteNumUnscopedMethod delete row count for hard deletes
func NewDeleteNumUnscopedMethod(qsTypeName, structTypeName string) DeleteNumUnscopedMethod {
return DeleteNumUnscopedMethod{
namedMethod: newNamedMethod("DeleteNumUnscoped"),
baseQuerySetMethod: newBaseQuerySetMethod(qsTypeName),
constRetMethod: newConstRetMethod("(int64, error)"),
constBodyMethod: newConstBodyMethod(
strings.Join([]string{
"db = qs.db.Delete(" + structTypeName + "{}" + ")",
"db := qs.db.Unscoped().Delete(" + structTypeName + "{}" + ")",
"return db.RowsAffected, db.Error",
}, "\n"),
),
Expand Down
3 changes: 2 additions & 1 deletion queryset/methodsbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ func (b *methodsBuilder) buildCRUDMethods() *methodsBuilder {
methods.NewDeleteMethod(b.qsTypeName(), b.s.TypeName),
methods.NewStructModifierMethod("Create", b.s.TypeName),
methods.NewStructModifierMethod("Delete", b.s.TypeName),
methods.NewDeleteNumMethod(b.qsTypeName(), b.s.TypeName))
methods.NewDeleteNumMethod(b.qsTypeName(), b.s.TypeName),
methods.NewDeleteNumUnscopedMethod(b.qsTypeName(), b.s.TypeName))
return b
}

Expand Down
32 changes: 32 additions & 0 deletions queryset/queryset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ func TestQueries(t *testing.T) {
testUserQueryFilters,
testUsersCount,
testUsersUpdateNum,
testUsersDeleteNum,
testUsersDeleteNumUnscoped,
}
for _, f := range funcs {
f := f // save range var
Expand Down Expand Up @@ -328,6 +330,36 @@ func testUserDeleteByPK(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
assert.Nil(t, u.Delete(db))
}

func testUsersDeleteNum(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
usersNum := 2
users := getTestUsers(usersNum)
req := "UPDATE `users` SET deleted_at=? WHERE `users`.deleted_at IS NULL AND ((email IN (?,?)))"
m.ExpectExec(fixedFullRe(req)).
WithArgs(sqlmock.AnyArg(), users[0].Email, users[1].Email).
WillReturnResult(sqlmock.NewResult(0, int64(usersNum)))

num, err := test.NewUserQuerySet(db).
EmailIn(users[0].Email, users[1].Email).
DeleteNum()
assert.Nil(t, err)
assert.Equal(t, int64(usersNum), num)
}

func testUsersDeleteNumUnscoped(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
usersNum := 2
users := getTestUsers(usersNum)
req := "DELETE FROM `users` WHERE (email IN (?,?))"
m.ExpectExec(fixedFullRe(req)).
WithArgs(users[0].Email, users[1].Email).
WillReturnResult(sqlmock.NewResult(0, int64(usersNum)))

num, err := test.NewUserQuerySet(db).
EmailIn(users[0].Email, users[1].Email).
DeleteNumUnscoped()
assert.Nil(t, err)
assert.Equal(t, int64(usersNum), num)
}

func testUsersUpdateNum(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
usersNum := 2
users := getTestUsers(usersNum)
Expand Down
Loading

0 comments on commit fdee4c1

Please sign in to comment.