forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
363 lines (287 loc) · 11.6 KB
/
command_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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (c) 2016 - The Event Horizon authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eventhorizon
import (
"testing"
"time"
"github.com/google/uuid"
)
func TestCreateCommand(t *testing.T) {
cmd, err := CreateCommand(TestCommandRegisterType)
if err != ErrCommandNotRegistered {
t.Error("there should be a command not registered error:", err)
}
RegisterCommand(func() Command { return &TestCommandRegister{} })
cmd, err = CreateCommand(TestCommandRegisterType)
if err != nil {
t.Error("there should be no error:", err)
}
if cmd.CommandType() != TestCommandRegisterType {
t.Error("the command type should be correct:", cmd.CommandType())
}
}
func TestRegisterCommandEmptyName(t *testing.T) {
defer func() {
if r := recover(); r == nil || r != "eventhorizon: attempt to register empty command type" {
t.Error("there should have been a panic:", r)
}
}()
RegisterCommand(func() Command { return &TestCommandRegisterEmpty{} })
}
func TestRegisterCommandNil(t *testing.T) {
defer func() {
if r := recover(); r == nil || r != "eventhorizon: created command is nil" {
t.Error("there should have been a panic:", r)
}
}()
RegisterCommand(func() Command { return nil })
}
func TestRegisterCommandTwice(t *testing.T) {
defer func() {
if r := recover(); r == nil || r != "eventhorizon: registering duplicate types for \"TestCommandRegisterTwice\"" {
t.Error("there should have been a panic:", r)
}
}()
RegisterCommand(func() Command { return &TestCommandRegisterTwice{} })
RegisterCommand(func() Command { return &TestCommandRegisterTwice{} })
}
func TestUnregisterCommandEmptyName(t *testing.T) {
defer func() {
if r := recover(); r == nil || r != "eventhorizon: attempt to unregister empty command type" {
t.Error("there should have been a panic:", r)
}
}()
UnregisterCommand(TestCommandUnregisterEmptyType)
}
func TestUnregisterCommandTwice(t *testing.T) {
defer func() {
if r := recover(); r == nil || r != "eventhorizon: unregister of non-registered type \"TestCommandUnregisterTwice\"" {
t.Error("there should have been a panic:", r)
}
}()
RegisterCommand(func() Command { return &TestCommandUnregisterTwice{} })
UnregisterCommand(TestCommandUnregisterTwiceType)
UnregisterCommand(TestCommandUnregisterTwiceType)
}
func TestCheckCommand(t *testing.T) {
// Check all fields.
err := CheckCommand(&TestCommandFields{uuid.New(), "command1"})
if err != nil {
t.Error("there should be no error:", err)
}
// Missing required string value.
err = CheckCommand(&TestCommandStringValue{TestID: uuid.New()})
if err == nil || err.Error() != "missing field: Content" {
t.Error("there should be a missing field error:", err)
}
// Missing required int value.
err = CheckCommand(&TestCommandIntValue{TestID: uuid.New()})
if err != nil {
t.Error("there should be no error:", err)
}
// Missing required float value.
err = CheckCommand(&TestCommandFloatValue{TestID: uuid.New()})
if err != nil {
t.Error("there should be no error:", err)
}
// Missing required bool value.
err = CheckCommand(&TestCommandBoolValue{TestID: uuid.New()})
if err != nil {
t.Error("there should be no error:", err)
}
// Missing required slice.
err = CheckCommand(&TestCommandSlice{TestID: uuid.New()})
if err == nil || err.Error() != "missing field: Slice" {
t.Error("there should be a missing field error:", err)
}
// Missing required map.
err = CheckCommand(&TestCommandMap{TestID: uuid.New()})
if err == nil || err.Error() != "missing field: Map" {
t.Error("there should be a missing field error:", err)
}
// Missing required struct.
err = CheckCommand(&TestCommandStruct{TestID: uuid.New()})
if err == nil || err.Error() != "missing field: Struct" {
t.Error("there should be a missing field error:", err)
}
// Missing required time.
err = CheckCommand(&TestCommandTime{TestID: uuid.New()})
if err == nil || err.Error() != "missing field: Time" {
t.Error("there should be a missing field error:", err)
}
// Missing optional field.
err = CheckCommand(&TestCommandOptional{TestID: uuid.New()})
if err != nil {
t.Error("there should be no error:", err)
}
// Missing private field.
err = CheckCommand(&TestCommandPrivate{TestID: uuid.New()})
if err != nil {
t.Error("there should be no error:", err)
}
// Check all array fields.
err = CheckCommand(&TestCommandArray{uuid.New(), [1]string{"string"}, [1]int{0}, [1]struct{ Test string }{struct{ Test string }{"struct"}}})
if err != nil {
t.Error("there should be no error:", err)
}
// Empty array field.
err = CheckCommand(&TestCommandArray{uuid.New(), [1]string{""}, [1]int{0}, [1]struct{ Test string }{struct{ Test string }{"struct"}}})
if err == nil || err.Error() != "missing field: StringArray" {
t.Error("there should be a missing field error:", err)
}
}
// Mocks for Register/Unregister.
const (
TestCommandRegisterType CommandType = "TestCommandRegister"
TestCommandRegisterEmptyType CommandType = ""
TestCommandRegisterTwiceType CommandType = "TestCommandRegisterTwice"
TestCommandUnregisterEmptyType CommandType = ""
TestCommandUnregisterTwiceType CommandType = "TestCommandUnregisterTwice"
TestAggregateType AggregateType = "TestAggregate"
)
type TestCommandRegister struct{}
var _ = Command(TestCommandRegister{})
func (a TestCommandRegister) AggregateID() uuid.UUID { return uuid.Nil }
func (a TestCommandRegister) AggregateType() AggregateType { return TestAggregateType }
func (a TestCommandRegister) CommandType() CommandType { return TestCommandRegisterType }
type TestCommandRegisterEmpty struct{}
var _ = Command(TestCommandRegisterEmpty{})
func (a TestCommandRegisterEmpty) AggregateID() uuid.UUID { return uuid.Nil }
func (a TestCommandRegisterEmpty) AggregateType() AggregateType { return TestAggregateType }
func (a TestCommandRegisterEmpty) CommandType() CommandType { return TestCommandRegisterEmptyType }
type TestCommandRegisterTwice struct{}
var _ = Command(TestCommandRegisterTwice{})
func (a TestCommandRegisterTwice) AggregateID() uuid.UUID { return uuid.Nil }
func (a TestCommandRegisterTwice) AggregateType() AggregateType { return TestAggregateType }
func (a TestCommandRegisterTwice) CommandType() CommandType { return TestCommandRegisterTwiceType }
type TestCommandUnregisterTwice struct{}
var _ = Command(TestCommandUnregisterTwice{})
func (a TestCommandUnregisterTwice) AggregateID() uuid.UUID { return uuid.Nil }
func (a TestCommandUnregisterTwice) AggregateType() AggregateType { return TestAggregateType }
func (a TestCommandUnregisterTwice) CommandType() CommandType { return TestCommandUnregisterTwiceType }
// Mocks for CheckCommand.
type TestCommandFields struct {
TestID uuid.UUID
Content string
}
var _ = Command(TestCommandFields{})
func (t TestCommandFields) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandFields) AggregateType() AggregateType { return TestAggregateType }
func (t TestCommandFields) CommandType() CommandType {
return CommandType("TestCommandFields")
}
type TestCommandStringValue struct {
TestID uuid.UUID
Content string
}
var _ = Command(TestCommandStringValue{})
func (t TestCommandStringValue) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandStringValue) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandStringValue) CommandType() CommandType {
return CommandType("TestCommandStringValue")
}
type TestCommandIntValue struct {
TestID uuid.UUID
Content int
}
var _ = Command(TestCommandIntValue{})
func (t TestCommandIntValue) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandIntValue) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandIntValue) CommandType() CommandType {
return CommandType("TestCommandIntValue")
}
type TestCommandFloatValue struct {
TestID uuid.UUID
Content float32
}
var _ = Command(TestCommandFloatValue{})
func (t TestCommandFloatValue) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandFloatValue) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandFloatValue) CommandType() CommandType {
return CommandType("TestCommandFloatValue")
}
type TestCommandBoolValue struct {
TestID uuid.UUID
Content bool
}
var _ = Command(TestCommandBoolValue{})
func (t TestCommandBoolValue) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandBoolValue) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandBoolValue) CommandType() CommandType {
return CommandType("TestCommandBoolValue")
}
type TestCommandSlice struct {
TestID uuid.UUID
Slice []string
}
var _ = Command(TestCommandSlice{})
func (t TestCommandSlice) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandSlice) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandSlice) CommandType() CommandType { return CommandType("TestCommandSlice") }
type TestCommandMap struct {
TestID uuid.UUID
Map map[string]string
}
var _ = Command(TestCommandMap{})
func (t TestCommandMap) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandMap) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandMap) CommandType() CommandType { return CommandType("TestCommandMap") }
type TestCommandStruct struct {
TestID uuid.UUID
Struct struct {
Test string
}
}
var _ = Command(TestCommandStruct{})
func (t TestCommandStruct) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandStruct) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandStruct) CommandType() CommandType { return CommandType("TestCommandStruct") }
type TestCommandTime struct {
TestID uuid.UUID
Time time.Time
}
var _ = Command(TestCommandTime{})
func (t TestCommandTime) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandTime) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandTime) CommandType() CommandType { return CommandType("TestCommandTime") }
type TestCommandOptional struct {
TestID uuid.UUID
Content string `eh:"optional"`
}
var _ = Command(TestCommandOptional{})
func (t TestCommandOptional) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandOptional) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandOptional) CommandType() CommandType {
return CommandType("TestCommandOptional")
}
type TestCommandPrivate struct {
TestID uuid.UUID
private string
}
var _ = Command(TestCommandPrivate{})
func (t TestCommandPrivate) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandPrivate) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandPrivate) CommandType() CommandType { return CommandType("TestCommandPrivate") }
type TestCommandArray struct {
TestID uuid.UUID
StringArray [1]string
IntArray [1]int
StructArray [1]struct {
Test string
}
}
var _ = Command(TestCommandArray{})
func (t TestCommandArray) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandArray) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandArray) CommandType() CommandType { return CommandType("TestCommandArray") }