Save method

This commit is contained in:
Eden Kirin
2024-06-26 01:55:33 +02:00
parent 3dc8d0d79f
commit bdc978aec1
6 changed files with 126 additions and 18 deletions

View File

@ -33,9 +33,9 @@ func NewMockDB() (*sql.DB, *gorm.DB, sqlmock.Sqlmock) {
}
type MyModel struct {
Id uuid.UUID
Id *uuid.UUID `gorm:"type(uuid);unique"`
Value string
Count int
Cnt int
}
func (m MyModel) TableName() string {
@ -46,7 +46,7 @@ type MyModelFilter struct {
Id *uuid.UUID `filterfield:"field=id,operator=EQ"`
Ids *[]uuid.UUID `filterfield:"field=id,operator=IN"`
Value *string `filterfield:"field=value,operator=EQ"`
Count *int `filterfield:"field=count,operator=GT"`
Cnt *int `filterfield:"field=cnt,operator=GT"`
}
func TestListMethod(t *testing.T) {
@ -65,13 +65,13 @@ func TestListMethod(t *testing.T) {
Direction: OrderASC,
},
{
Field: "count",
Field: "cnt",
Direction: OrderDESC,
},
},
}
sql := "SELECT * FROM my_models ORDER BY id,count DESC"
sql := "SELECT * FROM my_models ORDER BY id,cnt DESC"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
_, err := repo.List(filter, &options)
@ -198,10 +198,10 @@ func TestListMethod(t *testing.T) {
filter := MyModelFilter{
Id: &id,
Value: &value,
Count: &count,
Cnt: &count,
}
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.count > $3"
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.cnt > $3"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(id, value, count)
@ -226,7 +226,7 @@ func TestListMethod(t *testing.T) {
filter := MyModelFilter{
Id: &id,
Value: &value,
Count: &count,
Cnt: &count,
}
options := ListOptions{
Pagination: &Pagination{
@ -235,7 +235,7 @@ func TestListMethod(t *testing.T) {
},
}
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.count > $3 LIMIT $4 OFFSET $5"
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.cnt > $3 LIMIT $4 OFFSET $5"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(id, value, count, options.Pagination.Limit, options.Pagination.Offset)
@ -258,11 +258,11 @@ func TestListMethod(t *testing.T) {
options := ListOptions{
Only: &[]string{
"id",
"count",
"cnt",
},
}
sql := "SELECT id,count FROM my_models"
sql := "SELECT id,cnt FROM my_models"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
_, err := repo.List(filter, &options)