forked from jen20/go-event-sourcing-sample
-
Notifications
You must be signed in to change notification settings - Fork 23
/
aggregateroot_test.go
221 lines (183 loc) · 4.85 KB
/
aggregateroot_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package eventsourcing_test
import (
"errors"
"fmt"
"testing"
"time"
"github.com/hallgren/eventsourcing"
)
// Person aggregate
type Person struct {
eventsourcing.AggregateRoot
Name string
Age int
Dead int
}
// Born event
type Born struct {
Name string
}
// AgedOneYear event
type AgedOneYear struct {
}
// CreatePerson constructor for the Person
func CreatePerson(name string) (*Person, error) {
if name == "" {
return nil, errors.New("name can't be blank")
}
person := Person{}
person.TrackChange(&person, &Born{Name: name})
return &person, nil
}
// CreatePersonWithID constructor for the Person that sets the aggregate ID from the outside
func CreatePersonWithID(id, name string) (*Person, error) {
if name == "" {
return nil, errors.New("name can't be blank")
}
person := Person{}
err := person.SetID(id)
if err == eventsourcing.ErrAggregateAlreadyExists {
return nil, err
} else if err != nil {
return nil, err
}
person.TrackChange(&person, &Born{Name: name})
return &person, nil
}
// GrowOlder command
func (person *Person) GrowOlder() {
metaData := make(map[string]interface{})
metaData["foo"] = "bar"
person.TrackChangeWithMetadata(person, &AgedOneYear{}, metaData)
}
// Register bind the events to the repository when the aggregate is registered.
func (person *Person) Register(f eventsourcing.RegisterFunc) {
f(&Born{}, &AgedOneYear{})
}
// Transition the person state dependent on the events
func (person *Person) Transition(event eventsourcing.Event) {
switch e := event.Data().(type) {
case *Born:
person.Age = 0
person.Name = e.Name
case *AgedOneYear:
person.Age += 1
}
}
func TestPersonWithNoEvents(t *testing.T) {
person := Person{}
if person.Version() != 0 {
t.Fatalf("should have version 0 had %d", person.Version())
}
}
func TestCreateNewPerson(t *testing.T) {
timeBefore := time.Now().UTC()
person, err := CreatePerson("kalle")
if err != nil {
t.Fatal("Error when creating person", err.Error())
}
if person.Name != "kalle" {
t.Fatal("Wrong person Name")
}
if person.Age != 0 {
t.Fatal("Wrong person Age")
}
if len(person.Events()) != 1 {
t.Fatal("There should be one event on the person aggregateRoot")
}
if person.Version() != 1 {
t.Fatal("Wrong version on the person aggregateRoot", person.Version())
}
if person.Events()[0].Timestamp().Before(timeBefore) {
t.Fatal("event timestamp before timeBefore")
}
if person.Events()[0].Timestamp().After(time.Now().UTC()) {
t.Fatal("event timestamp after current time")
}
if person.Events()[0].GlobalVersion() != 0 {
t.Fatalf("global version should not be set when event is created, was %d", person.Events()[0].GlobalVersion())
}
if !person.UnsavedEvents() {
t.Fatal("there should be event on the aggregate")
}
}
func TestCreateNewPersonWithIDFromOutside(t *testing.T) {
id := "123"
person, err := CreatePersonWithID(id, "kalle")
if err != nil {
t.Fatal("Error when creating person", err.Error())
}
if person.ID() != id {
t.Fatal("Wrong aggregate ID on the person aggregateRoot", person.ID())
}
}
func TestBlankName(t *testing.T) {
_, err := CreatePerson("")
if err == nil {
t.Fatal("The constructor should return error on blank Name")
}
}
func TestSetIDOnExistingPerson(t *testing.T) {
person, err := CreatePerson("Kalle")
if err != nil {
t.Fatal("The constructor returned error")
}
err = person.SetID("new_id")
if err == nil {
t.Fatal("Should not be possible to set ID on already existing person")
}
}
func TestPersonAgedOneYear(t *testing.T) {
person, _ := CreatePerson("kalle")
person.GrowOlder()
if len(person.Events()) != 2 {
t.Fatal("There should be two event on the person aggregateRoot", person.Events())
}
if person.Events()[len(person.Events())-1].Reason() != "AgedOneYear" {
t.Fatal("The last event reason should be AgedOneYear", person.Events()[len(person.Events())-1].Reason())
}
d, ok := person.Events()[1].Metadata()["foo"]
if !ok {
t.Fatal("meta data not present")
}
if d.(string) != "bar" {
t.Fatal("wrong meta data")
}
if person.ID() == "" {
t.Fatal("aggregate ID should not be empty")
}
}
func TestPersonGrewTenYears(t *testing.T) {
person, _ := CreatePerson("kalle")
for i := 1; i <= 10; i++ {
person.GrowOlder()
}
if person.Age != 10 {
t.Fatal("person has the wrong Age")
}
}
func TestSetIDFunc(t *testing.T) {
var counter = 0
f := func() string {
counter++
return fmt.Sprint(counter)
}
eventsourcing.SetIDFunc(f)
for i := 1; i < 10; i++ {
person, _ := CreatePerson("kalle")
if person.ID() != fmt.Sprint(i) {
t.Fatalf("id not set via the new SetIDFunc, exp: %d got: %s", i, person.ID())
}
}
}
func TestIDFuncGeneratingRandomIDs(t *testing.T) {
var ids = map[string]struct{}{}
for i := 1; i < 100000; i++ {
person, _ := CreatePerson("kalle")
_, exists := ids[person.ID()]
if exists {
t.Fatalf("id: %s, already created", person.ID())
}
ids[person.ID()] = struct{}{}
}
}