fix: add tests

This commit is contained in:
adlerhurst 2020-10-14 12:43:31 +02:00
parent 71fd4bf9f9
commit 639872b82e
5 changed files with 187 additions and 20 deletions

View File

@ -276,6 +276,22 @@ func Test_eventData(t *testing.T) {
wantErr: true,
},
},
{
name: "invalid because invalid struct for json",
args: args{
event: &testEvent{
data: func() interface{} {
return struct {
Field chan string `json:"field"`
}{}
},
},
},
res: res{
jsonText: []byte(nil),
wantErr: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -0,0 +1,136 @@
package eventstore_test
import (
"context"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
)
type singleAggregateRepo struct {
events []*repository.Event
}
//Health checks if the connection to the storage is available
func (r *singleAggregateRepo) Health(ctx context.Context) error {
return nil
}
// PushEvents adds all events of the given aggregates to the eventstreams of the aggregates.
// This call is transaction save. The transaction will be rolled back if one event fails
func (r *singleAggregateRepo) Push(ctx context.Context, events ...*repository.Event) error {
for _, event := range events {
if event.AggregateType != "test.agg" || event.AggregateID != "test" {
return errors.ThrowPreconditionFailed(nil, "V2-ZVDcA", "wrong aggregate")
}
}
r.events = append(r.events, events...)
return nil
}
// Filter returns all events matching the given search query
func (r *singleAggregateRepo) Filter(ctx context.Context, searchQuery *repository.SearchQuery) (events []*repository.Event, err error) {
return r.events, nil
}
//LatestSequence returns the latests sequence found by the the search query
func (r *singleAggregateRepo) LatestSequence(ctx context.Context, queryFactory *repository.SearchQuery) (uint64, error) {
if len(r.events) == 0 {
return 0, nil
}
return r.events[len(r.events)-1].Sequence, nil
}
type UserAggregate struct {
FirstName string
}
func (a *UserAggregate) ID() string {
return "test"
}
func (a *UserAggregate) Type() eventstore.AggregateType {
return "test.agg"
}
func (a *UserAggregate) Events() []eventstore.Event {
return nil
}
func (a *UserAggregate) ResourceOwner() string {
return "caos"
}
func (a *UserAggregate) Version() eventstore.Version {
return "v1"
}
func (a *UserAggregate) PreviousSequence() uint64 {
return 0
}
type UserAddedEvent struct {
FirstName string
}
func (e *UserAddedEvent) CheckPrevious() bool {
return false
}
func (e *UserAddedEvent) EditorService() string {
return "test.suite"
}
func (e *UserAddedEvent) EditorUser() string {
return "adlerhurst"
}
func (e *UserAddedEvent) Type() eventstore.EventType {
return "user.added"
}
func (e *UserAddedEvent) Data() interface{} {
return e
}
type UserFirstNameChangedEvent struct {
FirstName string
}
func (e *UserFirstNameChangedEvent) CheckPrevious() bool {
return false
}
func (e *UserFirstNameChangedEvent) EditorService() string {
return "test.suite"
}
func (e *UserFirstNameChangedEvent) EditorUser() string {
return "adlerhurst"
}
func (e *UserFirstNameChangedEvent) Type() eventstore.EventType {
return "user.changed"
}
func (e *UserFirstNameChangedEvent) Data() interface{} {
return e
}
type UserReadModel struct {
eventstore.ReadModel
FirstName string
}
func (rm *UserReadModel) AppendEvents(events ...eventstore.Event) error {
rm.ReadModel.Append(events...)
return nil
}
func (rm *UserReadModel) Reduce() error {
for _, event := range rm.ReadModel.Events {
switch e := event.(type) {
case *UserAddedEvent:
rm.FirstName = e.FirstName
case *UserFirstNameChangedEvent:
rm.FirstName = e.FirstName
}
}
return nil
}

View File

@ -42,6 +42,26 @@ func (p *ProjectReadModel) Append(events ...Event) {
p.events = append(p.events, events...)
}
type AppReadModel struct {
ReadModel
Name string
}
//Reduce calculates the new state of the read model
func (a *AppReadModel) Reduce() error {
for _, event := range a.events {
switch e := event.(type) {
case *AddAppEvent:
a.Name = e.Name
a.ID = e.GetID()
case *UpdateAppEvent:
a.Name = e.Name
}
a.ProcessedSequence = event.GetSequence()
}
return nil
}
//Reduce calculates the new state of the read model
func (p *ProjectReadModel) Reduce() error {
for i := range p.Apps {
@ -69,26 +89,6 @@ func (p *ProjectReadModel) Reduce() error {
return nil
}
type AppReadModel struct {
ReadModel
Name string
}
//Reduce calculates the new state of the read model
func (a *AppReadModel) Reduce() error {
for _, event := range a.events {
switch e := event.(type) {
case *AddAppEvent:
a.Name = e.Name
a.ID = e.GetID()
case *UpdateAppEvent:
a.Name = e.Name
}
a.ProcessedSequence = event.GetSequence()
}
return nil
}
//Event is the minimal representation of a event
// which can be processed by the read models
type Event interface {

View File

@ -0,0 +1,15 @@
package eventstore
//ReadModel is the minimum representation of a View model.
// it might be saved in a database or in memory
type ReadModel struct {
ProcessedSequence uint64
ID string
Events []Event
}
//Append adds all the events to the aggregate.
// The function doesn't compute the new state of the read model
func (a *ReadModel) Append(events ...Event) {
a.Events = append(a.Events, events...)
}