mirror of
https://github.com/zitadel/zitadel.git
synced 2025-04-27 15:41:00 +00:00
fix: add tests
This commit is contained in:
parent
71fd4bf9f9
commit
639872b82e
@ -276,6 +276,22 @@ func Test_eventData(t *testing.T) {
|
|||||||
wantErr: true,
|
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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
136
internal/eventstore/v2/example_test.go
Normal file
136
internal/eventstore/v2/example_test.go
Normal 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
|
||||||
|
}
|
@ -42,6 +42,26 @@ func (p *ProjectReadModel) Append(events ...Event) {
|
|||||||
p.events = append(p.events, events...)
|
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
|
//Reduce calculates the new state of the read model
|
||||||
func (p *ProjectReadModel) Reduce() error {
|
func (p *ProjectReadModel) Reduce() error {
|
||||||
for i := range p.Apps {
|
for i := range p.Apps {
|
||||||
@ -69,26 +89,6 @@ func (p *ProjectReadModel) Reduce() error {
|
|||||||
return nil
|
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
|
//Event is the minimal representation of a event
|
||||||
// which can be processed by the read models
|
// which can be processed by the read models
|
||||||
type Event interface {
|
type Event interface {
|
15
internal/eventstore/v2/read_model.go
Normal file
15
internal/eventstore/v2/read_model.go
Normal 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...)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user