zitadel/internal/eventstore/v2/eventstore_test.go

1448 lines
31 KiB
Go
Raw Normal View History

2020-09-24 06:52:10 +00:00
package eventstore
2020-09-30 08:00:05 +00:00
import (
2020-10-21 17:00:41 +00:00
"context"
"fmt"
2020-09-30 08:00:05 +00:00
"reflect"
2020-10-21 17:00:41 +00:00
"sync"
2020-09-30 08:00:05 +00:00
"testing"
2020-11-06 21:09:19 +00:00
"github.com/caos/zitadel/internal/api/authz"
2020-11-26 08:19:14 +00:00
"github.com/caos/zitadel/internal/api/service"
2020-09-30 08:00:05 +00:00
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
)
2020-10-21 17:00:41 +00:00
type testAggregate struct {
id string
events []EventPusher
2020-10-21 17:00:41 +00:00
}
func (a *testAggregate) ID() string {
return a.id
}
func (a *testAggregate) Type() AggregateType {
return "test.aggregate"
}
2020-11-06 12:47:27 +00:00
func (a *testAggregate) Events() []EventPusher {
2020-10-21 17:00:41 +00:00
return a.events
}
func (a *testAggregate) ResourceOwner() string {
return "ro"
}
func (a *testAggregate) Version() Version {
return "v1"
}
2020-10-05 17:09:26 +00:00
// testEvent implements the Event interface
2020-09-30 08:00:05 +00:00
type testEvent struct {
2020-11-06 12:47:27 +00:00
BaseEvent
2020-09-30 08:00:05 +00:00
description string
shouldCheckPrevious bool
2020-10-05 20:02:59 +00:00
data func() interface{}
2020-09-30 08:00:05 +00:00
}
2020-11-06 12:47:27 +00:00
func newTestEvent(description string, data func() interface{}, checkPrevious bool) *testEvent {
return &testEvent{
description: description,
data: data,
shouldCheckPrevious: checkPrevious,
2020-11-06 16:25:07 +00:00
BaseEvent: *NewBaseEventForPush(
2020-11-26 08:19:14 +00:00
service.WithService(authz.NewMockContext("resourceOwner", "editorUser"), "editorService"),
2020-11-06 16:25:07 +00:00
"test.event",
),
2020-11-06 12:47:27 +00:00
}
}
2020-10-05 17:09:26 +00:00
func (e *testEvent) Data() interface{} {
2020-10-05 20:02:59 +00:00
return e.data()
2020-10-05 17:09:26 +00:00
}
func (e *testEvent) UniqueConstraint() []*EventUniqueConstraint {
return nil
}
2020-11-06 16:25:07 +00:00
func testFilterMapper(event *repository.Event) (EventReader, error) {
2020-11-06 12:47:27 +00:00
if event == nil {
return newTestEvent("hodor", nil, false), nil
}
return &testEvent{description: "hodor", BaseEvent: *BaseEventFromRepo(event)}, nil
2020-09-30 08:00:05 +00:00
}
func Test_eventstore_RegisterFilterEventMapper(t *testing.T) {
type fields struct {
eventMapper map[EventType]eventTypeInterceptors
}
type args struct {
eventType EventType
2020-11-06 16:25:07 +00:00
mapper func(*repository.Event) (EventReader, error)
2020-09-30 08:00:05 +00:00
}
type res struct {
2020-11-06 21:09:19 +00:00
event EventReader
2020-10-27 14:42:18 +00:00
mapperCount int
2020-09-30 08:00:05 +00:00
}
2020-09-24 06:52:10 +00:00
tests := []struct {
2020-09-30 08:00:05 +00:00
name string
fields fields
args args
res res
}{
{
name: "no event type",
args: args{
eventType: "",
mapper: testFilterMapper,
},
fields: fields{
eventMapper: map[EventType]eventTypeInterceptors{},
},
res: res{
2020-10-27 14:42:18 +00:00
mapperCount: 0,
2020-09-30 08:00:05 +00:00
},
},
{
name: "no event mapper",
args: args{
eventType: "event.type",
mapper: nil,
},
fields: fields{
eventMapper: map[EventType]eventTypeInterceptors{},
},
res: res{
2020-10-27 14:42:18 +00:00
mapperCount: 0,
2020-09-30 08:00:05 +00:00
},
},
{
name: "new interceptor",
fields: fields{
eventMapper: map[EventType]eventTypeInterceptors{},
},
args: args{
eventType: "event.type",
mapper: testFilterMapper,
},
res: res{
2020-11-06 12:47:27 +00:00
event: newTestEvent("hodor", nil, false),
2020-10-27 14:42:18 +00:00
mapperCount: 1,
2020-09-30 08:00:05 +00:00
},
},
{
name: "existing interceptor new filter mapper",
fields: fields{
eventMapper: map[EventType]eventTypeInterceptors{
"event.type": {},
},
},
args: args{
eventType: "new.event",
mapper: testFilterMapper,
},
res: res{
2020-11-06 12:47:27 +00:00
event: newTestEvent("hodor", nil, false),
2020-10-27 14:42:18 +00:00
mapperCount: 2,
2020-09-30 08:00:05 +00:00
},
},
{
name: "existing interceptor existing filter mapper",
fields: fields{
eventMapper: map[EventType]eventTypeInterceptors{
"event.type": {
2020-11-06 16:25:07 +00:00
eventMapper: func(*repository.Event) (EventReader, error) {
2020-09-30 08:00:05 +00:00
return nil, errors.ThrowUnimplemented(nil, "V2-1qPvn", "unimplemented")
},
},
},
},
args: args{
eventType: "new.event",
mapper: testFilterMapper,
},
res: res{
2020-11-06 12:47:27 +00:00
event: newTestEvent("hodor", nil, false),
2020-10-27 14:42:18 +00:00
mapperCount: 2,
2020-09-30 08:00:05 +00:00
},
},
}
2020-09-24 06:52:10 +00:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2020-09-30 08:00:05 +00:00
es := &Eventstore{
2020-10-27 15:03:17 +00:00
eventInterceptors: tt.fields.eventMapper,
2020-09-30 08:00:05 +00:00
}
2020-10-27 14:42:18 +00:00
es = es.RegisterFilterEventMapper(tt.args.eventType, tt.args.mapper)
2020-10-27 15:03:17 +00:00
if len(es.eventInterceptors) != tt.res.mapperCount {
t.Errorf("unexpected mapper count: want %d, got %d", tt.res.mapperCount, len(es.eventInterceptors))
2020-09-30 08:00:05 +00:00
return
}
2020-10-27 14:42:18 +00:00
if tt.res.mapperCount == 0 {
2020-09-30 08:00:05 +00:00
return
}
2020-10-27 15:03:17 +00:00
mapper := es.eventInterceptors[tt.args.eventType]
2020-10-23 14:16:46 +00:00
event, err := mapper.eventMapper(nil)
2020-09-30 08:00:05 +00:00
if err != nil {
t.Errorf("unexpected error %v", err)
}
if !reflect.DeepEqual(tt.res.event, event) {
2020-11-06 12:47:27 +00:00
t.Errorf("events should be deep equal. \ngot %#v\nwant %#v", event, tt.res.event)
2020-09-30 08:00:05 +00:00
}
2020-09-24 06:52:10 +00:00
})
}
}
2020-10-05 20:02:59 +00:00
func Test_eventData(t *testing.T) {
type args struct {
2020-11-06 12:47:27 +00:00
event EventPusher
2020-10-05 20:02:59 +00:00
}
type res struct {
jsonText []byte
wantErr bool
}
tests := []struct {
name string
args args
res res
}{
{
name: "data as json bytes",
args: args{
2020-11-06 12:47:27 +00:00
event: newTestEvent(
"hodor",
func() interface{} {
2020-10-05 20:02:59 +00:00
return []byte(`{"piff":"paff"}`)
},
2020-11-06 12:47:27 +00:00
false),
2020-10-05 20:02:59 +00:00
},
res: res{
jsonText: []byte(`{"piff":"paff"}`),
wantErr: false,
},
},
{
name: "data as invalid json bytes",
args: args{
2020-11-06 12:47:27 +00:00
event: newTestEvent(
"hodor",
func() interface{} {
2020-10-05 20:02:59 +00:00
return []byte(`{"piffpaff"}`)
},
2020-11-06 12:47:27 +00:00
false),
2020-10-05 20:02:59 +00:00
},
res: res{
jsonText: []byte(nil),
wantErr: true,
},
},
{
name: "data as struct",
args: args{
2020-11-06 12:47:27 +00:00
event: newTestEvent(
"hodor",
func() interface{} {
2020-10-05 20:02:59 +00:00
return struct {
Piff string `json:"piff"`
}{Piff: "paff"}
},
2020-11-06 12:47:27 +00:00
false),
2020-10-05 20:02:59 +00:00
},
res: res{
jsonText: []byte(`{"piff":"paff"}`),
wantErr: false,
},
},
{
name: "data as ptr to struct",
args: args{
2020-11-06 12:47:27 +00:00
event: newTestEvent(
"hodor",
func() interface{} {
2020-10-05 20:02:59 +00:00
return &struct {
Piff string `json:"piff"`
}{Piff: "paff"}
},
2020-11-06 12:47:27 +00:00
false),
2020-10-05 20:02:59 +00:00
},
res: res{
jsonText: []byte(`{"piff":"paff"}`),
wantErr: false,
},
},
{
name: "no data",
args: args{
2020-11-06 12:47:27 +00:00
event: newTestEvent(
"hodor",
func() interface{} {
2020-10-05 20:02:59 +00:00
return nil
},
2020-11-06 12:47:27 +00:00
false),
2020-10-05 20:02:59 +00:00
},
res: res{
jsonText: []byte(nil),
wantErr: false,
},
},
{
name: "invalid because primitive",
args: args{
2020-11-06 12:47:27 +00:00
event: newTestEvent(
"hodor",
func() interface{} {
2020-10-05 20:02:59 +00:00
return ""
},
2020-11-06 12:47:27 +00:00
false),
2020-10-05 20:02:59 +00:00
},
res: res{
jsonText: []byte(nil),
wantErr: true,
},
},
{
name: "invalid because pointer to primitive",
args: args{
2020-11-06 12:47:27 +00:00
event: newTestEvent(
"hodor",
func() interface{} {
2020-10-05 20:02:59 +00:00
var s string
return &s
},
2020-11-06 12:47:27 +00:00
false),
2020-10-05 20:02:59 +00:00
},
res: res{
jsonText: []byte(nil),
wantErr: true,
},
},
2020-10-14 10:43:31 +00:00
{
name: "invalid because invalid struct for json",
args: args{
2020-11-06 12:47:27 +00:00
event: newTestEvent(
"hodor",
func() interface{} {
2020-10-14 10:43:31 +00:00
return struct {
Field chan string `json:"field"`
}{}
},
2020-11-06 12:47:27 +00:00
false),
2020-10-14 10:43:31 +00:00
},
res: res{
jsonText: []byte(nil),
wantErr: true,
},
},
2020-10-05 20:02:59 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := eventData(tt.args.event)
if (err != nil) != tt.res.wantErr {
t.Errorf("eventData() error = %v, wantErr %v", err, tt.res.wantErr)
return
}
if !reflect.DeepEqual(got, tt.res.jsonText) {
t.Errorf("eventData() = %v, want %v", string(got), string(tt.res.jsonText))
}
})
}
}
2020-10-21 17:00:41 +00:00
func TestEventstore_aggregatesToEvents(t *testing.T) {
type args struct {
aggregates []Aggregater
2020-10-21 17:00:41 +00:00
}
type res struct {
wantErr bool
events []*repository.Event
}
tests := []struct {
name string
args args
res res
}{
{
name: "one aggregate one event",
args: args{
aggregates: []Aggregater{
2020-10-21 17:00:41 +00:00
&testAggregate{
id: "1",
2020-11-06 12:47:27 +00:00
events: []EventPusher{
newTestEvent(
"",
func() interface{} {
return nil
},
false),
2020-10-21 17:00:41 +00:00
},
},
},
},
res: res{
wantErr: false,
events: []*repository.Event{
{
AggregateID: "1",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
},
},
},
{
name: "one aggregate multiple events",
args: args{
aggregates: []Aggregater{
2020-10-21 17:00:41 +00:00
&testAggregate{
id: "1",
2020-11-06 12:47:27 +00:00
events: []EventPusher{
newTestEvent(
"",
func() interface{} {
return nil
},
false),
newTestEvent(
"",
func() interface{} {
return nil
},
false),
2020-10-21 17:00:41 +00:00
},
},
},
},
res: res{
wantErr: false,
events: []*repository.Event{
{
AggregateID: "1",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
{
AggregateID: "1",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
},
2020-10-21 17:00:41 +00:00
},
},
{
name: "invalid data",
args: args{
aggregates: []Aggregater{
2020-10-21 17:00:41 +00:00
&testAggregate{
id: "1",
2020-11-06 12:47:27 +00:00
events: []EventPusher{
newTestEvent(
"",
func() interface{} {
return `{"data":""`
},
false),
2020-10-21 17:00:41 +00:00
},
},
},
},
res: res{
wantErr: true,
},
},
{
name: "multiple aggregates",
args: args{
aggregates: []Aggregater{
2020-10-21 17:00:41 +00:00
&testAggregate{
id: "1",
2020-11-06 12:47:27 +00:00
events: []EventPusher{
newTestEvent(
"",
func() interface{} {
return nil
},
false),
newTestEvent(
"",
func() interface{} {
return nil
},
false),
2020-10-21 17:00:41 +00:00
},
},
&testAggregate{
id: "2",
2020-11-06 12:47:27 +00:00
events: []EventPusher{
newTestEvent(
"",
func() interface{} {
return nil
},
true),
2020-10-21 17:00:41 +00:00
},
},
},
},
res: res{
wantErr: false,
events: combineEventLists(
[]*repository.Event{
{
AggregateID: "1",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
{
AggregateID: "1",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
},
2020-10-21 17:00:41 +00:00
[]*repository.Event{
{
AggregateID: "2",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
},
),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
es := &Eventstore{}
events, _, err := es.aggregatesToEvents(tt.args.aggregates)
2020-10-21 17:00:41 +00:00
if (err != nil) != tt.res.wantErr {
t.Errorf("Eventstore.aggregatesToEvents() error = %v, wantErr %v", err, tt.res.wantErr)
return
}
if err != nil {
return
}
if len(tt.res.events) != len(events) {
t.Errorf("length of events unequal want: %d got %d", len(tt.res.events), len(events))
return
}
for i := 0; i < len(tt.res.events); i++ {
compareEvents(t, tt.res.events[i], events[i])
}
})
}
}
type testRepo struct {
events []*repository.Event
sequence uint64
err error
t *testing.T
}
func (repo *testRepo) Health(ctx context.Context) error {
return nil
}
func (repo *testRepo) Push(ctx context.Context, events []*repository.Event, uniqueConstraints ...*repository.UniqueConstraint) error {
2020-10-21 17:00:41 +00:00
if repo.err != nil {
return repo.err
}
if len(repo.events) != len(events) {
repo.t.Errorf("length of events unequal want: %d got %d", len(repo.events), len(events))
return fmt.Errorf("")
}
for i := 0; i < len(repo.events); i++ {
compareEvents(repo.t, repo.events[i], events[i])
}
return nil
}
2020-10-21 17:29:22 +00:00
func (repo *testRepo) Filter(ctx context.Context, searchQuery *repository.SearchQuery) ([]*repository.Event, error) {
2020-10-21 17:00:41 +00:00
if repo.err != nil {
2020-10-21 17:29:22 +00:00
return nil, repo.err
2020-10-21 17:00:41 +00:00
}
return repo.events, nil
}
func (repo *testRepo) LatestSequence(ctx context.Context, queryFactory *repository.SearchQuery) (uint64, error) {
if repo.err != nil {
return 0, repo.err
}
return repo.sequence, nil
}
func TestEventstore_Push(t *testing.T) {
type args struct {
aggregates []Aggregater
2020-10-21 17:00:41 +00:00
}
type fields struct {
repo *testRepo
2020-11-06 16:25:07 +00:00
eventMapper map[EventType]func(*repository.Event) (EventReader, error)
2020-10-21 17:00:41 +00:00
}
type res struct {
wantErr bool
}
tests := []struct {
name string
args args
fields fields
res res
}{
{
name: "one aggregate one event",
args: args{
aggregates: []Aggregater{
2020-10-21 17:00:41 +00:00
&testAggregate{
id: "1",
2020-11-06 12:47:27 +00:00
events: []EventPusher{
newTestEvent(
"",
func() interface{} {
return nil
},
false),
2020-10-21 17:00:41 +00:00
},
},
},
},
fields: fields{
repo: &testRepo{
t: t,
events: []*repository.Event{
{
AggregateID: "1",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
},
},
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(e *repository.Event) (EventReader, error) {
2020-10-21 17:00:41 +00:00
return &testEvent{}, nil
},
},
},
},
{
name: "one aggregate multiple events",
args: args{
aggregates: []Aggregater{
2020-10-21 17:00:41 +00:00
&testAggregate{
id: "1",
2020-11-06 12:47:27 +00:00
events: []EventPusher{
newTestEvent(
"",
func() interface{} {
return nil
},
false),
newTestEvent(
"",
func() interface{} {
return nil
},
false),
2020-10-21 17:00:41 +00:00
},
},
},
},
fields: fields{
repo: &testRepo{
t: t,
events: []*repository.Event{
{
AggregateID: "1",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
{
AggregateID: "1",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
},
2020-10-21 17:00:41 +00:00
},
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(e *repository.Event) (EventReader, error) {
2020-10-21 17:00:41 +00:00
return &testEvent{}, nil
},
},
},
res: res{
wantErr: false,
},
},
{
name: "multiple aggregates",
args: args{
aggregates: []Aggregater{
2020-10-21 17:00:41 +00:00
&testAggregate{
id: "1",
2020-11-06 12:47:27 +00:00
events: []EventPusher{
newTestEvent(
"",
func() interface{} {
return nil
},
false),
newTestEvent(
"",
func() interface{} {
return nil
},
false),
2020-10-21 17:00:41 +00:00
},
},
&testAggregate{
id: "2",
2020-11-06 12:47:27 +00:00
events: []EventPusher{
newTestEvent(
"",
func() interface{} {
return nil
},
true),
2020-10-21 17:00:41 +00:00
},
},
},
},
fields: fields{
repo: &testRepo{
t: t,
events: combineEventLists(
[]*repository.Event{
{
AggregateID: "1",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
{
AggregateID: "1",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
},
2020-10-21 17:00:41 +00:00
[]*repository.Event{
{
AggregateID: "2",
AggregateType: "test.aggregate",
Data: []byte(nil),
EditorService: "editorService",
EditorUser: "editorUser",
ResourceOwner: "ro",
Type: "test.event",
Version: "v1",
2020-10-21 17:00:41 +00:00
},
},
),
},
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(e *repository.Event) (EventReader, error) {
2020-10-21 17:00:41 +00:00
return &testEvent{}, nil
},
},
},
res: res{
wantErr: false,
},
},
{
name: "push fails",
args: args{
aggregates: []Aggregater{
2020-10-21 17:00:41 +00:00
&testAggregate{
id: "1",
2020-11-06 12:47:27 +00:00
events: []EventPusher{
newTestEvent(
"",
func() interface{} {
return nil
},
false),
2020-10-21 17:00:41 +00:00
},
},
},
},
fields: fields{
repo: &testRepo{
t: t,
err: errors.ThrowInternal(nil, "V2-qaa4S", "test err"),
},
},
res: res{
wantErr: true,
},
},
{
name: "aggreagtes to events mapping fails",
args: args{
aggregates: []Aggregater{
2020-10-21 17:00:41 +00:00
&testAggregate{
id: "1",
2020-11-06 12:47:27 +00:00
events: []EventPusher{
newTestEvent(
"",
func() interface{} {
return `{"data":""`
},
false),
2020-10-21 17:00:41 +00:00
},
},
},
},
fields: fields{
repo: &testRepo{
t: t,
err: errors.ThrowInternal(nil, "V2-qaa4S", "test err"),
},
},
res: res{
wantErr: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
es := &Eventstore{
2020-10-27 15:03:17 +00:00
repo: tt.fields.repo,
interceptorMutex: sync.Mutex{},
eventInterceptors: map[EventType]eventTypeInterceptors{},
2020-10-21 17:00:41 +00:00
}
for eventType, mapper := range tt.fields.eventMapper {
2020-10-27 14:42:18 +00:00
es = es.RegisterFilterEventMapper(eventType, mapper)
}
2020-10-27 15:03:17 +00:00
if len(es.eventInterceptors) != len(tt.fields.eventMapper) {
t.Errorf("register event mapper failed expected mapper amount: %d, got: %d", len(tt.fields.eventMapper), len(es.eventInterceptors))
2020-10-27 14:42:18 +00:00
t.FailNow()
2020-10-21 17:00:41 +00:00
}
_, err := es.PushAggregates(context.Background(), tt.args.aggregates...)
if (err != nil) != tt.res.wantErr {
t.Errorf("Eventstore.aggregatesToEvents() error = %v, wantErr %v", err, tt.res.wantErr)
}
})
}
}
2020-10-21 17:29:22 +00:00
func TestEventstore_FilterEvents(t *testing.T) {
type args struct {
2020-12-01 13:44:19 +00:00
query *SearchQueryBuilder
2020-10-21 17:29:22 +00:00
}
type fields struct {
repo *testRepo
2020-11-06 16:25:07 +00:00
eventMapper map[EventType]func(*repository.Event) (EventReader, error)
2020-10-21 17:29:22 +00:00
}
type res struct {
wantErr bool
}
tests := []struct {
name string
args args
fields fields
res res
}{
{
name: "invalid factory",
args: args{
query: nil,
},
res: res{
wantErr: true,
},
},
{
name: "no events",
args: args{
2020-12-01 13:44:19 +00:00
query: &SearchQueryBuilder{
2020-10-21 17:29:22 +00:00
aggregateTypes: []AggregateType{"no.aggregates"},
columns: repository.ColumnsEvent,
},
},
fields: fields{
repo: &testRepo{
events: []*repository.Event{},
t: t,
},
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(e *repository.Event) (EventReader, error) {
2020-10-21 17:29:22 +00:00
return &testEvent{}, nil
},
},
},
res: res{
wantErr: false,
},
},
{
name: "repo error",
args: args{
2020-12-01 13:44:19 +00:00
query: &SearchQueryBuilder{
2020-10-21 17:29:22 +00:00
aggregateTypes: []AggregateType{"no.aggregates"},
columns: repository.ColumnsEvent,
},
},
fields: fields{
repo: &testRepo{
t: t,
err: errors.ThrowInternal(nil, "V2-RfkBa", "test err"),
},
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(e *repository.Event) (EventReader, error) {
2020-10-21 17:29:22 +00:00
return &testEvent{}, nil
},
},
},
res: res{
wantErr: true,
},
},
{
name: "found events",
args: args{
2020-12-01 13:44:19 +00:00
query: &SearchQueryBuilder{
2020-10-21 17:29:22 +00:00
aggregateTypes: []AggregateType{"test.aggregate"},
columns: repository.ColumnsEvent,
},
},
fields: fields{
repo: &testRepo{
events: []*repository.Event{
{
AggregateID: "test.aggregate",
Type: "test.event",
},
},
t: t,
},
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(e *repository.Event) (EventReader, error) {
2020-10-21 17:29:22 +00:00
return &testEvent{}, nil
},
},
},
res: res{
wantErr: false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
es := &Eventstore{
2020-10-27 15:03:17 +00:00
repo: tt.fields.repo,
interceptorMutex: sync.Mutex{},
eventInterceptors: map[EventType]eventTypeInterceptors{},
2020-10-21 17:29:22 +00:00
}
2020-10-27 14:42:18 +00:00
2020-10-21 17:29:22 +00:00
for eventType, mapper := range tt.fields.eventMapper {
2020-10-27 14:42:18 +00:00
es = es.RegisterFilterEventMapper(eventType, mapper)
}
2020-10-27 15:03:17 +00:00
if len(es.eventInterceptors) != len(tt.fields.eventMapper) {
t.Errorf("register event mapper failed expected mapper amount: %d, got: %d", len(tt.fields.eventMapper), len(es.eventInterceptors))
2020-10-27 14:42:18 +00:00
t.FailNow()
2020-10-21 17:29:22 +00:00
}
_, err := es.FilterEvents(context.Background(), tt.args.query)
if (err != nil) != tt.res.wantErr {
t.Errorf("Eventstore.aggregatesToEvents() error = %v, wantErr %v", err, tt.res.wantErr)
}
})
}
}
func TestEventstore_LatestSequence(t *testing.T) {
type args struct {
2020-12-01 13:44:19 +00:00
query *SearchQueryBuilder
2020-10-21 17:29:22 +00:00
}
type fields struct {
repo *testRepo
}
type res struct {
wantErr bool
}
tests := []struct {
name string
args args
fields fields
res res
}{
{
name: "invalid factory",
args: args{
query: nil,
},
res: res{
wantErr: true,
},
},
{
name: "no events",
args: args{
2020-12-01 13:44:19 +00:00
query: &SearchQueryBuilder{
2020-10-21 17:29:22 +00:00
aggregateTypes: []AggregateType{"no.aggregates"},
columns: repository.ColumnsMaxSequence,
},
},
fields: fields{
repo: &testRepo{
events: []*repository.Event{},
t: t,
},
},
res: res{
wantErr: false,
},
},
{
name: "repo error",
args: args{
2020-12-01 13:44:19 +00:00
query: &SearchQueryBuilder{
2020-10-21 17:29:22 +00:00
aggregateTypes: []AggregateType{"no.aggregates"},
columns: repository.ColumnsMaxSequence,
},
},
fields: fields{
repo: &testRepo{
t: t,
err: errors.ThrowInternal(nil, "V2-RfkBa", "test err"),
},
},
res: res{
wantErr: true,
},
},
{
name: "found events",
args: args{
2020-12-01 13:44:19 +00:00
query: &SearchQueryBuilder{
2020-10-21 17:29:22 +00:00
aggregateTypes: []AggregateType{"test.aggregate"},
columns: repository.ColumnsMaxSequence,
},
},
fields: fields{
repo: &testRepo{
sequence: 50,
t: t,
},
},
res: res{
wantErr: false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
es := &Eventstore{
repo: tt.fields.repo,
}
_, err := es.LatestSequence(context.Background(), tt.args.query)
if (err != nil) != tt.res.wantErr {
t.Errorf("Eventstore.aggregatesToEvents() error = %v, wantErr %v", err, tt.res.wantErr)
}
})
}
}
2020-10-21 17:45:23 +00:00
type testReducer struct {
t *testing.T
2020-11-06 16:25:07 +00:00
events []EventReader
2020-10-21 17:45:23 +00:00
expectedLength int
err error
}
func (r *testReducer) Reduce() error {
r.t.Helper()
if len(r.events) != r.expectedLength {
r.t.Errorf("wrong amount of append events wanted: %d, got %d", r.expectedLength, len(r.events))
}
if r.err != nil {
return r.err
}
return nil
}
2020-11-26 08:19:14 +00:00
func (r *testReducer) AppendEvents(e ...EventReader) {
2020-10-21 17:45:23 +00:00
r.events = append(r.events, e...)
}
func TestEventstore_FilterToReducer(t *testing.T) {
type args struct {
2020-12-01 13:44:19 +00:00
query *SearchQueryBuilder
2020-10-21 17:45:23 +00:00
readModel reducer
}
type fields struct {
repo *testRepo
2020-11-06 16:25:07 +00:00
eventMapper map[EventType]func(*repository.Event) (EventReader, error)
2020-10-21 17:45:23 +00:00
}
type res struct {
wantErr bool
}
tests := []struct {
name string
args args
fields fields
res res
}{
{
name: "invalid factory",
args: args{
query: nil,
},
res: res{
wantErr: true,
},
},
{
name: "no events",
args: args{
2020-12-01 13:44:19 +00:00
query: &SearchQueryBuilder{
2020-10-21 17:45:23 +00:00
aggregateTypes: []AggregateType{"no.aggregates"},
columns: repository.ColumnsEvent,
},
readModel: &testReducer{
t: t,
expectedLength: 0,
},
},
fields: fields{
repo: &testRepo{
events: []*repository.Event{},
t: t,
},
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(e *repository.Event) (EventReader, error) {
2020-10-21 17:45:23 +00:00
return &testEvent{}, nil
},
},
},
res: res{
wantErr: false,
},
},
{
name: "repo error",
args: args{
2020-12-01 13:44:19 +00:00
query: &SearchQueryBuilder{
2020-10-21 17:45:23 +00:00
aggregateTypes: []AggregateType{"no.aggregates"},
columns: repository.ColumnsEvent,
},
readModel: &testReducer{
t: t,
expectedLength: 0,
},
},
fields: fields{
repo: &testRepo{
t: t,
err: errors.ThrowInternal(nil, "V2-RfkBa", "test err"),
},
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(e *repository.Event) (EventReader, error) {
2020-10-21 17:45:23 +00:00
return &testEvent{}, nil
},
},
},
res: res{
wantErr: true,
},
},
{
name: "found events",
args: args{
2020-12-01 13:44:19 +00:00
query: NewSearchQueryBuilder(repository.ColumnsEvent, "test.aggregate"),
2020-10-21 17:45:23 +00:00
readModel: &testReducer{
t: t,
expectedLength: 1,
},
},
fields: fields{
repo: &testRepo{
events: []*repository.Event{
{
AggregateID: "test.aggregate",
Type: "test.event",
},
},
t: t,
},
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(e *repository.Event) (EventReader, error) {
2020-10-21 17:45:23 +00:00
return &testEvent{}, nil
},
},
},
},
{
name: "append in reducer fails",
args: args{
2020-12-01 13:44:19 +00:00
query: &SearchQueryBuilder{
2020-10-21 17:45:23 +00:00
aggregateTypes: []AggregateType{"test.aggregate"},
columns: repository.ColumnsEvent,
},
readModel: &testReducer{
2020-11-26 08:19:14 +00:00
t: t,
err: errors.ThrowInvalidArgument(nil, "V2-W06TG", "test err"),
expectedLength: 1,
2020-10-21 17:45:23 +00:00
},
},
fields: fields{
repo: &testRepo{
events: []*repository.Event{
{
AggregateID: "test.aggregate",
Type: "test.event",
},
},
t: t,
},
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(e *repository.Event) (EventReader, error) {
2020-10-21 17:45:23 +00:00
return &testEvent{}, nil
},
},
},
res: res{
wantErr: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
es := &Eventstore{
2020-10-27 15:03:17 +00:00
repo: tt.fields.repo,
interceptorMutex: sync.Mutex{},
eventInterceptors: map[EventType]eventTypeInterceptors{},
2020-10-21 17:45:23 +00:00
}
for eventType, mapper := range tt.fields.eventMapper {
2020-10-27 14:42:18 +00:00
es = es.RegisterFilterEventMapper(eventType, mapper)
}
2020-10-27 15:03:17 +00:00
if len(es.eventInterceptors) != len(tt.fields.eventMapper) {
t.Errorf("register event mapper failed expected mapper amount: %d, got: %d", len(tt.fields.eventMapper), len(es.eventInterceptors))
2020-10-27 14:42:18 +00:00
t.FailNow()
2020-10-21 17:45:23 +00:00
}
err := es.FilterToReducer(context.Background(), tt.args.query, tt.args.readModel)
if (err != nil) != tt.res.wantErr {
t.Errorf("Eventstore.aggregatesToEvents() error = %v, wantErr %v", err, tt.res.wantErr)
}
})
}
}
2020-10-21 17:00:41 +00:00
func combineEventLists(lists ...[]*repository.Event) []*repository.Event {
events := []*repository.Event{}
for _, list := range lists {
events = append(events, list...)
}
return events
}
func linkEvents(events ...*repository.Event) []*repository.Event {
for i := 1; i < len(events); i++ {
// events[i].PreviousEvent = events[i-1]
2020-10-21 17:00:41 +00:00
}
return events
}
func compareEvents(t *testing.T, want, got *repository.Event) {
t.Helper()
if want.AggregateID != got.AggregateID {
t.Errorf("wrong aggregateID got %q want %q", want.AggregateID, got.AggregateID)
}
if want.AggregateType != got.AggregateType {
t.Errorf("wrong aggregateType got %q want %q", want.AggregateType, got.AggregateType)
}
if !reflect.DeepEqual(want.Data, got.Data) {
t.Errorf("wrong data got %s want %s", string(want.Data), string(got.Data))
}
if want.EditorService != got.EditorService {
t.Errorf("wrong editor service got %q want %q", got.EditorService, want.EditorService)
}
if want.EditorUser != got.EditorUser {
t.Errorf("wrong editor user got %q want %q", got.EditorUser, want.EditorUser)
}
if want.ResourceOwner != got.ResourceOwner {
t.Errorf("wrong resource owner got %q want %q", got.ResourceOwner, want.ResourceOwner)
}
if want.Type != got.Type {
t.Errorf("wrong event type got %q want %q", got.Type, want.Type)
}
if want.Version != got.Version {
t.Errorf("wrong version got %q want %q", got.Version, want.Version)
}
if want.PreviousSequence != got.PreviousSequence {
t.Errorf("wrong previous sequence got %d want %d", got.PreviousSequence, want.PreviousSequence)
}
}
2020-10-21 17:29:22 +00:00
func TestEventstore_mapEvents(t *testing.T) {
type fields struct {
2020-11-06 16:25:07 +00:00
eventMapper map[EventType]func(*repository.Event) (EventReader, error)
2020-10-21 17:29:22 +00:00
}
type args struct {
events []*repository.Event
}
type res struct {
2020-11-06 16:25:07 +00:00
events []EventReader
2020-10-21 17:29:22 +00:00
wantErr bool
}
tests := []struct {
name string
fields fields
args args
res res
}{
{
name: "no mapper",
args: args{
events: []*repository.Event{
{
Type: "no.mapper.found",
},
},
},
fields: fields{
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){},
2020-10-21 17:29:22 +00:00
},
res: res{
2020-11-26 08:19:14 +00:00
//TODO: as long as not all events are implemented in v2 eventstore doesn't return an error
// afterwards it will return an error on un
2020-10-21 17:29:22 +00:00
wantErr: true,
},
},
{
name: "mapping failed",
args: args{
events: []*repository.Event{
{
Type: "test.event",
},
},
},
fields: fields{
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(*repository.Event) (EventReader, error) {
2020-10-21 17:29:22 +00:00
return nil, errors.ThrowInternal(nil, "V2-8FbQk", "test err")
},
},
},
res: res{
wantErr: true,
},
},
{
name: "mapping succeeded",
args: args{
events: []*repository.Event{
{
Type: "test.event",
},
},
},
fields: fields{
2020-11-06 16:25:07 +00:00
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){
"test.event": func(*repository.Event) (EventReader, error) {
2020-10-21 17:29:22 +00:00
return &testEvent{}, nil
},
},
},
res: res{
2020-11-06 16:25:07 +00:00
events: []EventReader{
2020-10-21 17:29:22 +00:00
&testEvent{},
},
wantErr: false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
es := &Eventstore{
2020-10-27 15:03:17 +00:00
interceptorMutex: sync.Mutex{},
eventInterceptors: map[EventType]eventTypeInterceptors{},
2020-10-21 17:29:22 +00:00
}
for eventType, mapper := range tt.fields.eventMapper {
2020-10-27 14:42:18 +00:00
es = es.RegisterFilterEventMapper(eventType, mapper)
}
2020-10-27 15:03:17 +00:00
if len(es.eventInterceptors) != len(tt.fields.eventMapper) {
t.Errorf("register event mapper failed expected mapper amount: %d, got: %d", len(tt.fields.eventMapper), len(es.eventInterceptors))
2020-10-27 14:42:18 +00:00
t.FailNow()
2020-10-21 17:29:22 +00:00
}
gotMappedEvents, err := es.mapEvents(tt.args.events)
if (err != nil) != tt.res.wantErr {
t.Errorf("Eventstore.mapEvents() error = %v, wantErr %v", err, tt.res.wantErr)
return
}
if !reflect.DeepEqual(gotMappedEvents, tt.res.events) {
t.Errorf("Eventstore.mapEvents() = %v, want %v", gotMappedEvents, tt.res.events)
}
})
}
}