-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_mapper_test.ts
320 lines (294 loc) · 8.25 KB
/
data_mapper_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
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
import * as mf from "mock_fetch/mod.ts";
import { assertEquals, assertExists } from "./dependencies/testing_asserts.ts";
import { SlackAPI } from "./dependencies/deno_slack_api.ts";
import { DefineDatastore, Schema } from "./dependencies/deno_slack_sdk.ts";
import { DataMapper, Operator } from "./mod.ts";
import { compileExpression } from "./data_mapper.ts";
mf.install();
mf.mock("POST@/api/apps.datastore.put", () => {
return new Response(
JSON.stringify({
"ok": true,
"datastore": "suveys",
"item": {
"id": "123",
"title": "Off-site event ideas",
"questions": [
"Can you share a fun idea for our off-site event in December?",
],
"closed": false,
},
}),
{
status: 200,
},
);
});
mf.mock("POST@/api/apps.datastore.query", () => {
return new Response(
JSON.stringify({
"ok": true,
"datastore": "suveys",
"items": [
{
"id": "123",
"title": "Off-site event ideas",
"questions": [
"Can you share a fun idea for our off-site event in December?",
],
"closed": false,
},
],
}),
{
status: 200,
},
);
});
mf.mock("POST@/api/apps.datastore.get", () => {
return new Response(
JSON.stringify({
"ok": true,
"datastore": "suveys",
"item": {
"id": "123",
"title": "Off-site event ideas",
"questions": [
"Can you share a fun idea for our off-site event in December?",
],
"closed": false,
},
}),
{
status: 200,
},
);
});
mf.mock("POST@/api/apps.datastore.delete", () => {
return new Response(
JSON.stringify({ "ok": true }),
{ status: 200 },
);
});
export const Surveys = DefineDatastore(
{
name: "surveys",
primary_key: "id",
attributes: {
id: { type: Schema.types.string, required: true },
title: { type: Schema.types.string, required: true },
questions: {
type: Schema.types.array,
items: { type: Schema.types.string },
required: true,
},
maxParticipants: { type: Schema.types.number }, // optional
closed: { type: Schema.types.boolean, required: true },
},
} as const,
);
Deno.test("Save a record", async () => {
const client = SlackAPI("valid-token");
const dataMapper = new DataMapper<typeof Surveys.definition>({
client,
datastore: Surveys.definition,
logLevel: "DEBUG",
});
const result = await dataMapper.save({
datastore: Surveys.definition.name,
attributes: {
title: "Off-site event ideas",
questions: [
"Can you share a fun idea for our off-site event in December?",
],
maxParticipants: 300,
closed: false,
},
});
assertExists(result.item);
// Verify type-safety
// deno-lint-ignore no-unused-vars
const id: string = result.item.id;
// deno-lint-ignore no-unused-vars
const title: string = result.item.title;
// deno-lint-ignore no-unused-vars
const question: string[] = result.item.questions;
// deno-lint-ignore no-unused-vars
const maxParticipants: number | undefined = result.item.maxParticipants;
// deno-lint-ignore no-unused-vars
const closed: boolean = result.item.closed;
});
Deno.test("Run a query", async () => {
const client = SlackAPI("valid-token");
const dataMapper = new DataMapper<typeof Surveys.definition>({
client,
datastore: Surveys.definition,
logLevel: "DEBUG",
});
await dataMapper.findById("123");
await dataMapper.findById({ id: "123" });
await dataMapper.findAll();
await dataMapper.findAllBy({
expression: {
expression: "#title = :title",
attributes: { "#title": "title" },
values: { ":title": "Off-site event ideas" },
},
});
await dataMapper.findAllBy({
expression: "#title = :title",
attributes: { "#title": "title" },
values: { ":title": "Off-site event ideas" },
});
await dataMapper.deleteById("123");
await dataMapper.deleteById({ id: "123" });
});
Deno.test("Run a query with simple expressions", async () => {
const client = SlackAPI("valid-token");
const dataMapper = new DataMapper<typeof Surveys.definition>({
client,
datastore: Surveys.definition,
logLevel: "DEBUG",
});
const results = await dataMapper.findAllBy({
where: {
title: {
value: "Off-site event ideas",
operator: Operator.Equal,
},
},
cursor: "sdfsdfdsafsafdsafsdf",
limit: 10,
});
const r = results.items[0];
assertExists(r.title);
await dataMapper.findAllBy({
where: {
maxParticipants: {
value: 123,
operator: Operator.GreaterThan,
},
},
});
await dataMapper.findAllBy({
where: {
maxParticipants: {
value: [100, 200],
operator: Operator.Between,
},
},
});
await dataMapper.findAllBy({ where: { title: "Off-site event ideas" } });
await dataMapper.findAllBy({
where: {
maxParticipants: {
value: [100, 200],
operator: Operator.Between,
},
},
});
await dataMapper.findAllBy({
where: {
maxParticipants: {
value: [100, 200],
operator: Operator.Between,
},
},
});
await dataMapper.findAllBy({
where: {
and: [{
maxParticipants: { value: [100, 200], operator: Operator.Between },
}],
},
});
await dataMapper.findAllBy({
where: {
or: [
{
and: [{ id: "1" }, { title: "Off-site event ideas" }],
},
{ id: "1" },
{ title: "Off-site event ideas" },
],
},
cursor: "sdfsdfdsafsafdsafsdf",
limit: 10,
});
});
Deno.test("Construct a simplest condition", () => {
const result = compileExpression<typeof Surveys.definition>({
where: { title: "Off-site event ideas" },
});
assertEquals(Object.keys(result.attributes).length, 1);
assertEquals(Object.keys(result.values).length, 1);
let expression = result.expression;
for (const name of Object.keys(result.attributes)) {
expression = expression.replaceAll(name, "ATTR");
}
for (const name of Object.keys(result.values)) {
expression = expression.replaceAll(name, "VALUE");
}
assertEquals(expression, "ATTR = VALUE");
});
Deno.test("Construct a condition with an operator", () => {
const result = compileExpression<typeof Surveys.definition>({
where: {
maxParticipants: { value: [100, 200], operator: Operator.Between },
},
});
assertEquals(Object.keys(result.attributes).length, 1);
assertEquals(Object.keys(result.values).length, 2);
let expression = result.expression;
for (const name of Object.keys(result.attributes)) {
expression = expression.replaceAll(name, "ATTR");
}
for (const name of Object.keys(result.values)) {
expression = expression.replaceAll(name, "VALUE");
}
assertEquals(expression, "ATTR between VALUE and VALUE");
});
Deno.test("Construct complex conditions", () => {
const result = compileExpression<typeof Surveys.definition>({
where: {
or: [
{
and: [{ id: "1" }, { title: "Off-site event ideas" }],
},
{ id: "2" },
{ title: "New project ideas" },
],
},
});
assertEquals(Object.keys(result.attributes).length, 4);
assertEquals(Object.keys(result.values).length, 4);
let expression = result.expression;
for (const name of Object.keys(result.attributes)) {
expression = expression.replaceAll(name, "ATTR");
}
for (const name of Object.keys(result.values)) {
expression = expression.replaceAll(name, "VALUE");
}
assertEquals(
expression,
"((ATTR = VALUE) and (ATTR = VALUE)) or (ATTR = VALUE) or (ATTR = VALUE)",
);
});
Deno.test("Two conditions in a single condition object", () => {
const result = compileExpression<typeof Surveys.definition>({
where: { title: "New project ideas", closed: false },
});
assertEquals(Object.keys(result.attributes).length, 2);
assertEquals(Object.keys(result.values).length, 2);
let expression = result.expression;
for (const name of Object.keys(result.attributes)) {
expression = expression.replaceAll(name, "ATTR");
}
for (const name of Object.keys(result.values)) {
expression = expression.replaceAll(name, "VALUE");
}
assertEquals(
expression,
"(ATTR = VALUE) and (ATTR = VALUE)",
);
});