Skip to content

Commit e9ef4ed

Browse files
committed
Implements test recommendation about findOrCreate.
Per discussion on [pull request](#777), he test implements a findOrCreate strategy to create a GCS bucket needed to complete the test flow. This commit also updates the README file accordingly.
1 parent 3a196b8 commit e9ef4ed

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

functions/speech-to-speech/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ The sample requires the following environment variables:
3434
regions, see [Cloud Functions Locations][11] in the Functions documentation.
3535
* `GOOGLE_CLOUD_PROJECT`: The project id of your GCP project.
3636
* `BASE_URL`: The URL that the Cloud Functions emulator uses to serve requests.
37-
* `OUTPUT_BUCKET`: A bucket that the sample uses to drop translated files.
37+
* `OUTPUT_BUCKET`: A bucket that the sample uses to drop translated files. The
38+
test script creates this buckt if it doesn't exist.
3839
* `GOOGLE_APPLICATION_CREDENTIALS`: The path to your API key file.
3940
* `SUPPORTED_LANGUAGE_CODES`: Comma-separated list of languages that the sample
4041
translates messages to.
@@ -73,11 +74,10 @@ The test script performs the following tasks:
7374
1. Runs the linter.
7475
1. Deploys the function to the emulator.
7576
1. Runs tests that don't perform any calls to the Google Cloud APIs.
76-
1. Creates a temporary Google Cloud Storage bucket with a random name.
77+
1. Creates the output bucket if it doesn't exist.
7778
1. Runs tests that perform calls to the Google Cloud APIs and drop the
7879
translated messages to the bucket.
7980
1. Deletes the files created during the tests.
80-
1. Deletes the temporary bucket.
8181

8282
To run the tests, use the following commands from the
8383
`functions/speech-to-speech` folder:

functions/speech-to-speech/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"local-test": "mocha test/index.test.js",
2929
"system-test": "mocha --timeout 20000 test/sample.integration.http.test.js",
3030
"pretest": "npm run lint && sh test/updateFunctions.sh",
31-
"test": "export OUTPUT_BUCKET=speech-to-speech-test-$(uuidgen) && functions restart && npm run local-test && npm run system-test"
31+
"test": "npm run local-test && npm run system-test"
3232
},
3333
"dependencies": {
3434
"@google-cloud/speech": "^2.1.0",

functions/speech-to-speech/test/sample.integration.http.test.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,22 @@ describe('Tests that do not require a GCS bucket', function () {
6161
});
6262

6363
describe('Tests that require a GCS bucket', function () {
64-
// Create the Google Cloud Storage bucket to run the tests.
65-
// Caution: The 'after' hook deletes the bucket. To prevent deleting user data, the 'before' hook
66-
// fails if the bucket already exists.
67-
let bucketCreated = false;
64+
// Create the Google Cloud Storage bucket if it doesn't exist.
6865
before('Create GCS bucket.', function (done) {
69-
bucket.create({
70-
location: storageLocation,
71-
storageClass: storageClass
72-
}).then(function (data) {
73-
bucketCreated = true;
74-
done();
75-
}).catch(error => {
76-
done(error);
66+
bucket.exists().then(function (data) {
67+
const exists = data[0];
68+
if (!exists) {
69+
bucket.create({
70+
location: storageLocation,
71+
storageClass: storageClass
72+
}).then(function (data) {
73+
done();
74+
}).catch(error => {
75+
done(error);
76+
});
77+
} else {
78+
done();
79+
}
7780
});
7881
});
7982
it('should return a successful response.', function (done) {
@@ -100,10 +103,7 @@ describe('Tests that require a GCS bucket', function () {
100103
})
101104
.expect(/"transcription":"this is a test please translate this message"/, done);
102105
});
103-
after('Delete GCS bucket, files, and other test artifacts.', function (done) {
104-
if (!bucketCreated) {
105-
done(new Error('The before hook did not create the bucket, abort cleanup'));
106-
}
106+
after('Delete created files in the bucket and other test artifacts.', function (done) {
107107
// Load the response from the successful test.
108108
const responseBody = JSON.parse(fs.readFileSync('responseBody.test.json'));
109109

@@ -116,9 +116,6 @@ describe('Tests that require a GCS bucket', function () {
116116
}
117117

118118
Promise.all(deletedFiles).then(() => {
119-
// Delete the empty bucket.
120-
return bucket.delete();
121-
}).then(() => {
122119
// Delete the file that stores the response from the successful test.
123120
fs.unlinkSync('responseBody.test.json');
124121
done();

0 commit comments

Comments
 (0)