zitadel/internal/eventstore/v2/example_test.go

365 lines
9.0 KiB
Go
Raw Normal View History

2020-10-14 10:43:31 +00:00
package eventstore_test
import (
"context"
2020-10-23 14:16:46 +00:00
"encoding/json"
"errors"
"fmt"
"testing"
"time"
2020-10-14 10:43:31 +00:00
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
2020-10-23 14:16:46 +00:00
"github.com/caos/zitadel/internal/eventstore/v2/repository/sql"
2020-10-14 10:43:31 +00:00
)
2020-10-23 14:16:46 +00:00
// ------------------------------------------------------------
// User aggregate start
// ------------------------------------------------------------
2020-10-14 10:43:31 +00:00
type UserAggregate struct {
2020-11-06 12:47:27 +00:00
eventstore.BaseEvent
2020-10-23 14:16:46 +00:00
eventstore.Aggregate
2020-10-14 10:43:31 +00:00
FirstName string
}
func (a *UserAggregate) ID() string {
2020-10-23 14:16:46 +00:00
return a.Aggregate.ID
2020-10-14 10:43:31 +00:00
}
func (a *UserAggregate) Type() eventstore.AggregateType {
2020-10-23 14:16:46 +00:00
return "test.user"
2020-10-14 10:43:31 +00:00
}
2020-11-06 12:47:27 +00:00
func (a *UserAggregate) Events() []eventstore.EventPusher {
events := make([]eventstore.EventPusher, len(a.Aggregate.Events))
for i, event := range a.Aggregate.Events {
events[i] = event
}
return events
2020-10-14 10:43:31 +00:00
}
func (a *UserAggregate) ResourceOwner() string {
return "caos"
}
func (a *UserAggregate) Version() eventstore.Version {
return "v1"
}
func (a *UserAggregate) PreviousSequence() uint64 {
2020-10-23 14:16:46 +00:00
return a.Aggregate.PreviousSequence
}
func NewUserAggregate(id string) *UserAggregate {
return &UserAggregate{
Aggregate: *eventstore.NewAggregate(id),
}
}
func (rm *UserAggregate) AppendEvents(events ...eventstore.Event) *UserAggregate {
rm.Aggregate.AppendEvents(events...)
return rm
}
func (rm *UserAggregate) Reduce() error {
for _, event := range rm.Aggregate.Events {
switch e := event.(type) {
case *UserAddedEvent:
rm.FirstName = e.FirstName
case *UserFirstNameChangedEvent:
rm.FirstName = e.FirstName
}
}
return rm.Aggregate.Reduce()
2020-10-14 10:43:31 +00:00
}
2020-10-23 14:16:46 +00:00
// ------------------------------------------------------------
// User added event start
// ------------------------------------------------------------
2020-10-14 10:43:31 +00:00
type UserAddedEvent struct {
2020-11-06 12:47:27 +00:00
eventstore.BaseEvent `json:"-"`
2020-10-23 14:16:46 +00:00
FirstName string `json:"firstName"`
2020-11-06 12:47:27 +00:00
}
func NewUserAddedEvent(firstName string) *UserAddedEvent {
return &UserAddedEvent{
FirstName: firstName,
BaseEvent: eventstore.BaseEvent{
Service: "test.suite",
User: "adlerhurst",
EventType: "user.added",
},
}
2020-10-23 14:16:46 +00:00
}
func UserAddedEventMapper() (eventstore.EventType, func(*repository.Event) (eventstore.Event, error)) {
return "user.added", func(event *repository.Event) (eventstore.Event, error) {
e := &UserAddedEvent{
2020-11-06 12:47:27 +00:00
BaseEvent: *eventstore.BaseEventFromRepo(event),
2020-10-23 14:16:46 +00:00
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, err
}
return e, nil
}
2020-10-14 10:43:31 +00:00
}
func (e *UserAddedEvent) CheckPrevious() bool {
2020-10-23 14:16:46 +00:00
return true
2020-10-14 10:43:31 +00:00
}
func (e *UserAddedEvent) Data() interface{} {
return e
}
2020-10-23 14:16:46 +00:00
// ------------------------------------------------------------
// User first name changed event start
// ------------------------------------------------------------
2020-10-14 10:43:31 +00:00
type UserFirstNameChangedEvent struct {
2020-11-06 12:47:27 +00:00
eventstore.BaseEvent `json:"-"`
FirstName string `json:"firstName"`
}
func NewUserFirstNameChangedEvent(firstName string) *UserFirstNameChangedEvent {
return &UserFirstNameChangedEvent{
FirstName: firstName,
BaseEvent: eventstore.BaseEvent{
Service: "test.suite",
User: "adlerhurst",
EventType: "user.firstName.changed",
},
}
2020-10-23 14:16:46 +00:00
}
func UserFirstNameChangedMapper() (eventstore.EventType, func(*repository.Event) (eventstore.Event, error)) {
return "user.firstName.changed", func(event *repository.Event) (eventstore.Event, error) {
e := &UserFirstNameChangedEvent{
2020-11-06 12:47:27 +00:00
BaseEvent: *eventstore.BaseEventFromRepo(event),
2020-10-23 14:16:46 +00:00
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, err
}
return e, nil
}
2020-10-14 10:43:31 +00:00
}
func (e *UserFirstNameChangedEvent) CheckPrevious() bool {
2020-10-23 14:16:46 +00:00
return true
2020-10-14 10:43:31 +00:00
}
func (e *UserFirstNameChangedEvent) Data() interface{} {
return e
}
2020-10-23 14:16:46 +00:00
// ------------------------------------------------------------
// User password checked event start
// ------------------------------------------------------------
type UserPasswordCheckedEvent struct {
2020-11-06 12:47:27 +00:00
eventstore.BaseEvent `json:"-"`
}
func NewUserPasswordCheckedEvent() *UserPasswordCheckedEvent {
return &UserPasswordCheckedEvent{
BaseEvent: eventstore.BaseEvent{
Service: "test.suite",
User: "adlerhurst",
EventType: "user.password.checked",
},
}
2020-10-23 14:16:46 +00:00
}
func UserPasswordCheckedMapper() (eventstore.EventType, func(*repository.Event) (eventstore.Event, error)) {
return "user.password.checked", func(event *repository.Event) (eventstore.Event, error) {
return &UserPasswordCheckedEvent{
2020-11-06 12:47:27 +00:00
BaseEvent: *eventstore.BaseEventFromRepo(event),
2020-10-23 14:16:46 +00:00
}, nil
}
}
func (e *UserPasswordCheckedEvent) CheckPrevious() bool {
return false
}
func (e *UserPasswordCheckedEvent) Data() interface{} {
return nil
}
2020-10-27 15:03:17 +00:00
// ------------------------------------------------------------
// User deleted event
// ------------------------------------------------------------
type UserDeletedEvent struct {
2020-11-06 12:47:27 +00:00
eventstore.BaseEvent `json:"-"`
}
func NewUserDeletedEvent() *UserDeletedEvent {
return &UserDeletedEvent{
BaseEvent: eventstore.BaseEvent{
Service: "test.suite",
User: "adlerhurst",
EventType: "user.deleted",
},
}
2020-10-27 15:03:17 +00:00
}
func UserDeletedMapper() (eventstore.EventType, func(*repository.Event) (eventstore.Event, error)) {
return "user.deleted", func(event *repository.Event) (eventstore.Event, error) {
return &UserDeletedEvent{
2020-11-06 12:47:27 +00:00
BaseEvent: *eventstore.BaseEventFromRepo(event),
2020-10-27 15:03:17 +00:00
}, nil
}
}
func (e *UserDeletedEvent) CheckPrevious() bool {
return false
}
func (e *UserDeletedEvent) Data() interface{} {
return nil
}
2020-10-23 14:16:46 +00:00
// ------------------------------------------------------------
// Users read model start
// ------------------------------------------------------------
type UsersReadModel struct {
eventstore.ReadModel
Users []*UserReadModel
}
func NewUsersReadModel() *UsersReadModel {
return &UsersReadModel{
ReadModel: *eventstore.NewReadModel(""),
Users: []*UserReadModel{},
}
}
func (rm *UsersReadModel) AppendEvents(events ...eventstore.Event) (err error) {
rm.ReadModel.AppendEvents(events...)
for _, event := range events {
switch e := event.(type) {
case *UserAddedEvent:
2020-10-26 13:49:42 +00:00
//insert
2020-11-06 12:47:27 +00:00
user := NewUserReadModel(e.Base().AggregateID)
2020-10-23 14:16:46 +00:00
rm.Users = append(rm.Users, user)
err = user.AppendEvents(e)
case *UserFirstNameChangedEvent, *UserPasswordCheckedEvent:
2020-10-26 13:49:42 +00:00
//update
2020-11-06 12:47:27 +00:00
_, user := rm.userByID(e.Base().aggregateID)
2020-10-23 14:16:46 +00:00
if user == nil {
return errors.New("user not found")
}
err = user.AppendEvents(e)
2020-10-27 15:03:17 +00:00
case *UserDeletedEvent:
2020-11-06 12:47:27 +00:00
idx, _ := rm.userByID(e.Base().AggregateID)
2020-10-27 15:03:17 +00:00
if idx < 0 {
return nil
}
copy(rm.Users[idx:], rm.Users[idx+1:])
rm.Users[len(rm.Users)-1] = nil // or the zero value of T
rm.Users = rm.Users[:len(rm.Users)-1]
2020-10-23 14:16:46 +00:00
}
if err != nil {
return err
}
}
2020-10-26 13:49:42 +00:00
2020-10-23 14:16:46 +00:00
return nil
}
func (rm *UsersReadModel) Reduce() error {
for _, user := range rm.Users {
err := user.Reduce()
if err != nil {
return err
}
}
rm.ReadModel.Reduce()
return nil
}
func (rm *UsersReadModel) userByID(id string) (idx int, user *UserReadModel) {
for idx, user = range rm.Users {
if user.ReadModel.ID == id {
return idx, user
}
}
return -1, nil
}
// ------------------------------------------------------------
// User read model start
// ------------------------------------------------------------
2020-10-14 10:43:31 +00:00
type UserReadModel struct {
eventstore.ReadModel
2020-10-23 14:16:46 +00:00
FirstName string
pwCheckCount int
lastPasswordCheck time.Time
}
func NewUserReadModel(id string) *UserReadModel {
return &UserReadModel{
ReadModel: *eventstore.NewReadModel(id),
}
2020-10-14 10:43:31 +00:00
}
func (rm *UserReadModel) AppendEvents(events ...eventstore.Event) error {
2020-10-23 14:16:46 +00:00
rm.ReadModel.AppendEvents(events...)
2020-10-14 10:43:31 +00:00
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
2020-10-23 14:16:46 +00:00
case *UserPasswordCheckedEvent:
rm.pwCheckCount++
2020-11-06 12:47:27 +00:00
rm.lastPasswordCheck = e.Base().CreationDate
2020-10-14 10:43:31 +00:00
}
}
2020-10-23 14:16:46 +00:00
rm.ReadModel.Reduce()
2020-10-14 10:43:31 +00:00
return nil
}
2020-10-23 14:16:46 +00:00
// ------------------------------------------------------------
// Tests
// ------------------------------------------------------------
func TestUserReadModel(t *testing.T) {
es := eventstore.NewEventstore(sql.NewCRDB(testCRDBClient))
es.RegisterFilterEventMapper(UserAddedEventMapper()).
RegisterFilterEventMapper(UserFirstNameChangedMapper()).
2020-10-27 15:03:17 +00:00
RegisterFilterEventMapper(UserPasswordCheckedMapper()).
RegisterFilterEventMapper(UserDeletedMapper())
2020-10-23 14:16:46 +00:00
events, err := es.PushAggregates(context.Background(),
2020-11-06 12:47:27 +00:00
NewUserAggregate("1").AppendEvents(NewUserAddedEvent("hodor")),
NewUserAggregate("2").AppendEvents(NewUserAddedEvent("hodor"), NewUserPasswordCheckedEvent(), NewUserPasswordCheckedEvent(), NewUserFirstNameChangedEvent("ueli")),
NewUserAggregate("2").AppendEvents(NewUserDeletedEvent()),
2020-10-23 14:16:46 +00:00
)
if err != nil {
t.Errorf("unexpected error on push aggregates: %v", err)
}
events = append(events, nil)
fmt.Printf("%+v\n", events)
users := NewUsersReadModel()
err = es.FilterToReducer(context.Background(), eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, "test.user"), users)
if err != nil {
t.Errorf("unexpected error on filter to reducer: %v", err)
}
fmt.Printf("%+v", users)
}