Skip to content

Commit

Permalink
Added link to Tests badge for users to see uncached version.
Browse files Browse the repository at this point in the history
Added clearCache method to Tests to allow manual purge.
Silenced npm errors when running "lint" and "fix" scripts.
Updated packages.
  • Loading branch information
LaughDonor committed Jun 25, 2020
1 parent fe7b97a commit 773c237
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 246 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Contributions are welcome! To contribute, fork this repository and create a pull
}
```
* Add storable credentials to your script to connect to your Firestore database.
* Add test methods to `Test.ts` file which validates your change.
* Add test methods to `Tests.ts` file which validates your change. [GSUnit Reference](https://sites.google.com/site/scriptsexamples/custom-methods/gsunit)
* Debug/Test your code against your database.
- [ ] Have you [documented your functions and their parameters and return values with JSDoc](http://usejsdoc.org/about-getting-started.html)?
- [ ] Have you modernized the code to run with the new [V8 Runtime](https://developers.google.com/apps-script/guides/v8-runtime)?
Expand Down
2 changes: 1 addition & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](/prettier/prettier)
[![GitHub pull requests](https://img.shields.io/github/issues-pr/grahamearley/FirestoreGoogleAppsScript)](/grahamearley/FirestoreGoogleAppsScript/pulls)
[![GitHub issues](https://img.shields.io/github/issues/grahamearley/FirestoreGoogleAppsScript)](/grahamearley/FirestoreGoogleAppsScript/issues)
![Tests](https://img.shields.io/endpoint?url=https%3A%2F%2Fscript.google.com%2Fmacros%2Fs%2FAKfycbzle3ze4mtGAcTNPlqISSFxtmPqvdcNOFauiC4Q0g%2Fexec)
[![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)

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

Expand Down
40 changes: 29 additions & 11 deletions Tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ function StoreCredentials_(): void {
/** DO NOT SAVE CREDENTIALS HERE */
const email = '[email protected]';
const key = '-----BEGIN PRIVATE KEY-----\nLine\nLine\n-----END PRIVATE KEY-----';
const projectId = 'xxx';
PropertiesService.getUserProperties().setProperties({
email: email,
key: key,
project: email.substr(0, email.indexOf('@')),
project: projectId,
});
}

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

constructor(email: string, key: string, projectId: string, apiVersion: Version = 'v1') {
constructor(email: string, key: string, projectId: string, apiVersion: Version = 'v1', clearCollection = false) {
this.pass = [];
this.fail = {};

const funcs = Object.getOwnPropertyNames(Tests.prototype).filter(
let funcs = Object.getOwnPropertyNames(Tests.prototype).filter(
(property) => typeof (this as any)[property] === 'function' && property !== 'constructor'
);

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

/** Only run test to remove residual Test Documents **/
if (clearCollection) {
funcs = ['Test_Delete_Documents'];
}

/** Test all methods in this class */
for (const func of funcs) {
try {
Expand Down Expand Up @@ -332,7 +338,7 @@ class Tests implements TestManager {
}

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

function cacheResults_(): string {
function cacheResults_(cachedBadge: boolean): 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 maxCache = 3600;
const results = JSON.stringify(RunTests_());
CacheService.getUserCache()!.put('Test Results', results);
return results;
CacheService.getUserCache()!.put('Test Results', results, maxCache);
// Send the min cache allowed @see {@link https://shields.io/endpoint ShieldsIO Endpoint}
return results.replace(`"cacheSeconds":${maxCache}`, `"cacheSeconds":${cachedBadge ? maxCache : 300}`);
}

/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
function clearCache(): void {
/** Allow user to clear Cached Results **/
const scriptProps = PropertiesService.getUserProperties().getProperties();
new Tests(scriptProps['email'], scriptProps['key'], scriptProps['project'], 'v1', true);
CacheService.getUserCache()!.remove('Test 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_();
function doGet(evt: GoogleAppsScript.Events.AppsScriptHttpRequestEvent): GoogleAppsScript.Content.TextOutput {
// Sending /exec?nocache when calling to ignore the cache check
const useCache = evt.queryString !== 'nocache';
const results = (useCache && CacheService.getUserCache()!.get('Test Results')) || cacheResults_(useCache);
return ContentService.createTextOutput(results);
}
Loading

0 comments on commit 773c237

Please sign in to comment.