zitadel/internal/eventstore/v2/example_test.go

326 lines
8.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"
"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-10-23 14:16:46 +00:00
eventstore.Aggregate
2020-11-06 21:09:19 +00:00
2020-10-14 10:43:31 +00:00
FirstName string
}
2020-10-23 14:16:46 +00:00
func NewUserAggregate(id string) *UserAggregate {
return &UserAggregate{
2020-11-11 16:51:44 +00:00
Aggregate: *eventstore.NewAggregate(
id,
"test.user",
"caos",
"v1",
0,
),
2020-10-23 14:16:46 +00:00
}
}
func (rm *UserAggregate) Reduce() error {
2020-11-11 16:51:44 +00:00
for _, event := range rm.Aggregate.Events() {
2020-10-23 14:16:46 +00:00
switch e := event.(type) {
case *UserAddedEvent:
rm.FirstName = e.FirstName
case *UserFirstNameChangedEvent:
rm.FirstName = e.FirstName
}
}
2020-11-11 16:51:44 +00:00
return nil
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
}
2020-11-06 16:25:07 +00:00
func UserAddedEventMapper() (eventstore.EventType, func(*repository.Event) (eventstore.EventReader, error)) {
return "user.added", func(event *repository.Event) (eventstore.EventReader, error) {
2020-10-23 14:16:46 +00:00
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
}
2020-11-06 16:25:07 +00:00
func UserFirstNameChangedMapper() (eventstore.EventType, func(*repository.Event) (eventstore.EventReader, error)) {
return "user.firstName.changed", func(event *repository.Event) (eventstore.EventReader, error) {
2020-10-23 14:16:46 +00:00
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
}
2020-11-06 16:25:07 +00:00
func UserPasswordCheckedMapper() (eventstore.EventType, func(*repository.Event) (eventstore.EventReader, error)) {
return "user.password.checked", func(event *repository.Event) (eventstore.EventReader, error) {
2020-10-23 14:16:46 +00:00
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
}
2020-11-06 16:25:07 +00:00
func UserDeletedMapper() (eventstore.EventType, func(*repository.Event) (eventstore.EventReader, error)) {
return "user.deleted", func(event *repository.Event) (eventstore.EventReader, error) {
2020-10-27 15:03:17 +00:00
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
2020-11-11 16:51:44 +00:00
2020-10-23 14:16:46 +00:00
Users []*UserReadModel
}
2020-11-26 08:19:14 +00:00
func (rm *UsersReadModel) AppendEvents(events ...eventstore.EventReader) {
2020-10-23 14:16:46 +00:00
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 16:25:07 +00:00
user := NewUserReadModel(e.AggregateID())
2020-10-23 14:16:46 +00:00
rm.Users = append(rm.Users, user)
2020-11-26 08:19:14 +00:00
user.AppendEvents(e)
2020-10-23 14:16:46 +00:00
case *UserFirstNameChangedEvent, *UserPasswordCheckedEvent:
2020-10-26 13:49:42 +00:00
//update
2020-11-06 16:25:07 +00:00
_, user := rm.userByID(e.AggregateID())
2020-10-23 14:16:46 +00:00
if user == nil {
2020-11-26 08:19:14 +00:00
return
2020-10-23 14:16:46 +00:00
}
2020-11-26 08:19:14 +00:00
user.AppendEvents(e)
2020-10-27 15:03:17 +00:00
case *UserDeletedEvent:
2020-11-06 16:25:07 +00:00
idx, _ := rm.userByID(e.AggregateID())
2020-10-27 15:03:17 +00:00
if idx < 0 {
2020-11-26 08:19:14 +00:00
return
2020-10-27 15:03:17 +00:00
}
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
}
}
}
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 {
2020-11-11 16:51:44 +00:00
if user.ID == id {
2020-10-23 14:16:46 +00:00
return idx, user
}
}
return -1, nil
}
// ------------------------------------------------------------
// User read model start
// ------------------------------------------------------------
2020-10-14 10:43:31 +00:00
type UserReadModel struct {
eventstore.ReadModel
2020-11-11 16:51:44 +00:00
ID string
2020-10-23 14:16:46 +00:00
FirstName string
pwCheckCount int
lastPasswordCheck time.Time
}
func NewUserReadModel(id string) *UserReadModel {
return &UserReadModel{
2020-11-26 08:19:14 +00:00
ID: id,
2020-10-23 14:16:46 +00:00
}
2020-10-14 10:43:31 +00:00
}
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 16:25:07 +00:00
rm.lastPasswordCheck = e.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-11 16:51:44 +00:00
NewUserAggregate("1").PushEvents(NewUserAddedEvent("hodor")),
NewUserAggregate("2").PushEvents(NewUserAddedEvent("hodor"), NewUserPasswordCheckedEvent(), NewUserPasswordCheckedEvent(), NewUserFirstNameChangedEvent("ueli")),
NewUserAggregate("2").PushEvents(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)
2020-11-26 08:19:14 +00:00
users := UsersReadModel{}
err = es.FilterToReducer(context.Background(), eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, "test.user"), &users)
2020-10-23 14:16:46 +00:00
if err != nil {
t.Errorf("unexpected error on filter to reducer: %v", err)
}
fmt.Printf("%+v", users)
}