zitadel/internal/eventstore/v2/search_query_test.go

486 lines
13 KiB
Go
Raw Normal View History

2020-09-24 06:52:10 +00:00
package eventstore
import (
2020-10-06 19:28:09 +00:00
"math"
2020-09-24 06:52:10 +00:00
"reflect"
"testing"
"github.com/caos/zitadel/internal/errors"
2020-09-30 08:00:05 +00:00
"github.com/caos/zitadel/internal/eventstore/v2/repository"
2020-09-24 06:52:10 +00:00
)
func testSetColumns(columns Columns) func(factory *SearchQueryFactory) *SearchQueryFactory {
return func(factory *SearchQueryFactory) *SearchQueryFactory {
factory = factory.Columns(columns)
return factory
}
}
func testSetLimit(limit uint64) func(factory *SearchQueryFactory) *SearchQueryFactory {
return func(factory *SearchQueryFactory) *SearchQueryFactory {
factory = factory.Limit(limit)
return factory
}
}
func testSetSequence(sequence uint64) func(factory *SearchQueryFactory) *SearchQueryFactory {
return func(factory *SearchQueryFactory) *SearchQueryFactory {
factory = factory.SequenceGreater(sequence)
return factory
}
}
func testSetAggregateIDs(aggregateIDs ...string) func(factory *SearchQueryFactory) *SearchQueryFactory {
return func(factory *SearchQueryFactory) *SearchQueryFactory {
factory = factory.AggregateIDs(aggregateIDs...)
return factory
}
}
func testSetEventTypes(eventTypes ...EventType) func(factory *SearchQueryFactory) *SearchQueryFactory {
return func(factory *SearchQueryFactory) *SearchQueryFactory {
factory = factory.EventTypes(eventTypes...)
return factory
}
}
func testSetResourceOwner(resourceOwner string) func(factory *SearchQueryFactory) *SearchQueryFactory {
return func(factory *SearchQueryFactory) *SearchQueryFactory {
factory = factory.ResourceOwner(resourceOwner)
return factory
}
}
func testSetSortOrder(asc bool) func(factory *SearchQueryFactory) *SearchQueryFactory {
return func(factory *SearchQueryFactory) *SearchQueryFactory {
if asc {
factory = factory.OrderAsc()
} else {
factory = factory.OrderDesc()
}
return factory
}
}
func TestSearchQueryFactorySetters(t *testing.T) {
type args struct {
2020-10-06 19:28:09 +00:00
columns Columns
2020-09-24 06:52:10 +00:00
aggregateTypes []AggregateType
setters []func(*SearchQueryFactory) *SearchQueryFactory
}
tests := []struct {
name string
args args
res *SearchQueryFactory
}{
{
name: "New factory",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user", "org"},
},
res: &SearchQueryFactory{
2020-10-06 19:28:09 +00:00
columns: repository.Columns(ColumnsEvent),
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user", "org"},
},
},
{
name: "set columns",
args: args{
2020-10-06 19:28:09 +00:00
setters: []func(*SearchQueryFactory) *SearchQueryFactory{testSetColumns(repository.ColumnsMaxSequence)},
2020-09-24 06:52:10 +00:00
},
res: &SearchQueryFactory{
2020-10-06 19:28:09 +00:00
columns: repository.ColumnsMaxSequence,
2020-09-24 06:52:10 +00:00
},
},
{
name: "set limit",
args: args{
setters: []func(*SearchQueryFactory) *SearchQueryFactory{testSetLimit(100)},
},
res: &SearchQueryFactory{
limit: 100,
},
},
{
name: "set sequence",
args: args{
setters: []func(*SearchQueryFactory) *SearchQueryFactory{testSetSequence(90)},
},
res: &SearchQueryFactory{
eventSequence: 90,
},
},
{
name: "set aggregateIDs",
args: args{
setters: []func(*SearchQueryFactory) *SearchQueryFactory{testSetAggregateIDs("1235", "09824")},
},
res: &SearchQueryFactory{
aggregateIDs: []string{"1235", "09824"},
},
},
{
name: "set eventTypes",
args: args{
setters: []func(*SearchQueryFactory) *SearchQueryFactory{testSetEventTypes("user.created", "user.updated")},
},
res: &SearchQueryFactory{
eventTypes: []EventType{"user.created", "user.updated"},
},
},
{
name: "set resource owner",
args: args{
setters: []func(*SearchQueryFactory) *SearchQueryFactory{testSetResourceOwner("hodor")},
},
res: &SearchQueryFactory{
resourceOwner: "hodor",
},
},
{
name: "default search query",
args: args{
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{testSetAggregateIDs("1235", "024"), testSetSortOrder(false)},
},
res: &SearchQueryFactory{
aggregateTypes: []AggregateType{"user"},
aggregateIDs: []string{"1235", "024"},
desc: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2020-10-06 19:28:09 +00:00
factory := NewSearchQueryFactory(tt.args.columns, tt.args.aggregateTypes...)
2020-09-24 06:52:10 +00:00
for _, setter := range tt.args.setters {
factory = setter(factory)
}
if !reflect.DeepEqual(factory, tt.res) {
t.Errorf("NewSearchQueryFactory() = %v, want %v", factory, tt.res)
}
})
}
}
func TestSearchQueryFactoryBuild(t *testing.T) {
type args struct {
2020-10-06 19:28:09 +00:00
columns Columns
2020-09-24 06:52:10 +00:00
aggregateTypes []AggregateType
setters []func(*SearchQueryFactory) *SearchQueryFactory
}
type res struct {
isErr func(err error) bool
2020-09-30 08:00:05 +00:00
query *repository.SearchQuery
2020-09-24 06:52:10 +00:00
}
tests := []struct {
name string
args args
res res
}{
{
name: "no aggregate types",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{},
},
res: res{
isErr: errors.IsPreconditionFailed,
query: nil,
},
},
{
name: "invalid column (too low)",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{
testSetColumns(Columns(-1)),
},
},
res: res{
isErr: errors.IsPreconditionFailed,
},
},
{
name: "invalid column (too high)",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{
2020-10-06 19:28:09 +00:00
testSetColumns(math.MaxInt32),
2020-09-24 06:52:10 +00:00
},
},
res: res{
isErr: errors.IsPreconditionFailed,
},
},
{
name: "filter aggregate type",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{},
},
res: res{
isErr: nil,
2020-09-30 08:00:05 +00:00
query: &repository.SearchQuery{
2020-10-06 19:28:09 +00:00
Columns: repository.ColumnsEvent,
2020-09-24 06:52:10 +00:00
Desc: false,
Limit: 0,
2020-09-30 08:00:05 +00:00
Filters: []*repository.Filter{
2020-10-06 19:28:09 +00:00
repository.NewFilter(repository.FieldAggregateType, AggregateType("user"), repository.OperationEquals),
2020-09-24 06:52:10 +00:00
},
},
},
},
{
name: "filter aggregate types",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user", "org"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{},
},
res: res{
isErr: nil,
2020-09-30 08:00:05 +00:00
query: &repository.SearchQuery{
2020-10-06 19:28:09 +00:00
Columns: repository.ColumnsEvent,
2020-09-24 06:52:10 +00:00
Desc: false,
Limit: 0,
2020-09-30 08:00:05 +00:00
Filters: []*repository.Filter{
2020-10-06 19:28:09 +00:00
repository.NewFilter(repository.FieldAggregateType, []AggregateType{"user", "org"}, repository.OperationIn),
2020-09-24 06:52:10 +00:00
},
},
},
},
{
name: "filter aggregate type, limit, desc",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{
testSetLimit(5),
testSetSortOrder(false),
testSetSequence(100),
},
},
res: res{
isErr: nil,
2020-09-30 08:00:05 +00:00
query: &repository.SearchQuery{
2020-10-06 19:28:09 +00:00
Columns: repository.ColumnsEvent,
2020-09-24 06:52:10 +00:00
Desc: true,
Limit: 5,
2020-09-30 08:00:05 +00:00
Filters: []*repository.Filter{
2020-10-06 19:28:09 +00:00
repository.NewFilter(repository.FieldAggregateType, AggregateType("user"), repository.OperationEquals),
repository.NewFilter(repository.FieldSequence, uint64(100), repository.OperationLess),
2020-09-24 06:52:10 +00:00
},
},
},
},
{
name: "filter aggregate type, limit, asc",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{
testSetLimit(5),
testSetSortOrder(true),
testSetSequence(100),
},
},
res: res{
isErr: nil,
2020-09-30 08:00:05 +00:00
query: &repository.SearchQuery{
2020-10-06 19:28:09 +00:00
Columns: repository.ColumnsEvent,
2020-09-24 06:52:10 +00:00
Desc: false,
Limit: 5,
2020-09-30 08:00:05 +00:00
Filters: []*repository.Filter{
2020-10-06 19:28:09 +00:00
repository.NewFilter(repository.FieldAggregateType, AggregateType("user"), repository.OperationEquals),
repository.NewFilter(repository.FieldSequence, uint64(100), repository.OperationGreater),
2020-09-24 06:52:10 +00:00
},
},
},
},
{
name: "filter aggregate type, limit, desc, max event sequence cols",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{
testSetLimit(5),
testSetSortOrder(false),
testSetSequence(100),
2020-10-06 19:28:09 +00:00
testSetColumns(repository.ColumnsMaxSequence),
2020-09-24 06:52:10 +00:00
},
},
res: res{
isErr: nil,
2020-09-30 08:00:05 +00:00
query: &repository.SearchQuery{
2020-10-06 19:28:09 +00:00
Columns: repository.ColumnsMaxSequence,
2020-09-24 06:52:10 +00:00
Desc: true,
Limit: 5,
2020-09-30 08:00:05 +00:00
Filters: []*repository.Filter{
2020-10-06 19:28:09 +00:00
repository.NewFilter(repository.FieldAggregateType, AggregateType("user"), repository.OperationEquals),
repository.NewFilter(repository.FieldSequence, uint64(100), repository.OperationLess),
2020-09-24 06:52:10 +00:00
},
},
},
},
{
name: "filter aggregate type and aggregate id",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{
testSetAggregateIDs("1234"),
},
},
res: res{
isErr: nil,
2020-09-30 08:00:05 +00:00
query: &repository.SearchQuery{
2020-10-06 19:28:09 +00:00
Columns: repository.ColumnsEvent,
2020-09-24 06:52:10 +00:00
Desc: false,
Limit: 0,
2020-09-30 08:00:05 +00:00
Filters: []*repository.Filter{
2020-10-06 19:28:09 +00:00
repository.NewFilter(repository.FieldAggregateType, AggregateType("user"), repository.OperationEquals),
repository.NewFilter(repository.FieldAggregateID, "1234", repository.OperationEquals),
2020-09-24 06:52:10 +00:00
},
},
},
},
{
name: "filter aggregate type and aggregate ids",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{
testSetAggregateIDs("1234", "0815"),
},
},
res: res{
isErr: nil,
2020-09-30 08:00:05 +00:00
query: &repository.SearchQuery{
2020-10-06 19:28:09 +00:00
Columns: repository.ColumnsEvent,
2020-09-24 06:52:10 +00:00
Desc: false,
Limit: 0,
2020-09-30 08:00:05 +00:00
Filters: []*repository.Filter{
2020-10-06 19:28:09 +00:00
repository.NewFilter(repository.FieldAggregateType, AggregateType("user"), repository.OperationEquals),
repository.NewFilter(repository.FieldAggregateID, []string{"1234", "0815"}, repository.OperationIn),
2020-09-24 06:52:10 +00:00
},
},
},
},
{
name: "filter aggregate type and sequence greater",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{
testSetSequence(8),
},
},
res: res{
isErr: nil,
2020-09-30 08:00:05 +00:00
query: &repository.SearchQuery{
2020-10-06 19:28:09 +00:00
Columns: repository.ColumnsEvent,
2020-09-24 06:52:10 +00:00
Desc: false,
Limit: 0,
2020-09-30 08:00:05 +00:00
Filters: []*repository.Filter{
2020-10-06 19:28:09 +00:00
repository.NewFilter(repository.FieldAggregateType, AggregateType("user"), repository.OperationEquals),
repository.NewFilter(repository.FieldSequence, uint64(8), repository.OperationGreater),
2020-09-24 06:52:10 +00:00
},
},
},
},
{
name: "filter aggregate type and event type",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{
testSetEventTypes("user.created"),
},
},
res: res{
isErr: nil,
2020-09-30 08:00:05 +00:00
query: &repository.SearchQuery{
2020-10-06 19:28:09 +00:00
Columns: repository.ColumnsEvent,
2020-09-24 06:52:10 +00:00
Desc: false,
Limit: 0,
2020-09-30 08:00:05 +00:00
Filters: []*repository.Filter{
2020-10-06 19:28:09 +00:00
repository.NewFilter(repository.FieldAggregateType, AggregateType("user"), repository.OperationEquals),
repository.NewFilter(repository.FieldEventType, EventType("user.created"), repository.OperationEquals),
2020-09-24 06:52:10 +00:00
},
},
},
},
{
name: "filter aggregate type and event types",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{
testSetEventTypes("user.created", "user.changed"),
},
},
res: res{
isErr: nil,
2020-09-30 08:00:05 +00:00
query: &repository.SearchQuery{
2020-10-06 19:28:09 +00:00
Columns: repository.ColumnsEvent,
2020-09-24 06:52:10 +00:00
Desc: false,
Limit: 0,
2020-09-30 08:00:05 +00:00
Filters: []*repository.Filter{
2020-10-06 19:28:09 +00:00
repository.NewFilter(repository.FieldAggregateType, AggregateType("user"), repository.OperationEquals),
repository.NewFilter(repository.FieldEventType, []EventType{"user.created", "user.changed"}, repository.OperationIn),
2020-09-24 06:52:10 +00:00
},
},
},
},
{
name: "filter aggregate type resource owner",
args: args{
2020-10-06 19:28:09 +00:00
columns: ColumnsEvent,
2020-09-24 06:52:10 +00:00
aggregateTypes: []AggregateType{"user"},
setters: []func(*SearchQueryFactory) *SearchQueryFactory{
testSetResourceOwner("hodor"),
},
},
res: res{
isErr: nil,
2020-09-30 08:00:05 +00:00
query: &repository.SearchQuery{
2020-10-06 19:28:09 +00:00
Columns: repository.ColumnsEvent,
2020-09-24 06:52:10 +00:00
Desc: false,
Limit: 0,
2020-09-30 08:00:05 +00:00
Filters: []*repository.Filter{
2020-10-06 19:28:09 +00:00
repository.NewFilter(repository.FieldAggregateType, AggregateType("user"), repository.OperationEquals),
repository.NewFilter(repository.FieldResourceOwner, "hodor", repository.OperationEquals),
2020-09-24 06:52:10 +00:00
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2020-10-06 19:28:09 +00:00
factory := NewSearchQueryFactory(tt.args.columns, tt.args.aggregateTypes...)
2020-09-24 06:52:10 +00:00
for _, f := range tt.args.setters {
factory = f(factory)
}
2020-10-06 19:28:09 +00:00
query, err := factory.build()
2020-09-24 06:52:10 +00:00
if tt.res.isErr != nil && !tt.res.isErr(err) {
t.Errorf("wrong error: %v", err)
return
}
if err != nil && tt.res.isErr == nil {
t.Errorf("no error expected: %v", err)
return
}
if !reflect.DeepEqual(query, tt.res.query) {
t.Errorf("NewSearchQueryFactory() = %v, want %v", factory, tt.res)
}
})
}
}