|
| 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 | +}); |
0 commit comments