generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate_test.go
290 lines (267 loc) · 8.71 KB
/
aggregate_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
package configkit_test
import (
"context"
"errors"
"reflect"
. "github.com/dogmatiq/configkit"
"github.com/dogmatiq/configkit/fixtures" // can't dot-import due to conflicts
"github.com/dogmatiq/configkit/message"
"github.com/dogmatiq/dogma"
. "github.com/dogmatiq/enginekit/enginetest/stubs"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
var _ = Describe("func FromAggregate()", func() {
var handler *AggregateMessageHandlerStub
BeforeEach(func() {
handler = &AggregateMessageHandlerStub{
ConfigureFunc: func(c dogma.AggregateConfigurer) {
c.Identity("<name>", aggregateKey)
c.Routes(
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.HandlesCommand[CommandStub[TypeB]](),
dogma.RecordsEvent[EventStub[TypeA]](),
)
},
}
})
When("the configuration is valid", func() {
var cfg RichAggregate
JustBeforeEach(func() {
cfg = FromAggregate(handler)
})
Describe("func Identity()", func() {
It("returns the handler identity", func() {
Expect(cfg.Identity()).To(Equal(
MustNewIdentity("<name>", aggregateKey),
))
})
})
Describe("func MessageNames()", func() {
It("returns the expected message names", func() {
Expect(cfg.MessageNames()).To(Equal(
EntityMessageNames{
Produced: message.NameRoles{
message.NameFor[EventStub[TypeA]](): message.EventRole,
},
Consumed: message.NameRoles{
message.NameFor[CommandStub[TypeA]](): message.CommandRole,
message.NameFor[CommandStub[TypeB]](): message.CommandRole,
},
},
))
})
})
Describe("func MessageTypes()", func() {
It("returns the expected message types", func() {
Expect(cfg.MessageTypes()).To(Equal(
EntityMessageTypes{
Produced: message.TypeRoles{
message.TypeFor[EventStub[TypeA]](): message.EventRole,
},
Consumed: message.TypeRoles{
message.TypeFor[CommandStub[TypeA]](): message.CommandRole,
message.TypeFor[CommandStub[TypeB]](): message.CommandRole,
},
},
))
})
})
Describe("func TypeName()", func() {
It("returns the fully-qualified type name of the handler", func() {
Expect(cfg.TypeName()).To(Equal("*github.com/dogmatiq/enginekit/enginetest/stubs.AggregateMessageHandlerStub"))
})
})
Describe("func ReflectType()", func() {
It("returns the type of the handler", func() {
Expect(cfg.ReflectType()).To(Equal(reflect.TypeOf(handler)))
})
})
Describe("func AcceptVisitor()", func() {
It("calls the appropriate method on the visitor", func() {
v := &fixtures.Visitor{
VisitAggregateFunc: func(_ context.Context, c Aggregate) error {
Expect(c).To(BeIdenticalTo(cfg))
return errors.New("<error>")
},
}
err := cfg.AcceptVisitor(context.Background(), v)
Expect(err).To(MatchError("<error>"))
})
})
Describe("func AcceptRichVisitor()", func() {
It("calls the appropriate method on the visitor", func() {
v := &fixtures.RichVisitor{
VisitRichAggregateFunc: func(_ context.Context, c RichAggregate) error {
Expect(c).To(BeIdenticalTo(cfg))
return errors.New("<error>")
},
}
err := cfg.AcceptRichVisitor(context.Background(), v)
Expect(err).To(MatchError("<error>"))
})
})
Describe("func HandlerType()", func() {
It("returns AggregateHandlerType", func() {
Expect(cfg.HandlerType()).To(Equal(AggregateHandlerType))
})
})
Describe("func IsDisabled()", func() {
It("returns false", func() {
Expect(cfg.IsDisabled()).To(BeFalse())
})
})
Describe("func Handler()", func() {
It("returns the underlying handler", func() {
Expect(cfg.Handler()).To(BeIdenticalTo(handler))
})
})
When("the handler is disabled", func() {
BeforeEach(func() {
configure := handler.ConfigureFunc
handler.ConfigureFunc = func(c dogma.AggregateConfigurer) {
configure(c)
c.Disable()
}
})
Describe("func IsDisabled()", func() {
It("returns true", func() {
Expect(cfg.IsDisabled()).To(BeTrue())
})
})
})
})
DescribeTable(
"when the configuration is invalid",
func(
msg string,
fn func(dogma.AggregateConfigurer),
) {
handler.ConfigureFunc = fn
var err error
func() {
defer Recover(&err)
FromAggregate(handler)
}()
Expect(err).Should(HaveOccurred())
if msg != "" {
Expect(err).To(MatchError(msg))
}
},
Entry(
"when the handler does not configure anything",
"", // any error
nil,
),
Entry(
"when the handler does not configure an identity",
`*stubs.AggregateMessageHandlerStub is configured without an identity, Identity() must be called exactly once within Configure()`,
func(c dogma.AggregateConfigurer) {
c.Routes(
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.RecordsEvent[EventStub[TypeA]](),
)
},
),
Entry(
"when the handler configures multiple identities",
`*stubs.AggregateMessageHandlerStub is configured with multiple identities (<name>/14769f7f-87fe-48dd-916e-5bcab6ba6aca and <other>/14769f7f-87fe-48dd-916e-5bcab6ba6aca), Identity() must be called exactly once within Configure()`,
func(c dogma.AggregateConfigurer) {
c.Identity("<name>", aggregateKey)
c.Identity("<other>", aggregateKey)
c.Routes(
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.RecordsEvent[EventStub[TypeA]](),
)
},
),
Entry(
"when the handler configures an invalid name",
`*stubs.AggregateMessageHandlerStub is configured with an invalid identity, invalid name "\t \n", names must be non-empty, printable UTF-8 strings with no whitespace`,
func(c dogma.AggregateConfigurer) {
c.Identity("\t \n", appKey)
c.Routes(
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.RecordsEvent[EventStub[TypeA]](),
)
},
),
Entry(
"when the handler configures an invalid key",
`*stubs.AggregateMessageHandlerStub is configured with an invalid identity, invalid key "\t \n", keys must be RFC 4122 UUIDs`,
func(c dogma.AggregateConfigurer) {
c.Identity("<name>", "\t \n")
c.Routes(
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.RecordsEvent[EventStub[TypeA]](),
)
},
),
Entry(
"when the handler does not configure any command routes",
`*stubs.AggregateMessageHandlerStub (<name>) is not configured to handle any commands, at least one HandlesCommand() route must be added within Configure()`,
func(c dogma.AggregateConfigurer) {
c.Identity("<name>", aggregateKey)
c.Routes(
dogma.RecordsEvent[EventStub[TypeA]](),
)
},
),
Entry(
"when the handler configures multiple routes for the same command",
`*stubs.AggregateMessageHandlerStub (<name>) is configured with multiple HandlesCommand() routes for stubs.CommandStub[TypeA], should these refer to different message types?`,
func(c dogma.AggregateConfigurer) {
c.Identity("<name>", aggregateKey)
c.Routes(
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.RecordsEvent[EventStub[TypeA]](),
)
},
),
Entry(
"when the handler does not configure any event routes",
`*stubs.AggregateMessageHandlerStub (<name>) is not configured to record any events, at least one RecordsEvent() route must be added within Configure()`,
func(c dogma.AggregateConfigurer) {
c.Identity("<name>", aggregateKey)
c.Routes(
dogma.HandlesCommand[CommandStub[TypeA]](),
)
},
),
Entry(
"when the handler configures multiple routes for the same event",
`*stubs.AggregateMessageHandlerStub (<name>) is configured with multiple RecordsEvent() routes for stubs.EventStub[TypeA], should these refer to different message types?`,
func(c dogma.AggregateConfigurer) {
c.Identity("<name>", aggregateKey)
c.Routes(
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.RecordsEvent[EventStub[TypeA]](),
dogma.RecordsEvent[EventStub[TypeA]](),
)
},
),
Entry(
"when the handler configures the same message type with different roles",
`*stubs.AggregateMessageHandlerStub (<name>) is configured to use stubs.CommandStub[TypeA] as both a command and an event`,
func(c dogma.AggregateConfigurer) {
c.Identity("<name>", aggregateKey)
c.Routes(
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.RecordsEvent[CommandStub[TypeA]](),
)
},
),
Entry(
"when an error occurs before the identity is configured it omits the handler name",
`*stubs.AggregateMessageHandlerStub is configured with multiple HandlesCommand() routes for stubs.CommandStub[TypeA], should these refer to different message types?`,
func(c dogma.AggregateConfigurer) {
c.Routes(
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.HandlesCommand[CommandStub[TypeA]](),
)
},
),
)
})