forked from hallgren/eventsourcing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository_test.go
165 lines (145 loc) · 4.09 KB
/
repository_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
package eventsourcing_test
import (
"github.com/hallgren/eventsourcing"
"github.com/hallgren/eventsourcing/eventstore/memory"
"github.com/hallgren/eventsourcing/serializer/json"
"github.com/hallgren/eventsourcing/snapshotstore"
"testing"
)
func TestSaveAndGetAggregate(t *testing.T) {
serializer := json.New()
serializer.Register(&Person{}, &Born{}, &AgedOneYear{})
repo := eventsourcing.NewRepository(memory.Create(serializer), nil)
person, err := CreatePerson("kalle")
if err != nil {
t.Fatal(err)
}
err = repo.Save(person)
if err != nil {
t.Fatal("could not save aggregate")
}
twin := Person{}
err = repo.Get(string(person.AggregateID), &twin)
if err != nil {
t.Fatal("could not get aggregate")
}
// Check internal aggregate version
if person.AggregateVersion != twin.AggregateVersion {
t.Fatalf("Wrong version org %q copy %q", person.AggregateVersion, twin.AggregateVersion)
}
// Check person Name
if person.Name != twin.Name {
t.Fatalf("Wrong Name org %q copy %q", person.Name, twin.Name)
}
}
func TestSaveAndGetAggregateSnapshotAndEvents(t *testing.T) {
serializer := json.New()
serializer.Register(&Person{}, &Born{}, &AgedOneYear{})
snapshot := snapshotstore.New(serializer)
repo := eventsourcing.NewRepository(memory.Create(serializer), snapshot)
person, err := CreatePerson("kalle")
if err != nil {
t.Fatal(err)
}
err = repo.Save(person)
if err != nil {
t.Fatal("could not save aggregate")
}
// save person to snapshot store
err = repo.SaveSnapshot(person)
if err != nil {
t.Fatal(err)
}
person.GrowOlder()
repo.Save(person)
twin := Person{}
err = repo.Get(string(person.AggregateID), &twin)
if err != nil {
t.Fatal("could not get aggregate")
}
// Check internal aggregate version
if person.AggregateVersion != twin.AggregateVersion {
t.Fatalf("Wrong version org %q copy %q", person.AggregateVersion, twin.AggregateVersion)
}
// Check person Name
if person.Name != twin.Name {
t.Fatalf("Wrong Name org %q copy %q", person.Name, twin.Name)
}
}
func TestSaveSnapshotWithUnsavedEvents(t *testing.T) {
serializer := json.New()
serializer.Register(&Person{}, &Born{}, &AgedOneYear{})
snapshot := snapshotstore.New(serializer)
repo := eventsourcing.NewRepository(memory.Create(serializer), snapshot)
person, err := CreatePerson("kalle")
if err != nil {
t.Fatal(err)
}
// save person to snapshot store
err = repo.SaveSnapshot(person)
if err == nil {
t.Fatalf("should not save snapshot with unsaved events %v", err)
}
}
func TestSaveSnapshotWithoutSnapshotStore(t *testing.T) {
serializer := json.New()
serializer.Register(&Person{}, &Born{}, &AgedOneYear{})
repo := eventsourcing.NewRepository(memory.Create(serializer), nil)
person, err := CreatePerson("kalle")
if err != nil {
t.Fatal(err)
}
// save person to snapshot store
err = repo.SaveSnapshot(person)
if err == nil {
t.Fatal("should not save snapshot when there is no snapshot store")
}
}
func TestSubscription(t *testing.T) {
counter := 0
f := func(e eventsourcing.Event) {
counter++
}
serializer := json.New()
serializer.Register(&Person{}, &Born{}, &AgedOneYear{})
repo := eventsourcing.NewRepository(memory.Create(serializer), nil)
repo.Subscribe(f)
person, err := CreatePerson("kalle")
if err != nil {
t.Fatal(err)
}
person.GrowOlder()
person.GrowOlder()
person.GrowOlder()
err = repo.Save(person)
if err != nil {
t.Fatal("could not save aggregate")
}
if counter != 4 {
t.Errorf("No global events was received from the stream, got %q", counter)
}
}
func TestSubscriptionSpecific(t *testing.T) {
counter := 0
f := func(e eventsourcing.Event) {
counter++
}
serializer := json.New()
serializer.Register(&Person{}, &Born{}, &AgedOneYear{})
repo := eventsourcing.NewRepository(memory.Create(serializer), nil)
repo.Subscribe(f, &Born{}, &AgedOneYear{})
person, err := CreatePerson("kalle")
if err != nil {
t.Fatal(err)
}
person.GrowOlder()
person.GrowOlder()
person.GrowOlder()
err = repo.Save(person)
if err != nil {
t.Fatal("could not save aggregate")
}
if counter != 4 {
t.Errorf("No global events was received from the stream, got %q", counter)
}
}