-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpectation.message.command_test.go
More file actions
292 lines (277 loc) · 8.22 KB
/
expectation.message.command_test.go
File metadata and controls
292 lines (277 loc) · 8.22 KB
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
package testkit_test
import (
"context"
"testing"
"time"
"github.com/dogmatiq/dogma"
. "github.com/dogmatiq/enginekit/enginetest/stubs"
. "github.com/dogmatiq/testkit"
"github.com/dogmatiq/testkit/engine"
"github.com/dogmatiq/testkit/internal/testingmock"
"github.com/dogmatiq/testkit/internal/x/xtesting"
)
func TestToExecuteCommand(t *testing.T) {
type (
EventThatIsIgnored = EventStub[TypeX]
EventThatExecutesCommand = EventStub[TypeC]
EventThatSchedulesTimeout = EventStub[TypeT]
CommandThatIsExecuted = CommandStub[TypeC]
CommandThatIsNeverExecuted = CommandStub[TypeX]
CommandThatIsOnlyConsumed = CommandStub[TypeO]
TimeoutThatIsScheduled = TimeoutStub[TypeT]
)
app := &ApplicationStub{
ConfigureFunc: func(c dogma.ApplicationConfigurer) {
c.Identity("<app>", "ce773269-4ad7-4c7f-a0ff-cda2e5899743")
c.Routes(
dogma.ViaProcess(&ProcessMessageHandlerStub[*ProcessRootStub]{
ConfigureFunc: func(c dogma.ProcessConfigurer) {
c.Identity("<process>", "8b4c4701-be92-4b28-83b6-0d69b97fb451")
c.Routes(
dogma.HandlesEvent[*EventThatIsIgnored](),
dogma.HandlesEvent[*EventThatExecutesCommand](),
dogma.ExecutesCommand[*CommandThatIsExecuted](),
dogma.ExecutesCommand[*CommandThatIsNeverExecuted](),
dogma.HandlesEvent[*EventThatSchedulesTimeout](),
dogma.SchedulesTimeout[*TimeoutThatIsScheduled](),
)
},
RouteEventToInstanceFunc: func(
context.Context,
dogma.Event,
) (string, bool, error) {
return "<instance>", true, nil
},
HandleEventFunc: func(
_ context.Context,
_ *ProcessRootStub,
s dogma.ProcessEventScope[*ProcessRootStub],
m dogma.Event,
) error {
switch m := m.(type) {
case *EventThatExecutesCommand:
s.ExecuteCommand(
&CommandThatIsExecuted{
Content: m.Content,
},
)
case *EventThatSchedulesTimeout:
s.ScheduleTimeout(
&TimeoutThatIsScheduled{
Content: m.Content,
},
time.Now().Add(1*time.Hour),
)
}
return nil
},
}),
dogma.ViaIntegration(&IntegrationMessageHandlerStub{
ConfigureFunc: func(c dogma.IntegrationConfigurer) {
c.Identity("<integration>", "49fa7c5f-7682-4743-bf8a-ed96dee2d81a")
c.Routes(
dogma.HandlesCommand[*CommandThatIsOnlyConsumed](),
)
},
}),
)
},
}
cases := []struct {
Name string
Action func(*testing.T, *Test) Action
Expectation Expectation
Passes bool
Report reportMatcher
Options []TestOption
}{
{
"command executed as expected",
func(t *testing.T, tc *Test) Action {
return RecordEvent(&EventThatExecutesCommand{})
},
ToExecuteCommand(&CommandThatIsExecuted{}),
true,
expectReport(
`✓ execute a specific '*stubs.CommandStub[TypeC]' command`,
),
nil,
},
{
"no matching command executed",
func(t *testing.T, tc *Test) Action {
return RecordEvent(&EventThatExecutesCommand{})
},
ToExecuteCommand(&CommandThatIsNeverExecuted{}),
false,
expectReport(
`✗ execute a specific '*stubs.CommandStub[TypeX]' command`,
``,
` | EXPLANATION`,
` | none of the engaged handlers executed a matching command`,
` | `,
` | SUGGESTIONS`,
` | • verify the logic within the '<process>' process message handler`,
),
nil,
},
{
"no messages produced at all",
func(t *testing.T, tc *Test) Action {
return RecordEvent(&EventThatIsIgnored{})
},
ToExecuteCommand(&CommandThatIsExecuted{}),
false,
expectReport(
`✗ execute a specific '*stubs.CommandStub[TypeC]' command`,
``,
` | EXPLANATION`,
` | no messages were produced at all`,
` | `,
` | SUGGESTIONS`,
` | • verify the logic within the '<process>' process message handler`,
),
nil,
},
{
"no commands produced at all",
func(t *testing.T, tc *Test) Action {
return RecordEvent(&EventThatSchedulesTimeout{})
},
ToExecuteCommand(&CommandThatIsExecuted{}),
false,
expectReport(
`✗ execute a specific '*stubs.CommandStub[TypeC]' command`,
``,
` | EXPLANATION`,
` | no commands were executed at all`,
` | `,
` | SUGGESTIONS`,
` | • verify the logic within the '<process>' process message handler`,
),
nil,
},
{
"no matching command executed and all relevant handler types disabled",
func(t *testing.T, tc *Test) Action {
return RecordEvent(&EventThatExecutesCommand{})
},
ToExecuteCommand(&CommandThatIsExecuted{}),
false,
expectReport(
`✗ execute a specific '*stubs.CommandStub[TypeC]' command`,
``,
` | EXPLANATION`,
` | no relevant handler types were enabled`,
` | `,
` | SUGGESTIONS`,
` | • enable process handlers using the EnableHandlerType() option`,
),
[]TestOption{
WithUnsafeOperationOptions(
engine.EnableProcesses(false),
),
},
},
{
"similar command executed with a different value",
func(t *testing.T, tc *Test) Action {
return RecordEvent(&EventThatExecutesCommand{Content: "<content>"})
},
ToExecuteCommand(&CommandThatIsExecuted{Content: "<different>"}),
false,
expectReport(
`✗ execute a specific '*stubs.CommandStub[TypeC]' command`,
``,
` | EXPLANATION`,
` | a similar command was executed by the '<process>' process message handler`,
` | `,
` | SUGGESTIONS`,
` | • check the content of the message`,
` | `,
` | MESSAGE DIFF`,
` | *stubs.CommandStub[github.com/dogmatiq/enginekit/enginetest/stubs.TypeC]{`,
` | Content: "<[-differ-]{+cont+}ent>"`,
` | ValidationError: ""`,
` | }`,
),
nil,
},
{
"does not include an explanation when negated and a sibling expectation passes",
func(t *testing.T, tc *Test) Action {
return RecordEvent(&EventThatExecutesCommand{})
},
NoneOf(
ToExecuteCommand(&CommandThatIsExecuted{}),
ToExecuteCommand(&CommandThatIsNeverExecuted{}),
),
false,
expectReport(
`✗ none of (1 of the expectations passed unexpectedly)`,
` ✓ execute a specific '*stubs.CommandStub[TypeC]' command`,
` ✗ execute a specific '*stubs.CommandStub[TypeX]' command`,
),
nil,
},
{
"fails the test if the message type is unrecognized",
func(*testing.T, *Test) Action { return noop },
ToExecuteCommand(CommandU1),
false,
expectReport(
`✗ execute a specific '*stubs.CommandStub[TypeU]' command`,
``,
` | EXPLANATION`,
` | a command of type *stubs.CommandStub[TypeU] can never be executed, the application does not use this message type`,
` | `,
` | SUGGESTIONS`,
` | • add a route for *stubs.CommandStub[TypeU] to the application's configuration`,
),
nil,
},
{
"fails the test if the message type is not produced by any handlers",
func(*testing.T, *Test) Action { return noop },
ToExecuteCommand(&CommandThatIsOnlyConsumed{}),
false,
expectReport(
`✗ execute a specific '*stubs.CommandStub[TypeO]' command`,
``,
` | EXPLANATION`,
` | no handlers execute commands of type *stubs.CommandStub[TypeO], it is only ever consumed`,
` | `,
` | SUGGESTIONS`,
` | • add an outbound route for *stubs.CommandStub[TypeO] to a handler in the application's configuration`,
),
nil,
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
mt := &testingmock.T{FailSilently: true}
tc := Begin(mt, app, c.Options...)
tc.Expect(c.Action(t, tc), c.Expectation)
c.Report(mt)
if mt.Failed() != !c.Passes {
t.Fatalf(
"expectation should have %s but %s",
map[bool]string{true: "passed", false: "failed"}[c.Passes],
map[bool]string{true: "passed", false: "failed"}[mt.Failed()],
)
}
})
}
}
func TestToExecuteCommand_NilMessage(t *testing.T) {
xtesting.ExpectPanic(t, "ToExecuteCommand(<nil>): message must not be nil", func() {
ToExecuteCommand(nil)
})
}
func TestToExecuteCommand_InvalidMessage(t *testing.T) {
xtesting.ExpectPanic(t, "ToExecuteCommand(*stubs.CommandStub[TypeA]): <invalid>", func() {
ToExecuteCommand(&CommandStub[TypeA]{
ValidationError: "<invalid>",
})
})
}