-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_test.ts
166 lines (158 loc) · 4.12 KB
/
functions_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
import * as mf from "mock_fetch/mod.ts";
import { assertExists } from "./dependencies/testing_asserts.ts";
import { SlackAPI } from "./dependencies/deno_slack_api.ts";
import { save } from "./mod.ts";
import { DefineDatastore, Schema } from "./dependencies/deno_slack_sdk.ts";
import { countBy, findAllBy } from "./functions.ts";
import { findAllByIds } from "./functions.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",
"question":
"Can you share a fun idea for our off-site event in December?",
},
}),
{
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.bulkGet", () => {
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.count", () => {
return new Response(
JSON.stringify({
"ok": true,
"datastore": "suveys",
"count": 123,
}),
{
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 result = await save<typeof Surveys.definition>({
client,
datastore: "suveys",
attributes: {
title: "Off-site event ideas",
questions: [
"Can you share a fun idea for our off-site event in December?",
],
},
});
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 questions: 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 result = await findAllBy<typeof Surveys.definition>({
client,
datastore: "suveys",
expression: {
expression: "#id = :id",
attributes: { "#id": "id" },
values: { ":id": "123" },
},
cursor: "sdfdsfdasfasfafdsa",
limit: 10,
});
assertExists(result.items);
});
Deno.test("Run a bulk get request", async () => {
const client = SlackAPI("valid-token");
const result = await findAllByIds<typeof Surveys.definition>({
client,
datastore: "suveys",
ids: ["1", "2", "3"],
});
assertExists(result.items);
});
Deno.test("Run a count query", async () => {
const client = SlackAPI("valid-token");
const result = await countBy<typeof Surveys.definition>({
client,
datastore: "suveys",
expression: {
expression: "#id = :id",
attributes: { "#id": "id" },
values: { ":id": "123" },
},
});
assertExists(result.count);
});