test(eventstore): push

This commit is contained in:
adlerhurst 2020-10-15 08:44:17 +02:00
parent 639872b82e
commit d400b02e53
2 changed files with 78 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package sql package sql
import ( import (
"context"
"testing" "testing"
"github.com/caos/zitadel/internal/eventstore/v2/repository" "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)
}
})
}
}

View File

@ -20,7 +20,7 @@ import (
var ( var (
migrationsPath = os.ExpandEnv("${GOPATH}/src/github.com/caos/zitadel/migrations/cockroach") migrationsPath = os.ExpandEnv("${GOPATH}/src/github.com/caos/zitadel/migrations/cockroach")
db *sql.DB testCRDBClient *sql.DB
) )
func TestMain(m *testing.M) { 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") 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 { if err != nil {
logging.LogWithFields("REPOS-CF6dQ", "error", err).Fatal("unable to connect to db") logging.LogWithFields("REPOS-CF6dQ", "error", err).Fatal("unable to connect to db")
} }
defer func() { defer func() {
db.Close() testCRDBClient.Close()
ts.Stop() ts.Stop()
}() }()
@ -47,7 +47,7 @@ func TestMain(m *testing.M) {
} }
func TestInsert(t *testing.T) { func TestInsert(t *testing.T) {
crdb := &CRDB{client: db} crdb := &CRDB{client: testCRDBClient}
e1 := &repository.Event{ e1 := &repository.Event{
AggregateID: "agg.id", AggregateID: "agg.id",
AggregateType: "agg.type", AggregateType: "agg.type",
@ -155,7 +155,7 @@ func TestInsert(t *testing.T) {
} }
fmt.Println("====================") 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() defer rows.Close()
fmt.Println(err) fmt.Println(err)
@ -184,7 +184,7 @@ func TestInsert(t *testing.T) {
fmt.Printf("%+v\n", event) fmt.Printf("%+v\n", event)
} }
fmt.Println("====================") 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() defer rows.Close()
fmt.Println(err) fmt.Println(err)
@ -209,10 +209,10 @@ func executeMigrations() error {
return err return err
} }
transactionInMigration := strings.Contains(string(migration), "BEGIN;") transactionInMigration := strings.Contains(string(migration), "BEGIN;")
exec := db.Exec exec := testCRDBClient.Exec
var tx *sql.Tx var tx *sql.Tx
if !transactionInMigration { if !transactionInMigration {
tx, err = db.Begin() tx, err = testCRDBClient.Begin()
if err != nil { if err != nil {
return fmt.Errorf("begin file: %v || err: %w", file, err) return fmt.Errorf("begin file: %v || err: %w", file, err)
} }