forked from hallgren/eventsourcing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshotrepository_test.go
172 lines (140 loc) · 4.03 KB
/
snapshotrepository_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
package eventsourcing_test
import (
"context"
"errors"
"testing"
"github.com/hallgren/eventsourcing"
"github.com/hallgren/eventsourcing/eventstore/memory"
snap "github.com/hallgren/eventsourcing/snapshotstore/memory"
)
func setupSnapshotRepository() *eventsourcing.SnapshotRepository {
eventrepo := eventsourcing.NewEventRepository(memory.Create())
eventrepo.Register(&Person{})
return eventsourcing.NewSnapshotRepository(snap.Create(), eventrepo)
}
func TestSaveAndGetSnapshot(t *testing.T) {
snapshotrepo := setupSnapshotRepository()
person, err := CreatePerson("kalle")
if err != nil {
t.Fatal(err)
}
err = snapshotrepo.Save(person)
if err != nil {
t.Fatalf("could not save aggregate, err: %v", err)
}
twin := Person{}
err = snapshotrepo.GetWithContext(context.Background(), person.ID(), &twin)
if err != nil {
t.Fatal("could not get aggregate")
}
// Check internal aggregate version
if person.Version() != twin.Version() {
t.Fatalf("Wrong version org %q copy %q", person.Version(), twin.Version())
}
if person.ID() != twin.ID() {
t.Fatalf("Wrong id org %q copy %q", person.ID(), twin.ID())
}
if person.Name != twin.Name {
t.Fatalf("Wrong name org: %q copy %q", person.Name, twin.Name)
}
}
func TestGetNoneExistingSnapshotOrEvents(t *testing.T) {
snapshotrepo := setupSnapshotRepository()
person := Person{}
err := snapshotrepo.GetWithContext(context.Background(), "none_existing_id", &person)
if !errors.Is(err, eventsourcing.ErrAggregateNotFound) {
t.Fatal("should get error when no snapshot or event stored for aggregate")
}
}
func TestGetNoneExistingSnapshot(t *testing.T) {
snapshotrepo := setupSnapshotRepository()
person := Person{}
err := snapshotrepo.GetSnapshot(context.Background(), "none_existing_id", &person)
if !errors.Is(err, eventsourcing.ErrAggregateNotFound) {
t.Fatal("should get error when no snapshot stored for aggregate")
}
}
func TestSaveSnapshotWithUnsavedEvents(t *testing.T) {
snapshotrepo := setupSnapshotRepository()
person, err := CreatePerson("kalle")
if err != nil {
t.Fatal(err)
}
err = snapshotrepo.SaveSnapshot(person)
if err == nil {
t.Fatalf("should not be able to save snapshot with unsaved events")
}
}
// test custom snapshot struct to handle non-exported properties on aggregate
type snapshot struct {
eventsourcing.AggregateRoot
unexported string
Exported string
}
type Event struct{}
type Event2 struct{}
func New() *snapshot {
s := snapshot{}
s.TrackChange(&s, &Event{})
return &s
}
func (s *snapshot) Command() {
s.TrackChange(s, &Event2{})
}
func (s *snapshot) Transition(e eventsourcing.Event) {
switch e.Data().(type) {
case *Event:
s.unexported = "unexported"
s.Exported = "Exported"
case *Event2:
s.unexported = "unexported2"
s.Exported = "Exported2"
}
}
// Register bind the events to the repository when the aggregate is registered.
func (s *snapshot) Register(f eventsourcing.RegisterFunc) {
f(&Event{}, &Event2{})
}
type snapshotInternal struct {
UnExported string
Exported string
}
func (s *snapshot) SerializeSnapshot(m eventsourcing.SerializeFunc) ([]byte, error) {
snap := snapshotInternal{
UnExported: s.unexported,
Exported: s.Exported,
}
return m(snap)
}
func (s *snapshot) DeserializeSnapshot(m eventsourcing.DeserializeFunc, b []byte) error {
snap := snapshotInternal{}
err := m(b, &snap)
if err != nil {
return err
}
s.unexported = snap.UnExported
s.Exported = snap.Exported
return nil
}
func TestSnapshotNoneExported(t *testing.T) {
snapshotrepo := setupSnapshotRepository()
snapshotrepo.Register(&snapshot{})
snap := New()
err := snapshotrepo.Save(snap)
if err != nil {
t.Fatal(err)
}
snap.Command()
snapshotrepo.Save(snap)
snap2 := snapshot{}
err = snapshotrepo.GetWithContext(context.Background(), snap.ID(), &snap2)
if err != nil {
t.Fatal(err)
}
if snap.unexported != snap2.unexported {
t.Fatalf("none exported value differed %s %s", snap.unexported, snap2.unexported)
}
if snap.Exported != snap2.Exported {
t.Fatalf("exported value differed %s %s", snap.Exported, snap2.Exported)
}
}