zitadel/internal/eventstore/example_test.go

326 lines
8.2 KiB
Go
Raw Normal View History

2020-10-14 12:43:31 +02:00
package eventstore_test
import (
"context"
2020-10-23 16:16:46 +02:00
"testing"
"time"
2020-10-14 12:43:31 +02:00
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/eventstore"
query_repo "github.com/zitadel/zitadel/internal/eventstore/repository/sql"
v3 "github.com/zitadel/zitadel/internal/eventstore/v3"
2020-10-14 12:43:31 +02:00
)
2020-10-23 16:16:46 +02:00
// ------------------------------------------------------------
// User aggregate start
// ------------------------------------------------------------
func NewUserAggregate(id string) *eventstore.Aggregate {
return eventstore.NewAggregate(
authz.NewMockContext("zitadel", "caos", "adlerhurst"),
id,
"test.user",
"v1",
)
2020-10-14 12:43:31 +02:00
}
2020-10-23 16:16:46 +02:00
// ------------------------------------------------------------
// User added event start
// ------------------------------------------------------------
2020-10-14 12:43:31 +02:00
type UserAddedEvent struct {
eventstore.BaseEvent `json:"-"`
2020-11-06 13:47:27 +01:00
2020-10-23 16:16:46 +02:00
FirstName string `json:"firstName"`
2020-11-06 13:47:27 +01:00
}
func NewUserAddedEvent(id string, firstName string) *UserAddedEvent {
2020-11-06 13:47:27 +01:00
return &UserAddedEvent{
FirstName: firstName,
BaseEvent: *eventstore.NewBaseEventForPush(
context.Background(),
NewUserAggregate(id),
"user.added"),
2020-11-06 13:47:27 +01:00
}
2020-10-23 16:16:46 +02:00
}
func UserAddedEventMapper() (eventstore.AggregateType, eventstore.EventType, func(eventstore.Event) (eventstore.Event, error)) {
return "user", "user.added", func(event eventstore.Event) (eventstore.Event, error) {
2020-10-23 16:16:46 +02:00
e := &UserAddedEvent{
2020-11-06 13:47:27 +01:00
BaseEvent: *eventstore.BaseEventFromRepo(event),
2020-10-23 16:16:46 +02:00
}
err := event.Unmarshal(e)
2020-10-23 16:16:46 +02:00
if err != nil {
return nil, err
}
return e, nil
}
2020-10-14 12:43:31 +02:00
}
func (e *UserAddedEvent) Payload() interface{} {
2020-10-14 12:43:31 +02:00
return e
}
func (e *UserAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func (e *UserAddedEvent) Assets() []*eventstore.Asset {
return nil
}
2020-10-23 16:16:46 +02:00
// ------------------------------------------------------------
// User first name changed event start
// ------------------------------------------------------------
2020-10-14 12:43:31 +02:00
type UserFirstNameChangedEvent struct {
2020-11-06 13:47:27 +01:00
eventstore.BaseEvent `json:"-"`
FirstName string `json:"firstName"`
}
func NewUserFirstNameChangedEvent(id, firstName string) *UserFirstNameChangedEvent {
2020-11-06 13:47:27 +01:00
return &UserFirstNameChangedEvent{
FirstName: firstName,
BaseEvent: *eventstore.NewBaseEventForPush(
context.Background(),
NewUserAggregate(id),
"user.firstname.changed"),
2020-11-06 13:47:27 +01:00
}
2020-10-23 16:16:46 +02:00
}
func UserFirstNameChangedMapper() (eventstore.AggregateType, eventstore.EventType, func(eventstore.Event) (eventstore.Event, error)) {
return "user", "user.firstName.changed", func(event eventstore.Event) (eventstore.Event, error) {
2020-10-23 16:16:46 +02:00
e := &UserFirstNameChangedEvent{
2020-11-06 13:47:27 +01:00
BaseEvent: *eventstore.BaseEventFromRepo(event),
2020-10-23 16:16:46 +02:00
}
err := event.Unmarshal(e)
2020-10-23 16:16:46 +02:00
if err != nil {
return nil, err
}
return e, nil
}
2020-10-14 12:43:31 +02:00
}
func (e *UserFirstNameChangedEvent) Payload() interface{} {
2020-10-14 12:43:31 +02:00
return e
}
func (e *UserFirstNameChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func (e *UserFirstNameChangedEvent) Assets() []*eventstore.Asset {
return nil
}
2020-10-23 16:16:46 +02:00
// ------------------------------------------------------------
// User password checked event start
// ------------------------------------------------------------
type UserPasswordCheckedEvent struct {
2020-11-06 13:47:27 +01:00
eventstore.BaseEvent `json:"-"`
}
func NewUserPasswordCheckedEvent(id string) *UserPasswordCheckedEvent {
2020-11-06 13:47:27 +01:00
return &UserPasswordCheckedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
context.Background(),
NewUserAggregate(id),
"user.password.checked"),
2020-11-06 13:47:27 +01:00
}
2020-10-23 16:16:46 +02:00
}
func UserPasswordCheckedMapper() (eventstore.AggregateType, eventstore.EventType, func(eventstore.Event) (eventstore.Event, error)) {
return "user", "user.password.checked", func(event eventstore.Event) (eventstore.Event, error) {
2020-10-23 16:16:46 +02:00
return &UserPasswordCheckedEvent{
2020-11-06 13:47:27 +01:00
BaseEvent: *eventstore.BaseEventFromRepo(event),
2020-10-23 16:16:46 +02:00
}, nil
}
}
func (e *UserPasswordCheckedEvent) Payload() interface{} {
2020-10-23 16:16:46 +02:00
return nil
}
func (e *UserPasswordCheckedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func (e *UserPasswordCheckedEvent) Assets() []*eventstore.Asset {
return nil
}
2020-10-27 16:03:17 +01:00
// ------------------------------------------------------------
// User deleted event
// ------------------------------------------------------------
type UserDeletedEvent struct {
2020-11-06 13:47:27 +01:00
eventstore.BaseEvent `json:"-"`
}
func NewUserDeletedEvent(id string) *UserDeletedEvent {
2020-11-06 13:47:27 +01:00
return &UserDeletedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
context.Background(),
NewUserAggregate(id),
"user.deleted"),
2020-11-06 13:47:27 +01:00
}
2020-10-27 16:03:17 +01:00
}
func UserDeletedMapper() (eventstore.AggregateType, eventstore.EventType, func(eventstore.Event) (eventstore.Event, error)) {
return "user", "user.deleted", func(event eventstore.Event) (eventstore.Event, error) {
2020-10-27 16:03:17 +01:00
return &UserDeletedEvent{
2020-11-06 13:47:27 +01:00
BaseEvent: *eventstore.BaseEventFromRepo(event),
2020-10-27 16:03:17 +01:00
}, nil
}
}
func (e *UserDeletedEvent) Payload() interface{} {
2020-10-27 16:03:17 +01:00
return nil
}
func (e *UserDeletedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func (e *UserDeletedEvent) Assets() []*eventstore.Asset {
return nil
}
2020-10-23 16:16:46 +02:00
// ------------------------------------------------------------
// Users read model start
// ------------------------------------------------------------
type UsersReadModel struct {
eventstore.ReadModel
2020-11-11 17:51:44 +01:00
2020-10-23 16:16:46 +02:00
Users []*UserReadModel
}
func (rm *UsersReadModel) AppendEvents(events ...eventstore.Event) {
2020-10-23 16:16:46 +02:00
rm.ReadModel.AppendEvents(events...)
for _, event := range events {
switch e := event.(type) {
case *UserAddedEvent:
2020-10-26 14:49:42 +01:00
//insert
user := NewUserReadModel(e.Aggregate().ID)
2020-10-23 16:16:46 +02:00
rm.Users = append(rm.Users, user)
2020-11-26 09:19:14 +01:00
user.AppendEvents(e)
2020-10-23 16:16:46 +02:00
case *UserFirstNameChangedEvent, *UserPasswordCheckedEvent:
2020-10-26 14:49:42 +01:00
//update
_, user := rm.userByID(e.Aggregate().ID)
2020-10-23 16:16:46 +02:00
if user == nil {
2020-11-26 09:19:14 +01:00
return
2020-10-23 16:16:46 +02:00
}
2020-11-26 09:19:14 +01:00
user.AppendEvents(e)
2020-10-27 16:03:17 +01:00
case *UserDeletedEvent:
idx, _ := rm.userByID(e.Aggregate().ID)
2020-10-27 16:03:17 +01:00
if idx < 0 {
2020-11-26 09:19:14 +01:00
return
2020-10-27 16:03:17 +01: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 16:16:46 +02: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 17:51:44 +01:00
if user.ID == id {
2020-10-23 16:16:46 +02:00
return idx, user
}
}
return -1, nil
}
// ------------------------------------------------------------
// User read model start
// ------------------------------------------------------------
2020-10-14 12:43:31 +02:00
type UserReadModel struct {
eventstore.ReadModel
2020-11-11 17:51:44 +01:00
ID string
2020-10-23 16:16:46 +02:00
FirstName string
pwCheckCount int
lastPasswordCheck time.Time
}
func NewUserReadModel(id string) *UserReadModel {
return &UserReadModel{
2020-11-26 09:19:14 +01:00
ID: id,
2020-10-23 16:16:46 +02:00
}
2020-10-14 12:43:31 +02: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 16:16:46 +02:00
case *UserPasswordCheckedEvent:
rm.pwCheckCount++
2020-11-06 17:25:07 +01:00
rm.lastPasswordCheck = e.CreationDate()
2020-10-14 12:43:31 +02:00
}
}
2020-10-23 16:16:46 +02:00
rm.ReadModel.Reduce()
2020-10-14 12:43:31 +02:00
return nil
}
2020-10-23 16:16:46 +02:00
// ------------------------------------------------------------
// Tests
// ------------------------------------------------------------
func TestUserReadModel(t *testing.T) {
es := eventstore.NewEventstore(
&eventstore.Config{
Querier: query_repo.NewCRDB(testCRDBClient),
Pusher: v3.NewEventstore(testCRDBClient),
},
)
2020-10-23 16:16:46 +02:00
es.RegisterFilterEventMapper(UserAddedEventMapper()).
RegisterFilterEventMapper(UserFirstNameChangedMapper()).
2020-10-27 16:03:17 +01:00
RegisterFilterEventMapper(UserPasswordCheckedMapper()).
RegisterFilterEventMapper(UserDeletedMapper())
2020-10-23 16:16:46 +02:00
events, err := es.Push(context.Background(),
NewUserAddedEvent("1", "hodor"),
NewUserAddedEvent("2", "hodor"),
NewUserPasswordCheckedEvent("2"),
NewUserPasswordCheckedEvent("2"),
NewUserFirstNameChangedEvent("2", "ueli"),
NewUserDeletedEvent("2"))
2020-10-23 16:16:46 +02:00
if err != nil {
t.Errorf("unexpected error on push aggregates: %v", err)
}
events = append(events, nil)
t.Logf("%+v\n", events)
2020-10-23 16:16:46 +02:00
2020-11-26 09:19:14 +01:00
users := UsersReadModel{}
err = es.FilterToReducer(context.Background(), eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).AddQuery().AggregateTypes("test.user").Builder(), &users)
2020-10-23 16:16:46 +02:00
if err != nil {
t.Errorf("unexpected error on filter to reducer: %v", err)
}
t.Logf("%+v", users)
2020-10-23 16:16:46 +02:00
}