Skip to content

Commit ddd6e8f

Browse files
committed
test: system test
1 parent 29c9829 commit ddd6e8f

File tree

3 files changed

+147
-7
lines changed

3 files changed

+147
-7
lines changed

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class Spanner extends GrpcService {
276276
directedReadOptions: google.spanner.v1.IDirectedReadOptions | null;
277277
defaultTransactionOptions: RunTransactionOptions;
278278
_observabilityOptions: ObservabilityOptions | undefined;
279-
private _universeDomain: string;
279+
private _universeDomain: string | undefined;
280280
readonly _nthClientId: number;
281281

282282
/**
@@ -390,10 +390,11 @@ class Spanner extends GrpcService {
390390
options.sslCreds = grpc.credentials.createInsecure();
391391
}
392392

393-
const universeDomain = getDomain('spanner', options);
394-
393+
const domain = getDomain('spanner', options);
394+
const universeDomain = getUniverseDomainOnly(options);
395+
options.universeDomain = universeDomain;
395396
const config = {
396-
baseUrl: options.apiEndpoint || options.servicePath || universeDomain,
397+
baseUrl: options.apiEndpoint || options.servicePath || domain,
397398
protosDir: path.resolve(__dirname, '../protos'),
398399
protoServices: {
399400
Operations: {

system-test/tpc-test.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright 2025 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+
// https://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+
import {after, describe, it} from 'mocha';
16+
import {Spanner} from '../src';
17+
import * as assert from 'assert';
18+
19+
// INSTRUCTIONS FOR RUNNING TEST:
20+
// 1. Change describe.skip to describe.only below.
21+
// 2. Reassign process.env.GOOGLE_APPLICATION_CREDENTIALS to local key file.
22+
// 3. Reassign UNIVERSE_DOMAIN_CONSTANT to the universe domain to test.
23+
// 4. Run `npm run system-test`.
24+
25+
describe.skip('Universe domain tests', () => {
26+
// These tests are only designed to pass when using the service account
27+
// credentials for the universe domain environment so we skip them in the CI pipeline.
28+
//
29+
// To see successful tests, uncomment the following line:
30+
process.env.GOOGLE_APPLICATION_CREDENTIALS = 'path to your credential file';
31+
const UNIVERSE_DOMAIN_CONSTANT = 'my-universe-domain';
32+
33+
const projectId = 'tpc-project-id';
34+
35+
async function runTest(spanner: Spanner, instanceId, databaseId) {
36+
try {
37+
const instance = spanner.instance(instanceId);
38+
const database = instance.database(databaseId);
39+
const tableName = 'VenueDetails';
40+
const table = database.table(tableName);
41+
42+
const schema = `CREATE TABLE ${tableName} (
43+
VenueId INT64 NOT NULL,
44+
VenueName STRING(100),
45+
Capacity INT64,
46+
) PRIMARY KEY (VenueId)`;
47+
48+
console.log(`Creating table ${table.name}`);
49+
const [, operation] = await table.create(schema);
50+
51+
await operation.promise();
52+
53+
console.log(`${table.name} create successfully.`);
54+
55+
const venuesTable = database.table(tableName);
56+
await venuesTable.insert([
57+
{VenueId: 1, VenueName: 'Marc', Capacity: 100},
58+
{VenueId: 2, VenueName: 'Marc', Capacity: 200},
59+
{VenueId: 3, VenueName: 'Marc', Capacity: 300},
60+
{VenueId: 4, VenueName: 'Marc', Capacity: 400},
61+
]);
62+
console.log(`Inserted data into the table ${table.name}`);
63+
64+
const query = {
65+
columns: ['VenueId', 'VenueName', 'Capacity'],
66+
keySet: {
67+
all: true,
68+
},
69+
};
70+
71+
console.log(`Reading rows in the table ${table.name}`);
72+
73+
const [rows] = await venuesTable.read(query);
74+
75+
rows.forEach(row => {
76+
const json = row.toJSON();
77+
console.log(
78+
`VenueId: ${json.VenueId}, VenueName: ${json.VenueName}, Capacity: ${json.Capacity}`,
79+
);
80+
});
81+
82+
console.log(`deleting table ${table.name}`);
83+
await table.delete();
84+
} catch (e) {
85+
assert.ifError(e);
86+
}
87+
}
88+
89+
describe('set universe with spanner client option', () => {
90+
it('should set universeDomain option', async () => {
91+
const universeDomain = UNIVERSE_DOMAIN_CONSTANT;
92+
const options = {
93+
projectId,
94+
universeDomain,
95+
};
96+
const spanner = new Spanner(options);
97+
const instanceId = 'test-instance';
98+
const databaseId = 'test-database';
99+
100+
try {
101+
await runTest(spanner, instanceId, databaseId);
102+
} catch (e) {
103+
assert.ifError(e);
104+
}
105+
});
106+
107+
it('should set universe_domain option', async () => {
108+
const universe_domain = UNIVERSE_DOMAIN_CONSTANT;
109+
const options = {
110+
projectId,
111+
universe_domain,
112+
};
113+
const spanner = new Spanner(options);
114+
const instanceId = 'test-instance';
115+
const databaseId = 'test-database';
116+
117+
try {
118+
await runTest(spanner, instanceId, databaseId);
119+
} catch (e) {
120+
assert.ifError(e);
121+
}
122+
});
123+
});
124+
125+
describe('set universe with GOOGLE_CLOUD_UNIVERSE_DOMAIN env', () => {
126+
it('Should set GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable', async () => {
127+
process.env.GOOGLE_CLOUD_UNIVERSE_DOMAIN = UNIVERSE_DOMAIN_CONSTANT;
128+
const spanner = new Spanner({projectId});
129+
const instanceId = 'test-instance';
130+
const databaseId = 'test-database';
131+
try {
132+
await runTest(spanner, instanceId, databaseId);
133+
} catch (e) {
134+
assert.ifError(e);
135+
}
136+
delete process.env.GOOGLE_CLOUD_UNIVERSE_DOMAIN;
137+
});
138+
});
139+
});

test/spanner.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,7 @@ describe('Spanner with mock server', () => {
19611961
});
19621962

19631963
describe('should allow overriding TPC universe', () => {
1964-
it('when domain main passed via env', () => {
1964+
it('when domain name passed via env', () => {
19651965
process.env.GOOGLE_CLOUD_UNIVERSE_DOMAIN = 'fake-tpc-env.example.com';
19661966
const spanner = new Spanner();
19671967
const universeDomain = spanner.universeDomain;
@@ -1974,7 +1974,7 @@ describe('Spanner with mock server', () => {
19741974
delete process.env.GOOGLE_CLOUD_UNIVERSE_DOMAIN;
19751975
});
19761976

1977-
it('when domain main passed via spanner options using universeDomain', () => {
1977+
it('when domain name passed via spanner options using universeDomain', () => {
19781978
const fakeUniverseDomain = 'fake-tpc.example.com';
19791979
const spanner = new Spanner({
19801980
universeDomain: fakeUniverseDomain,
@@ -1984,7 +1984,7 @@ describe('Spanner with mock server', () => {
19841984
assert.deepStrictEqual(universeDomain, 'spanner.fake-tpc.example.com');
19851985
});
19861986

1987-
it('when domain main passed via spanner options using universe_domain', () => {
1987+
it('when domain name passed via spanner options using universe_domain', () => {
19881988
const fakeUniverseDomain = 'fake-tpc.example.com';
19891989
const spanner = new Spanner({
19901990
universe_domain: fakeUniverseDomain,

0 commit comments

Comments
 (0)