Skip to content

Commit d37469d

Browse files
authored
Merge branch 'main' into chore/update-actions-checkout-v6
2 parents 9e5fea8 + a9fbacb commit d37469d

3 files changed

Lines changed: 91 additions & 28 deletions

File tree

spanner/quickstart.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2016 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START spanner_quickstart]
18+
const {Spanner} = require('@google-cloud/spanner');
19+
const {status} = require('@grpc/grpc-js');
20+
21+
// Creates a client using the ambient project ID or environment configuration
22+
const spanner = new Spanner();
23+
24+
/**
25+
* Executes a query against a Cloud Spanner database.
26+
*
27+
* @param {string} projectId The project ID containing the Spanner instance (for example, 'example-project-id').
28+
* @param {string} instanceId The ID of the Spanner instance (for example, 'example-instance').
29+
* @param {string} databaseId The ID of the Spanner database (for example 'example-database').
30+
*/
31+
async function quickstart(projectId, instanceId, databaseId) {
32+
try {
33+
// Gets a reference to a Cloud Spanner instance and database
34+
const instance = spanner.instance(instanceId);
35+
const database = instance.database(databaseId);
36+
37+
const query = {
38+
sql: 'SELECT 1',
39+
};
40+
41+
const [rows] = await database.run(query);
42+
43+
console.log(`Query successfully executed. ${rows.length} row(s) found.`);
44+
rows.forEach(row => {
45+
console.log(` Row details: ${JSON.stringify(row)}`);
46+
});
47+
} catch (err) {
48+
if (err.code === status.NOT_FOUND) {
49+
console.error(
50+
`Resource not found. Please verify that instance '${instanceId}' and database '${databaseId}' exist under project '${projectId}'.`
51+
);
52+
} else {
53+
console.error('An unexpected error occurred during execution:', err);
54+
}
55+
}
56+
}
57+
// [END spanner_quickstart]
58+
59+
if (require.main === module) {
60+
quickstart(...process.argv.slice(2));
61+
}
62+
63+
module.exports = {quickstart};

spanner/system-test/spanner.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,14 +585,17 @@ describe('Autogenerated Admin Clients', () => {
585585
});
586586
});
587587

588-
describe.skip('quickstart', () => {
588+
describe('quickstart', () => {
589589
// Running the quickstart test in here since there's already a spanner
590590
// instance and database set up at this point.
591591
it('should query a table', async () => {
592592
const output = execSync(
593593
`node quickstart ${PROJECT_ID} ${INSTANCE_ID} ${DATABASE_ID}`
594594
);
595-
assert.match(output, /Query: \d+ found./);
595+
assert.match(
596+
output,
597+
/Query successfully executed\. \d+ row\(s\) found\./
598+
);
596599
});
597600
});
598601

translate/test/v3/translate_batch_translate_text.test.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,29 @@ describe(REGION_TAG, () => {
6161
console.error(error);
6262
}
6363
});
64-
}),
65-
it('should batch translate the input text', async function () {
66-
this.retries(3);
67-
const projectId = await translationClient.getProjectId();
68-
await clearBucket(projectId, storage, bucketUuid);
69-
const inputUri = 'gs://cloud-samples-data/translation/text.txt';
64+
});
7065

71-
const outputUri = `gs://${projectId}/${bucketName}`;
72-
const output = execSync(
73-
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri}`
74-
);
75-
assert.match(output, /Total Characters: 13/);
76-
assert.match(output, /Translated Characters: 13/);
77-
}),
78-
// Delete the folder from GCS for cleanup
79-
after(async () => {
80-
const projectId = await translationClient.getProjectId();
81-
const options = {
82-
prefix: `translation-${bucketUuid}`,
83-
};
66+
it('should batch translate the input text', async function () {
67+
this.retries(3);
68+
const projectId = await translationClient.getProjectId();
69+
await clearBucket(projectId, storage, bucketUuid);
70+
const inputUri = 'gs://cloud-samples-data/translation/text.txt';
71+
72+
const outputUri = `gs://${projectId}/${bucketName}`;
73+
const output = execSync(
74+
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri}`
75+
);
76+
assert.match(output, /Total Characters: 13/);
77+
assert.match(output, /Translated Characters: 13/);
78+
});
79+
// Delete the folder from GCS for cleanup
80+
after(async () => {
81+
const projectId = await translationClient.getProjectId();
82+
const options = {
83+
prefix: `translation-${bucketUuid}`,
84+
};
8485

85-
const bucket = await storage.bucket(projectId);
86-
const [files] = await bucket.getFiles(options);
87-
const length = files.length;
88-
if (length > 0) {
89-
await Promise.all(files.map(file => file.delete()));
90-
}
91-
});
86+
const bucket = await storage.bucket(projectId);
87+
await bucket.deleteFiles(options);
88+
});
9289
});

0 commit comments

Comments
 (0)