diff --git a/internal/eventstore/v2/repository/sql/crdb_test.go b/internal/eventstore/v2/repository/sql/crdb_test.go index 0ef58a5f92..ce3d02cb3e 100644 --- a/internal/eventstore/v2/repository/sql/crdb_test.go +++ b/internal/eventstore/v2/repository/sql/crdb_test.go @@ -1,6 +1,7 @@ package sql import ( + "context" "testing" "github.com/caos/zitadel/internal/eventstore/v2/repository" @@ -260,3 +261,72 @@ func TestCRDB_columnName(t *testing.T) { }) } } + +func TestCRDB_Push(t *testing.T) { + type args struct { + events []*repository.Event + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "push no events", + args: args{ + events: []*repository.Event{}, + }, + wantErr: false, + }, + { + name: "push 1 event with check previous", + args: args{ + events: []*repository.Event{ + { + // AggregateID: t.Name(), + AggregateType: "test", + CheckPreviousSequence: true, + EditorService: "svc", + EditorUser: "user", + PreviousEvent: nil, + PreviousSequence: 0, + ResourceOwner: "ro", + Type: "test.created", + Version: "v1", + }, + }, + }, + wantErr: false, + }, + { + name: "push 1 event with check previous wrong sequence", + args: args{ + events: []*repository.Event{ + { + // AggregateID: t.Name(), + AggregateType: "test", + CheckPreviousSequence: true, + EditorService: "svc", + EditorUser: "user", + PreviousEvent: nil, + PreviousSequence: 5, + ResourceOwner: "ro", + Type: "test.created", + Version: "v1", + }, + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + db := &CRDB{ + client: testCRDBClient, + } + if err := db.Push(context.Background(), tt.args.events...); (err != nil) != tt.wantErr { + t.Errorf("CRDB.Push() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/eventstore/v2/repository/sql/local_crdb_test.go b/internal/eventstore/v2/repository/sql/local_crdb_test.go index 1b347105f6..df45be8230 100644 --- a/internal/eventstore/v2/repository/sql/local_crdb_test.go +++ b/internal/eventstore/v2/repository/sql/local_crdb_test.go @@ -20,7 +20,7 @@ import ( var ( migrationsPath = os.ExpandEnv("${GOPATH}/src/github.com/caos/zitadel/migrations/cockroach") - db *sql.DB + testCRDBClient *sql.DB ) func TestMain(m *testing.M) { @@ -29,13 +29,13 @@ func TestMain(m *testing.M) { logging.LogWithFields("REPOS-RvjLG", "error", err).Fatal("unable to start db") } - db, err = sql.Open("postgres", ts.PGURL().String()) + testCRDBClient, err = sql.Open("postgres", ts.PGURL().String()) if err != nil { logging.LogWithFields("REPOS-CF6dQ", "error", err).Fatal("unable to connect to db") } defer func() { - db.Close() + testCRDBClient.Close() ts.Stop() }() @@ -47,7 +47,7 @@ func TestMain(m *testing.M) { } func TestInsert(t *testing.T) { - crdb := &CRDB{client: db} + crdb := &CRDB{client: testCRDBClient} e1 := &repository.Event{ AggregateID: "agg.id", AggregateType: "agg.type", @@ -155,7 +155,7 @@ func TestInsert(t *testing.T) { } fmt.Println("====================") - rows, err := db.Query("select * from eventstore.events order by event_sequence") + rows, err := testCRDBClient.Query("select * from eventstore.events order by event_sequence") defer rows.Close() fmt.Println(err) @@ -184,7 +184,7 @@ func TestInsert(t *testing.T) { fmt.Printf("%+v\n", event) } fmt.Println("====================") - rows, err = db.Query("select max(event_sequence), count(*) from eventstore.events where aggregate_type = 'agg.type' and aggregate_id = 'agg.id'") + rows, err = testCRDBClient.Query("select max(event_sequence), count(*) from eventstore.events where aggregate_type = 'agg.type' and aggregate_id = 'agg.id'") defer rows.Close() fmt.Println(err) @@ -209,10 +209,10 @@ func executeMigrations() error { return err } transactionInMigration := strings.Contains(string(migration), "BEGIN;") - exec := db.Exec + exec := testCRDBClient.Exec var tx *sql.Tx if !transactionInMigration { - tx, err = db.Begin() + tx, err = testCRDBClient.Begin() if err != nil { return fmt.Errorf("begin file: %v || err: %w", file, err) }