-
Notifications
You must be signed in to change notification settings - Fork 1
/
domain_test.ts
194 lines (177 loc) · 6.08 KB
/
domain_test.ts
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
import {
RestaurantCommand,
restaurantDecider,
RestaurantEvent,
restaurantView,
} from "./domain.ts";
import { restaurantCommandSchema, restaurantEventSchema } from "./schema.ts";
import { ViewSpecification } from "./test_specification.ts";
import { DeciderSpecification } from "./test_specification.ts";
// A convinient testing specififcation for the Decider - GIVEN / WHEN / THEN
// Json representation of the command
const createRestaurantCommandJson = `
{
"commandId": "691490bb-c4d3-45b8-99d0-efcf20e353ag",
"decider": "Restaurant",
"kind": "CreateRestaurantCommand",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ao",
"name": "Eat at Joes",
"menu": {
"menuItems": [
{"menuItemId": "1", "name": "Salad", "price": "8.99"},
{"menuItemId": "2", "name": "Soup", "price": "6.99"},
{"menuItemId": "3", "name": "Steak", "price": "19.99"}
],
"menuId": "34110e19-ca72-45e7-b969-61bebf54da08",
"cuisine": "SERBIAN"
}
}
`;
// Json representation of the expected event(s)
const restaurantCreatedEventJson = `
{
"version": 1,
"decider": "Restaurant",
"kind": "RestaurantCreatedEvent",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ao",
"name": "Eat at Joes",
"menu": {
"menuItems": [
{"menuItemId": "1", "name": "Salad", "price": "8.99"},
{"menuItemId": "2", "name": "Soup", "price": "6.99"},
{"menuItemId": "3", "name": "Steak", "price": "19.99"}
],
"menuId": "34110e19-ca72-45e7-b969-61bebf54da08",
"cuisine": "SERBIAN"
},
"final": false
}
`;
// Json representation of the command
const changeRestaurantMenuCommandJson = `
{
"commandId": "691490bb-c4d3-45b8-99d0-efcf20e353ag",
"decider": "Restaurant",
"kind": "ChangeRestaurantMenuCommand",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ao",
"menu": {
"menuItems": [
{"menuItemId": "1", "name": "Salad2", "price": "18.59"},
{"menuItemId": "2", "name": "Soup2", "price": "16.94"},
{"menuItemId": "3", "name": "Steak2", "price": "19.89"}
],
"menuId": "34110e19-ca72-45e7-b969-61bebf54da08",
"cuisine": "SERBIAN"
}
}
`;
// Json representation of the expected event(s)
const restaurantMenuChangedEventJson = `
{
"version": 1,
"decider": "Restaurant",
"kind": "RestaurantMenuChangedEvent",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ao",
"menu": {
"menuItems": [
{"menuItemId": "1", "name": "Salad2", "price": "18.59"},
{"menuItemId": "2", "name": "Soup2", "price": "16.94"},
{"menuItemId": "3", "name": "Steak2", "price": "19.89"}
],
"menuId": "34110e19-ca72-45e7-b969-61bebf54da08",
"cuisine": "SERBIAN"
},
"final": false
}
`;
// The Decider test for the restaurant create command
Deno.test(function createRestaurantDeciderTest() {
// Parse the restaurant command from the request / Zod validation/parsing
const createRestaurantCommand: RestaurantCommand = restaurantCommandSchema
.parse(
JSON.parse(createRestaurantCommandJson),
);
// Parse the restaurant event.
const restaurantCreatedEvent: RestaurantEvent = restaurantEventSchema
.parse(
JSON.parse(restaurantCreatedEventJson),
);
// Run the test specification for the restaurant decider
DeciderSpecification.for(restaurantDecider)
.given([])
.when(createRestaurantCommand)
.then([restaurantCreatedEvent]);
});
// The Decider test for the restaurant change menu command
Deno.test(function changeRestaurantMenuDeciderTest() {
// Parse the restaurant command from the request / Zod validation/parsing
const changeRestaurantMenuCommand: RestaurantCommand = restaurantCommandSchema
.parse(
JSON.parse(changeRestaurantMenuCommandJson),
);
// Parse the restaurant event.
const restaurantCreatedEvent: RestaurantEvent = restaurantEventSchema
.parse(
JSON.parse(restaurantCreatedEventJson),
);
// Parse the restaurant event.
const restaurantMenuChangedEvent: RestaurantEvent = restaurantEventSchema
.parse(
JSON.parse(restaurantMenuChangedEventJson),
);
// Run the second test specification for the restaurant decider
DeciderSpecification.for(restaurantDecider)
.given([restaurantCreatedEvent])
.when(changeRestaurantMenuCommand)
.then([restaurantMenuChangedEvent]);
});
Deno.test(function reastaurantCreatedViewTest() {
// Parse the restaurant event.
const restaurantCreatedEvent: RestaurantEvent = restaurantEventSchema
.parse(
JSON.parse(restaurantCreatedEventJson),
);
ViewSpecification.for(restaurantView)
.given([restaurantCreatedEvent])
.then({
restaurantId: "691490bb-c4d3-45b8-99d0-efcf20e353ao",
name: "Eat at Joes",
menu: {
menuItems: [
{ menuItemId: "1", name: "Salad", price: "8.99" },
{ menuItemId: "2", name: "Soup", price: "6.99" },
{ menuItemId: "3", name: "Steak", price: "19.99" },
],
menuId: "34110e19-ca72-45e7-b969-61bebf54da08",
cuisine: "SERBIAN",
},
});
});
Deno.test(function rrestaurantMenuChangedViewTest() {
// Parse the restaurant event.
const restaurantCreatedEvent: RestaurantEvent = restaurantEventSchema
.parse(
JSON.parse(restaurantCreatedEventJson),
);
// Parse the restaurant event.
const restaurantMenuChangedEvent: RestaurantEvent = restaurantEventSchema
.parse(
JSON.parse(restaurantMenuChangedEventJson),
);
ViewSpecification.for(restaurantView)
.given([restaurantCreatedEvent, restaurantMenuChangedEvent])
.then({
restaurantId: "691490bb-c4d3-45b8-99d0-efcf20e353ao",
name: "Eat at Joes",
menu: {
menuItems: [
{ menuItemId: "1", name: "Salad2", price: "18.59" },
{ menuItemId: "2", name: "Soup2", price: "16.94" },
{ menuItemId: "3", name: "Steak2", price: "19.89" },
],
menuId: "34110e19-ca72-45e7-b969-61bebf54da08",
cuisine: "SERBIAN",
},
});
});
// Run the tests: `deno test`