Tests
This commit is contained in:
67
app/repository/method_get_test.go
Normal file
67
app/repository/method_get_test.go
Normal file
@ -0,0 +1,67 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetMethod(t *testing.T) {
|
||||
t.Run("With id filter, suppress errors", func(t *testing.T) {
|
||||
sqldb, db, mock := NewMockDB()
|
||||
defer sqldb.Close()
|
||||
|
||||
repo := RepoBase[MyModel]{}
|
||||
repo.Init(db)
|
||||
|
||||
id := uuid.New()
|
||||
filter := MyModelFilter{
|
||||
Id: &id,
|
||||
}
|
||||
options := GetOptions{}
|
||||
|
||||
sql := "SELECT * FROM my_models WHERE my_models.id = $1 ORDER BY my_models.id LIMIT $2"
|
||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).WithArgs(id, 1)
|
||||
|
||||
result, err := repo.Get(filter, &options)
|
||||
assert.Nil(t, result)
|
||||
assert.Nil(t, err)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("With id filter, raise error", func(t *testing.T) {
|
||||
sqldb, db, mock := NewMockDB()
|
||||
defer sqldb.Close()
|
||||
|
||||
repo := RepoBase[MyModel]{}
|
||||
repo.Init(db)
|
||||
|
||||
id := uuid.New()
|
||||
raiseError := true
|
||||
filter := MyModelFilter{
|
||||
Id: &id,
|
||||
}
|
||||
options := GetOptions{
|
||||
RaiseError: &raiseError,
|
||||
}
|
||||
|
||||
sql := "SELECT * FROM my_models WHERE my_models.id = $1 ORDER BY my_models.id LIMIT $2"
|
||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||
WithArgs(id, 1).
|
||||
WillReturnError(nil)
|
||||
|
||||
result, err := repo.Get(filter, &options)
|
||||
assert.Nil(t, result)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user