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

324 lines
5.5 KiB
Go
Raw Normal View History

2020-10-05 18:39:36 +00:00
package sql
import (
"database/sql"
2020-10-05 18:39:36 +00:00
"testing"
"github.com/zitadel/zitadel/internal/eventstore"
"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.events2",
2020-10-05 18:39:36 +00:00
},
res: res{
query: "SELECT * FROM eventstore.events2",
2020-10-05 18:39:36 +00:00
},
},
{
name: "one placeholder",
args: args{
query: "SELECT * FROM eventstore.events2 WHERE aggregate_type = ?",
2020-10-05 18:39:36 +00:00
},
res: res{
query: "SELECT * FROM eventstore.events2 WHERE aggregate_type = $1",
2020-10-05 18:39:36 +00:00
},
},
{
name: "multiple placeholders",
args: args{
query: "SELECT * FROM eventstore.events2 WHERE aggregate_type = ? AND aggregate_id = ? LIMIT ?",
2020-10-05 18:39:36 +00:00
},
res: res{
query: "SELECT * FROM eventstore.events2 WHERE aggregate_type = $1 AND aggregate_id = $2 LIMIT $3",
2020-10-05 18:39:36 +00:00
},
},
}
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
useV1 bool
2020-10-05 18:39:36 +00:00
}
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,
useV1: true,
2020-10-05 18:39:36 +00:00
},
res: res{
name: "editor_service",
},
},
{
name: "editor service v2",
2020-10-21 07:39:24 +00:00
args: args{
field: repository.FieldEditorService,
2020-10-21 07:39:24 +00:00
},
res: res{
name: "",
2020-10-21 07:39:24 +00:00
},
},
{
name: "editor user",
2020-10-19 07:53:32 +00:00
args: args{
field: repository.FieldEditorUser,
useV1: true,
2020-10-19 07:53:32 +00:00
},
res: res{
name: "editor_user",
2020-10-19 07:53:32 +00:00
},
},
{
name: "editor user v2",
args: args{
field: repository.FieldEditorUser,
},
res: res{
name: "creator",
},
},
{
name: "event type",
args: args{
field: repository.FieldEventType,
},
res: res{
name: "event_type",
},
},
{
name: "latest sequence",
args: args{
field: repository.FieldSequence,
useV1: true,
},
res: res{
name: "event_sequence",
},
},
{
name: "latest sequence v2",
args: args{
field: repository.FieldSequence,
},
res: res{
name: `"sequence"`,
},
},
{
name: "resource owner",
args: args{
field: repository.FieldResourceOwner,
useV1: true,
},
res: res{
name: "resource_owner",
},
},
{
name: "resource owner v2",
args: args{
field: repository.FieldResourceOwner,
},
res: res{
name: `"owner"`,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &CRDB{}
if got := db.columnName(tt.args.field, tt.args.useV1); got != tt.res.name {
t.Errorf("CRDB.operation() = %v, want %v", got, tt.res.name)
}
})
}
}
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: eventstore.AggregateType(t.Name()),
EditorUser: "user",
ResourceOwner: sql.NullString{String: "ro", Valid: true},
Typ: "test.created",
Version: "v1",
Pos: 42,
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
}