-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhippo_test.go
304 lines (246 loc) · 7.4 KB
/
hippo_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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package hippo_test
import (
"context"
"sync"
"testing"
"github.com/aukbit/hippo"
"github.com/aukbit/hippo/mock"
pb "github.com/aukbit/hippo/test/proto"
"github.com/aukbit/rand"
"github.com/paulormart/assert"
)
func TestStore_Dispatch(t *testing.T) {
user := pb.User{
Id: rand.String(10),
Name: "Luke",
Email: "[email protected]",
}
// Create new event for user_created topic.
ev1 := hippo.NewEventProto("user_created", user.GetId(), &user)
var ss mock.StoreService
var es mock.EventService
// Mock StoreService.EventService() call.
ss.EventServiceFn = func() *mock.EventService {
return &es
}
// Mock EventService.List()
es.ListFn = func(ctx context.Context, p hippo.Params) ([]*hippo.Event, error) {
return []*hippo.Event{ev1}, nil
}
// Mock EventService.GetLastVersion()
es.GetLastVersionFn = func(ctx context.Context, aggregateID string) (int64, error) {
return 0, nil
}
// Mock EventService.GetLastVersion()
es.CreateFn = func(ctx context.Context, e *hippo.Event) error {
return nil
}
// Domain Type Rules
rules := func(topic string, buffer, previous interface{}) (next interface{}) {
switch topic {
default:
return previous
case "user_created":
return buffer
}
}
clt := hippo.NewClient(&ss)
clt.RegisterDomainRules(rules, &pb.User{})
ctx := context.Background()
// Create event 1 in store.
if store, err := clt.Dispatch(ctx, ev1, &user); err != nil {
t.Fatal(err)
} else if _, ok := store.State.(*pb.User); !ok {
t.Fatalf("unexpected store state type: %T ", store.State)
} else if store.Version != 1 {
t.Fatalf("unexpected store version: %d ", store.Version)
}
assert.Equal(t, true, ss.EventServiceInvoked)
assert.Equal(t, true, es.CreateInvoked)
assert.Equal(t, true, es.GetLastVersionInvoked)
assert.Equal(t, true, es.ListInvoked)
// Create event 2 for nothing_changed topic.
user2 := pb.User{
Id: user.GetId(),
}
ev2 := hippo.NewEventProto("nothing_changed", user.GetId(), &user2)
// Mock EventService.List()
es.ListFn = func(ctx context.Context, p hippo.Params) ([]*hippo.Event, error) {
return []*hippo.Event{ev1, ev2}, nil
}
// Create event 2 in store.
if store, err := clt.Dispatch(ctx, ev2, &user2); err != nil {
t.Fatal(err)
} else if u, ok := store.State.(*pb.User); !ok {
t.Fatalf("unexpected store state type: %T ", store.State)
} else if u.GetId() != user.GetId() {
t.Fatalf("unexpected id %v ", u.GetId())
} else if u.GetName() != user.GetName() {
t.Fatalf("unexpected name %v ", u.GetName())
} else if u.GetEmail() != user.GetEmail() {
t.Fatalf("unexpected email %v ", u.GetEmail())
}
}
func TestStore_FetchNoEvents(t *testing.T) {
var ss mock.StoreService
var es mock.EventService
// Mock StoreService.EventService() call.
ss.EventServiceFn = func() *mock.EventService {
return &es
}
// Mock EventService.List()
es.ListFn = func(ctx context.Context, p hippo.Params) ([]*hippo.Event, error) {
return []*hippo.Event{}, nil
}
// Mock EventService.GetLastVersion()
es.GetLastVersionFn = func(ctx context.Context, aggregateID string) (int64, error) {
return 0, nil
}
// Mock EventService.GetLastVersion()
es.CreateFn = func(ctx context.Context, e *hippo.Event) error {
return nil
}
// Domain Type Rules
rules := func(topic string, buffer, previous interface{}) (next interface{}) {
switch topic {
default:
return previous
case "user_created":
return buffer
}
}
clt := hippo.NewClient(&ss)
clt.RegisterDomainRules(rules, &pb.User{})
ctx := context.Background()
_, err := clt.Fetch(ctx, "someid", &pb.User{})
assert.Equal(t, hippo.ErrAggregateIDWithoutEvents, err)
}
func TestStore_WithCache(t *testing.T) {
user := pb.User{
Id: rand.String(10),
Name: "test",
Email: "[email protected]",
}
// Create new event for user_created topic.
ev1 := hippo.NewEventProto("user_created", user.GetId(), &user)
var ss mock.StoreService
var es mock.EventService
var cs mock.CacheService
// Mock StoreService.EventService() call.
ss.EventServiceFn = func() *mock.EventService {
return &es
}
// Mock EventService.GetLastVersion()
es.GetLastVersionFn = func(ctx context.Context, aggregateID string) (int64, error) {
return 0, nil
}
// Mock EventService.GetLastVersion()
es.CreateFn = func(ctx context.Context, e *hippo.Event) error {
return nil
}
// Mock CacheService.Get() call.
cs.GetFn = func(ctx context.Context, aggregateID string, out *hippo.Aggregate) error {
return nil
}
cs.SetFn = func(ctx context.Context, aggregateID string, in *hippo.Aggregate) error {
return nil
}
// Domain Type Rules
rules := func(topic string, buffer, previous interface{}) (next interface{}) {
p := previous.(*pb.User)
switch topic {
default:
return previous
case "user_created":
return buffer
case "user_updated":
b := buffer.(*pb.User)
if b.GetName() != "" && p.GetName() != b.GetName() {
p.Name = b.GetName()
}
return p
}
}
clt := hippo.NewClient(&ss)
clt.RegisterDomainRules(rules, &pb.User{})
//
clt.RegisterCacheService(&cs)
ctx := context.Background()
// Create event 1 in store.
if store, err := clt.Dispatch(ctx, ev1, &pb.User{}); err != nil {
t.Fatal(err)
} else if _, ok := store.State.(*pb.User); !ok {
t.Fatalf("unexpected store state type: %T ", store.State)
} else if store.Version != 1 {
t.Fatalf("unexpected store version: %d ", store.Version)
}
assert.Equal(t, true, ss.EventServiceInvoked)
assert.Equal(t, true, cs.GetInvoked)
assert.Equal(t, true, cs.SetInvoked)
assert.Equal(t, true, es.CreateInvoked)
assert.Equal(t, true, es.GetLastVersionInvoked)
}
func TestStore_WithSubscribers(t *testing.T) {
wg := &sync.WaitGroup{}
user := pb.User{
Id: rand.String(10),
Name: "Luke",
Email: "[email protected]",
}
// Create new event for user_created topic.
ev1 := hippo.NewEventProto("user_created", user.GetId(), &user)
var ss mock.StoreService
var es mock.EventService
// Mock StoreService.EventService() call.
ss.EventServiceFn = func() *mock.EventService {
return &es
}
// Mock EventService.List()
es.ListFn = func(ctx context.Context, p hippo.Params) ([]*hippo.Event, error) {
return []*hippo.Event{ev1}, nil
}
// Mock EventService.GetLastVersion()
es.GetLastVersionFn = func(ctx context.Context, aggregateID string) (int64, error) {
return 0, nil
}
// Mock EventService.GetLastVersion()
es.CreateFn = func(ctx context.Context, e *hippo.Event) error {
return nil
}
// Domain Type Rules
rules := func(topic string, buffer, previous interface{}) (next interface{}) {
switch topic {
default:
return previous
case "user_created":
return buffer
}
}
clt := hippo.NewClient(&ss)
clt.RegisterDomainRules(rules, &pb.User{})
ctx := context.Background()
// Subscribe
wg.Add(1)
c1 := make(chan *hippo.Event, 1)
hippo.Subscribe(c1, hippo.ActionTopics{ev1.GetTopic(): []hippo.ActionFn{}})
go func() {
defer wg.Done()
ev1 := <-c1
assert.Equal(t, "user_created", ev1.Topic)
}()
// Create event 1 in store.
if store, err := clt.Dispatch(ctx, ev1, &pb.User{}); err != nil {
t.Fatal(err)
} else if _, ok := store.State.(*pb.User); !ok {
t.Fatalf("unexpected store state type: %T ", store.State)
} else if store.Version != 1 {
t.Fatalf("unexpected store version: %d ", store.Version)
}
assert.Equal(t, true, ss.EventServiceInvoked)
assert.Equal(t, true, es.CreateInvoked)
assert.Equal(t, true, es.GetLastVersionInvoked)
assert.Equal(t, true, es.ListInvoked)
// Wait and unsubscribe
wg.Wait()
hippo.Unsubscribe(c1)
}