feat(eventstore): Precondition (#69)

* start org

* refactor(eventstore): filter in sql for querier

* feat(eventstore): Aggregate precondition

preconditions are checked right before insert. Insert is still transaction save

* feat(eventstore): check preconditions in repository

* test(eventstore): test precondition in models

* test(eventstore): precondition-tests

* refactor(eventstore): querier as type

* fix(precondition): rename validation from precondition to validation

* test(eventstore): isErr func instead of wantErr bool

* fix: delete org files

* remove comment

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
This commit is contained in:
Silvan
2020-04-28 16:01:00 +02:00
committed by GitHub
parent ff11cdba40
commit 33a4802425
10 changed files with 451 additions and 133 deletions

View File

@@ -15,15 +15,21 @@ func (at AggregateType) String() string {
type Aggregates []*Aggregate
type Aggregate struct {
id string
typ AggregateType
latestSequence uint64
version Version
id string
typ AggregateType
PreviousSequence uint64
version Version
editorService string
editorUser string
resourceOwner string
Events []*Event
Precondition *precondition
}
type precondition struct {
Query *SearchQuery
Validation func(...*Event) error
}
func (a *Aggregate) AppendEvent(typ EventType, payload interface{}) (*Aggregate, error) {
@@ -39,7 +45,6 @@ func (a *Aggregate) AppendEvent(typ EventType, payload interface{}) (*Aggregate,
CreationDate: time.Now(),
Data: data,
Type: typ,
PreviousSequence: a.latestSequence,
AggregateID: a.id,
AggregateType: a.typ,
AggregateVersion: a.version,
@@ -52,6 +57,11 @@ func (a *Aggregate) AppendEvent(typ EventType, payload interface{}) (*Aggregate,
return a, nil
}
func (a *Aggregate) SetPrecondition(query *SearchQuery, validateFunc func(...*Event) error) *Aggregate {
a.Precondition = &precondition{Query: query, Validation: validateFunc}
return a
}
func (a *Aggregate) Validate() error {
if a == nil {
return errors.ThrowPreconditionFailed(nil, "MODEL-yi5AC", "aggregate is nil")
@@ -75,6 +85,9 @@ func (a *Aggregate) Validate() error {
if a.resourceOwner == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-eBYUW", "resource owner not set")
}
if a.Precondition != nil && (a.Precondition.Query == nil || a.Precondition.Query.Validate() != nil || a.Precondition.Validation == nil) {
return errors.ThrowPreconditionFailed(nil, "MODEL-EEUvA", "invalid precondition")
}
return nil
}

View File

@@ -16,20 +16,20 @@ func NewAggregateCreator(serviceName string) *AggregateCreator {
type option func(*Aggregate)
func (c *AggregateCreator) NewAggregate(ctx context.Context, id string, typ AggregateType, version Version, latestSequence uint64, opts ...option) (*Aggregate, error) {
func (c *AggregateCreator) NewAggregate(ctx context.Context, id string, typ AggregateType, version Version, previousSequence uint64, opts ...option) (*Aggregate, error) {
ctxData := auth.GetCtxData(ctx)
editorUser := ctxData.UserID
resourceOwner := ctxData.OrgID
aggregate := &Aggregate{
id: id,
typ: typ,
latestSequence: latestSequence,
version: version,
Events: make([]*Event, 0, 2),
editorService: c.serviceName,
editorUser: editorUser,
resourceOwner: resourceOwner,
id: id,
typ: typ,
PreviousSequence: previousSequence,
version: version,
Events: make([]*Event, 0, 2),
editorService: c.serviceName,
editorUser: editorUser,
resourceOwner: resourceOwner,
}
for _, opt := range opts {

View File

@@ -2,6 +2,8 @@ package models
import (
"testing"
"github.com/caos/zitadel/internal/errors"
)
func TestAggregate_AppendEvent(t *testing.T) {
@@ -34,17 +36,24 @@ func TestAggregate_AppendEvent(t *testing.T) {
wantErr: true,
},
{
name: "event added",
fields: fields{aggregate: &Aggregate{Events: []*Event{}}},
args: args{typ: "user.deactivated"},
want: &Aggregate{Events: []*Event{&Event{Type: "user.deactivated"}}},
name: "event added",
fields: fields{aggregate: &Aggregate{Events: []*Event{}}},
args: args{typ: "user.deactivated"},
want: &Aggregate{Events: []*Event{
{Type: "user.deactivated"},
}},
wantErr: false,
},
{
name: "event added",
fields: fields{aggregate: &Aggregate{Events: []*Event{&Event{}}}},
args: args{typ: "user.deactivated"},
want: &Aggregate{Events: []*Event{&Event{}, &Event{Type: "user.deactivated"}}},
name: "event added",
fields: fields{aggregate: &Aggregate{Events: []*Event{
{},
}}},
args: args{typ: "user.deactivated"},
want: &Aggregate{Events: []*Event{
{},
{Type: "user.deactivated"},
}},
wantErr: false,
},
}
@@ -84,90 +93,218 @@ func TestAggregate_Validate(t *testing.T) {
name: "no id error",
wantErr: true,
fields: fields{aggregate: &Aggregate{
typ: "user",
version: "v1.0.0",
editorService: "svc",
editorUser: "hodor",
resourceOwner: "org",
latestSequence: 5,
Events: []*Event{&Event{
AggregateType: "user",
AggregateVersion: "v1.0.0",
EditorService: "management",
EditorUser: "hodor",
ResourceOwner: "org",
Type: "born",
}},
typ: "user",
version: "v1.0.0",
editorService: "svc",
editorUser: "hodor",
resourceOwner: "org",
PreviousSequence: 5,
Events: []*Event{
{
AggregateType: "user",
AggregateVersion: "v1.0.0",
EditorService: "management",
EditorUser: "hodor",
ResourceOwner: "org",
Type: "born",
}},
}},
},
{
name: "no type error",
wantErr: true,
fields: fields{aggregate: &Aggregate{
id: "aggID",
version: "v1.0.0",
editorService: "svc",
editorUser: "hodor",
resourceOwner: "org",
latestSequence: 5,
Events: []*Event{&Event{
AggregateID: "hodor",
AggregateVersion: "v1.0.0",
EditorService: "management",
EditorUser: "hodor",
ResourceOwner: "org",
Type: "born",
}},
id: "aggID",
version: "v1.0.0",
editorService: "svc",
editorUser: "hodor",
resourceOwner: "org",
PreviousSequence: 5,
Events: []*Event{
{
AggregateID: "hodor",
AggregateVersion: "v1.0.0",
EditorService: "management",
EditorUser: "hodor",
ResourceOwner: "org",
Type: "born",
}},
}},
},
{
name: "invalid version error",
wantErr: true,
fields: fields{aggregate: &Aggregate{
id: "aggID",
typ: "user",
editorService: "svc",
editorUser: "hodor",
resourceOwner: "org",
latestSequence: 5,
Events: []*Event{&Event{
AggregateID: "hodor",
AggregateType: "user",
EditorService: "management",
EditorUser: "hodor",
ResourceOwner: "org",
Type: "born",
}},
id: "aggID",
typ: "user",
editorService: "svc",
editorUser: "hodor",
resourceOwner: "org",
PreviousSequence: 5,
Events: []*Event{
{
AggregateID: "hodor",
AggregateType: "user",
EditorService: "management",
EditorUser: "hodor",
ResourceOwner: "org",
Type: "born",
}},
}},
},
{
name: "validation ok",
name: "no query in precondition error",
wantErr: true,
fields: fields{aggregate: &Aggregate{
id: "aggID",
typ: "user",
version: "v1.0.0",
editorService: "svc",
editorUser: "hodor",
resourceOwner: "org",
PreviousSequence: 5,
Precondition: &precondition{
Validation: func(...*Event) error { return nil },
},
Events: []*Event{
{
AggregateID: "hodor",
AggregateType: "user",
AggregateVersion: "v1.0.0",
EditorService: "management",
EditorUser: "hodor",
ResourceOwner: "org",
Type: "born",
}},
}},
},
{
name: "no func in precondition error",
wantErr: true,
fields: fields{aggregate: &Aggregate{
id: "aggID",
typ: "user",
version: "v1.0.0",
editorService: "svc",
editorUser: "hodor",
resourceOwner: "org",
PreviousSequence: 5,
Precondition: &precondition{
Query: NewSearchQuery().AggregateIDFilter("hodor"),
},
Events: []*Event{
{
AggregateID: "hodor",
AggregateType: "user",
AggregateVersion: "v1.0.0",
EditorService: "management",
EditorUser: "hodor",
ResourceOwner: "org",
Type: "born",
}},
}},
},
{
name: "validation without precondition ok",
wantErr: false,
fields: fields{aggregate: &Aggregate{
id: "aggID",
typ: "user",
version: "v1.0.0",
editorService: "svc",
editorUser: "hodor",
resourceOwner: "org",
latestSequence: 5,
Events: []*Event{&Event{
AggregateID: "hodor",
AggregateType: "user",
AggregateVersion: "v1.0.0",
EditorService: "management",
EditorUser: "hodor",
ResourceOwner: "org",
Type: "born",
}},
id: "aggID",
typ: "user",
version: "v1.0.0",
editorService: "svc",
editorUser: "hodor",
resourceOwner: "org",
PreviousSequence: 5,
Events: []*Event{
{
AggregateID: "hodor",
AggregateType: "user",
AggregateVersion: "v1.0.0",
EditorService: "management",
EditorUser: "hodor",
ResourceOwner: "org",
Type: "born",
}},
}},
},
{
name: "validation with precondition ok",
wantErr: false,
fields: fields{aggregate: &Aggregate{
id: "aggID",
typ: "user",
version: "v1.0.0",
editorService: "svc",
editorUser: "hodor",
resourceOwner: "org",
PreviousSequence: 5,
Precondition: &precondition{
Validation: func(...*Event) error { return nil },
Query: NewSearchQuery().AggregateIDFilter("hodor"),
},
Events: []*Event{
{
AggregateID: "hodor",
AggregateType: "user",
AggregateVersion: "v1.0.0",
EditorService: "management",
EditorUser: "hodor",
ResourceOwner: "org",
Type: "born",
}},
}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.fields.aggregate.Validate(); (err != nil) != tt.wantErr {
err := tt.fields.aggregate.Validate()
if (err != nil) != tt.wantErr {
t.Errorf("Aggregate.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr && !errors.IsPreconditionFailed(err) {
t.Errorf("error must extend precondition failed: %v", err)
}
})
}
}
func TestAggregate_SetPrecondition(t *testing.T) {
type fields struct {
aggregate *Aggregate
}
type args struct {
query *SearchQuery
validateFunc func(...*Event) error
}
tests := []struct {
name string
fields fields
args args
want *Aggregate
}{
{
name: "set precondition",
fields: fields{aggregate: &Aggregate{}},
args: args{
query: &SearchQuery{},
validateFunc: func(...*Event) error { return nil },
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.fields.aggregate.SetPrecondition(tt.args.query, tt.args.validateFunc)
if got.Precondition == nil {
t.Error("precondition must not be nil")
t.FailNow()
}
if got.Precondition.Query == nil {
t.Error("query of precondition must not be nil")
}
if got.Precondition.Validation == nil {
t.Error("precondition func must not be nil")
}
})
}
}

View File

@@ -64,6 +64,9 @@ func (q *SearchQuery) Validate() error {
if q == nil {
return errors.ThrowPreconditionFailed(nil, "MODEL-J5xQi", "search query is nil")
}
if len(q.Filters) == 0 {
return errors.ThrowPreconditionFailed(nil, "MODEL-pF3DR", "no filters set")
}
for _, filter := range q.Filters {
if err := filter.Validate(); err != nil {
return err

View File

@@ -21,17 +21,23 @@ func TestSearchQuery_setFilter(t *testing.T) {
{
name: "set idFilter",
fields: fields{query: NewSearchQuery()},
args: args{filters: []*Filter{&Filter{field: Field_AggregateID, operation: Operation_Equals, value: "hodor"}}},
want: &SearchQuery{Filters: []*Filter{&Filter{field: Field_AggregateID, operation: Operation_Equals, value: "hodor"}}},
args: args{filters: []*Filter{
{field: Field_AggregateID, operation: Operation_Equals, value: "hodor"},
}},
want: &SearchQuery{Filters: []*Filter{
{field: Field_AggregateID, operation: Operation_Equals, value: "hodor"},
}},
},
{
name: "overwrite idFilter",
fields: fields{query: NewSearchQuery()},
args: args{filters: []*Filter{
&Filter{field: Field_AggregateID, operation: Operation_Equals, value: "hodor"},
&Filter{field: Field_AggregateID, operation: Operation_Equals, value: "ursli"},
{field: Field_AggregateID, operation: Operation_Equals, value: "hodor"},
{field: Field_AggregateID, operation: Operation_Equals, value: "ursli"},
}},
want: &SearchQuery{Filters: []*Filter{
{field: Field_AggregateID, operation: Operation_Equals, value: "ursli"},
}},
want: &SearchQuery{Filters: []*Filter{&Filter{field: Field_AggregateID, operation: Operation_Equals, value: "ursli"}}},
},
}
for _, tt := range tests {