Tests
This commit is contained in:
73
app/repository/method_delete_test.go
Normal file
73
app/repository/method_delete_test.go
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDeleteMethod(t *testing.T) {
|
||||||
|
t.Run("With filter", func(t *testing.T) {
|
||||||
|
sqldb, db, mock := NewMockDB()
|
||||||
|
defer sqldb.Close()
|
||||||
|
|
||||||
|
repo := RepoBase[MyModel]{}
|
||||||
|
repo.Init(db, nil)
|
||||||
|
|
||||||
|
id := uuid.New()
|
||||||
|
filter := MyModelFilter{
|
||||||
|
Id: &id,
|
||||||
|
}
|
||||||
|
|
||||||
|
sql := "DELETE FROM my_models WHERE my_models.id = $1"
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||||
|
WithArgs(id).
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 111))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
|
||||||
|
deleted, err := repo.Delete(filter)
|
||||||
|
assert.Equal(t, int64(111), deleted)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("With multiple filters", func(t *testing.T) {
|
||||||
|
sqldb, db, mock := NewMockDB()
|
||||||
|
defer sqldb.Close()
|
||||||
|
|
||||||
|
repo := RepoBase[MyModel]{}
|
||||||
|
repo.Init(db, nil)
|
||||||
|
|
||||||
|
id1 := uuid.New()
|
||||||
|
id2 := uuid.New()
|
||||||
|
id3 := uuid.New()
|
||||||
|
cnt := 3456
|
||||||
|
filter := MyModelFilter{
|
||||||
|
Ids: &[]uuid.UUID{id1, id2, id3},
|
||||||
|
CntGT: &cnt,
|
||||||
|
}
|
||||||
|
|
||||||
|
sql := "DELETE FROM my_models WHERE my_models.id IN ($1,$2,$3) AND my_models.cnt > $4"
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||||
|
WithArgs(id1, id2, id3, cnt).
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 123))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
|
||||||
|
deleted, err := repo.Delete(filter)
|
||||||
|
assert.Equal(t, int64(123), deleted)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -46,7 +46,7 @@ type MyModelFilter struct {
|
|||||||
Id *uuid.UUID `filterfield:"field=id,operator=EQ"`
|
Id *uuid.UUID `filterfield:"field=id,operator=EQ"`
|
||||||
Ids *[]uuid.UUID `filterfield:"field=id,operator=IN"`
|
Ids *[]uuid.UUID `filterfield:"field=id,operator=IN"`
|
||||||
Value *string `filterfield:"field=value,operator=EQ"`
|
Value *string `filterfield:"field=value,operator=EQ"`
|
||||||
Cnt *int `filterfield:"field=cnt,operator=GT"`
|
CntGT *int `filterfield:"field=cnt,operator=GT"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestListMethod(t *testing.T) {
|
func TestListMethod(t *testing.T) {
|
||||||
@ -198,7 +198,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
filter := MyModelFilter{
|
filter := MyModelFilter{
|
||||||
Id: &id,
|
Id: &id,
|
||||||
Value: &value,
|
Value: &value,
|
||||||
Cnt: &count,
|
CntGT: &count,
|
||||||
}
|
}
|
||||||
|
|
||||||
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.cnt > $3"
|
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.cnt > $3"
|
||||||
@ -226,7 +226,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
filter := MyModelFilter{
|
filter := MyModelFilter{
|
||||||
Id: &id,
|
Id: &id,
|
||||||
Value: &value,
|
Value: &value,
|
||||||
Cnt: &count,
|
CntGT: &count,
|
||||||
}
|
}
|
||||||
options := ListOptions{
|
options := ListOptions{
|
||||||
Pagination: &Pagination{
|
Pagination: &Pagination{
|
||||||
|
|||||||
Reference in New Issue
Block a user