forked from grahamearley/FirestoreGoogleAppsScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTests.ts
439 lines (389 loc) · 14.6 KB
/
Tests.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
function StoreCredentials_(): void {
/** DO NOT SAVE CREDENTIALS HERE */
const email = 'xxx@appspot.gserviceaccount.com';
const key = '-----BEGIN PRIVATE KEY-----\nLine\nLine\n-----END PRIVATE KEY-----';
PropertiesService.getUserProperties().setProperties({
email: email,
key: key,
project: email.substr(0, email.indexOf('@')),
});
}
class Tests implements TestManager {
db!: Firestore;
pass: string[];
fail: Record<string, Error>;
expected_!: Record<string, Value>;
constructor(email: string, key: string, projectId: string, apiVersion: Version = 'v1') {
this.pass = [];
this.fail = {};
const funcs = Object.getOwnPropertyNames(Tests.prototype).filter(
(property) => typeof (this as any)[property] === 'function' && property !== 'constructor'
);
/** Test Initializer */
try {
this.db = getFirestore(email, key, projectId, apiVersion);
this.pass.push('Test_Get_Firestore');
} catch (e) {
/** On failure, fail the other tests too */
this.fail['Test_Get_Firestore'] = e;
const err: Error = {
name: 'Test Error',
message: 'Test Initialization Error',
stack: 'See Test_Get_Firestore Error',
};
for (const func of funcs) {
this.fail[func] = err;
}
return;
}
this.expected_ = {
'array value': ['string123', 42, false],
'number value': 100,
'string value 이': 'The fox jumps over the lazy dog 름',
'boolean value': true,
'map value (nested object)': {
foo: 'bar',
},
'null value': null,
'timestamp value': new Date(),
'geopoint value': {
latitude: 29.9792,
longitude: 31.1342,
},
'reference value': this.db.basePath + 'Test Collection/New Document',
};
/** Test all methods in this class */
for (const func of funcs) {
try {
(this as any)[func]();
this.pass.push(func);
} catch (e) {
this.fail[func] = typeof e === 'string' ? { message: 'AssertionError', stack: e } : e;
}
}
}
/**
* All Test methods should not take any parameters, and return nothing.
* Tests should throw exceptions to fail.
* Can leverage {@link https://sites.google.com/site/scriptsexamples/custom-methods/gsunit GSUnit}.
*/
Test_Create_Document_Bare(): void {
const path = 'Test Collection';
const newDoc = this.db.createDocument(path);
GSUnit.assertNotUndefined(newDoc);
GSUnit.assertNotUndefined(newDoc.name);
GSUnit.assertTrue(newDoc.name!.startsWith(this.db.basePath + path + '/'));
GSUnit.assertNotUndefined(newDoc.createTime);
GSUnit.assertNotUndefined(newDoc.updateTime);
GSUnit.assertEquals(newDoc.createTime, newDoc.updateTime);
GSUnit.assertRoughlyEquals(+new Date(), +newDoc.created, 1000);
}
Test_Create_Document_Name(): void {
const path = 'Test Collection/New Document';
const newDoc = this.db.createDocument(path);
GSUnit.assertNotUndefined(newDoc);
GSUnit.assertEquals(this.db.basePath + path, newDoc.name);
GSUnit.assertNotUndefined(newDoc.createTime);
GSUnit.assertNotUndefined(newDoc.updateTime);
GSUnit.assertRoughlyEquals(+new Date(), +newDoc.updated, 1000);
}
Test_Create_Document_Name_Duplicate(): void {
const path = 'Test Collection/New Document';
try {
this.db.createDocument(path);
GSUnit.fail('Duplicate document without error');
} catch (_e) {
/* db.createDocument should be throwing an error */
}
}
Test_Create_Document_Data(): void {
const path = 'Test Collection/New Document !@#$%^&*(),.<>?;\':"[]{}|-=_+áéíóúæÆÑ';
const newDoc = this.db.createDocument(path, this.expected_);
GSUnit.assertObjectEquals(this.expected_, newDoc.obj);
}
Test_Update_Document_Overwrite_Default(): void {
const original = {
'number value': -100,
'string value 이': 'The fox jumps over the lazy dog 름',
};
const path = 'Test Collection/Updatable Document Default';
this.db.createDocument(path, original);
const expected = {
'string value 이': 'Qwerty निर्वाण',
'null value': 'Not a Null',
};
const updatedDoc = this.db.updateDocument(path, expected);
GSUnit.assertObjectEquals(expected, updatedDoc.obj);
}
Test_Update_Document_Overwrite(): void {
const original = {
'number value': 10,
'string value 이': 'The fox jumps over the lazy dog 름',
};
const path = 'Test Collection/Updatable Document Overwrite';
this.db.createDocument(path, original);
const expected = { 'number value': 42 };
const updatedDoc = this.db.updateDocument(path, expected, false);
GSUnit.assertObjectEquals(expected, updatedDoc.obj);
}
Test_Update_Document_Mask(): void {
const expected = {
'number value': 1234567890,
'string value 이': 'The fox jumps over the lazy dog 름',
};
const path = 'Test Collection/Updatable Document Mask';
this.db.createDocument(path, expected);
const updater = { 'string value 이': 'The new wave `~' };
const updatedDoc = this.db.updateDocument(path, updater, true);
Object.assign(expected, updater);
GSUnit.assertObjectEquals(expected, updatedDoc.obj);
}
Test_Update_Document_Overwrite_Missing(): void {
const path = 'Test Collection/Missing Document Overwrite';
const expected = { 'boolean value': false };
const updatedDoc = this.db.updateDocument(path, expected, false);
GSUnit.assertObjectEquals(expected, updatedDoc.obj);
}
Test_Update_Document_Mask_Missing(): void {
const path = 'Test Collection/Missing Document Mask';
const expected = { 'boolean value': true };
const updatedDoc = this.db.updateDocument(path, expected, true);
GSUnit.assertObjectEquals(expected, updatedDoc.obj);
}
Test_Get_Document(): void {
const path = 'Test Collection/New Document !@#$%^&*(),.<>?;\':"[]{}|-=_+áéíóúæÆÑ';
const doc = this.db.getDocument(path);
GSUnit.assertObjectEquals(this.expected_, doc.obj);
}
Test_Get_Document_Missing(): void {
const path = 'Test Collection/Missing Document';
try {
this.db.getDocument(path);
GSUnit.fail('Missing document without error');
} catch (_e) {
/* db.getDocument should be throwing an error */
}
}
Test_Get_Documents(): void {
const path = 'Test Collection';
const docs = this.db.getDocuments(path);
GSUnit.assertEquals(8, docs.length);
}
Test_Get_Documents_By_ID(): void {
const path = 'Test Collection';
const ids = [
'New Document',
'Updatable Document Default',
'Updatable Document Overwrite',
'Updatable Document Mask',
'Missing Document',
];
const docs = this.db.getDocuments(path, ids);
GSUnit.assertEquals(ids.length - 1, docs.length);
}
Test_Get_Documents_By_ID_Missing(): void {
const path = 'Missing Collection';
const ids = [
'New Document',
'Updatable Document Default',
'Updatable Document Overwrite',
'Updatable Document Mask',
'Missing Document',
];
const docs = this.db.getDocuments(path, ids);
GSUnit.assertEquals(0, docs.length);
}
Test_Get_Documents_By_ID_Empty(): void {
const path = 'Test Collection';
const ids: string[] = [];
const docs = this.db.getDocuments(path, ids);
GSUnit.assertEquals(0, docs.length);
}
Test_Get_Document_IDs(): void {
const path = 'Test Collection';
const docs = this.db.getDocumentIds(path);
GSUnit.assertEquals(8, docs.length);
}
Test_Get_Document_IDs_Missing(): void {
const path = 'Missing Collection';
try {
this.db.getDocumentIds(path);
GSUnit.fail('Missing collection without error');
} catch (_e) {
/* db.getDocumentIds should be throwing an error */
}
}
Test_Query_All(): void {
const path = 'Test Collection';
const expected = this.db.getDocuments(path);
expected.forEach((doc) => {
delete doc.readTime;
});
const docs = this.db.query(path).Execute();
docs.forEach((doc) => {
delete doc.readTime;
});
GSUnit.assertArrayEquals(expected, docs);
}
Test_Query_Select_Name(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Select().Execute();
GSUnit.assertEquals(8, docs.length);
}
Test_Query_Select_Name_Number(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Select().Select('number value').Execute();
GSUnit.assertEquals(8, docs.length);
}
Test_Query_Select_String(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Select('string value 이').Execute();
GSUnit.assertEquals(8, docs.length);
}
Test_Query_Where_EqEq_String(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Where('string value 이', '==', 'The fox jumps over the lazy dog 름').Execute();
GSUnit.assertEquals(1, docs.length);
}
Test_Query_Where_EqEqEq_String(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Where('string value 이', '===', 'The fox jumps over the lazy dog 름').Execute();
GSUnit.assertEquals(1, docs.length);
}
Test_Query_Where_Eq_Number(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Where('number value', '==', 100).Execute();
GSUnit.assertEquals(1, docs.length);
}
Test_Query_Where_Lt_Number(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Where('number value', '<', 100).Execute();
GSUnit.assertEquals(1, docs.length);
}
Test_Query_Where_Lte_Number(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Where('number value', '<=', 100).Execute();
GSUnit.assertEquals(2, docs.length);
}
Test_Query_Where_Gt_Number(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Where('number value', '>', 100).Execute();
GSUnit.assertEquals(1, docs.length);
}
Test_Query_Where_Gte_Number(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Where('number value', '>=', 100).Execute();
GSUnit.assertEquals(2, docs.length);
}
Test_Query_Where_Contains(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Where('array value', 'contains', 42).Execute();
GSUnit.assertEquals(1, docs.length);
}
Test_Query_Where_Contains_Any(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Where('array value', 'containsany', [false, 0, 42, 'bar']).Execute();
GSUnit.assertEquals(1, docs.length);
}
Test_Query_Where_In(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Where('number value', 'in', [0, 100, 42]).Execute();
GSUnit.assertEquals(2, docs.length);
}
Test_Query_Where_Nan(): void {
/** Unable to store NaN values to Firestore */
const path = 'Test Collection';
const docs = this.db.query(path).Where('number value', NaN).Execute();
GSUnit.assertEquals(0, docs.length);
}
Test_Query_Where_Null(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Where('null value', null).Execute();
GSUnit.assertEquals(1, docs.length);
}
Test_Query_OrderBy_Number(): void {
const expected = [
'Updatable Document Overwrite',
'New Document !@#$%^&*(),.<>?;\':"[]{}|-=_+áéíóúæÆÑ',
'Updatable Document Mask',
];
const path = 'Test Collection';
const docs = this.db.query(path).Select().OrderBy('number value').Execute();
GSUnit.assertArrayEquals(expected, Util_.stripBasePath(path, docs));
}
Test_Query_OrderBy_Number_ASC(): void {
const expected = [
'Updatable Document Overwrite',
'New Document !@#$%^&*(),.<>?;\':"[]{}|-=_+áéíóúæÆÑ',
'Updatable Document Mask',
];
const path = 'Test Collection';
const docs = this.db.query(path).Select().OrderBy('number value', 'asc').Execute();
GSUnit.assertArrayEquals(expected, Util_.stripBasePath(path, docs));
}
Test_Query_OrderBy_Number_DESC(): void {
const expected = [
'Updatable Document Mask',
'New Document !@#$%^&*(),.<>?;\':"[]{}|-=_+áéíóúæÆÑ',
'Updatable Document Overwrite',
];
const path = 'Test Collection';
const docs = this.db.query(path).Select().OrderBy('number value', 'desc').Execute();
GSUnit.assertArrayEquals(expected, Util_.stripBasePath(path, docs));
}
Test_Query_Offset(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Offset(2).Execute();
GSUnit.assertEquals(6, docs.length);
}
Test_Query_Limit(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Limit(2).Execute();
GSUnit.assertEquals(2, docs.length);
}
Test_Query_Range(): void {
const path = 'Test Collection';
const docs = this.db.query(path).Range(2, 5).Execute();
GSUnit.assertEquals(5 - 2, docs.length);
}
Test_Delete_Documents(): void {
const collection = 'Test Collection';
const docs = this.db.getDocumentIds(collection).map((path) => this.db.deleteDocument(collection + '/' + path));
docs.forEach((doc) => GSUnit.assertObjectEquals({}, doc));
}
Test_Delete_Document_Missing() {
const path = 'Test Collection/Missing Document';
const noDoc = this.db.deleteDocument(path);
GSUnit.assertObjectEquals({}, noDoc);
}
}
function RunTests_(): Shield {
const scriptProps = PropertiesService.getUserProperties().getProperties();
const tests = new Tests(scriptProps['email'], scriptProps['key'], scriptProps['project'], 'v1');
const { pass, fail } = tests;
Object.entries(fail).forEach(([func, err]: [string, Error]) =>
console.log(`Test Failed: ${func} (${err.message})\n${err.stack}`)
);
return {
schemaVersion: 1,
label: 'tests',
message: `✔ ${pass.length}, ✘ ${Object.keys(fail).length}`,
color: Object.keys(fail).length ? 'red' : 'green',
cacheSeconds: 3600,
};
}
function cacheResults_(): string {
/* Script owner should set up a trigger for this function to cache the test results.
* The badge fetching these Test Results (on README) is set to cache the image after 1 hour.
* GitHub creates anonymized URLs which timeout after 3 seconds,
* which is longer than the time it takes to execute all the tests.
*/
const results = JSON.stringify(RunTests_());
CacheService.getUserCache()!.put('Test Results', results);
return results;
}
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
function doGet(_e: GoogleAppsScript.Events.AppsScriptHttpRequestEvent): GoogleAppsScript.Content.TextOutput {
const results = CacheService.getUserCache()!.get('Test Results') || cacheResults_();
return ContentService.createTextOutput(results);
}