Skip to content

Commit 76a5a52

Browse files
authored
test: remove await from code samples (#1229)
* Remove await Remove await from the sample. It is preventing the sample from working. * Remove await from samples await causes the code sample not to work so needs to be removed * Restructure the OR query code sample The OR query code sample should be run in a similar way to the other code samples. * Cleanup and change assert There is no guarantee that the integration test database will have no Buy Milk or Feed cats data in the database to begin with so we will have to make sure to remove all such data so that the test starts with no such data in datastore. This is a requirement for the console output to be correct. Also, since we now use execSync, the standard output cannot be found in console.log.firstCall.args[0] so we have to get it from stdout just like in the other tests. * inline the test variable Inline simple local variables as suggested in PR feedback.
1 parent 4e2cd7e commit 76a5a52

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

samples/export.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async function main(bucket = 'YOUR_BUCKET_NAME') {
4040
console.log(specificExportOperation.result.outputUrl);
4141
}
4242

43-
await exportEntities();
43+
exportEntities();
4444
// [END datastore_admin_entities_export]
4545
}
4646

samples/import.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async function main(file = 'YOUR_FILE_NAME') {
4545
await specificImportOperation.cancel();
4646
}
4747

48-
await importEntities();
48+
importEntities();
4949
// [END datastore_admin_entities_import]
5050
}
5151

samples/indexes.get.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function main(indexId = 'YOUR_INDEX_ID') {
3131
console.log('Properties:', metadata.properties);
3232
}
3333

34-
await getIndex();
34+
getIndex();
3535
// [END datastore_admin_index_get]
3636
}
3737

samples/indexes.list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async function main() {
3737
});
3838
}
3939

40-
await listIndexes();
40+
listIndexes();
4141
// [END datastore_admin_index_list]
4242
}
4343

samples/queryFilterOr.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ async function main() {
4646
}
4747
}
4848

49-
await queryFilterOr();
49+
queryFilterOr();
5050
// [END datastore_query_filter_or]
5151
}
5252

53-
exports.queryFilterOr = main;
53+
const args = process.argv.slice(2);
54+
main(...args).catch(console.error);

samples/test/queryFilterOr.test.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ const path = require('path');
2020
const {assert} = require('chai');
2121
const {describe, it, after, before} = require('mocha');
2222
const sinon = require('sinon');
23-
const {Datastore} = require('@google-cloud/datastore');
23+
const {Datastore, PropertyFilter, or} = require('@google-cloud/datastore');
2424
const datastore = new Datastore();
2525

26-
const {queryFilterOr} = require('../queryFilterOr');
26+
const cp = require('child_process');
2727
let taskKey1, taskKey2;
2828

29+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
30+
2931
describe('Creating a union query', () => {
3032
const stubConsole = function () {
3133
sinon.stub(console, 'error');
@@ -57,6 +59,18 @@ describe('Creating a union query', () => {
5759
},
5860
};
5961

62+
// Ensure the datastore database has no existing data for the task keys.
63+
const query = datastore
64+
.createQuery('Task')
65+
.filter(
66+
or([
67+
new PropertyFilter('description', '=', 'Buy milk'),
68+
new PropertyFilter('description', '=', 'Feed cats'),
69+
])
70+
);
71+
const [entities] = await datastore.runQuery(query);
72+
await datastore.delete(entities.map(entity => entity[datastore.KEY]));
73+
6074
await datastore.upsert(entity1);
6175
await datastore.upsert(entity2);
6276
});
@@ -67,7 +81,6 @@ describe('Creating a union query', () => {
6781
});
6882

6983
it('should get a combination of items from the Datastore', async () => {
70-
await queryFilterOr();
71-
assert.include(console.log.firstCall.args[0], 'Entity');
84+
assert.strictEqual(execSync(`node ./queryFilterOr.js`), 'Entity found: Feed cats\nEntity found: Buy milk\n');
7285
});
7386
});

0 commit comments

Comments
 (0)