Skip to content

Commit 773c237

Browse files
committed
Added link to Tests badge for users to see uncached version.
Added clearCache method to Tests to allow manual purge. Silenced npm errors when running "lint" and "fix" scripts. Updated packages.
1 parent fe7b97a commit 773c237

File tree

5 files changed

+85
-246
lines changed

5 files changed

+85
-246
lines changed

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Contributions are welcome! To contribute, fork this repository and create a pull
1919
}
2020
```
2121
* Add storable credentials to your script to connect to your Firestore database.
22-
* Add test methods to `Test.ts` file which validates your change.
22+
* Add test methods to `Tests.ts` file which validates your change. [GSUnit Reference](https://sites.google.com/site/scriptsexamples/custom-methods/gsunit)
2323
* Debug/Test your code against your database.
2424
- [ ] Have you [documented your functions and their parameters and return values with JSDoc](http://usejsdoc.org/about-getting-started.html)?
2525
- [ ] Have you modernized the code to run with the new [V8 Runtime](https://developers.google.com/apps-script/guides/v8-runtime)?

.github/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](/prettier/prettier)
88
[![GitHub pull requests](https://img.shields.io/github/issues-pr/grahamearley/FirestoreGoogleAppsScript)](/grahamearley/FirestoreGoogleAppsScript/pulls)
99
[![GitHub issues](https://img.shields.io/github/issues/grahamearley/FirestoreGoogleAppsScript)](/grahamearley/FirestoreGoogleAppsScript/issues)
10-
![Tests](https://img.shields.io/endpoint?url=https%3A%2F%2Fscript.google.com%2Fmacros%2Fs%2FAKfycbzle3ze4mtGAcTNPlqISSFxtmPqvdcNOFauiC4Q0g%2Fexec)
10+
[![Tests](https://img.shields.io/endpoint?url=https%3A%2F%2Fscript.google.com%2Fmacros%2Fs%2FAKfycbzle3ze4mtGAcTNPlqISSFxtmPqvdcNOFauiC4Q0g%2Fexec)](https://img.shields.io/endpoint?url=https%3A%2F%2Fscript.google.com%2Fmacros%2Fs%2FAKfycbzle3ze4mtGAcTNPlqISSFxtmPqvdcNOFauiC4Q0g%2Fexec?nocache)
1111

1212
### A Google Apps Script library for accessing Google Cloud Firestore.
1313

Tests.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ function StoreCredentials_(): void {
22
/** DO NOT SAVE CREDENTIALS HERE */
33
const email = 'xxx@appspot.gserviceaccount.com';
44
const key = '-----BEGIN PRIVATE KEY-----\nLine\nLine\n-----END PRIVATE KEY-----';
5+
const projectId = 'xxx';
56
PropertiesService.getUserProperties().setProperties({
67
email: email,
78
key: key,
8-
project: email.substr(0, email.indexOf('@')),
9+
project: projectId,
910
});
1011
}
1112

@@ -15,11 +16,11 @@ class Tests implements TestManager {
1516
fail: Record<string, Error>;
1617
expected_!: Record<string, Value>;
1718

18-
constructor(email: string, key: string, projectId: string, apiVersion: Version = 'v1') {
19+
constructor(email: string, key: string, projectId: string, apiVersion: Version = 'v1', clearCollection = false) {
1920
this.pass = [];
2021
this.fail = {};
2122

22-
const funcs = Object.getOwnPropertyNames(Tests.prototype).filter(
23+
let funcs = Object.getOwnPropertyNames(Tests.prototype).filter(
2324
(property) => typeof (this as any)[property] === 'function' && property !== 'constructor'
2425
);
2526

@@ -28,7 +29,7 @@ class Tests implements TestManager {
2829
this.db = getFirestore(email, key, projectId, apiVersion);
2930
this.pass.push('Test_Get_Firestore');
3031
} catch (e) {
31-
/** On failure, fail the other tests too */
32+
// On failure, fail the remaining tests without execution
3233
this.fail['Test_Get_Firestore'] = e;
3334
const err: Error = {
3435
name: 'Test Error',
@@ -58,6 +59,11 @@ class Tests implements TestManager {
5859
'reference value': this.db.basePath + 'Test Collection/New Document',
5960
};
6061

62+
/** Only run test to remove residual Test Documents **/
63+
if (clearCollection) {
64+
funcs = ['Test_Delete_Documents'];
65+
}
66+
6167
/** Test all methods in this class */
6268
for (const func of funcs) {
6369
try {
@@ -332,7 +338,7 @@ class Tests implements TestManager {
332338
}
333339

334340
Test_Query_Where_Nan(): void {
335-
/** Unable to store NaN values to Firestore */
341+
// Unable to store NaN values to Firestore, so no results
336342
const path = 'Test Collection';
337343
const docs = this.db.query(path).Where('number value', NaN).Execute();
338344
GSUnit.assertEquals(0, docs.length);
@@ -421,23 +427,35 @@ function RunTests_(): Shield {
421427
label: 'tests',
422428
message: `✔ ${pass.length}, ✘ ${Object.keys(fail).length}`,
423429
color: Object.keys(fail).length ? 'red' : 'green',
424-
cacheSeconds: 3600,
430+
cacheSeconds: 3600, // Always cache for 1 hour
425431
};
426432
}
427433

428-
function cacheResults_(): string {
434+
function cacheResults_(cachedBadge: boolean): string {
429435
/* Script owner should set up a trigger for this function to cache the test results.
430436
* The badge fetching these Test Results (on README) is set to cache the image after 1 hour.
431437
* GitHub creates anonymized URLs which timeout after 3 seconds,
432438
* which is longer than the time it takes to execute all the tests.
433439
*/
440+
const maxCache = 3600;
434441
const results = JSON.stringify(RunTests_());
435-
CacheService.getUserCache()!.put('Test Results', results);
436-
return results;
442+
CacheService.getUserCache()!.put('Test Results', results, maxCache);
443+
// Send the min cache allowed @see {@link https://shields.io/endpoint ShieldsIO Endpoint}
444+
return results.replace(`"cacheSeconds":${maxCache}`, `"cacheSeconds":${cachedBadge ? maxCache : 300}`);
445+
}
446+
447+
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
448+
function clearCache(): void {
449+
/** Allow user to clear Cached Results **/
450+
const scriptProps = PropertiesService.getUserProperties().getProperties();
451+
new Tests(scriptProps['email'], scriptProps['key'], scriptProps['project'], 'v1', true);
452+
CacheService.getUserCache()!.remove('Test Results');
437453
}
438454

439455
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
440-
function doGet(_e: GoogleAppsScript.Events.AppsScriptHttpRequestEvent): GoogleAppsScript.Content.TextOutput {
441-
const results = CacheService.getUserCache()!.get('Test Results') || cacheResults_();
456+
function doGet(evt: GoogleAppsScript.Events.AppsScriptHttpRequestEvent): GoogleAppsScript.Content.TextOutput {
457+
// Sending /exec?nocache when calling to ignore the cache check
458+
const useCache = evt.queryString !== 'nocache';
459+
const results = (useCache && CacheService.getUserCache()!.get('Test Results')) || cacheResults_(useCache);
442460
return ContentService.createTextOutput(results);
443461
}

0 commit comments

Comments
 (0)