Compare commits
2 Commits
43824af97c
...
30714bb3da
| Author | SHA1 | Date | |
|---|---|---|---|
| 30714bb3da | |||
| 8a81173fc8 |
22
app/main.go
22
app/main.go
@ -31,6 +31,10 @@ type CertFilter struct {
|
|||||||
Timestamps *[]time.Time `filterfield:"field=created_at,operator=IN"`
|
Timestamps *[]time.Time `filterfield:"field=created_at,operator=IN"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CompanyFilter struct {
|
||||||
|
IsActive *bool `filterfield:"field=is_active,operator=EQ"`
|
||||||
|
}
|
||||||
|
|
||||||
func doMagic(db *gorm.DB) {
|
func doMagic(db *gorm.DB) {
|
||||||
var err error
|
var err error
|
||||||
query := db
|
query := db
|
||||||
@ -140,6 +144,21 @@ func doSave(db *gorm.DB) {
|
|||||||
fmt.Printf("New company id: %s\n", company.Id.String())
|
fmt.Printf("New company id: %s\n", company.Id.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func doDelete(db *gorm.DB) {
|
||||||
|
repo := repository.RepoBase[models.Company]{}
|
||||||
|
repo.Init(db, nil)
|
||||||
|
|
||||||
|
filter := CompanyFilter{
|
||||||
|
IsActive: &FALSE,
|
||||||
|
}
|
||||||
|
|
||||||
|
deletedCount, err := repo.Delete(filter)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Printf(">> DELETED: %d\n", deletedCount)
|
||||||
|
}
|
||||||
|
|
||||||
func doExists(db *gorm.DB) {
|
func doExists(db *gorm.DB) {
|
||||||
repo := repository.RepoBase[models.Cert]{}
|
repo := repository.RepoBase[models.Cert]{}
|
||||||
repo.Init(db, nil)
|
repo.Init(db, nil)
|
||||||
@ -167,7 +186,8 @@ func main() {
|
|||||||
// doMagic(db)
|
// doMagic(db)
|
||||||
// doList(db)
|
// doList(db)
|
||||||
// doCount(db)
|
// doCount(db)
|
||||||
doSave(db)
|
// doSave(db)
|
||||||
|
doDelete(db)
|
||||||
// doGet(db)
|
// doGet(db)
|
||||||
// doExists(db)
|
// doExists(db)
|
||||||
// inheritance.DoInheritanceTest()
|
// inheritance.DoInheritanceTest()
|
||||||
|
|||||||
@ -14,7 +14,7 @@ func (m *UpdateMethod[T]) Init(repo *RepoBase[T]) {
|
|||||||
m.repo = repo
|
m.repo = repo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m UpdateMethod[T]) Update(filter interface{}, values map[string]interface{}) (int64, error) {
|
func (m UpdateMethod[T]) Update(filter interface{}, values map[string]any) (int64, error) {
|
||||||
var (
|
var (
|
||||||
model T
|
model T
|
||||||
)
|
)
|
||||||
@ -23,7 +23,7 @@ func (m UpdateMethod[T]) Update(filter interface{}, values map[string]interface{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
result := query.Updates(values)
|
result := query.Model(&model).Updates(values)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return 0, result.Error
|
return 0, result.Error
|
||||||
}
|
}
|
||||||
|
|||||||
43
app/repository/method_update_test.go
Normal file
43
app/repository/method_update_test.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUpdateMethod(t *testing.T) {
|
||||||
|
t.Run("Update", func(t *testing.T) {
|
||||||
|
sqldb, db, mock := NewMockDB()
|
||||||
|
defer sqldb.Close()
|
||||||
|
|
||||||
|
repo := RepoBase[MyModel]{}
|
||||||
|
repo.Init(db, nil)
|
||||||
|
|
||||||
|
cnt := 10
|
||||||
|
filter := MyModelFilter{
|
||||||
|
CntGT: &cnt,
|
||||||
|
}
|
||||||
|
values := map[string]any{
|
||||||
|
"cnt": 111,
|
||||||
|
"value": 222,
|
||||||
|
}
|
||||||
|
|
||||||
|
sql := "UPDATE my_models SET cnt=$1,value=$2 WHERE my_models.cnt > $3"
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||||
|
WithArgs(values["cnt"], values["value"], cnt).
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
|
||||||
|
_, err := repo.Update(filter, values)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user