mirror of
https://github.com/zitadel/zitadel.git
synced 2025-11-14 21:43:20 +00:00
feat: View (#27)
* feat: eventstore repository * fix: remove gorm * version * feat: pkg * feat: eventstore without eventstore-lib * rename files * gnueg * fix: add object * fix: global model * feat: add global view functions * feat(eventstore): sdk * fix(eventstore): search query * fix(eventstore): rename app to eventstore * delete empty test * remove unused func * merge master * fix(eventstore): tests * fix(models): delete unused struct * feat(eventstore): implemented push events * feat(eventstore): overwrite context data * feat(types): SQL-config * feat(eventstore): options to overwrite editor * fix: use global sql config * fix: changes from mr * fix: add some tests * Update internal/eventstore/models/field.go Co-Authored-By: livio-a <livio.a@gmail.com> * fix(eventstore): code quality * fix: try tests * fix: query tests * fix: use prepare funcs * fix: go mod * fix: go tests * fix: better error func testing * fix: merge master * fix: changes for mr * fix: change value to interface * fix: searchmethods * fix: check if value is string on equal ignore case Co-authored-by: adlerhurst <silvan.reusser@gmail.com> Co-authored-by: livio-a <livio.a@gmail.com>
This commit is contained in:
156
internal/view/query_test.go
Normal file
156
internal/view/query_test.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/model"
|
||||
"github.com/jinzhu/gorm"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPrepareSearchQuery(t *testing.T) {
|
||||
type args struct {
|
||||
table string
|
||||
searchRequest SearchRequest
|
||||
}
|
||||
type res struct {
|
||||
count int
|
||||
wantErr bool
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
db *dbMock
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
"search with no params",
|
||||
mockDB(t).
|
||||
expectGetSearchRequestNoParams("TESTTABLE", 1, 1),
|
||||
args{
|
||||
table: "TESTTABLE",
|
||||
searchRequest: TestSearchRequest{},
|
||||
},
|
||||
res{
|
||||
count: 1,
|
||||
wantErr: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
"search with limit",
|
||||
mockDB(t).
|
||||
expectGetSearchRequestWithLimit("TESTTABLE", 2, 2, 5),
|
||||
args{
|
||||
table: "TESTTABLE",
|
||||
searchRequest: TestSearchRequest{limit: 2},
|
||||
},
|
||||
res{
|
||||
count: 5,
|
||||
wantErr: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
"search with offset",
|
||||
mockDB(t).
|
||||
expectGetSearchRequestWithOffset("TESTTABLE", 2, 2, 2),
|
||||
args{
|
||||
table: "TESTTABLE",
|
||||
searchRequest: TestSearchRequest{offset: 2},
|
||||
},
|
||||
res{
|
||||
count: 2,
|
||||
wantErr: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
"search with sorting asc",
|
||||
mockDB(t).
|
||||
expectGetSearchRequestWithSorting("TESTTABLE", "ASC", TestSearchKey_ID, 2, 2),
|
||||
args{
|
||||
table: "TESTTABLE",
|
||||
searchRequest: TestSearchRequest{sortingColumn: TestSearchKey_ID, asc: true},
|
||||
},
|
||||
res{
|
||||
count: 2,
|
||||
wantErr: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
"search with sorting asc",
|
||||
mockDB(t).
|
||||
expectGetSearchRequestWithSorting("TESTTABLE", "DESC", TestSearchKey_ID, 2, 2),
|
||||
args{
|
||||
table: "TESTTABLE",
|
||||
searchRequest: TestSearchRequest{sortingColumn: TestSearchKey_ID},
|
||||
},
|
||||
res{
|
||||
count: 2,
|
||||
wantErr: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
"search with search query",
|
||||
mockDB(t).
|
||||
expectGetSearchRequestWithSearchQuery("TESTTABLE", TestSearchKey_ID.ToColumnName(), "=", "ID", 2, 2),
|
||||
args{
|
||||
table: "TESTTABLE",
|
||||
searchRequest: TestSearchRequest{queries: []SearchQuery{TestSearchQuery{key: TestSearchKey_ID, method: model.SEARCHMETHOD_EQUALS_IGNORE_CASE, value: "ID"}}},
|
||||
},
|
||||
res{
|
||||
count: 2,
|
||||
wantErr: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
"search with all params",
|
||||
mockDB(t).
|
||||
expectGetSearchRequestWithAllParams("TESTTABLE", TestSearchKey_ID.ToColumnName(), "=", "ID", "ASC", TestSearchKey_ID, 2, 2, 2, 5),
|
||||
args{
|
||||
table: "TESTTABLE",
|
||||
searchRequest: TestSearchRequest{limit: 2, offset: 2, sortingColumn: TestSearchKey_ID, asc: true, queries: []SearchQuery{TestSearchQuery{key: TestSearchKey_ID, method: model.SEARCHMETHOD_EQUALS_IGNORE_CASE, value: "ID"}}},
|
||||
},
|
||||
res{
|
||||
count: 5,
|
||||
wantErr: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
"search db error",
|
||||
mockDB(t).
|
||||
expectGetSearchRequestErr("TESTTABLE", 1, 1, gorm.ErrUnaddressable),
|
||||
args{
|
||||
table: "TESTTABLE",
|
||||
searchRequest: TestSearchRequest{},
|
||||
},
|
||||
res{
|
||||
count: 1,
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsInternal,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
res := &Test{}
|
||||
getQuery := PrepareSearchQuery(tt.args.table, tt.args.searchRequest)
|
||||
count, err := getQuery(tt.db.db, res)
|
||||
|
||||
if !tt.res.wantErr && err != nil {
|
||||
t.Errorf("got wrong err should be nil: %v ", err)
|
||||
}
|
||||
|
||||
if !tt.res.wantErr && count != tt.res.count {
|
||||
t.Errorf("got wrong count: %v ", err)
|
||||
}
|
||||
|
||||
if tt.res.wantErr && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
if err := tt.db.mock.ExpectationsWereMet(); !tt.res.wantErr && err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
|
||||
tt.db.close()
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user