mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-15 04:18:01 +00:00
9c3e5e467b
# Which Problems Are Solved Queries currently execute 3 statements, begin, query, commit # How the Problems Are Solved remove transaction handling from query methods in database package # Additional Changes - Bump versions of `core_grpc_dependencies`-receipt in Makefile # Additional info During load tests we saw a lot of idle transactions of `zitadel_queries` application name which is the connection pool used to query data in zitadel. Executed query: `select query_start - xact_start, pid, application_name, backend_start, xact_start, query_start, state_change, wait_event_type, wait_event,substring(query, 1, 200) query from pg_stat_activity where datname = 'zitadel' and state <> 'idle';` Mostly the last query executed was `begin isolation level read committed read only`. example: ``` ?column? | pid | application_name | backend_start | xact_start | query_start | state_change | wait_event_type | wait_event | query -----------------+-------+----------------------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 00:00:00 | 33030 | zitadel_queries | 2024-10-16 16:25:53.906036+00 | 2024-10-16 16:30:19.191661+00 | 2024-10-16 16:30:19.191661+00 | 2024-10-16 16:30:19.19169+00 | Client | ClientRead | begin isolation level read committed read only 00:00:00 | 33035 | zitadel_queries | 2024-10-16 16:25:53.909629+00 | 2024-10-16 16:30:19.19179+00 | 2024-10-16 16:30:19.19179+00 | 2024-10-16 16:30:19.191805+00 | Client | ClientRead | begin isolation level read committed read only 00:00:00.00412 | 33028 | zitadel_queries | 2024-10-16 16:25:53.904247+00 | 2024-10-16 16:30:19.187734+00 | 2024-10-16 16:30:19.191854+00 | 2024-10-16 16:30:19.191964+00 | Client | ClientRead | SELECT created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type 00:00:00.084662 | 33134 | zitadel_es_pusher | 2024-10-16 16:29:54.979692+00 | 2024-10-16 16:30:19.178578+00 | 2024-10-16 16:30:19.26324+00 | 2024-10-16 16:30:19.263267+00 | Client | ClientRead | RELEASE SAVEPOINT cockroach_restart 00:00:00.084768 | 33139 | zitadel_es_pusher | 2024-10-16 16:29:54.979585+00 | 2024-10-16 16:30:19.180762+00 | 2024-10-16 16:30:19.26553+00 | 2024-10-16 16:30:19.265531+00 | LWLock | WALWriteLock | commit 00:00:00.077377 | 33136 | zitadel_es_pusher | 2024-10-16 16:29:54.978582+00 | 2024-10-16 16:30:19.187883+00 | 2024-10-16 16:30:19.26526+00 | 2024-10-16 16:30:19.265431+00 | Client | ClientRead | WITH existing AS ( + | | | | | | | | | (SELECT instance_id, aggregate_type, aggregate_id, "sequence" FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type = $2 AND aggregate_id = $3 ORDER BY "sequence" DE 00:00:00.012309 | 33123 | zitadel_es_pusher | 2024-10-16 16:29:54.963484+00 | 2024-10-16 16:30:19.175066+00 | 2024-10-16 16:30:19.187375+00 | 2024-10-16 16:30:19.187376+00 | IO | WalSync | commit 00:00:00 | 33034 | zitadel_queries | 2024-10-16 16:25:53.90791+00 | 2024-10-16 16:30:19.262921+00 | 2024-10-16 16:30:19.262921+00 | 2024-10-16 16:30:19.263133+00 | Client | ClientRead | begin isolation level read committed read only 00:00:00 | 33039 | zitadel_queries | 2024-10-16 16:25:53.914106+00 | 2024-10-16 16:30:19.191676+00 | 2024-10-16 16:30:19.191676+00 | 2024-10-16 16:30:19.191687+00 | Client | ClientRead | begin isolation level read committed read only 00:00:00.24539 | 33083 | zitadel_projection_spooler | 2024-10-16 16:27:49.895548+00 | 2024-10-16 16:30:19.020058+00 | 2024-10-16 16:30:19.265448+00 | 2024-10-16 16:30:19.26546+00 | Client | ClientRead | SAVEPOINT exec_stmt 00:00:00 | 33125 | zitadel_es_pusher | 2024-10-16 16:29:54.963859+00 | 2024-10-16 16:30:19.191715+00 | 2024-10-16 16:30:19.191715+00 | 2024-10-16 16:30:19.191729+00 | Client | ClientRead | begin 00:00:00.004292 | 33032 | zitadel_queries | 2024-10-16 16:25:53.906624+00 | 2024-10-16 16:30:19.187713+00 | 2024-10-16 16:30:19.192005+00 | 2024-10-16 16:30:19.192062+00 | Client | ClientRead | SELECT created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type 00:00:00 | 33031 | zitadel_queries | 2024-10-16 16:25:53.906422+00 | 2024-10-16 16:30:19.191625+00 | 2024-10-16 16:30:19.191625+00 | 2024-10-16 16:30:19.191645+00 | Client | ClientRead | begin isolation level read committed read only ``` The amount of idle transactions is significantly less if the query transactions are removed: example: ``` ?column? | pid | application_name | backend_start | xact_start | query_start | state_change | wait_event_type | wait_event | query -----------------+-------+----------------------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 00:00:00.000094 | 32741 | zitadel_queries | 2024-10-16 16:23:49.73935+00 | 2024-10-16 16:24:59.785589+00 | 2024-10-16 16:24:59.785683+00 | 2024-10-16 16:24:59.785684+00 | | | SELECT created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type 00:00:00 | 32762 | zitadel_es_pusher | 2024-10-16 16:24:02.275136+00 | 2024-10-16 16:24:59.784586+00 | 2024-10-16 16:24:59.784586+00 | 2024-10-16 16:24:59.784607+00 | Client | ClientRead | begin 00:00:00.000167 | 32742 | zitadel_queries | 2024-10-16 16:23:49.740489+00 | 2024-10-16 16:24:59.784274+00 | 2024-10-16 16:24:59.784441+00 | 2024-10-16 16:24:59.784442+00 | | | with usr as ( + | | | | | | | | | select u.id, u.creation_date, u.change_date, u.sequence, u.state, u.resource_owner, u.username, n.login_name as preferred_login_name + | | | | | | | | | from projections.users13 u + | | | | | | | | | left join projections.l 00:00:00.256014 | 32759 | zitadel_projection_spooler | 2024-10-16 16:24:01.418429+00 | 2024-10-16 16:24:59.52959+00 | 2024-10-16 16:24:59.785604+00 | 2024-10-16 16:24:59.785649+00 | Client | ClientRead | UPDATE projections.milestones SET reached_date = $1 WHERE (instance_id = $2) AND (type = $3) AND (reached_date IS NULL) 00:00:00.014199 | 32773 | zitadel_es_pusher | 2024-10-16 16:24:02.320404+00 | 2024-10-16 16:24:59.769509+00 | 2024-10-16 16:24:59.783708+00 | 2024-10-16 16:24:59.783709+00 | IO | WalSync | commit 00:00:00 | 32765 | zitadel_es_pusher | 2024-10-16 16:24:02.28173+00 | 2024-10-16 16:24:59.780413+00 | 2024-10-16 16:24:59.780413+00 | 2024-10-16 16:24:59.780426+00 | Client | ClientRead | begin 00:00:00.012729 | 32777 | zitadel_es_pusher | 2024-10-16 16:24:02.339737+00 | 2024-10-16 16:24:59.767432+00 | 2024-10-16 16:24:59.780161+00 | 2024-10-16 16:24:59.780195+00 | Client | ClientRead | RELEASE SAVEPOINT cockroach_restart ``` --------- Co-authored-by: Tim Möhlmann <tim+github@zitadel.com> Co-authored-by: Livio Spring <livio.a@gmail.com> Co-authored-by: Max Peintner <max@caos.ch> Co-authored-by: Elio Bischof <elio@zitadel.com> Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com> Co-authored-by: Miguel Cabrerizo <30386061+doncicuto@users.noreply.github.com> Co-authored-by: Joakim Lodén <Loddan@users.noreply.github.com> Co-authored-by: Yxnt <Yxnt@users.noreply.github.com> Co-authored-by: Stefan Benz <stefan@caos.ch> Co-authored-by: Harsha Reddy <harsha.reddy@klaviyo.com> Co-authored-by: Zach H <zhirschtritt@gmail.com>
424 lines
12 KiB
Go
424 lines
12 KiB
Go
package repository
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"fmt"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"github.com/jinzhu/gorm"
|
|
|
|
db_mock "github.com/zitadel/zitadel/internal/database/mock"
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
)
|
|
|
|
var (
|
|
expectedGetByID = `SELECT \* FROM "%s" WHERE \(%s = \$1\) LIMIT 1`
|
|
expectedGetByQuery = `SELECT \* FROM "%s" WHERE \(LOWER\(%s\) %s LOWER\(\$1\)\) LIMIT 1`
|
|
expectedGetByQueryCaseSensitive = `SELECT \* FROM "%s" WHERE \(%s %s \$1\) LIMIT 1`
|
|
expectedSave = `UPDATE "%s" SET "test" = \$1 WHERE "%s"."%s" = \$2`
|
|
expectedRemove = `DELETE FROM "%s" WHERE \(%s = \$1\)`
|
|
expectedRemoveByKeys = func(i int, table string) string {
|
|
sql := fmt.Sprintf(`DELETE FROM "%s"`, table)
|
|
sql += ` WHERE \(%s = \$1\)`
|
|
for j := 1; j < i; j++ {
|
|
sql = sql + ` AND \(%s = \$` + strconv.Itoa(j+1) + `\)`
|
|
}
|
|
return sql
|
|
}
|
|
expectedRemoveByObject = `DELETE FROM "%s" WHERE "%s"."%s" = \$1`
|
|
expectedRemoveByObjectMultiplePK = `DELETE FROM "%s" WHERE "%s"."%s" = \$1 AND "%s"."%s" = \$2`
|
|
expectedTruncate = `TRUNCATE %s;`
|
|
expectedSearch = `SELECT \* FROM "%s" OFFSET 0`
|
|
expectedSearchCount = `SELECT count\(\*\) FROM "%s"`
|
|
expectedSearchLimit = `SELECT \* FROM "%s" LIMIT %v OFFSET 0`
|
|
expectedSearchLimitCount = `SELECT count\(\*\) FROM "%s"`
|
|
expectedSearchOffset = `SELECT \* FROM "%s" OFFSET %v`
|
|
expectedSearchOffsetCount = `SELECT count\(\*\) FROM "%s"`
|
|
expectedSearchSorting = `SELECT \* FROM "%s" ORDER BY %s %s OFFSET 0`
|
|
expectedSearchSortingCount = `SELECT count\(\*\) FROM "%s"`
|
|
expectedSearchQuery = `SELECT \* FROM "%s" WHERE \(LOWER\(%s\) %s LOWER\(\$1\)\) OFFSET 0`
|
|
expectedSearchQueryCount = `SELECT count\(\*\) FROM "%s" WHERE \(LOWER\(%s\) %s LOWER\(\$1\)\)`
|
|
expectedSearchQueryAllParams = `SELECT \* FROM "%s" WHERE \(LOWER\(%s\) %s LOWER\(\$1\)\) ORDER BY %s %s LIMIT %v OFFSET %v`
|
|
expectedSearchQueryAllParamCount = `SELECT count\(\*\) FROM "%s" WHERE \(LOWER\(%s\) %s LOWER\(\$1\)\)`
|
|
)
|
|
|
|
type TestSearchRequest struct {
|
|
limit uint64
|
|
offset uint64
|
|
sortingColumn ColumnKey
|
|
asc bool
|
|
queries []SearchQuery
|
|
}
|
|
|
|
func (req TestSearchRequest) GetLimit() uint64 {
|
|
return req.limit
|
|
}
|
|
|
|
func (req TestSearchRequest) GetOffset() uint64 {
|
|
return req.offset
|
|
}
|
|
|
|
func (req TestSearchRequest) GetSortingColumn() ColumnKey {
|
|
return req.sortingColumn
|
|
}
|
|
|
|
func (req TestSearchRequest) GetAsc() bool {
|
|
return req.asc
|
|
}
|
|
|
|
func (req TestSearchRequest) GetQueries() []SearchQuery {
|
|
return req.queries
|
|
}
|
|
|
|
type TestSearchQuery struct {
|
|
key TestSearchKey
|
|
method domain.SearchMethod
|
|
value string
|
|
}
|
|
|
|
func (req TestSearchQuery) GetKey() ColumnKey {
|
|
return req.key
|
|
}
|
|
|
|
func (req TestSearchQuery) GetMethod() domain.SearchMethod {
|
|
return req.method
|
|
}
|
|
|
|
func (req TestSearchQuery) GetValue() interface{} {
|
|
return req.value
|
|
}
|
|
|
|
type TestSearchKey int32
|
|
|
|
const (
|
|
TestSearchKey_UNDEFINED TestSearchKey = iota
|
|
TestSearchKey_TEST
|
|
TestSearchKey_ID
|
|
)
|
|
|
|
func (key TestSearchKey) ToColumnName() string {
|
|
switch TestSearchKey(key) {
|
|
case TestSearchKey_TEST:
|
|
return "test"
|
|
case TestSearchKey_ID:
|
|
return "id"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
type Test struct {
|
|
ID string `json:"-" gorm:"column:primary_id;primary_key"`
|
|
Test string `json:"test" gorm:"column:test"`
|
|
}
|
|
|
|
type TestMultiplePK struct {
|
|
TestID string `gorm:"column:testId;primary_key"`
|
|
HodorID string `gorm:"column:hodorId;primary_key"`
|
|
Test string `gorm:"column:test"`
|
|
}
|
|
|
|
type dbMock struct {
|
|
db *gorm.DB
|
|
mock sqlmock.Sqlmock
|
|
}
|
|
|
|
func (db *dbMock) close() {
|
|
db.db.Close()
|
|
}
|
|
|
|
func mockDB(t *testing.T) *dbMock {
|
|
mockDB := dbMock{}
|
|
db, mock, err := sqlmock.New(sqlmock.ValueConverterOption(new(db_mock.TypeConverter)))
|
|
if err != nil {
|
|
t.Fatalf("error occurred while creating stub db %v", err)
|
|
}
|
|
|
|
mockDB.mock = mock
|
|
mockDB.db, err = gorm.Open("postgres", db)
|
|
if err != nil {
|
|
t.Fatalf("error occurred while connecting to stub db: %v", err)
|
|
}
|
|
|
|
mockDB.mock.MatchExpectationsInOrder(true)
|
|
|
|
return &mockDB
|
|
}
|
|
|
|
func (db *dbMock) expectBegin(err error) *dbMock {
|
|
if err != nil {
|
|
db.mock.ExpectBegin().WillReturnError(err)
|
|
} else {
|
|
db.mock.ExpectBegin()
|
|
}
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectCommit(err error) *dbMock {
|
|
if err != nil {
|
|
db.mock.ExpectCommit().WillReturnError(err)
|
|
} else {
|
|
db.mock.ExpectCommit()
|
|
}
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectRollback(err error) *dbMock {
|
|
if err != nil {
|
|
db.mock.ExpectRollback().WillReturnError(err)
|
|
} else {
|
|
db.mock.ExpectRollback()
|
|
}
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectGetByID(table, key, value string) *dbMock {
|
|
query := fmt.Sprintf(expectedGetByID, table, key)
|
|
db.mock.ExpectQuery(query).
|
|
WithArgs(value).
|
|
WillReturnRows(db.mock.NewRows([]string{key}).
|
|
AddRow(key))
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectGetByIDErr(table, key, value string, err error) *dbMock {
|
|
query := fmt.Sprintf(expectedGetByID, table, key)
|
|
db.mock.ExpectQuery(query).
|
|
WithArgs(value).
|
|
WillReturnError(err)
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectGetByQuery(table, key, method, value string) *dbMock {
|
|
query := fmt.Sprintf(expectedGetByQuery, table, key, method)
|
|
db.mock.ExpectQuery(query).
|
|
WithArgs(value).
|
|
WillReturnRows(db.mock.NewRows([]string{key}).
|
|
AddRow(key))
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectGetByQueryCaseSensitive(table, key, method, value string) *dbMock {
|
|
query := fmt.Sprintf(expectedGetByQueryCaseSensitive, table, key, method)
|
|
db.mock.ExpectQuery(query).
|
|
WithArgs(value).
|
|
WillReturnRows(db.mock.NewRows([]string{key}).
|
|
AddRow(key))
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectGetByQueryErr(table, key, method, value string, err error) *dbMock {
|
|
query := fmt.Sprintf(expectedGetByQuery, table, key, method)
|
|
db.mock.ExpectQuery(query).
|
|
WithArgs(value).
|
|
WillReturnError(err)
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectSave(table string, object Test) *dbMock {
|
|
query := fmt.Sprintf(expectedSave, table, table, "primary_id")
|
|
db.mock.ExpectExec(query).
|
|
WithArgs(object.Test, object.ID).
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectSaveErr(table string, object Test, err error) *dbMock {
|
|
query := fmt.Sprintf(expectedSave, table, table, "id")
|
|
db.mock.ExpectExec(query).
|
|
WithArgs(object.Test, object.ID).
|
|
WillReturnError(err)
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectRemove(table, key, value string) *dbMock {
|
|
query := fmt.Sprintf(expectedRemove, table, key)
|
|
db.mock.ExpectExec(query).
|
|
WithArgs(value).
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectRemoveKeys(table string, keys ...Key) *dbMock {
|
|
keyNames := make([]interface{}, len(keys))
|
|
keyValues := make([]driver.Value, len(keys))
|
|
for i, key := range keys {
|
|
keyNames[i] = key.Key.ToColumnName()
|
|
keyValues[i] = key.Value
|
|
}
|
|
query := fmt.Sprintf(expectedRemoveByKeys(len(keys), table), keyNames...)
|
|
db.mock.ExpectExec(query).
|
|
WithArgs(keyValues...).
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectRemoveByObject(table string, object Test) *dbMock {
|
|
query := fmt.Sprintf(expectedRemoveByObject, table, table, "primary_id")
|
|
db.mock.ExpectExec(query).
|
|
WithArgs(object.ID).
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectRemoveByObjectMultiplePKs(table string, object TestMultiplePK) *dbMock {
|
|
query := fmt.Sprintf(expectedRemoveByObjectMultiplePK, table, table, "testId", table, "hodorId")
|
|
db.mock.ExpectExec(query).
|
|
WithArgs(object.TestID, object.HodorID).
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectRemoveErr(table, key, value string, err error) *dbMock {
|
|
query := fmt.Sprintf(expectedRemove, table, key)
|
|
db.mock.ExpectExec(query).
|
|
WithArgs(value).
|
|
WillReturnError(err)
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectTruncate(table string) *dbMock {
|
|
query := fmt.Sprintf(expectedTruncate, table)
|
|
db.mock.ExpectExec(query).
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
return db
|
|
}
|
|
func (db *dbMock) expectTruncateErr(table string, err error) *dbMock {
|
|
query := fmt.Sprintf(expectedTruncate, table)
|
|
db.mock.ExpectExec(query).
|
|
WillReturnError(err)
|
|
|
|
return db
|
|
}
|
|
func (db *dbMock) expectGetSearchRequestNoParams(table string, resultAmount, total int) *dbMock {
|
|
query := fmt.Sprintf(expectedSearch, table)
|
|
queryCount := fmt.Sprintf(expectedSearchCount, table)
|
|
|
|
rows := db.mock.NewRows([]string{"id"})
|
|
for i := 0; i < resultAmount; i++ {
|
|
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
|
}
|
|
|
|
db.mock.ExpectQuery(queryCount).
|
|
WillReturnRows(db.mock.NewRows([]string{"count"}).AddRow(total))
|
|
db.mock.ExpectQuery(query).
|
|
WillReturnRows(rows)
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectGetSearchRequestWithLimit(table string, limit, resultAmount, total int) *dbMock {
|
|
query := fmt.Sprintf(expectedSearchLimit, table, limit)
|
|
queryCount := fmt.Sprintf(expectedSearchLimitCount, table)
|
|
|
|
rows := db.mock.NewRows([]string{"id"})
|
|
for i := 0; i < resultAmount; i++ {
|
|
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
|
}
|
|
|
|
db.mock.ExpectQuery(queryCount).
|
|
WillReturnRows(db.mock.NewRows([]string{"count"}).AddRow(total))
|
|
db.mock.ExpectQuery(query).
|
|
WillReturnRows(rows)
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectGetSearchRequestWithOffset(table string, offset, resultAmount, total int) *dbMock {
|
|
query := fmt.Sprintf(expectedSearchOffset, table, offset)
|
|
queryCount := fmt.Sprintf(expectedSearchOffsetCount, table)
|
|
|
|
rows := db.mock.NewRows([]string{"id"})
|
|
for i := 0; i < resultAmount; i++ {
|
|
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
|
}
|
|
|
|
db.mock.ExpectQuery(queryCount).
|
|
WillReturnRows(db.mock.NewRows([]string{"count"}).AddRow(total))
|
|
db.mock.ExpectQuery(query).
|
|
WillReturnRows(rows)
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectGetSearchRequestWithSorting(table, sorting string, sortingColumn ColumnKey, resultAmount, total int) *dbMock {
|
|
query := fmt.Sprintf(expectedSearchSorting, table, sortingColumn.ToColumnName(), sorting)
|
|
queryCount := fmt.Sprintf(expectedSearchSortingCount, table)
|
|
|
|
rows := db.mock.NewRows([]string{"id"})
|
|
for i := 0; i < resultAmount; i++ {
|
|
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
|
}
|
|
|
|
db.mock.ExpectQuery(queryCount).
|
|
WillReturnRows(db.mock.NewRows([]string{"count"}).AddRow(total))
|
|
db.mock.ExpectQuery(query).
|
|
WillReturnRows(rows)
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectGetSearchRequestWithSearchQuery(table, key, method, value string, resultAmount, total int) *dbMock {
|
|
query := fmt.Sprintf(expectedSearchQuery, table, key, method)
|
|
queryCount := fmt.Sprintf(expectedSearchQueryCount, table, key, method)
|
|
|
|
rows := db.mock.NewRows([]string{"id"})
|
|
for i := 0; i < resultAmount; i++ {
|
|
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
|
}
|
|
|
|
db.mock.ExpectQuery(queryCount).
|
|
WithArgs(value).
|
|
WillReturnRows(db.mock.NewRows([]string{"count"}).AddRow(total))
|
|
db.mock.ExpectQuery(query).
|
|
WithArgs(value).
|
|
WillReturnRows(rows)
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectGetSearchRequestWithAllParams(table, key, method, value, sorting string, sortingColumn ColumnKey, limit, offset, resultAmount, total int) *dbMock {
|
|
query := fmt.Sprintf(expectedSearchQueryAllParams, table, key, method, sortingColumn.ToColumnName(), sorting, limit, offset)
|
|
queryCount := fmt.Sprintf(expectedSearchQueryAllParamCount, table, key, method)
|
|
|
|
rows := db.mock.NewRows([]string{"id"})
|
|
for i := 0; i < resultAmount; i++ {
|
|
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
|
}
|
|
|
|
db.mock.ExpectQuery(queryCount).
|
|
WithArgs(value).
|
|
WillReturnRows(db.mock.NewRows([]string{"count"}).AddRow(total))
|
|
db.mock.ExpectQuery(query).
|
|
WithArgs(value).
|
|
WillReturnRows(rows)
|
|
return db
|
|
}
|
|
|
|
func (db *dbMock) expectGetSearchRequestErr(table string, resultAmount, total int, err error) *dbMock {
|
|
query := fmt.Sprintf(expectedSearch, table)
|
|
queryCount := fmt.Sprintf(expectedSearchCount, table)
|
|
|
|
rows := db.mock.NewRows([]string{"id"})
|
|
for i := 0; i < resultAmount; i++ {
|
|
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
|
}
|
|
|
|
db.mock.ExpectQuery(queryCount).
|
|
WillReturnRows(db.mock.NewRows([]string{"count"}).AddRow(total))
|
|
db.mock.ExpectQuery(query).
|
|
WillReturnError(err)
|
|
return db
|
|
}
|