zitadel/internal/eventstore/repository/sql/crdb_test.go

1253 lines
30 KiB
Go
Raw Normal View History

2020-10-05 18:39:36 +00:00
package sql
import (
2020-10-15 06:44:17 +00:00
"context"
"database/sql"
2020-10-19 07:53:32 +00:00
"sync"
2020-10-05 18:39:36 +00:00
"testing"
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore/repository"
2020-10-05 18:39:36 +00:00
)
func TestCRDB_placeholder(t *testing.T) {
type args struct {
query string
}
type res struct {
query string
}
tests := []struct {
name string
args args
res res
}{
{
name: "no placeholders",
args: args{
query: "SELECT * FROM eventstore.events",
},
res: res{
query: "SELECT * FROM eventstore.events",
},
},
{
name: "one placeholder",
args: args{
query: "SELECT * FROM eventstore.events WHERE aggregate_type = ?",
},
res: res{
query: "SELECT * FROM eventstore.events WHERE aggregate_type = $1",
},
},
{
name: "multiple placeholders",
args: args{
query: "SELECT * FROM eventstore.events WHERE aggregate_type = ? AND aggregate_id = ? LIMIT ?",
},
res: res{
query: "SELECT * FROM eventstore.events WHERE aggregate_type = $1 AND aggregate_id = $2 LIMIT $3",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{}
if query := db.placeholder(tt.args.query); query != tt.res.query {
t.Errorf("CRDB.placeholder() = %v, want %v", query, tt.res.query)
}
})
}
}
func TestCRDB_operation(t *testing.T) {
type res struct {
op string
}
type args struct {
operation repository.Operation
}
tests := []struct {
name string
args args
res res
}{
{
name: "no op",
args: args{
operation: repository.Operation(-1),
},
res: res{
op: "",
},
},
{
name: "greater",
args: args{
2020-10-06 19:28:09 +00:00
operation: repository.OperationGreater,
2020-10-05 18:39:36 +00:00
},
res: res{
op: ">",
},
},
{
name: "less",
args: args{
2020-10-06 19:28:09 +00:00
operation: repository.OperationLess,
2020-10-05 18:39:36 +00:00
},
res: res{
op: "<",
},
},
{
name: "equals",
args: args{
2020-10-06 19:28:09 +00:00
operation: repository.OperationEquals,
2020-10-05 18:39:36 +00:00
},
res: res{
op: "=",
},
},
{
name: "in",
args: args{
2020-10-06 19:28:09 +00:00
operation: repository.OperationIn,
2020-10-05 18:39:36 +00:00
},
res: res{
op: "=",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{}
if got := db.operation(tt.args.operation); got != tt.res.op {
t.Errorf("CRDB.operation() = %v, want %v", got, tt.res.op)
}
})
}
}
func TestCRDB_conditionFormat(t *testing.T) {
type res struct {
format string
}
type args struct {
operation repository.Operation
}
tests := []struct {
name string
args args
res res
}{
{
name: "default",
args: args{
2020-10-06 19:28:09 +00:00
operation: repository.OperationEquals,
2020-10-05 18:39:36 +00:00
},
res: res{
format: "%s %s ?",
},
},
{
name: "in",
args: args{
2020-10-06 19:28:09 +00:00
operation: repository.OperationIn,
2020-10-05 18:39:36 +00:00
},
res: res{
format: "%s %s ANY(?)",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{}
if got := db.conditionFormat(tt.args.operation); got != tt.res.format {
t.Errorf("CRDB.conditionFormat() = %v, want %v", got, tt.res.format)
}
})
}
}
func TestCRDB_columnName(t *testing.T) {
type res struct {
name string
}
type args struct {
field repository.Field
}
tests := []struct {
name string
args args
res res
}{
{
name: "invalid field",
args: args{
field: repository.Field(-1),
},
res: res{
name: "",
},
},
{
name: "aggregate id",
args: args{
2020-10-06 19:28:09 +00:00
field: repository.FieldAggregateID,
2020-10-05 18:39:36 +00:00
},
res: res{
name: "aggregate_id",
},
},
{
name: "aggregate type",
args: args{
2020-10-06 19:28:09 +00:00
field: repository.FieldAggregateType,
2020-10-05 18:39:36 +00:00
},
res: res{
name: "aggregate_type",
},
},
{
name: "editor service",
args: args{
2020-10-06 19:28:09 +00:00
field: repository.FieldEditorService,
2020-10-05 18:39:36 +00:00
},
res: res{
name: "editor_service",
},
},
{
name: "editor user",
args: args{
2020-10-06 19:28:09 +00:00
field: repository.FieldEditorUser,
2020-10-05 18:39:36 +00:00
},
res: res{
name: "editor_user",
},
},
{
name: "event type",
args: args{
2020-10-06 19:28:09 +00:00
field: repository.FieldEventType,
2020-10-05 18:39:36 +00:00
},
res: res{
name: "event_type",
},
},
{
name: "latest sequence",
args: args{
2020-10-06 19:28:09 +00:00
field: repository.FieldSequence,
2020-10-05 18:39:36 +00:00
},
res: res{
name: "event_sequence",
},
},
{
name: "resource owner",
args: args{
2020-10-06 19:28:09 +00:00
field: repository.FieldResourceOwner,
2020-10-05 18:39:36 +00:00
},
res: res{
name: "resource_owner",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{}
if got := db.columnName(tt.args.field); got != tt.res.name {
t.Errorf("CRDB.operation() = %v, want %v", got, tt.res.name)
}
})
}
}
2020-10-15 06:44:17 +00:00
2020-10-15 11:25:25 +00:00
func TestCRDB_Push_OneAggregate(t *testing.T) {
2020-10-15 06:44:17 +00:00
type args struct {
feat(cli): setup (#3267) * commander * commander * selber! * move to packages * fix(errors): implement Is interface * test: command * test: commands * add init steps * setup tenant * add default step yaml * possibility to set password * merge v2 into v2-commander * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: search query builder can filter events in memory * fix: filters for add member * fix(setup): add `ExternalSecure` to config * chore: name iam to instance * fix: matching * remove unsued func * base url * base url * test(command): filter funcs * test: commands * fix: rename orgiampolicy to domain policy * start from init * commands * config * fix indexes and add constraints * fixes * fix: merge conflicts * fix: protos * fix: md files * setup * add deprecated org iam policy again * typo * fix search query * fix filter * Apply suggestions from code review * remove custom org from org setup * add todos for verification * change apps creation * simplify package structure * fix error * move preparation helper for tests * fix unique constraints * fix config mapping in setup * fix error handling in encryption_keys.go * fix projection config * fix query from old views to projection * fix setup of mgmt api * set iam project and fix instance projection * imports Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-03-28 08:05:09 +00:00
ctx context.Context
events []*repository.Event
uniqueConstraints *repository.UniqueConstraint
uniqueDataType string
uniqueDataField string
uniqueDataInstanceID string
2020-10-15 06:44:17 +00:00
}
2020-10-15 11:25:25 +00:00
type eventsRes struct {
pushedEventsCount int
uniqueCount int
assetCount int
2020-10-15 11:25:25 +00:00
aggType repository.AggregateType
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
aggID database.StringArray
2020-10-15 11:25:25 +00:00
}
type res struct {
wantErr bool
eventsRes eventsRes
}
2020-10-15 06:44:17 +00:00
tests := []struct {
2020-10-15 11:25:25 +00:00
name string
args args
res res
2020-10-15 06:44:17 +00:00
}{
{
name: "push 1 event",
2020-10-15 06:44:17 +00:00
args: args{
2020-10-19 07:53:32 +00:00
ctx: context.Background(),
2020-10-15 06:44:17 +00:00
events: []*repository.Event{
generateEvent(t, "1"),
2020-10-15 11:25:25 +00:00
},
},
res: res{
wantErr: false,
eventsRes: eventsRes{
pushedEventsCount: 1,
aggID: []string{"1"},
aggType: repository.AggregateType(t.Name()),
}},
},
{
name: "push two events on agg",
2020-10-15 11:25:25 +00:00
args: args{
2020-10-19 07:53:32 +00:00
ctx: context.Background(),
2020-10-15 11:25:25 +00:00
events: []*repository.Event{
generateEvent(t, "6"),
generateEvent(t, "6"),
2020-10-15 11:25:25 +00:00
},
},
res: res{
wantErr: false,
eventsRes: eventsRes{
pushedEventsCount: 2,
aggID: []string{"6"},
aggType: repository.AggregateType(t.Name()),
},
},
},
2020-10-19 07:53:32 +00:00
{
name: "failed push because context canceled",
args: args{
ctx: canceledCtx(),
events: []*repository.Event{
generateEvent(t, "9"),
2020-10-19 07:53:32 +00:00
},
},
res: res{
wantErr: true,
eventsRes: eventsRes{
pushedEventsCount: 0,
aggID: []string{"9"},
aggType: repository.AggregateType(t.Name()),
},
},
},
{
name: "push 1 event and add unique constraint",
args: args{
ctx: context.Background(),
events: []*repository.Event{
generateEvent(t, "10"),
},
uniqueConstraints: generateAddUniqueConstraint(t, "usernames", "field"),
},
res: res{
wantErr: false,
eventsRes: eventsRes{
pushedEventsCount: 1,
uniqueCount: 1,
aggID: []string{"10"},
aggType: repository.AggregateType(t.Name()),
}},
},
{
name: "push 1 event and remove unique constraint",
args: args{
ctx: context.Background(),
events: []*repository.Event{
generateEvent(t, "11"),
},
uniqueConstraints: generateRemoveUniqueConstraint(t, "usernames", "testremove"),
uniqueDataType: "usernames",
uniqueDataField: "testremove",
},
res: res{
wantErr: false,
eventsRes: eventsRes{
pushedEventsCount: 1,
uniqueCount: 0,
aggID: []string{"11"},
aggType: repository.AggregateType(t.Name()),
}},
},
{
2022-10-26 13:06:48 +00:00
name: "push 1 event and remove instance unique constraints",
args: args{
ctx: context.Background(),
events: []*repository.Event{
generateEvent(t, "12"),
},
2022-10-26 13:06:48 +00:00
uniqueConstraints: generateRemoveInstanceUniqueConstraints(t, "instanceID"),
uniqueDataType: "usernames",
uniqueDataField: "testremove",
uniqueDataInstanceID: "instanceID",
},
res: res{
wantErr: false,
eventsRes: eventsRes{
pushedEventsCount: 1,
2022-10-26 13:06:48 +00:00
uniqueCount: 0,
aggID: []string{"12"},
aggType: repository.AggregateType(t.Name()),
}},
},
{
2022-10-26 13:06:48 +00:00
name: "push 1 event and add asset",
args: args{
ctx: context.Background(),
events: []*repository.Event{
generateEvent(t, "13"),
},
},
res: res{
wantErr: false,
eventsRes: eventsRes{
pushedEventsCount: 1,
2022-10-26 13:06:48 +00:00
assetCount: 1,
aggID: []string{"13"},
aggType: repository.AggregateType(t.Name()),
}},
},
2022-10-26 13:06:48 +00:00
{
name: "push 1 event and remove asset",
args: args{
ctx: context.Background(),
events: []*repository.Event{
generateEvent(t, "14"),
},
},
res: res{
wantErr: false,
eventsRes: eventsRes{
pushedEventsCount: 1,
assetCount: 0,
aggID: []string{"14"},
aggType: repository.AggregateType(t.Name()),
}},
},
2020-10-15 11:25:25 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{
DB: &database.DB{
DB: testCRDBClient,
Database: new(testDB),
},
2020-10-15 11:25:25 +00:00
}
if tt.args.uniqueDataType != "" && tt.args.uniqueDataField != "" {
feat(cli): setup (#3267) * commander * commander * selber! * move to packages * fix(errors): implement Is interface * test: command * test: commands * add init steps * setup tenant * add default step yaml * possibility to set password * merge v2 into v2-commander * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: search query builder can filter events in memory * fix: filters for add member * fix(setup): add `ExternalSecure` to config * chore: name iam to instance * fix: matching * remove unsued func * base url * base url * test(command): filter funcs * test: commands * fix: rename orgiampolicy to domain policy * start from init * commands * config * fix indexes and add constraints * fixes * fix: merge conflicts * fix: protos * fix: md files * setup * add deprecated org iam policy again * typo * fix search query * fix filter * Apply suggestions from code review * remove custom org from org setup * add todos for verification * change apps creation * simplify package structure * fix error * move preparation helper for tests * fix unique constraints * fix config mapping in setup * fix error handling in encryption_keys.go * fix projection config * fix query from old views to projection * fix setup of mgmt api * set iam project and fix instance projection * imports Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-03-28 08:05:09 +00:00
err := fillUniqueData(tt.args.uniqueDataType, tt.args.uniqueDataField, tt.args.uniqueDataInstanceID)
if err != nil {
t.Error("unable to prefill insert unique data: ", err)
return
}
}
if err := db.Push(tt.args.ctx, tt.args.events, tt.args.uniqueConstraints); (err != nil) != tt.res.wantErr {
2020-10-15 11:25:25 +00:00
t.Errorf("CRDB.Push() error = %v, wantErr %v", err, tt.res.wantErr)
}
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
countEventRow := testCRDBClient.QueryRow("SELECT COUNT(*) FROM eventstore.events where aggregate_type = $1 AND aggregate_id = ANY($2)", tt.res.eventsRes.aggType, tt.res.eventsRes.aggID)
var eventCount int
err := countEventRow.Scan(&eventCount)
2020-10-15 11:25:25 +00:00
if err != nil {
t.Error("unable to query inserted rows: ", err)
return
}
if eventCount != tt.res.eventsRes.pushedEventsCount {
t.Errorf("expected push count %d got %d", tt.res.eventsRes.pushedEventsCount, eventCount)
}
if tt.args.uniqueConstraints != nil {
feat(cli): setup (#3267) * commander * commander * selber! * move to packages * fix(errors): implement Is interface * test: command * test: commands * add init steps * setup tenant * add default step yaml * possibility to set password * merge v2 into v2-commander * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: search query builder can filter events in memory * fix: filters for add member * fix(setup): add `ExternalSecure` to config * chore: name iam to instance * fix: matching * remove unsued func * base url * base url * test(command): filter funcs * test: commands * fix: rename orgiampolicy to domain policy * start from init * commands * config * fix indexes and add constraints * fixes * fix: merge conflicts * fix: protos * fix: md files * setup * add deprecated org iam policy again * typo * fix search query * fix filter * Apply suggestions from code review * remove custom org from org setup * add todos for verification * change apps creation * simplify package structure * fix error * move preparation helper for tests * fix unique constraints * fix config mapping in setup * fix error handling in encryption_keys.go * fix projection config * fix query from old views to projection * fix setup of mgmt api * set iam project and fix instance projection * imports Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-03-28 08:05:09 +00:00
countUniqueRow := testCRDBClient.QueryRow("SELECT COUNT(*) FROM eventstore.unique_constraints where unique_type = $1 AND unique_field = $2 AND instance_id = $3", tt.args.uniqueConstraints.UniqueType, tt.args.uniqueConstraints.UniqueField, tt.args.uniqueConstraints.InstanceID)
var uniqueCount int
err := countUniqueRow.Scan(&uniqueCount)
if err != nil {
t.Error("unable to query inserted rows: ", err)
return
}
if uniqueCount != tt.res.eventsRes.uniqueCount {
t.Errorf("expected unique count %d got %d", tt.res.eventsRes.uniqueCount, uniqueCount)
}
2020-10-15 11:25:25 +00:00
}
})
}
}
func TestCRDB_Push_MultipleAggregate(t *testing.T) {
type args struct {
events []*repository.Event
}
type eventsRes struct {
pushedEventsCount int
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
aggType database.StringArray
aggID database.StringArray
2020-10-15 11:25:25 +00:00
}
type res struct {
wantErr bool
eventsRes eventsRes
}
tests := []struct {
name string
args args
res res
}{
{
name: "push two aggregates",
2020-10-15 06:44:17 +00:00
args: args{
events: []*repository.Event{
generateEvent(t, "100"),
generateEvent(t, "101"),
2020-10-15 11:25:25 +00:00
},
},
res: res{
wantErr: false,
eventsRes: eventsRes{
pushedEventsCount: 2,
aggID: []string{"100", "101"},
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
aggType: database.StringArray{t.Name()},
2020-10-15 11:25:25 +00:00
},
},
},
{
name: "push two aggregates both multiple events",
2020-10-15 11:25:25 +00:00
args: args{
events: []*repository.Event{
generateEvent(t, "102"),
generateEvent(t, "102"),
generateEvent(t, "103"),
generateEvent(t, "103"),
},
2020-10-15 11:25:25 +00:00
},
res: res{
wantErr: false,
eventsRes: eventsRes{
pushedEventsCount: 4,
aggID: []string{"102", "103"},
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
aggType: database.StringArray{t.Name()},
2020-10-15 11:25:25 +00:00
},
},
},
{
name: "push two aggregates mixed multiple events",
2020-10-15 11:25:25 +00:00
args: args{
events: []*repository.Event{
generateEvent(t, "106"),
generateEvent(t, "106"),
generateEvent(t, "106"),
generateEvent(t, "106"),
generateEvent(t, "107"),
generateEvent(t, "107"),
generateEvent(t, "107"),
generateEvent(t, "107"),
generateEvent(t, "108"),
generateEvent(t, "108"),
generateEvent(t, "108"),
generateEvent(t, "108"),
2020-10-19 07:53:32 +00:00
},
},
res: res{
wantErr: false,
2020-10-19 07:53:32 +00:00
eventsRes: eventsRes{
pushedEventsCount: 12,
aggID: []string{"106", "107", "108"},
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
aggType: database.StringArray{t.Name()},
2020-10-15 06:44:17 +00:00
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{
DB: &database.DB{
DB: testCRDBClient,
Database: new(testDB),
},
2020-10-15 06:44:17 +00:00
}
if err := db.Push(context.Background(), tt.args.events); (err != nil) != tt.res.wantErr {
2020-10-15 11:25:25 +00:00
t.Errorf("CRDB.Push() error = %v, wantErr %v", err, tt.res.wantErr)
}
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
countRow := testCRDBClient.QueryRow("SELECT COUNT(*) FROM eventstore.events where aggregate_type = ANY($1) AND aggregate_id = ANY($2)", tt.res.eventsRes.aggType, tt.res.eventsRes.aggID)
2020-10-15 11:25:25 +00:00
var count int
err := countRow.Scan(&count)
if err != nil {
t.Error("unable to query inserted rows: ", err)
return
}
if count != tt.res.eventsRes.pushedEventsCount {
t.Errorf("expected push count %d got %d", tt.res.eventsRes.pushedEventsCount, count)
2020-10-15 06:44:17 +00:00
}
})
}
}
2020-10-15 11:25:25 +00:00
func TestCRDB_CreateInstance(t *testing.T) {
type args struct {
instanceID string
}
type res struct {
wantErr bool
exists bool
}
tests := []struct {
name string
args args
res res
}{
{
name: "no number",
args: args{
instanceID: "asdf;use defaultdb;DROP DATABASE zitadel;--",
},
res: res{
wantErr: true,
exists: false,
},
},
{
name: "no instance id",
args: args{
instanceID: "",
},
res: res{
wantErr: true,
exists: false,
},
},
{
name: "correct number",
args: args{
instanceID: "1235",
},
res: res{
wantErr: false,
exists: true,
},
},
{
name: "correct text",
args: args{
instanceID: "system",
},
res: res{
wantErr: false,
exists: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{
DB: &database.DB{DB: testCRDBClient},
}
if err := db.CreateInstance(context.Background(), tt.args.instanceID); (err != nil) != tt.res.wantErr {
t.Errorf("CRDB.CreateInstance() error = %v, wantErr %v", err, tt.res.wantErr)
}
sequenceRow := testCRDBClient.QueryRow("SELECT EXISTS(SELECT 1 FROM [SHOW SEQUENCES] WHERE sequence_schema = 'eventstore' AND sequence_name like $1)", "i_"+tt.args.instanceID+"%")
var exists bool
err := sequenceRow.Scan(&exists)
if err != nil {
t.Error("unable to query inserted rows: ", err)
return
}
if exists != tt.res.exists {
t.Errorf("expected exists %v got %v", tt.res.exists, exists)
}
})
}
}
2020-10-19 07:53:32 +00:00
func TestCRDB_Push_Parallel(t *testing.T) {
type args struct {
events [][]*repository.Event
}
type eventsRes struct {
pushedEventsCount int
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
aggTypes database.StringArray
aggIDs database.StringArray
2020-10-19 07:53:32 +00:00
}
type res struct {
errCount int
eventsRes eventsRes
}
tests := []struct {
name string
args args
res res
}{
{
name: "clients push different aggregates",
args: args{
events: [][]*repository.Event{
{
generateEvent(t, "200"),
generateEvent(t, "200"),
generateEvent(t, "200"),
generateEvent(t, "201"),
generateEvent(t, "201"),
generateEvent(t, "201"),
},
{
generateEvent(t, "202"),
generateEvent(t, "203"),
generateEvent(t, "203"),
},
2020-10-19 07:53:32 +00:00
},
},
res: res{
errCount: 0,
eventsRes: eventsRes{
aggIDs: []string{"200", "201", "202", "203"},
pushedEventsCount: 9,
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
aggTypes: database.StringArray{t.Name()},
2020-10-19 07:53:32 +00:00
},
},
},
{
name: "clients push same aggregates",
2020-10-19 07:53:32 +00:00
args: args{
events: [][]*repository.Event{
{
generateEvent(t, "204"),
generateEvent(t, "204"),
},
{
generateEvent(t, "204"),
generateEvent(t, "204"),
},
{
generateEvent(t, "205"),
generateEvent(t, "205"),
generateEvent(t, "205"),
generateEvent(t, "206"),
generateEvent(t, "206"),
generateEvent(t, "206"),
},
{
generateEvent(t, "204"),
generateEvent(t, "205"),
generateEvent(t, "205"),
generateEvent(t, "206"),
},
2020-10-19 07:53:32 +00:00
},
},
res: res{
errCount: 0,
eventsRes: eventsRes{
aggIDs: []string{"204", "205", "206"},
pushedEventsCount: 14,
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
aggTypes: database.StringArray{t.Name()},
2020-10-19 07:53:32 +00:00
},
},
},
{
name: "clients push different aggregates",
2020-10-19 07:53:32 +00:00
args: args{
events: [][]*repository.Event{
{
generateEvent(t, "207"),
generateEvent(t, "207"),
generateEvent(t, "207"),
generateEvent(t, "207"),
generateEvent(t, "207"),
generateEvent(t, "207"),
},
{
generateEvent(t, "208"),
generateEvent(t, "208"),
generateEvent(t, "208"),
generateEvent(t, "208"),
generateEvent(t, "208"),
},
2020-10-19 07:53:32 +00:00
},
},
res: res{
errCount: 0,
eventsRes: eventsRes{
aggIDs: []string{"207", "208"},
pushedEventsCount: 11,
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
aggTypes: database.StringArray{t.Name()},
2020-10-19 07:53:32 +00:00
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{
DB: &database.DB{
DB: testCRDBClient,
Database: new(testDB),
},
2020-10-19 07:53:32 +00:00
}
wg := sync.WaitGroup{}
errs := make([]error, 0, tt.res.errCount)
errsMu := sync.Mutex{}
for _, events := range tt.args.events {
wg.Add(1)
go func(events []*repository.Event) {
err := db.Push(context.Background(), events, nil)
2020-10-19 07:53:32 +00:00
if err != nil {
errsMu.Lock()
errs = append(errs, err)
errsMu.Unlock()
}
wg.Done()
}(events)
}
wg.Wait()
if len(errs) != tt.res.errCount {
t.Errorf("CRDB.Push() error count = %d, wanted err count %d, errs: %v", len(errs), tt.res.errCount, errs)
}
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
rows, err := testCRDBClient.Query("SELECT event_data FROM eventstore.events where aggregate_type = ANY($1) AND aggregate_id = ANY($2) order by event_sequence", tt.res.eventsRes.aggTypes, tt.res.eventsRes.aggIDs)
2020-10-19 07:53:32 +00:00
if err != nil {
t.Error("unable to query inserted rows: ", err)
return
}
var count int
for rows.Next() {
count++
data := make(Data, 0)
err := rows.Scan(&data)
if err != nil {
t.Error("unable to query inserted rows: ", err)
return
}
t.Logf("inserted data: %v", string(data))
}
if count != tt.res.eventsRes.pushedEventsCount {
t.Errorf("expected push count %d got %d", tt.res.eventsRes.pushedEventsCount, count)
}
})
}
}
2020-10-21 17:00:41 +00:00
func TestCRDB_Filter(t *testing.T) {
2020-10-19 07:53:32 +00:00
type args struct {
searchQuery *repository.SearchQuery
}
type fields struct {
existingEvents []*repository.Event
}
type res struct {
2020-10-21 07:39:24 +00:00
eventCount int
2020-10-19 07:53:32 +00:00
}
tests := []struct {
name string
fields fields
args args
res res
wantErr bool
}{
{
name: "aggregate type filter no events",
2020-10-21 07:39:24 +00:00
args: args{
searchQuery: &repository.SearchQuery{
Columns: repository.ColumnsEvent,
Filters: [][]*repository.Filter{
{
repository.NewFilter(repository.FieldAggregateType, "not found", repository.OperationEquals),
},
2020-10-21 07:39:24 +00:00
},
},
},
fields: fields{
existingEvents: []*repository.Event{
generateEvent(t, "300"),
generateEvent(t, "300"),
generateEvent(t, "300"),
2020-10-21 07:39:24 +00:00
},
},
res: res{
eventCount: 0,
},
wantErr: false,
},
{
2020-10-21 17:00:41 +00:00
name: "aggregate type and id filter events found",
2020-10-21 07:39:24 +00:00
args: args{
searchQuery: &repository.SearchQuery{
Columns: repository.ColumnsEvent,
Filters: [][]*repository.Filter{
{
repository.NewFilter(repository.FieldAggregateType, t.Name(), repository.OperationEquals),
repository.NewFilter(repository.FieldAggregateID, "303", repository.OperationEquals),
},
2020-10-21 07:39:24 +00:00
},
},
},
fields: fields{
existingEvents: []*repository.Event{
generateEvent(t, "303"),
generateEvent(t, "303"),
generateEvent(t, "303"),
generateEvent(t, "305"),
2020-10-21 07:39:24 +00:00
},
},
res: res{
eventCount: 3,
},
wantErr: false,
},
2020-10-21 17:00:41 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{
DB: &database.DB{
DB: testCRDBClient,
Database: new(testDB),
},
2020-10-21 17:00:41 +00:00
}
// setup initial data for query
if err := db.Push(context.Background(), tt.fields.existingEvents); err != nil {
2020-10-21 17:00:41 +00:00
t.Errorf("error in setup = %v", err)
return
}
events, err := db.Filter(context.Background(), tt.args.searchQuery)
if (err != nil) != tt.wantErr {
t.Errorf("CRDB.query() error = %v, wantErr %v", err, tt.wantErr)
}
if len(events) != tt.res.eventCount {
t.Errorf("CRDB.query() expected event count: %d got %d", tt.res.eventCount, len(events))
}
})
}
}
func TestCRDB_LatestSequence(t *testing.T) {
type args struct {
searchQuery *repository.SearchQuery
}
type fields struct {
existingEvents []*repository.Event
}
type res struct {
sequence uint64
}
tests := []struct {
name string
fields fields
args args
res res
wantErr bool
}{
2020-10-21 07:39:24 +00:00
{
2020-10-21 17:00:41 +00:00
name: "aggregate type filter no sequence",
2020-10-21 07:39:24 +00:00
args: args{
searchQuery: &repository.SearchQuery{
2020-10-21 17:00:41 +00:00
Columns: repository.ColumnsMaxSequence,
Filters: [][]*repository.Filter{
{
repository.NewFilter(repository.FieldAggregateType, "not found", repository.OperationEquals),
},
2020-10-21 07:39:24 +00:00
},
},
},
fields: fields{
existingEvents: []*repository.Event{
generateEvent(t, "400"),
generateEvent(t, "400"),
generateEvent(t, "400"),
2020-10-21 07:39:24 +00:00
},
},
res: res{
2020-10-21 17:00:41 +00:00
sequence: 0,
2020-10-21 07:39:24 +00:00
},
wantErr: false,
},
{
2020-10-21 17:00:41 +00:00
name: "aggregate type filter sequence",
2020-10-19 07:53:32 +00:00
args: args{
2020-10-21 17:00:41 +00:00
searchQuery: &repository.SearchQuery{
Columns: repository.ColumnsMaxSequence,
Filters: [][]*repository.Filter{
{
repository.NewFilter(repository.FieldAggregateType, t.Name(), repository.OperationEquals),
},
2020-10-21 17:00:41 +00:00
},
},
2020-10-19 07:53:32 +00:00
},
fields: fields{
2020-10-21 17:00:41 +00:00
existingEvents: []*repository.Event{
generateEvent(t, "401"),
generateEvent(t, "401"),
generateEvent(t, "401"),
2020-10-21 17:00:41 +00:00
},
2020-10-19 07:53:32 +00:00
},
res: res{
2020-10-21 17:00:41 +00:00
sequence: 3,
2020-10-19 07:53:32 +00:00
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{
DB: &database.DB{
DB: testCRDBClient,
Database: new(testDB),
},
2020-10-19 07:53:32 +00:00
}
// setup initial data for query
if err := db.Push(context.Background(), tt.fields.existingEvents); err != nil {
2020-10-19 07:53:32 +00:00
t.Errorf("error in setup = %v", err)
return
}
2020-10-21 17:00:41 +00:00
sequence, err := db.LatestSequence(context.Background(), tt.args.searchQuery)
if (err != nil) != tt.wantErr {
2020-10-19 07:53:32 +00:00
t.Errorf("CRDB.query() error = %v, wantErr %v", err, tt.wantErr)
}
2020-10-21 17:00:41 +00:00
if sequence < tt.res.sequence {
t.Errorf("CRDB.query() expected sequence: %d got %d", tt.res.sequence, sequence)
}
2020-10-19 07:53:32 +00:00
})
}
}
func TestCRDB_Push_ResourceOwner(t *testing.T) {
type args struct {
events []*repository.Event
}
type res struct {
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
resourceOwners database.StringArray
}
type fields struct {
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00
aggregateIDs database.StringArray
aggregateType string
}
tests := []struct {
name string
args args
res res
fields fields
}{
{
name: "two events of same aggregate same resource owner",
args: args{
events: []*repository.Event{
generateEvent(t, "500", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "caos", Valid: true} }),
generateEvent(t, "500", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "caos", Valid: true} }),
},
},
fields: fields{
aggregateIDs: []string{"500"},
aggregateType: t.Name(),
},
res: res{
resourceOwners: []string{"caos", "caos"},
},
},
{
name: "two events of different aggregate same resource owner",
args: args{
events: []*repository.Event{
generateEvent(t, "501", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "caos", Valid: true} }),
generateEvent(t, "502", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "caos", Valid: true} }),
},
},
fields: fields{
aggregateIDs: []string{"501", "502"},
aggregateType: t.Name(),
},
res: res{
resourceOwners: []string{"caos", "caos"},
},
},
{
name: "two events of different aggregate different resource owner",
args: args{
events: []*repository.Event{
generateEvent(t, "503", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "caos", Valid: true} }),
generateEvent(t, "504", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "zitadel", Valid: true} }),
},
},
fields: fields{
aggregateIDs: []string{"503", "504"},
aggregateType: t.Name(),
},
res: res{
resourceOwners: []string{"caos", "zitadel"},
},
},
{
name: "events of different aggregate different resource owner",
args: args{
events: []*repository.Event{
generateEvent(t, "505", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "caos", Valid: true} }),
generateEvent(t, "505", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "caos", Valid: true} }),
generateEvent(t, "506", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "zitadel", Valid: true} }),
generateEvent(t, "506", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "zitadel", Valid: true} }),
},
},
fields: fields{
aggregateIDs: []string{"505", "506"},
aggregateType: t.Name(),
},
res: res{
resourceOwners: []string{"caos", "caos", "zitadel", "zitadel"},
},
},
{
name: "events of different aggregate different resource owner per event",
args: args{
events: []*repository.Event{
generateEvent(t, "507", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "caos", Valid: true} }),
generateEvent(t, "507", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "ignored", Valid: true} }),
generateEvent(t, "508", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "zitadel", Valid: true} }),
generateEvent(t, "508", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "ignored", Valid: true} }),
},
},
fields: fields{
aggregateIDs: []string{"507", "508"},
aggregateType: t.Name(),
},
res: res{
resourceOwners: []string{"caos", "caos", "zitadel", "zitadel"},
},
},
{
name: "events of one aggregate different resource owner per event",
args: args{
events: []*repository.Event{
generateEvent(t, "509", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "caos", Valid: true} }),
generateEvent(t, "509", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "ignored", Valid: true} }),
generateEvent(t, "509", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "ignored", Valid: true} }),
generateEvent(t, "509", func(e *repository.Event) { e.ResourceOwner = sql.NullString{String: "ignored", Valid: true} }),
},
},
fields: fields{
aggregateIDs: []string{"509"},
aggregateType: t.Name(),
},
res: res{
resourceOwners: []string{"caos", "caos", "caos", "caos"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{
DB: &database.DB{
DB: testCRDBClient,
Database: new(testDB),
},
}
if err := db.Push(context.Background(), tt.args.events); err != nil {
t.Errorf("CRDB.Push() error = %v", err)
}
if len(tt.args.events) != len(tt.res.resourceOwners) {
t.Errorf("length of events (%d) and resource owners (%d) must be equal", len(tt.args.events), len(tt.res.resourceOwners))
return
}
for i, event := range tt.args.events {
if event.ResourceOwner.String != tt.res.resourceOwners[i] {
t.Errorf("resource owner not expected want: %q got: %q", tt.res.resourceOwners[i], event.ResourceOwner.String)
}
}
rows, err := testCRDBClient.Query("SELECT resource_owner FROM eventstore.events WHERE aggregate_type = $1 AND aggregate_id = ANY($2) ORDER BY creation_date, event_sequence", tt.fields.aggregateType, tt.fields.aggregateIDs)
if err != nil {
t.Error("unable to query inserted rows: ", err)
return
}
eventCount := 0
for i := 0; rows.Next(); i++ {
var resourceOwner string
err = rows.Scan(&resourceOwner)
if err != nil {
t.Error("unable to scan row: ", err)
return
}
if resourceOwner != tt.res.resourceOwners[i] {
t.Errorf("unexpected resource owner in queried event. want %q, got: %q", tt.res.resourceOwners[i], resourceOwner)
}
eventCount++
}
if eventCount != len(tt.res.resourceOwners) {
t.Errorf("wrong queried event count: want %d, got %d", len(tt.res.resourceOwners), eventCount)
}
})
}
}
2020-10-19 07:53:32 +00:00
func canceledCtx() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
}
func generateEvent(t *testing.T, aggregateID string, opts ...func(*repository.Event)) *repository.Event {
2020-10-15 11:25:25 +00:00
t.Helper()
2020-10-21 17:00:41 +00:00
e := &repository.Event{
AggregateID: aggregateID,
AggregateType: repository.AggregateType(t.Name()),
EditorService: "svc",
EditorUser: "user",
ResourceOwner: sql.NullString{String: "ro", Valid: true},
Type: "test.created",
Version: "v1",
2020-10-15 11:25:25 +00:00
}
2020-10-21 17:00:41 +00:00
for _, opt := range opts {
opt(e)
}
return e
2020-10-15 11:25:25 +00:00
}
2020-10-19 07:53:32 +00:00
func generateAddUniqueConstraint(t *testing.T, table, uniqueField string) *repository.UniqueConstraint {
t.Helper()
e := &repository.UniqueConstraint{
UniqueType: table,
UniqueField: uniqueField,
Action: repository.UniqueConstraintAdd,
}
return e
}
func generateRemoveUniqueConstraint(t *testing.T, table, uniqueField string) *repository.UniqueConstraint {
t.Helper()
e := &repository.UniqueConstraint{
UniqueType: table,
UniqueField: uniqueField,
feat(cli): setup (#3267) * commander * commander * selber! * move to packages * fix(errors): implement Is interface * test: command * test: commands * add init steps * setup tenant * add default step yaml * possibility to set password * merge v2 into v2-commander * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: search query builder can filter events in memory * fix: filters for add member * fix(setup): add `ExternalSecure` to config * chore: name iam to instance * fix: matching * remove unsued func * base url * base url * test(command): filter funcs * test: commands * fix: rename orgiampolicy to domain policy * start from init * commands * config * fix indexes and add constraints * fixes * fix: merge conflicts * fix: protos * fix: md files * setup * add deprecated org iam policy again * typo * fix search query * fix filter * Apply suggestions from code review * remove custom org from org setup * add todos for verification * change apps creation * simplify package structure * fix error * move preparation helper for tests * fix unique constraints * fix config mapping in setup * fix error handling in encryption_keys.go * fix projection config * fix query from old views to projection * fix setup of mgmt api * set iam project and fix instance projection * imports Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-03-28 08:05:09 +00:00
InstanceID: "",
Action: repository.UniqueConstraintRemoved,
}
return e
}
2022-10-26 13:06:48 +00:00
func generateRemoveInstanceUniqueConstraints(t *testing.T, instanceID string) *repository.UniqueConstraint {
t.Helper()
e := &repository.UniqueConstraint{
InstanceID: instanceID,
Action: repository.UniqueConstraintInstanceRemoved,
}
return e
}