Only option

This commit is contained in:
Eden Kirin
2024-06-24 01:08:39 +02:00
parent 295e915f89
commit bd510d958b
5 changed files with 147 additions and 82 deletions

View File

@ -56,21 +56,23 @@ func TestListMethod(t *testing.T) {
repo.Init(db)
filter := MyModelFilter{}
ordering := []Order{
{
Field: "id",
Direction: OrderASC,
},
{
Field: "count",
Direction: OrderDESC,
options := ListOptions{
Ordering: &[]Order{
{
Field: "id",
Direction: OrderASC,
},
{
Field: "count",
Direction: OrderDESC,
},
},
}
sql := "SELECT * FROM my_models ORDER BY id,count DESC"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
_, err := repo.List(filter, &ordering, nil)
_, err := repo.List(filter, &options)
assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil {
@ -86,16 +88,17 @@ func TestListMethod(t *testing.T) {
repo.Init(db)
filter := MyModelFilter{}
pagination := Pagination{
Limit: 111,
Offset: 0,
options := ListOptions{
Pagination: &Pagination{
Limit: 111,
},
}
sql := "SELECT * FROM my_models LIMIT $1"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(pagination.Limit)
WithArgs(options.Pagination.Limit)
_, err := repo.List(filter, nil, &pagination)
_, err := repo.List(filter, &options)
assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil {
@ -111,16 +114,17 @@ func TestListMethod(t *testing.T) {
repo.Init(db)
filter := MyModelFilter{}
pagination := Pagination{
Limit: 0,
Offset: 222,
options := ListOptions{
Pagination: &Pagination{
Offset: 222,
},
}
sql := "SELECT * FROM my_models OFFSET $1"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(pagination.Offset)
WithArgs(options.Pagination.Offset)
_, err := repo.List(filter, nil, &pagination)
_, err := repo.List(filter, &options)
assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil {
@ -136,16 +140,18 @@ func TestListMethod(t *testing.T) {
repo.Init(db)
filter := MyModelFilter{}
pagination := Pagination{
Limit: 111,
Offset: 222,
options := ListOptions{
Pagination: &Pagination{
Limit: 111,
Offset: 222,
},
}
sql := "SELECT * FROM my_models LIMIT $1 OFFSET $2"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(pagination.Limit, pagination.Offset)
WithArgs(options.Pagination.Limit, options.Pagination.Offset)
_, err := repo.List(filter, nil, &pagination)
_, err := repo.List(filter, &options)
assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil {
@ -169,7 +175,7 @@ func TestListMethod(t *testing.T) {
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(id)
_, err := repo.List(filter, nil, nil)
_, err := repo.List(filter, nil)
assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil {
@ -197,7 +203,7 @@ func TestListMethod(t *testing.T) {
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(id, value, count)
_, err := repo.List(filter, nil, nil)
_, err := repo.List(filter, nil)
assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil {
@ -220,16 +226,44 @@ func TestListMethod(t *testing.T) {
Value: &value,
Count: &count,
}
pagination := Pagination{
Offset: 111,
Limit: 222,
options := ListOptions{
Pagination: &Pagination{
Offset: 111,
Limit: 222,
},
}
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.count > $3 LIMIT $4 OFFSET $5"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
WithArgs(id, value, count, pagination.Limit, pagination.Offset)
WithArgs(id, value, count, options.Pagination.Limit, options.Pagination.Offset)
_, err := repo.List(filter, nil, &pagination)
_, err := repo.List(filter, &options)
assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
})
t.Run("Only id and count", func(t *testing.T) {
sqldb, db, mock := NewMockDB()
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
filter := MyModelFilter{}
options := ListOptions{
Only: &[]string{
"id",
"count",
},
}
sql := "SELECT id,count FROM my_models"
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
_, err := repo.List(filter, &options)
assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil {