Skip to content
This repository was archived by the owner on Feb 20, 2026. It is now read-only.

test: remove await from code samples#1229

Merged
danieljbruce merged 8 commits intogoogleapis:mainfrom
danieljbruce:or-query-samples
Jan 29, 2024
Merged

test: remove await from code samples#1229
danieljbruce merged 8 commits intogoogleapis:mainfrom
danieljbruce:or-query-samples

Conversation

@danieljbruce
Copy link
Copy Markdown
Contributor

@danieljbruce danieljbruce commented Jan 26, 2024

General changes:

A customer raised an issue where they ran the OR query sample from https://cloud.google.com/datastore/docs/samples/datastore-query-filter-or and an error was thrown on the line that starts with await. await can only be used inside an async function so the code sample should not contain await when it is not in an async function. This PR removes await from code samples where await is not used inside an async function.

OR query sample changes:

In the OR query sample tests, the assert check no longer works after removing await as console.log.firstCall now equals null. This is because console log output inside an async function is not visible outside of the function if await is not used. Therefore, we should use the execSync function like the code in the import-export.test.js sample test file to get console output from running the sample. This way the it block with the assert statement can read the console output and ensure it equals “Entity found: Feed cats\nEntity found: Buy milk\n”.

It is also possible that the database currently contains “Buy milk” and “Feed cats” data before the code sample is run which will affect the console output when running the OR query sample. Therefore, a script is added which runs before the OR query sample test is run. This script removes “Buy milk” and “Feed cats” data from the database so that the database starts from a consistent state and the console output is consistent in the assertion check.

Remove await from the sample. It is preventing the sample from working.
await causes the code sample not to work so needs to be removed
@product-auto-label product-auto-label bot added size: s Pull request size is small. api: datastore Issues related to the googleapis/nodejs-datastore API. samples Issues that are directly related to samples. labels Jan 26, 2024
The OR query code sample should be run in a similar way to the other code samples.
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.
@danieljbruce danieljbruce changed the title Or query samples test: remove await from code samples because await does not work when the user copy/pastes the code sample Jan 26, 2024
@danieljbruce danieljbruce changed the title test: remove await from code samples because await does not work when the user copy/pastes the code sample test: remove await from code samples Jan 26, 2024
@danieljbruce danieljbruce marked this pull request as ready for review January 26, 2024 22:29
@danieljbruce danieljbruce requested a review from a team as a code owner January 26, 2024 22:29
@danieljbruce danieljbruce requested a review from a team January 26, 2024 22:29
@danieljbruce danieljbruce added the owlbot:run Add this label to trigger the Owlbot post processor. label Jan 29, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Jan 29, 2024
it('should get a combination of items from the Datastore', async () => {
await queryFilterOr();
assert.include(console.log.firstCall.args[0], 'Entity');
const stdout = execSync(`node ./queryFilterOr.js`);
Copy link
Copy Markdown
Contributor

@cindy-peng cindy-peng Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Inline simple local variables: go/unit-testing-practices#example-inlining-simple-local-variables

@danieljbruce danieljbruce added the owlbot:run Add this label to trigger the Owlbot post processor. label Jan 29, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Jan 29, 2024
@danieljbruce danieljbruce merged commit 76a5a52 into googleapis:main Jan 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: datastore Issues related to the googleapis/nodejs-datastore API. samples Issues that are directly related to samples. size: s Pull request size is small.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants