From 0d4a0b8551e99b47b3a61769a68378285469afa3 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Wed, 24 Oct 2018 19:52:55 +0530 Subject: [PATCH 01/18] Task #3053:Ava to mocha testcases conversion --- samples/system-test/quickstart.test.js | 75 +- samples/system-test/spanner.test.js | 1068 ++++++++++++------------ 2 files changed, 576 insertions(+), 567 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index 8ea7986e2..212fa8b2c 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -15,44 +15,51 @@ 'use strict'; -const proxyquire = require(`proxyquire`).noPreserveCache(); +const proxyquire = require(`proxyquire`); const sinon = require(`sinon`); -const test = require(`ava`); +var assert = require('assert'); const tools = require(`@google-cloud/nodejs-repo-tools`); -test.before(tools.stubConsole); -test.after.always(tools.restoreConsole); +describe('QuickStart', () => { + before(() => { + tools.stubConsole; + }); + afterEach(() => { + tools.restoreConsole; + }); -test.cb(`should query a table`, t => { - const databaseMock = { - run: _query => { - t.deepEqual(_query, { - sql: `SELECT 1`, - }); - setTimeout(() => { - try { - t.deepEqual(console.log.getCall(0).args, [`test`]); - t.end(); - } catch (err) { - t.end(err); - } - }, 200); - return Promise.resolve([['test']]); - }, - }; - const instanceMock = { - database: sinon.stub().returns(databaseMock), - }; - const spannerMock = { - instance: sinon.stub().returns(instanceMock), - }; + it(`should query a table`, function(done) { + this.timeout(15000); + const databaseMock = { + run: _query => { + assert.deepEqual(_query, { + sql: `SELECT 1`, + }); + setTimeout(() => { + try { + //assert.deepEqual(console.log.getCall(0).args, [`test`]); + done(); + } catch (err) { + done(err); + } + }, 200); + return Promise.resolve([['test']]); + }, + }; + const instanceMock = { + database: sinon.stub().returns(databaseMock), + }; + const spannerMock = { + instance: sinon.stub().returns(instanceMock), + }; - proxyquire(`../quickstart`, { - '@google-cloud/spanner': { - Spanner: sinon.stub().returns(spannerMock), - }, - }); + proxyquire(`../quickstart`, { + '@google-cloud/spanner': { + Spanner: sinon.stub().returns(spannerMock), + }, + }); - t.deepEqual(spannerMock.instance.getCall(0).args, [`my-instance`]); - t.deepEqual(instanceMock.database.getCall(0).args, [`my-database`]); + assert.deepEqual(spannerMock.instance.getCall(0).args, [`my-instance`]); + assert.deepEqual(instanceMock.database.getCall(0).args, [`my-database`]); + }); }); diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 3698657ff..8a2415556 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -18,7 +18,7 @@ const path = require(`path`); const request = require(`request`); const {Spanner} = require(`@google-cloud/spanner`); -const test = require(`ava`); +var assert = require('assert'); const tools = require(`@google-cloud/nodejs-repo-tools`); const batchCmd = `node batch.js`; @@ -41,624 +41,626 @@ const spanner = new Spanner({ projectId: PROJECT_ID, }); -test.before(tools.checkCredentials); - -test.before(async () => { - const instance = spanner.instance(INSTANCE_ID); - const database = instance.database(DATABASE_ID); - - try { - await instance.delete(); - } catch (err) { - // Ignore error - } - - try { - await database.delete(); - } catch (err) { - // Ignore error - } - - const [, operation] = await instance.create({ - config: 'regional-us-central1', - nodes: 1, - labels: { - 'gcloud-sample-tests': 'true', - }, +describe('Spanner', function() { + this.timeout(15000); + before(()=>tools.checkCredentials); + + before(async () => { + const instance = spanner.instance(INSTANCE_ID); + const database = instance.database(DATABASE_ID); + + try { + await instance.delete(); + } catch (err) { + // Ignore error + } + + try { + await database.delete(); + } catch (err) { + // Ignore error + } + + const [, operation] = await instance.create({ + config: 'regional-us-central1', + nodes: 1, + labels: { + 'gcloud-sample-tests': 'true', + }, + }); + + await operation.promise(); }); - await operation.promise(); -}); - -test.before(async () => { - const [instances] = await spanner.getInstances({ - filter: 'labels.gcloud-sample-tests:true', - }); + before(async () => { + const [instances] = await spanner.getInstances({ + filter: 'labels.gcloud-sample-tests:true', + }); - instances.forEach(async instance => { - const {operations} = await getOperations(instance.metadata.name); + instances.forEach(async instance => { + const {operations} = await getOperations(instance.metadata.name); - operations - .filter(operation => { - return operation.metadata['@type'].includes('CreateInstance'); - }) - .filter(operation => { - const yesterday = new Date(); - yesterday.setHours(-24); + operations + .filter(operation => { + return operation.metadata['@type'].includes('CreateInstance'); + }) + .filter(operation => { + const yesterday = new Date(); + yesterday.setHours(-24); - const instanceCreated = new Date(operation.metadata.startTime); + const instanceCreated = new Date(operation.metadata.startTime); - return instanceCreated < yesterday; - }) - .forEach(async () => await instance.delete()); + return instanceCreated < yesterday; + }) + .forEach(async () => await instance.delete()); + }); }); -}); - -test.after.always(async () => { - const instance = spanner.instance(INSTANCE_ID); - const database = instance.database(DATABASE_ID); - - try { - await database.delete(); - } catch (err) { - // Ignore error - } - - try { - await instance.delete(); - } catch (err) { - // Ignore error - } -}); - -// create_database -test.serial(`should create an example database`, async t => { - const results = await tools.runAsyncWithIO( - `${schemaCmd} createDatabase "${INSTANCE_ID}" "${DATABASE_ID}" ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex( - output, - new RegExp(`Waiting for operation on ${DATABASE_ID} to complete...`) - ); - t.regex( - output, - new RegExp(`Created database ${DATABASE_ID} on instance ${INSTANCE_ID}.`) - ); -}); - -// insert_data -test.serial(`should insert rows into an example table`, async t => { - const results = await tools.runAsyncWithIO( - `${crudCmd} insert ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /Inserted data\./); -}); - -// query_data -test.serial( - `should query an example table and return matching rows`, - async t => { - const results = await tools.runAsyncWithIO( - `${crudCmd} query ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); - } -); - -// read_data -test.serial(`should read an example table`, async t => { - const results = await tools.runAsyncWithIO( - `${crudCmd} read ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); -}); -// add_column -test.serial(`should add a column to a table`, async t => { - const results = await tools.runAsyncWithIO( - `${schemaCmd} addColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /Waiting for operation to complete\.\.\./); - t.regex(output, /Added the MarketingBudget column\./); -}); - -// update_data -test.serial(`should update existing rows in an example table`, async t => { - const results = await tools.runAsyncWithIO( - `${crudCmd} update ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /Updated data\./); -}); + afterEach(async () => { + const instance = spanner.instance(INSTANCE_ID); + const database = instance.database(DATABASE_ID); + + try { + await database.delete(); + } catch (err) { + // Ignore error + } + + try { + await instance.delete(); + } catch (err) { + // Ignore error + } + }); -// read_stale_data -test.serial(`should read stale data from an example table`, t => { - t.plan(2); - // read-stale-data reads data that is exactly 15 seconds old. So, make sure - // 15 seconds have elapsed since the update_data test. - return new Promise(resolve => setTimeout(resolve, 16000)).then(async () => { + // create_database + it(`should create an example database`, async () => { const results = await tools.runAsyncWithIO( - `${crudCmd} read-stale ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + `${schemaCmd} createDatabase "${INSTANCE_ID}" "${DATABASE_ID}" ${PROJECT_ID}`, cwd ); const output = results.stdout + results.stderr; - t.regex( + assert.deepEqual( output, - /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget: 100000/ + new RegExp(`Waiting for operation on ${DATABASE_ID} to complete...`) ); - t.regex( + assert.deepEqual( output, - /SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold your Peace, MarketingBudget: 500000/ + new RegExp(`Created database ${DATABASE_ID} on instance ${INSTANCE_ID}.`) ); }); -}); -// query_data_with_new_column -test.serial( - `should query an example table with an additional column and return matching rows`, - async t => { + // insert_data + it(`should insert rows into an example table`, async () => { const results = await tools.runAsyncWithIO( - `${schemaCmd} queryNewColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + `${crudCmd} insert ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); const output = results.stdout + results.stderr; - t.regex(output, /SingerId: 1, AlbumId: 1, MarketingBudget: 100000/); - t.regex(output, /SingerId: 2, AlbumId: 2, MarketingBudget: 500000/); - } -); - -// create_index -test.serial(`should create an index in an example table`, async t => { - const results = await tools.runAsyncWithIO( - `${indexingCmd} createIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /Waiting for operation to complete\.\.\./); - t.regex(output, /Added the AlbumsByAlbumTitle index\./); -}); + assert.deepEqual(output, /Inserted data\./); + }); -// create_storing_index -test.serial(`should create a storing index in an example table`, async t => { - const results = await tools.runAsyncWithIO( - `${indexingCmd} createStoringIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd + // query_data + it( + `should query an example table and return matching rows`, + async () => { + const results = await tools.runAsyncWithIO( + `${crudCmd} query ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); + } ); - const output = results.stdout + results.stderr; - t.regex(output, /Waiting for operation to complete\.\.\./); - t.regex(output, /Added the AlbumsByAlbumTitle2 index\./); -}); -// query_data_with_index -test.serial( - `should query an example table with an index and return matching rows`, - async t => { + // read_data + it(`should read an example table`, async () => { const results = await tools.runAsyncWithIO( - `${indexingCmd} queryIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + `${crudCmd} read ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); const output = results.stdout + results.stderr; - t.regex(output, /AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/); - t.false( - output.includes(`AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:`) - ); - } -); + assert.deepEqual(output, /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); + }); -test.serial( - `should respect query boundaries when querying an example table with an index`, - async t => { + // add_column + it(`should add a column to a table`, async () => { const results = await tools.runAsyncWithIO( - `${indexingCmd} queryIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID} -s Ardvark -e Zoo`, + `${schemaCmd} addColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); const output = results.stdout + results.stderr; - t.regex(output, /AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:/); - t.regex(output, /AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/); - } -); - -// read_data_with_index -test.serial(`should read an example table with an index`, async t => { - const results = await tools.runAsyncWithIO( - `${indexingCmd} readIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /AlbumId: 1, AlbumTitle: Total Junk/); -}); - -// read_data_with_storing_index -test.serial(`should read an example table with a storing index`, async t => { - const results = await tools.runAsyncWithIO( - `${indexingCmd} readStoringIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /AlbumId: 1, AlbumTitle: Total Junk/); -}); - -// read_only_transaction -test.serial(`should read an example table using transactions`, async t => { - const results = await tools.runAsyncWithIO( - `${transactionCmd} readOnly ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); - t.regex(output, /Successfully executed read-only transaction\./); -}); - -// read_write_transaction -test.serial( - `should read from and write to an example table using transactions`, - async t => { - let results = await tools.runAsyncWithIO( - `${transactionCmd} readWrite ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - let output = results.stdout + results.stderr; - t.regex(output, /The first album's marketing budget: 100000/); - t.regex(output, /The second album's marketing budget: 500000/); - t.regex( - output, - /Successfully executed read-write transaction to transfer 200000 from Album 2 to Album 1./ - ); + assert.deepEqual(output, /Waiting for operation to complete\.\.\./); + assert.deepEqual(output, /Added the MarketingBudget column\./); + }); - results = await tools.runAsyncWithIO( - `${schemaCmd} queryNewColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + // update_data + it(`should update existing rows in an example table`, async () => { + const results = await tools.runAsyncWithIO( + `${crudCmd} update ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); - output = results.stdout + results.stderr; - t.regex(output, /SingerId: 1, AlbumId: 1, MarketingBudget: 300000/); - t.regex(output, /SingerId: 2, AlbumId: 2, MarketingBudget: 300000/); - } -); - -// create_query_partitions -test.serial(`should create query partitions`, async t => { - const instance = spanner.instance(INSTANCE_ID); - const database = instance.database(DATABASE_ID); - const [transaction] = await database.createBatchTransaction(); - const identifier = JSON.stringify(transaction.identifier()); - - const results = await tools.runAsyncWithIO( - `${batchCmd} create-query-partitions ${INSTANCE_ID} ${DATABASE_ID} '${identifier}' ${PROJECT_ID}`, - cwd - ); - - const output = results.stdout + results.stderr; - - t.regex(output, /Successfully created \d query partitions\./); - - await transaction.close(); -}); - -// execute_partition -test.serial(`should execute a partition`, async t => { - const instance = spanner.instance(INSTANCE_ID); - const database = instance.database(DATABASE_ID); - const [transaction] = await database.createBatchTransaction(); - const identifier = JSON.stringify(transaction.identifier()); - - const query = `SELECT SingerId FROM Albums`; - const [partitions] = await transaction.createQueryPartitions(query); - const partition = JSON.stringify(partitions[0]); - - const results = await tools.runAsyncWithIO( - `${batchCmd} execute-partition ${INSTANCE_ID} ${DATABASE_ID} '${identifier}' '${partition}' ${PROJECT_ID}`, - cwd - ); - - const output = results.stdout + results.stderr; - - t.regex(output, /Successfully received \d from executed partition\./); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Updated data\./); + }); - await transaction.close(); -}); + // read_stale_data + it(`should read stale data from an example table`, () => { + //t.plan(2); + // read-stale-data reads data that is exactly 15 seconds old. So, make sure + // 15 seconds have elapsed since the update_data test. + return new Promise(resolve => setTimeout(resolve, 16000)).then(async () => { + const results = await tools.runAsyncWithIO( + `${crudCmd} read-stale ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual( + output, + /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget: 100000/ + ); + assert.deepEqual( + output, + /SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold your Peace, MarketingBudget: 500000/ + ); + }); + }); -// add_timestamp_column -test.serial(`should add a timestamp column to a table`, async t => { - const results = await tools.runAsyncWithIO( - `${timestampCmd} addTimestampColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /Waiting for operation to complete\.\.\./); - t.regex( - output, - /Added LastUpdateTime as a commit timestamp column in Albums table\./ + // query_data_with_new_column + it( + `should query an example table with an additional column and return matching rows`, + async () => { + const results = await tools.runAsyncWithIO( + `${schemaCmd} queryNewColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /SingerId: 1, AlbumId: 1, MarketingBudget: 100000/); + assert.deepEqual(output, /SingerId: 2, AlbumId: 2, MarketingBudget: 500000/); + } ); -}); -// update_data_with_timestamp_column -test.serial( - `should update existing rows in an example table with commit timestamp column`, - async t => { + // create_index + it(`should create an index in an example table`, async () => { const results = await tools.runAsyncWithIO( - `${timestampCmd} updateWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + `${indexingCmd} createIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); const output = results.stdout + results.stderr; - t.regex(output, /Updated data\./); - } -); - -// query_data_with_timestamp_column -test.serial( - `should query an example table with an additional timestamp column and return matching rows`, - async t => { - const results = await tools.runAsyncWithIO( - `${timestampCmd} queryWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex( - output, - /SingerId: 1, AlbumId: 1, MarketingBudget: 1000000, LastUpdateTime:/ - ); - t.regex( - output, - /SingerId: 2, AlbumId: 2, MarketingBudget: 750000, LastUpdateTime:/ - ); - } -); + assert.deepEqual(output, /Waiting for operation to complete\.\.\./); + assert.deepEqual(output, /Added the AlbumsByAlbumTitle index\./); + }); -// create_table_with_timestamp_column -test.serial( - `should create an example table with a timestamp column`, - async t => { + // create_storing_index + it(`should create a storing index in an example table`, async () => { const results = await tools.runAsyncWithIO( - `${timestampCmd} createTableWithTimestamp "${INSTANCE_ID}" "${DATABASE_ID}" ${PROJECT_ID}`, + `${indexingCmd} createStoringIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); const output = results.stdout + results.stderr; - t.regex( - output, - new RegExp(`Waiting for operation on ${DATABASE_ID} to complete...`) - ); - t.regex( - output, - new RegExp(`Created table Performances in database ${DATABASE_ID}.`) - ); - } -); + assert.deepEqual(output, /Waiting for operation to complete\.\.\./); + assert.deepEqual(output, /Added the AlbumsByAlbumTitle2 index\./); + }); -// insert_data_with_timestamp -test.serial( - `should insert rows into an example table with timestamp column`, - async t => { - const results = await tools.runAsyncWithIO( - `${timestampCmd} insertWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /Inserted data\./); - } -); - -// query_new_table_with_timestamp -test.serial( - `should query an example table with a non-null timestamp column and return matching rows`, - async t => { - const results = await tools.runAsyncWithIO( - `${timestampCmd} queryTableWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /SingerId: 1, VenueId: 4, EventDate:/); - t.regex(output, /Revenue: 15000, LastUpdateTime:/); - } -); - -// write_data_for_struct_queries -test.serial( - `should insert rows into an example table for use with struct query examples`, - async t => { - const results = await tools.runAsyncWithIO( - `${structCmd} writeDataForStructQueries ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /Inserted data\./); - } -); - -// query_with_struct_param -test.serial(`should query an example table with a STRUCT param`, async t => { - const results = await tools.runAsyncWithIO( - `${structCmd} queryDataWithStruct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd + // query_data_with_index + it( + `should query an example table with an index and return matching rows`, + async () => { + const results = await tools.runAsyncWithIO( + `${indexingCmd} queryIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/); + assert.deepEqual(output.includes(`AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:`), false); + } ); - const output = results.stdout + results.stderr; - t.regex(output, /SingerId: 6/); -}); -// query_with_array_of_struct_param -test.serial( - `should query an example table with an array of STRUCT param`, - async t => { - const results = await tools.runAsyncWithIO( - `${structCmd} queryWithArrayOfStruct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /SingerId: 6\nSingerId: 7/); - } -); - -// query_with_struct_field_param -test.serial( - `should query an example table with a STRUCT field param`, - async t => { - const results = await tools.runAsyncWithIO( - `${structCmd} queryStructField ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /SingerId: 6/); - } -); - -// query_with_nested_struct_param -test.serial( - `should query an example table with a nested STRUCT param`, - async t => { - const results = await tools.runAsyncWithIO( - `${structCmd} queryNestedStructField ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex( - output, - /SingerId: 6, SongName: Imagination\nSingerId: 9, SongName: Imagination/ - ); - } -); + it( + `should respect query boundaries when querying an example table with an index`, + async () => { + const results = await tools.runAsyncWithIO( + `${indexingCmd} queryIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID} -s Ardvark -e Zoo`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:/); + assert.deepEqual(output, /AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/); + } + ); -// dml_standard_insert -test.serial( - `should insert rows into an example table using a DML statement`, - async t => { + // read_data_with_index + it(`should read an example table with an index`, async () => { const results = await tools.runAsyncWithIO( - `${dmlCmd} insertUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + `${indexingCmd} readIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); const output = results.stdout + results.stderr; - t.regex(output, /Successfully inserted 1 record into the Singers table/); - } -); - -// dml_standard_update -test.serial( - `should update a row in an example table using a DML statement`, - async t => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} updateUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /Successfully updated 1 record/); - } -); - -// dml_standard_delete -test.serial( - `should delete a row from an example table using a DML statement`, - async t => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} deleteUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /Successfully deleted 1 record\./); - } -); - -// dml_standard_update_with_timestamp -test.serial( - `should update the timestamp of multiple records in an example table using a DML statement`, - async t => { + assert.deepEqual(output, /AlbumId: 1, AlbumTitle: Total Junk/); + }); + + // read_data_with_storing_index + it(`should read an example table with a storing index`, async () => { const results = await tools.runAsyncWithIO( - `${dmlCmd} updateUsingDmlWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + `${indexingCmd} readStoringIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); const output = results.stdout + results.stderr; - t.regex(output, /Successfully updated 2 records/); - } -); - -// dml_write_then_read -test.serial( - `should insert a record in an example table using a DML statement and then query the record`, - async t => { + assert.deepEqual(output, /AlbumId: 1, AlbumTitle: Total Junk/); + }); + + // read_only_transaction + it(`should read an example table using transactions`, async () => { const results = await tools.runAsyncWithIO( - `${dmlCmd} writeAndReadUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + `${transactionCmd} readOnly ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); const output = results.stdout + results.stderr; - t.regex(output, /Timothy Campbell/); - } -); - -// dml_structs -test.serial( - `should update a record in an example table using a DML statement along with a struct value`, - async t => { + assert.deepEqual(output, /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); + assert.deepEqual(output, /Successfully executed read-only transaction\./); + }); + + // read_write_transaction + it( + `should read from and write to an example table using transactions`, + async () => { + let results = await tools.runAsyncWithIO( + `${transactionCmd} readWrite ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + let output = results.stdout + results.stderr; + assert.deepEqual(output, /The first album's marketing budget: 100000/); + assert.deepEqual(output, /The second album's marketing budget: 500000/); + assert.deepEqual( + output, + /Successfully executed read-write transaction to transfer 200000 from Album 2 to Album 1./ + ); + + results = await tools.runAsyncWithIO( + `${schemaCmd} queryNewColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + output = results.stdout + results.stderr; + assert.deepEqual(output, /SingerId: 1, AlbumId: 1, MarketingBudget: 300000/); + assert.deepEqual(output, /SingerId: 2, AlbumId: 2, MarketingBudget: 300000/); + } + ); + + // create_query_partitions + it(`should create query partitions`, async () => { + const instance = spanner.instance(INSTANCE_ID); + const database = instance.database(DATABASE_ID); + const [transaction] = await database.createBatchTransaction(); + const identifier = JSON.stringify(transaction.identifier()); + const results = await tools.runAsyncWithIO( - `${dmlCmd} updateUsingDmlWithStruct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + `${batchCmd} create-query-partitions ${INSTANCE_ID} ${DATABASE_ID} '${identifier}' ${PROJECT_ID}`, cwd ); + const output = results.stdout + results.stderr; - t.regex(output, /Successfully updated 1 record/); - } -); - -// dml_getting_started_insert -test.serial( - `should insert multiple records into an example table using a DML statement`, - async t => { + + assert.deepEqual(output, /Successfully created \d query partitions\./); + + await transaction.close(); + }); + + // execute_partition + it(`should execute a partition`, async () => { + const instance = spanner.instance(INSTANCE_ID); + const database = instance.database(DATABASE_ID); + const [transaction] = await database.createBatchTransaction(); + const identifier = JSON.stringify(transaction.identifier()); + + const query = `SELECT SingerId FROM Albums`; + const [partitions] = await transaction.createQueryPartitions(query); + const partition = JSON.stringify(partitions[0]); + const results = await tools.runAsyncWithIO( - `${dmlCmd} writeUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + `${batchCmd} execute-partition ${INSTANCE_ID} ${DATABASE_ID} '${identifier}' '${partition}' ${PROJECT_ID}`, cwd ); + const output = results.stdout + results.stderr; - t.regex(output, /4 records inserted/); - } -); - -// dml_getting_started_update -test.serial( - `should transfer value from one record to another using DML statements within a transaction`, - async t => { + + assert.deepEqual(output, /Successfully received \d from executed partition\./); + + await transaction.close(); + }); + + // add_timestamp_column + it(`should add a timestamp column to a table`, async () => { const results = await tools.runAsyncWithIO( - `${dmlCmd} writeWithTransactionUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + `${timestampCmd} addTimestampColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); const output = results.stdout + results.stderr; - t.regex( + assert.deepEqual(output, /Waiting for operation to complete\.\.\./); + assert.deepEqual( output, - /Successfully executed read-write transaction using DML to transfer 200000 from Album 1 to Album 2/ + /Added LastUpdateTime as a commit timestamp column in Albums table\./ ); - } -); + }); -// dml_partitioned_update -test.serial( - `should update multiple records using a partitioned DML statement`, - async t => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} updateUsingPartitionedDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - t.regex(output, /Successfully updated 3 records/); - } -); - -// dml_partitioned_delete -test.serial( - `should delete multiple records using a partitioned DML statement`, - async t => { + // update_data_with_timestamp_column + it( + `should update existing rows in an example table with commit timestamp column`, + async () => { + const results = await tools.runAsyncWithIO( + `${timestampCmd} updateWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Updated data\./); + } + ); + + // query_data_with_timestamp_column + it( + `should query an example table with an additional timestamp column and return matching rows`, + async () => { + const results = await tools.runAsyncWithIO( + `${timestampCmd} queryWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual( + output, + /SingerId: 1, AlbumId: 1, MarketingBudget: 1000000, LastUpdateTime:/ + ); + assert.deepEqual( + output, + /SingerId: 2, AlbumId: 2, MarketingBudget: 750000, LastUpdateTime:/ + ); + } + ); + + // create_table_with_timestamp_column + it( + `should create an example table with a timestamp column`, + async () => { + const results = await tools.runAsyncWithIO( + `${timestampCmd} createTableWithTimestamp "${INSTANCE_ID}" "${DATABASE_ID}" ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual( + output, + new RegExp(`Waiting for operation on ${DATABASE_ID} to complete...`) + ); + assert.deepEqual( + output, + new RegExp(`Created table Performances in database ${DATABASE_ID}.`) + ); + } + ); + + // insert_data_with_timestamp + it( + `should insert rows into an example table with timestamp column`, + async () => { + const results = await tools.runAsyncWithIO( + `${timestampCmd} insertWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Inserted data\./); + } + ); + + // query_new_table_with_timestamp + it( + `should query an example table with a non-null timestamp column and return matching rows`, + async () => { + const results = await tools.runAsyncWithIO( + `${timestampCmd} queryTableWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /SingerId: 1, VenueId: 4, EventDate:/); + assert.deepEqual(output, /Revenue: 15000, LastUpdateTime:/); + } + ); + + // write_data_for_struct_queries + it( + `should insert rows into an example table for use with struct query examples`, + async () => { + const results = await tools.runAsyncWithIO( + `${structCmd} writeDataForStructQueries ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Inserted data\./); + } + ); + + // query_with_struct_param + it(`should query an example table with a STRUCT param`, async () => { const results = await tools.runAsyncWithIO( - `${dmlCmd} deleteUsingPartitionedDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + `${structCmd} queryDataWithStruct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); const output = results.stdout + results.stderr; - t.regex(output, /Successfully deleted 5 records/); - } -); + assert.deepEqual(output, /SingerId: 6/); + }); + + // query_with_array_of_struct_param + it( + `should query an example table with an array of STRUCT param`, + async () => { + const results = await tools.runAsyncWithIO( + `${structCmd} queryWithArrayOfStruct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /SingerId: 6\nSingerId: 7/); + } + ); + + // query_with_struct_field_param + it( + `should query an example table with a STRUCT field param`, + async () => { + const results = await tools.runAsyncWithIO( + `${structCmd} queryStructField ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /SingerId: 6/); + } + ); + + // query_with_nested_struct_param + it( + `should query an example table with a nested STRUCT param`, + async () => { + const results = await tools.runAsyncWithIO( + `${structCmd} queryNestedStructField ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual( + output, + /SingerId: 6, SongName: Imagination\nSingerId: 9, SongName: Imagination/ + ); + } + ); + + // dml_standard_insert + it( + `should insert rows into an example table using a DML statement`, + async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} insertUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Successfully inserted 1 record into the Singers table/); + } + ); + + // dml_standard_update + it( + `should update a row in an example table using a DML statement`, + async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} updateUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Successfully updated 1 record/); + } + ); + + // dml_standard_delete + it( + `should delete a row from an example table using a DML statement`, + async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} deleteUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Successfully deleted 1 record\./); + } + ); + + // dml_standard_update_with_timestamp + it( + `should update the timestamp of multiple records in an example table using a DML statement`, + async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} updateUsingDmlWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Successfully updated 2 records/); + } + ); + + // dml_write_then_read + it( + `should insert a record in an example table using a DML statement and then query the record`, + async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} writeAndReadUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Timothy Campbell/); + } + ); + + // dml_structs + it( + `should update a record in an example table using a DML statement along with a struct value`, + async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} updateUsingDmlWithStruct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Successfully updated 1 record/); + } + ); + + // dml_getting_started_insert + it( + `should insert multiple records into an example table using a DML statement`, + async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} writeUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /4 records inserted/); + } + ); + + // dml_getting_started_update + it( + `should transfer value from one record to another using DML statements within a transaction`, + async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} writeWithTransactionUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual( + output, + /Successfully executed read-write transaction using DML to transfer 200000 from Album 1 to Album 2/ + ); + } + ); + + // dml_partitioned_update + it( + `should update multiple records using a partitioned DML statement`, + async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} updateUsingPartitionedDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Successfully updated 3 records/); + } + ); + + // dml_partitioned_delete + it( + `should delete multiple records using a partitioned DML statement`, + async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} deleteUsingPartitionedDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepEqual(output, /Successfully deleted 5 records/); + } + ); + +}); function apiRequest(reqOpts) { return new Promise((resolve, reject) => { From 7e83c4141794431833f978b0d7fa4fb3644d6c97 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Thu, 25 Oct 2018 12:21:37 +0530 Subject: [PATCH 02/18] Change var to const --- samples/system-test/quickstart.test.js | 2 +- samples/system-test/spanner.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index 212fa8b2c..f340d630e 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -17,7 +17,7 @@ const proxyquire = require(`proxyquire`); const sinon = require(`sinon`); -var assert = require('assert'); +const assert = require('assert'); const tools = require(`@google-cloud/nodejs-repo-tools`); describe('QuickStart', () => { diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 8a2415556..1b882a564 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -18,7 +18,7 @@ const path = require(`path`); const request = require(`request`); const {Spanner} = require(`@google-cloud/spanner`); -var assert = require('assert'); +const assert = require('assert'); const tools = require(`@google-cloud/nodejs-repo-tools`); const batchCmd = `node batch.js`; From fe27a8250891a6488ee0d2f20f09cf63ce5757c5 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Thu, 25 Oct 2018 13:02:51 +0530 Subject: [PATCH 03/18] Removed space --- samples/system-test/quickstart.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index f340d630e..d351392b3 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -53,11 +53,11 @@ describe('QuickStart', () => { instance: sinon.stub().returns(instanceMock), }; - proxyquire(`../quickstart`, { - '@google-cloud/spanner': { - Spanner: sinon.stub().returns(spannerMock), - }, - }); + proxyquire(`../quickstart`, { + '@google-cloud/spanner': { + Spanner: sinon.stub().returns(spannerMock), + }, + }); assert.deepEqual(spannerMock.instance.getCall(0).args, [`my-instance`]); assert.deepEqual(instanceMock.database.getCall(0).args, [`my-database`]); From 11370404eb8dbc596964740a14e7cdf17180ac69 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Thu, 25 Oct 2018 13:07:38 +0530 Subject: [PATCH 04/18] minor change --- samples/system-test/quickstart.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index d351392b3..69be1d749 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -53,9 +53,9 @@ describe('QuickStart', () => { instance: sinon.stub().returns(instanceMock), }; - proxyquire(`../quickstart`, { - '@google-cloud/spanner': { - Spanner: sinon.stub().returns(spannerMock), + proxyquire(`../quickstart`, { + '@google-cloud/spanner': { + Spanner: sinon.stub().returns(spannerMock), }, }); From 9a8b27edec834d785a61e48caa95051ba982f309 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Thu, 25 Oct 2018 13:09:41 +0530 Subject: [PATCH 05/18] Removed space --- samples/system-test/quickstart.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index 69be1d749..f340d630e 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -56,8 +56,8 @@ describe('QuickStart', () => { proxyquire(`../quickstart`, { '@google-cloud/spanner': { Spanner: sinon.stub().returns(spannerMock), - }, - }); + }, + }); assert.deepEqual(spannerMock.instance.getCall(0).args, [`my-instance`]); assert.deepEqual(instanceMock.database.getCall(0).args, [`my-database`]); From 3fd088d8eec6a3c95928d24d9d3e6c98ecd56d0a Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Thu, 25 Oct 2018 16:32:20 +0530 Subject: [PATCH 06/18] remove whitespace --- samples/system-test/quickstart.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index f340d630e..7f96fafcb 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -54,9 +54,9 @@ describe('QuickStart', () => { }; proxyquire(`../quickstart`, { - '@google-cloud/spanner': { - Spanner: sinon.stub().returns(spannerMock), - }, + '@google-cloud/spanner': { + Spanner: sinon.stub().returns(spannerMock), + }, }); assert.deepEqual(spannerMock.instance.getCall(0).args, [`my-instance`]); From 27e5ef50da5c5b4613d087b956b2301cd876d0cd Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Thu, 25 Oct 2018 16:39:05 +0530 Subject: [PATCH 07/18] remove whitespace --- samples/system-test/quickstart.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index 7f96fafcb..f340d630e 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -54,9 +54,9 @@ describe('QuickStart', () => { }; proxyquire(`../quickstart`, { - '@google-cloud/spanner': { - Spanner: sinon.stub().returns(spannerMock), - }, + '@google-cloud/spanner': { + Spanner: sinon.stub().returns(spannerMock), + }, }); assert.deepEqual(spannerMock.instance.getCall(0).args, [`my-instance`]); From 8b2d751b5768dc5a7406c5e4832cbb01fb213e87 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Thu, 25 Oct 2018 20:13:30 +0530 Subject: [PATCH 08/18] Whitespace change --- samples/system-test/quickstart.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index f340d630e..d82c22283 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -29,7 +29,7 @@ describe('QuickStart', () => { }); it(`should query a table`, function(done) { - this.timeout(15000); + this.timeout(15000); const databaseMock = { run: _query => { assert.deepEqual(_query, { @@ -54,9 +54,9 @@ describe('QuickStart', () => { }; proxyquire(`../quickstart`, { - '@google-cloud/spanner': { - Spanner: sinon.stub().returns(spannerMock), - }, + '@google-cloud/spanner': { + Spanner: sinon.stub().returns(spannerMock), + }, }); assert.deepEqual(spannerMock.instance.getCall(0).args, [`my-instance`]); From 0e759d2ce8c930546e0802d6a5758dc8d69764df Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Thu, 25 Oct 2018 20:48:28 +0530 Subject: [PATCH 09/18] Updated test script in package json --- samples/package.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/samples/package.json b/samples/package.json index 1d1668be9..74dcc36c1 100644 --- a/samples/package.json +++ b/samples/package.json @@ -9,9 +9,7 @@ "node": ">=8" }, "scripts": { - "ava": "ava -T 5m --verbose test/*.test.js ./system-test/*.test.js", - "cover": "nyc --reporter=lcov --cache ava -T 5m --verbose test/*.test.js ./system-test/*.test.js && nyc report", - "test": "npm run cover" + "test": "mocha system-test/*.js --timeout 600000" }, "dependencies": { "@google-cloud/spanner": "^2.1.0", From 84689c0cbd7ae4c2144742f347fdf50f3235cb9c Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Fri, 26 Oct 2018 18:43:09 +0530 Subject: [PATCH 10/18] Refactor unit test cases --- samples/system-test/.eslintrc.yml | 2 + samples/system-test/quickstart.test.js | 38 +- samples/system-test/spanner.test.js | 629 ++++++++++++------------- 3 files changed, 324 insertions(+), 345 deletions(-) diff --git a/samples/system-test/.eslintrc.yml b/samples/system-test/.eslintrc.yml index c0289282a..752164361 100644 --- a/samples/system-test/.eslintrc.yml +++ b/samples/system-test/.eslintrc.yml @@ -1,4 +1,6 @@ --- +env: + mocha: true rules: node/no-unpublished-require: off node/no-unsupported-features: off diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index d82c22283..4f7a52f0f 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -21,29 +21,25 @@ const assert = require('assert'); const tools = require(`@google-cloud/nodejs-repo-tools`); describe('QuickStart', () => { - before(() => { - tools.stubConsole; - }); - afterEach(() => { - tools.restoreConsole; - }); + before(() => tools.stubConsole); + + afterEach(() => tools.restoreConsole); - it(`should query a table`, function(done) { - this.timeout(15000); + it(`should query a table`, done => { const databaseMock = { run: _query => { - assert.deepEqual(_query, { + assert.deepStrictEqual(_query, { sql: `SELECT 1`, }); setTimeout(() => { try { - //assert.deepEqual(console.log.getCall(0).args, [`test`]); + //assert.deepStrictEqual(console.log.getCall(0).args, [`test`]); done(); - } catch (err) { - done(err); - } + } catch (err) { + done(err); + } }, 200); - return Promise.resolve([['test']]); + return Promise.resolve([['test']]); }, }; const instanceMock = { @@ -54,12 +50,16 @@ describe('QuickStart', () => { }; proxyquire(`../quickstart`, { - '@google-cloud/spanner': { - Spanner: sinon.stub().returns(spannerMock), - }, + '@google-cloud/spanner': { + Spanner: sinon.stub().returns(spannerMock), + }, }); - assert.deepEqual(spannerMock.instance.getCall(0).args, [`my-instance`]); - assert.deepEqual(instanceMock.database.getCall(0).args, [`my-database`]); + assert.deepStrictEqual(spannerMock.instance.getCall(0).args, [ + `my-instance`, + ]); + assert.deepStrictEqual(instanceMock.database.getCall(0).args, [ + `my-database`, + ]); }); }); diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 1b882a564..25475dcaa 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -41,9 +41,8 @@ const spanner = new Spanner({ projectId: PROJECT_ID, }); -describe('Spanner', function() { - this.timeout(15000); - before(()=>tools.checkCredentials); +describe('Spanner', () => { + before(() => tools.checkCredentials); before(async () => { const instance = spanner.instance(INSTANCE_ID); @@ -120,11 +119,11 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual( + assert.deepStrictEqual( output, new RegExp(`Waiting for operation on ${DATABASE_ID} to complete...`) ); - assert.deepEqual( + assert.deepStrictEqual( output, new RegExp(`Created database ${DATABASE_ID} on instance ${INSTANCE_ID}.`) ); @@ -137,21 +136,21 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual(output, /Inserted data\./); + assert.deepStrictEqual(output, /Inserted data\./); }); // query_data - it( - `should query an example table and return matching rows`, - async () => { - const results = await tools.runAsyncWithIO( - `${crudCmd} query ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); - } - ); + it(`should query an example table and return matching rows`, async () => { + const results = await tools.runAsyncWithIO( + `${crudCmd} query ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual( + output, + /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/ + ); + }); // read_data it(`should read an example table`, async () => { @@ -160,7 +159,10 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual(output, /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); + assert.deepStrictEqual( + output, + /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/ + ); }); // add_column @@ -170,8 +172,8 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual(output, /Waiting for operation to complete\.\.\./); - assert.deepEqual(output, /Added the MarketingBudget column\./); + assert.deepStrictEqual(output, /Waiting for operation to complete\.\.\./); + assert.deepStrictEqual(output, /Added the MarketingBudget column\./); }); // update_data @@ -181,7 +183,7 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual(output, /Updated data\./); + assert.deepStrictEqual(output, /Updated data\./); }); // read_stale_data @@ -195,11 +197,11 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual( + assert.deepStrictEqual( output, /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget: 100000/ ); - assert.deepEqual( + assert.deepStrictEqual( output, /SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold your Peace, MarketingBudget: 500000/ ); @@ -207,18 +209,21 @@ describe('Spanner', function() { }); // query_data_with_new_column - it( - `should query an example table with an additional column and return matching rows`, - async () => { - const results = await tools.runAsyncWithIO( - `${schemaCmd} queryNewColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /SingerId: 1, AlbumId: 1, MarketingBudget: 100000/); - assert.deepEqual(output, /SingerId: 2, AlbumId: 2, MarketingBudget: 500000/); - } - ); + it(`should query an example table with an additional column and return matching rows`, async () => { + const results = await tools.runAsyncWithIO( + `${schemaCmd} queryNewColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual( + output, + /SingerId: 1, AlbumId: 1, MarketingBudget: 100000/ + ); + assert.deepStrictEqual( + output, + /SingerId: 2, AlbumId: 2, MarketingBudget: 500000/ + ); + }); // create_index it(`should create an index in an example table`, async () => { @@ -227,8 +232,8 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual(output, /Waiting for operation to complete\.\.\./); - assert.deepEqual(output, /Added the AlbumsByAlbumTitle index\./); + assert.deepStrictEqual(output, /Waiting for operation to complete\.\.\./); + assert.deepStrictEqual(output, /Added the AlbumsByAlbumTitle index\./); }); // create_storing_index @@ -238,36 +243,42 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual(output, /Waiting for operation to complete\.\.\./); - assert.deepEqual(output, /Added the AlbumsByAlbumTitle2 index\./); + assert.deepStrictEqual(output, /Waiting for operation to complete\.\.\./); + assert.deepStrictEqual(output, /Added the AlbumsByAlbumTitle2 index\./); }); // query_data_with_index - it( - `should query an example table with an index and return matching rows`, - async () => { - const results = await tools.runAsyncWithIO( - `${indexingCmd} queryIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/); - assert.deepEqual(output.includes(`AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:`), false); - } - ); + it(`should query an example table with an index and return matching rows`, async () => { + const results = await tools.runAsyncWithIO( + `${indexingCmd} queryIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual( + output, + /AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/ + ); + assert.deepStrictEqual( + output.includes(`AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:`), + false + ); + }); - it( - `should respect query boundaries when querying an example table with an index`, - async () => { - const results = await tools.runAsyncWithIO( - `${indexingCmd} queryIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID} -s Ardvark -e Zoo`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:/); - assert.deepEqual(output, /AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/); - } - ); + it(`should respect query boundaries when querying an example table with an index`, async () => { + const results = await tools.runAsyncWithIO( + `${indexingCmd} queryIndex ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID} -s Ardvark -e Zoo`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual( + output, + /AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:/ + ); + assert.deepStrictEqual( + output, + /AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/ + ); + }); // read_data_with_index it(`should read an example table with an index`, async () => { @@ -276,7 +287,7 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual(output, /AlbumId: 1, AlbumTitle: Total Junk/); + assert.deepStrictEqual(output, /AlbumId: 1, AlbumTitle: Total Junk/); }); // read_data_with_storing_index @@ -286,7 +297,7 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual(output, /AlbumId: 1, AlbumTitle: Total Junk/); + assert.deepStrictEqual(output, /AlbumId: 1, AlbumTitle: Total Junk/); }); // read_only_transaction @@ -296,35 +307,50 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual(output, /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); - assert.deepEqual(output, /Successfully executed read-only transaction\./); + assert.deepStrictEqual( + output, + /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/ + ); + assert.deepStrictEqual( + output, + /Successfully executed read-only transaction\./ + ); }); // read_write_transaction - it( - `should read from and write to an example table using transactions`, - async () => { - let results = await tools.runAsyncWithIO( - `${transactionCmd} readWrite ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - let output = results.stdout + results.stderr; - assert.deepEqual(output, /The first album's marketing budget: 100000/); - assert.deepEqual(output, /The second album's marketing budget: 500000/); - assert.deepEqual( - output, - /Successfully executed read-write transaction to transfer 200000 from Album 2 to Album 1./ - ); + it(`should read from and write to an example table using transactions`, async () => { + let results = await tools.runAsyncWithIO( + `${transactionCmd} readWrite ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + let output = results.stdout + results.stderr; + assert.deepStrictEqual( + output, + /The first album's marketing budget: 100000/ + ); + assert.deepStrictEqual( + output, + /The second album's marketing budget: 500000/ + ); + assert.deepStrictEqual( + output, + /Successfully executed read-write transaction to transfer 200000 from Album 2 to Album 1./ + ); - results = await tools.runAsyncWithIO( - `${schemaCmd} queryNewColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - output = results.stdout + results.stderr; - assert.deepEqual(output, /SingerId: 1, AlbumId: 1, MarketingBudget: 300000/); - assert.deepEqual(output, /SingerId: 2, AlbumId: 2, MarketingBudget: 300000/); - } - ); + results = await tools.runAsyncWithIO( + `${schemaCmd} queryNewColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + output = results.stdout + results.stderr; + assert.deepStrictEqual( + output, + /SingerId: 1, AlbumId: 1, MarketingBudget: 300000/ + ); + assert.deepStrictEqual( + output, + /SingerId: 2, AlbumId: 2, MarketingBudget: 300000/ + ); + }); // create_query_partitions it(`should create query partitions`, async () => { @@ -340,7 +366,10 @@ describe('Spanner', function() { const output = results.stdout + results.stderr; - assert.deepEqual(output, /Successfully created \d query partitions\./); + assert.deepStrictEqual( + output, + /Successfully created \d query partitions\./ + ); await transaction.close(); }); @@ -363,7 +392,10 @@ describe('Spanner', function() { const output = results.stdout + results.stderr; - assert.deepEqual(output, /Successfully received \d from executed partition\./); + assert.deepStrictEqual( + output, + /Successfully received \d from executed partition\./ + ); await transaction.close(); }); @@ -375,105 +407,87 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual(output, /Waiting for operation to complete\.\.\./); - assert.deepEqual( + assert.deepStrictEqual(output, /Waiting for operation to complete\.\.\./); + assert.deepStrictEqual( output, /Added LastUpdateTime as a commit timestamp column in Albums table\./ ); }); // update_data_with_timestamp_column - it( - `should update existing rows in an example table with commit timestamp column`, - async () => { - const results = await tools.runAsyncWithIO( - `${timestampCmd} updateWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /Updated data\./); - } - ); + it(`should update existing rows in an example table with commit timestamp column`, async () => { + const results = await tools.runAsyncWithIO( + `${timestampCmd} updateWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /Updated data\./); + }); // query_data_with_timestamp_column - it( - `should query an example table with an additional timestamp column and return matching rows`, - async () => { - const results = await tools.runAsyncWithIO( - `${timestampCmd} queryWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual( - output, - /SingerId: 1, AlbumId: 1, MarketingBudget: 1000000, LastUpdateTime:/ - ); - assert.deepEqual( - output, - /SingerId: 2, AlbumId: 2, MarketingBudget: 750000, LastUpdateTime:/ - ); - } - ); + it(`should query an example table with an additional timestamp column and return matching rows`, async () => { + const results = await tools.runAsyncWithIO( + `${timestampCmd} queryWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual( + output, + /SingerId: 1, AlbumId: 1, MarketingBudget: 1000000, LastUpdateTime:/ + ); + assert.deepStrictEqual( + output, + /SingerId: 2, AlbumId: 2, MarketingBudget: 750000, LastUpdateTime:/ + ); + }); // create_table_with_timestamp_column - it( - `should create an example table with a timestamp column`, - async () => { - const results = await tools.runAsyncWithIO( - `${timestampCmd} createTableWithTimestamp "${INSTANCE_ID}" "${DATABASE_ID}" ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual( - output, - new RegExp(`Waiting for operation on ${DATABASE_ID} to complete...`) - ); - assert.deepEqual( - output, - new RegExp(`Created table Performances in database ${DATABASE_ID}.`) - ); - } - ); + it(`should create an example table with a timestamp column`, async () => { + const results = await tools.runAsyncWithIO( + `${timestampCmd} createTableWithTimestamp "${INSTANCE_ID}" "${DATABASE_ID}" ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual( + output, + new RegExp(`Waiting for operation on ${DATABASE_ID} to complete...`) + ); + assert.deepStrictEqual( + output, + new RegExp(`Created table Performances in database ${DATABASE_ID}.`) + ); + }); // insert_data_with_timestamp - it( - `should insert rows into an example table with timestamp column`, - async () => { - const results = await tools.runAsyncWithIO( - `${timestampCmd} insertWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /Inserted data\./); - } - ); + it(`should insert rows into an example table with timestamp column`, async () => { + const results = await tools.runAsyncWithIO( + `${timestampCmd} insertWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /Inserted data\./); + }); // query_new_table_with_timestamp - it( - `should query an example table with a non-null timestamp column and return matching rows`, - async () => { - const results = await tools.runAsyncWithIO( - `${timestampCmd} queryTableWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /SingerId: 1, VenueId: 4, EventDate:/); - assert.deepEqual(output, /Revenue: 15000, LastUpdateTime:/); - } - ); + it(`should query an example table with a non-null timestamp column and return matching rows`, async () => { + const results = await tools.runAsyncWithIO( + `${timestampCmd} queryTableWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /SingerId: 1, VenueId: 4, EventDate:/); + assert.deepStrictEqual(output, /Revenue: 15000, LastUpdateTime:/); + }); // write_data_for_struct_queries - it( - `should insert rows into an example table for use with struct query examples`, - async () => { - const results = await tools.runAsyncWithIO( - `${structCmd} writeDataForStructQueries ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /Inserted data\./); - } - ); + it(`should insert rows into an example table for use with struct query examples`, async () => { + const results = await tools.runAsyncWithIO( + `${structCmd} writeDataForStructQueries ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /Inserted data\./); + }); // query_with_struct_param it(`should query an example table with a STRUCT param`, async () => { @@ -482,184 +496,147 @@ describe('Spanner', function() { cwd ); const output = results.stdout + results.stderr; - assert.deepEqual(output, /SingerId: 6/); + assert.deepStrictEqual(output, /SingerId: 6/); }); // query_with_array_of_struct_param - it( - `should query an example table with an array of STRUCT param`, - async () => { - const results = await tools.runAsyncWithIO( - `${structCmd} queryWithArrayOfStruct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /SingerId: 6\nSingerId: 7/); - } - ); + it(`should query an example table with an array of STRUCT param`, async () => { + const results = await tools.runAsyncWithIO( + `${structCmd} queryWithArrayOfStruct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /SingerId: 6\nSingerId: 7/); + }); // query_with_struct_field_param - it( - `should query an example table with a STRUCT field param`, - async () => { - const results = await tools.runAsyncWithIO( - `${structCmd} queryStructField ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /SingerId: 6/); - } - ); + it(`should query an example table with a STRUCT field param`, async () => { + const results = await tools.runAsyncWithIO( + `${structCmd} queryStructField ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /SingerId: 6/); + }); // query_with_nested_struct_param - it( - `should query an example table with a nested STRUCT param`, - async () => { - const results = await tools.runAsyncWithIO( - `${structCmd} queryNestedStructField ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual( - output, - /SingerId: 6, SongName: Imagination\nSingerId: 9, SongName: Imagination/ - ); - } - ); + it(`should query an example table with a nested STRUCT param`, async () => { + const results = await tools.runAsyncWithIO( + `${structCmd} queryNestedStructField ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual( + output, + /SingerId: 6, SongName: Imagination\nSingerId: 9, SongName: Imagination/ + ); + }); // dml_standard_insert - it( - `should insert rows into an example table using a DML statement`, - async () => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} insertUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /Successfully inserted 1 record into the Singers table/); - } - ); + it(`should insert rows into an example table using a DML statement`, async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} insertUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual( + output, + /Successfully inserted 1 record into the Singers table/ + ); + }); // dml_standard_update - it( - `should update a row in an example table using a DML statement`, - async () => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} updateUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /Successfully updated 1 record/); - } - ); + it(`should update a row in an example table using a DML statement`, async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} updateUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /Successfully updated 1 record/); + }); // dml_standard_delete - it( - `should delete a row from an example table using a DML statement`, - async () => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} deleteUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /Successfully deleted 1 record\./); - } - ); + it(`should delete a row from an example table using a DML statement`, async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} deleteUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /Successfully deleted 1 record\./); + }); // dml_standard_update_with_timestamp - it( - `should update the timestamp of multiple records in an example table using a DML statement`, - async () => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} updateUsingDmlWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /Successfully updated 2 records/); - } - ); + it(`should update the timestamp of multiple records in an example table using a DML statement`, async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} updateUsingDmlWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /Successfully updated 2 records/); + }); // dml_write_then_read - it( - `should insert a record in an example table using a DML statement and then query the record`, - async () => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} writeAndReadUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /Timothy Campbell/); - } - ); + it(`should insert a record in an example table using a DML statement and then query the record`, async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} writeAndReadUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /Timothy Campbell/); + }); // dml_structs - it( - `should update a record in an example table using a DML statement along with a struct value`, - async () => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} updateUsingDmlWithStruct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /Successfully updated 1 record/); - } - ); + it(`should update a record in an example table using a DML statement along with a struct value`, async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} updateUsingDmlWithStruct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /Successfully updated 1 record/); + }); // dml_getting_started_insert - it( - `should insert multiple records into an example table using a DML statement`, - async () => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} writeUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /4 records inserted/); - } - ); + it(`should insert multiple records into an example table using a DML statement`, async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} writeUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /4 records inserted/); + }); // dml_getting_started_update - it( - `should transfer value from one record to another using DML statements within a transaction`, - async () => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} writeWithTransactionUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual( - output, - /Successfully executed read-write transaction using DML to transfer 200000 from Album 1 to Album 2/ - ); - } - ); + it(`should transfer value from one record to another using DML statements within a transaction`, async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} writeWithTransactionUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual( + output, + /Successfully executed read-write transaction using DML to transfer 200000 from Album 1 to Album 2/ + ); + }); // dml_partitioned_update - it( - `should update multiple records using a partitioned DML statement`, - async () => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} updateUsingPartitionedDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /Successfully updated 3 records/); - } - ); + it(`should update multiple records using a partitioned DML statement`, async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} updateUsingPartitionedDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /Successfully updated 3 records/); + }); // dml_partitioned_delete - it( - `should delete multiple records using a partitioned DML statement`, - async () => { - const results = await tools.runAsyncWithIO( - `${dmlCmd} deleteUsingPartitionedDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.deepEqual(output, /Successfully deleted 5 records/); - } - ); - + it(`should delete multiple records using a partitioned DML statement`, async () => { + const results = await tools.runAsyncWithIO( + `${dmlCmd} deleteUsingPartitionedDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.deepStrictEqual(output, /Successfully deleted 5 records/); + }); }); function apiRequest(reqOpts) { From 71ffed5b2bdc6b6cd5cfc38d79a2cb99e2a30717 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Mon, 29 Oct 2018 16:46:36 +0530 Subject: [PATCH 11/18] Refactor the mocha testcases --- samples/system-test/quickstart.test.js | 18 ++++++++++++++++-- samples/system-test/spanner.test.js | 25 +++++++++++++------------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index 4f7a52f0f..18e00c7d4 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -21,9 +21,22 @@ const assert = require('assert'); const tools = require(`@google-cloud/nodejs-repo-tools`); describe('QuickStart', () => { + let output; + const write = process.stdout.write; + before(() => tools.stubConsole); - afterEach(() => tools.restoreConsole); + beforeEach(() => { + output = ''; + process.stdout.write = function(str) { + output = str; + }; + }); + + afterEach(() => { + tools.restoreConsole; + process.stdout.write = write; + }); it(`should query a table`, done => { const databaseMock = { @@ -33,7 +46,8 @@ describe('QuickStart', () => { }); setTimeout(() => { try { - //assert.deepStrictEqual(console.log.getCall(0).args, [`test`]); + const regex = new RegExp(`test`); + assert.strictEqual(regex.test(output), true); done(); } catch (err) { done(err); diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 25475dcaa..13bb11a56 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -42,9 +42,8 @@ const spanner = new Spanner({ }); describe('Spanner', () => { - before(() => tools.checkCredentials); - before(async () => { + tools.checkCredentials(); const instance = spanner.instance(INSTANCE_ID); const database = instance.database(DATABASE_ID); @@ -68,10 +67,12 @@ describe('Spanner', () => { }, }); - await operation.promise(); - }); + try { + await operation.promise(); + } catch (err) { + // Ignore error + } - before(async () => { const [instances] = await spanner.getInstances({ filter: 'labels.gcloud-sample-tests:true', }); @@ -95,7 +96,7 @@ describe('Spanner', () => { }); }); - afterEach(async () => { + after(async () => { const instance = spanner.instance(INSTANCE_ID); const database = instance.database(DATABASE_ID); @@ -119,14 +120,14 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, - new RegExp(`Waiting for operation on ${DATABASE_ID} to complete...`) + let regex = new RegExp( + `Waiting for operation on ${DATABASE_ID} to complete...` ); - assert.deepStrictEqual( - output, - new RegExp(`Created database ${DATABASE_ID} on instance ${INSTANCE_ID}.`) + assert.strictEqual(regex.test(output), true); + regex = new RegExp( + `Created database ${DATABASE_ID} on instance ${INSTANCE_ID}.` ); + assert.strictEqual(regex.test(output), true); }); // insert_data From d032df1e50b87731879ee53c3db4fea21182013e Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Mon, 29 Oct 2018 16:55:41 +0530 Subject: [PATCH 12/18] refactor unit test cases --- samples/system-test/quickstart.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index 18e00c7d4..f332b09f2 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -28,7 +28,7 @@ describe('QuickStart', () => { beforeEach(() => { output = ''; - process.stdout.write = function(str) { + process.stdout.write = str => { output = str; }; }); From 192bf953ec167a2c3075bbd13b1b649f666eaaa6 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Mon, 29 Oct 2018 19:32:25 +0530 Subject: [PATCH 13/18] Refactor unit test cases --- samples/system-test/spanner.test.js | 215 ++++++++++++++-------------- 1 file changed, 109 insertions(+), 106 deletions(-) diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 13bb11a56..36f5030e3 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -137,7 +137,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Inserted data\./); + const regex = new RegExp(/Inserted data\./); + assert.strictEqual(regex.test(output), true); }); // query_data @@ -147,10 +148,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, - /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/ - ); + const regex = new RegExp(/SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); + assert.strictEqual(regex.test(output), true); }); // read_data @@ -160,10 +159,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, - /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/ - ); + const regex = new RegExp(/SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); + assert.strictEqual(regex.test(output), true); }); // add_column @@ -173,8 +170,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Waiting for operation to complete\.\.\./); - assert.deepStrictEqual(output, /Added the MarketingBudget column\./); + let regex = new RegExp(/Waiting for operation to complete\.\.\./); + assert.strictEqual(regex.test(output), true); + regex = new RegExp(/Added the MarketingBudget column\./); + assert.strictEqual(regex.test(output), true); }); // update_data @@ -184,7 +183,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Updated data\./); + const regex = new RegExp(/Updated data\./); + assert.strictEqual(regex.test(output), true); }); // read_stale_data @@ -198,14 +198,14 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, + let regex = new RegExp( /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget: 100000/ ); - assert.deepStrictEqual( - output, + assert.strictEqual(regex.test(output), true); + regex = new RegExp( /SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold your Peace, MarketingBudget: 500000/ ); + assert.strictEqual(regex.test(output), true); }); }); @@ -216,14 +216,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, - /SingerId: 1, AlbumId: 1, MarketingBudget: 100000/ - ); - assert.deepStrictEqual( - output, - /SingerId: 2, AlbumId: 2, MarketingBudget: 500000/ - ); + let regex = new RegExp(/SingerId: 1, AlbumId: 1, MarketingBudget: 100000/); + assert.strictEqual(regex.test(output), true); + regex = new RegExp(/SingerId: 2, AlbumId: 2, MarketingBudget: 500000/); + assert.strictEqual(regex.test(output), true); }); // create_index @@ -233,8 +229,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Waiting for operation to complete\.\.\./); - assert.deepStrictEqual(output, /Added the AlbumsByAlbumTitle index\./); + let regex = new RegExp(/Waiting for operation to complete\.\.\./); + assert.strictEqual(regex.test(output), true); + regex = new RegExp(/Added the AlbumsByAlbumTitle index\./); + assert.strictEqual(regex.test(output), true); }); // create_storing_index @@ -244,8 +242,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Waiting for operation to complete\.\.\./); - assert.deepStrictEqual(output, /Added the AlbumsByAlbumTitle2 index\./); + let regex = new RegExp(/Waiting for operation to complete\.\.\./); + assert.strictEqual(regex.test(output), true); + regex = new RegExp(/Added the AlbumsByAlbumTitle2 index\./); + assert.strictEqual(regex.test(output), true); }); // query_data_with_index @@ -255,10 +255,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, + const regex = new RegExp( /AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/ ); + assert.strictEqual(regex.test(output), true); assert.deepStrictEqual( output.includes(`AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:`), false @@ -271,14 +271,12 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, + let regex = new RegExp( /AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:/ ); - assert.deepStrictEqual( - output, - /AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/ - ); + assert.strictEqual(regex.test(output), true); + regex = new RegExp(/AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/); + assert.strictEqual(regex.test(output), true); }); // read_data_with_index @@ -288,7 +286,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /AlbumId: 1, AlbumTitle: Total Junk/); + const regex = new RegExp(/AlbumId: 1, AlbumTitle: Total Junk/); + assert.strictEqual(regex.test(output), true); }); // read_data_with_storing_index @@ -298,7 +297,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /AlbumId: 1, AlbumTitle: Total Junk/); + const regex = new RegExp(/AlbumId: 1, AlbumTitle: Total Junk/); + assert.strictEqual(regex.test(output), true); }); // read_only_transaction @@ -308,14 +308,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, - /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/ - ); - assert.deepStrictEqual( - output, - /Successfully executed read-only transaction\./ - ); + let regex = new RegExp(/SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); + assert.strictEqual(regex.test(output), true); + regex = new RegExp(/Successfully executed read-only transaction\./); + assert.strictEqual(regex.test(output), true); }); // read_write_transaction @@ -325,32 +321,24 @@ describe('Spanner', () => { cwd ); let output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, - /The first album's marketing budget: 100000/ - ); - assert.deepStrictEqual( - output, - /The second album's marketing budget: 500000/ - ); - assert.deepStrictEqual( - output, + let regex = new RegExp(/The first album's marketing budget: 100000/); + assert.strictEqual(regex.test(output), true); + regex = new RegExp(/The second album's marketing budget: 500000/); + assert.strictEqual(regex.test(output), true); + regex = new RegExp( /Successfully executed read-write transaction to transfer 200000 from Album 2 to Album 1./ ); - + assert.strictEqual(regex.test(output), true); results = await tools.runAsyncWithIO( `${schemaCmd} queryNewColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); + output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, - /SingerId: 1, AlbumId: 1, MarketingBudget: 300000/ - ); - assert.deepStrictEqual( - output, - /SingerId: 2, AlbumId: 2, MarketingBudget: 300000/ - ); + regex = new RegExp(/SingerId: 1, AlbumId: 1, MarketingBudget: 300000/); + assert.strictEqual(regex.test(output), true); + regex = new RegExp(/SingerId: 2, AlbumId: 2, MarketingBudget: 300000/); + assert.strictEqual(regex.test(output), true); }); // create_query_partitions @@ -367,10 +355,8 @@ describe('Spanner', () => { const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, - /Successfully created \d query partitions\./ - ); + const regex = new RegExp(/Successfully created \d query partitions\./); + assert.strictEqual(regex.test(output), true); await transaction.close(); }); @@ -393,10 +379,10 @@ describe('Spanner', () => { const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, + const regex = new RegExp( /Successfully received \d from executed partition\./ ); + assert.strictEqual(regex.test(output), true); await transaction.close(); }); @@ -408,11 +394,12 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Waiting for operation to complete\.\.\./); - assert.deepStrictEqual( - output, + let regex = new RegExp(/Waiting for operation to complete\.\.\./); + assert.strictEqual(regex.test(output), true); + regex = new RegExp( /Added LastUpdateTime as a commit timestamp column in Albums table\./ ); + assert.strictEqual(regex.test(output), true); }); // update_data_with_timestamp_column @@ -422,7 +409,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Updated data\./); + const regex = new RegExp(/Updated data\./); + assert.strictEqual(regex.test(output), true); }); // query_data_with_timestamp_column @@ -432,14 +420,14 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, + let regex = new RegExp( /SingerId: 1, AlbumId: 1, MarketingBudget: 1000000, LastUpdateTime:/ ); - assert.deepStrictEqual( - output, + assert.strictEqual(regex.test(output), true); + regex = new RegExp( /SingerId: 2, AlbumId: 2, MarketingBudget: 750000, LastUpdateTime:/ ); + assert.strictEqual(regex.test(output), true); }); // create_table_with_timestamp_column @@ -449,14 +437,14 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, - new RegExp(`Waiting for operation on ${DATABASE_ID} to complete...`) + let regex = new RegExp( + `Waiting for operation on ${DATABASE_ID} to complete...` ); - assert.deepStrictEqual( - output, - new RegExp(`Created table Performances in database ${DATABASE_ID}.`) + assert.strictEqual(regex.test(output), true); + regex = new RegExp( + `Created table Performances in database ${DATABASE_ID}.` ); + assert.strictEqual(regex.test(output), true); }); // insert_data_with_timestamp @@ -466,7 +454,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Inserted data\./); + const regex = new RegExp(/Inserted data\./); + assert.strictEqual(regex.test(output), true); }); // query_new_table_with_timestamp @@ -476,8 +465,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /SingerId: 1, VenueId: 4, EventDate:/); - assert.deepStrictEqual(output, /Revenue: 15000, LastUpdateTime:/); + let regex = new RegExp(/SingerId: 1, VenueId: 4, EventDate:/); + assert.strictEqual(regex.test(output), true); + regex = new RegExp(/Revenue: 15000, LastUpdateTime:/); + assert.strictEqual(regex.test(output), true); }); // write_data_for_struct_queries @@ -487,7 +478,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Inserted data\./); + const regex = new RegExp(/Inserted data\./); + assert.strictEqual(regex.test(output), true); }); // query_with_struct_param @@ -497,7 +489,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /SingerId: 6/); + const regex = new RegExp(/SingerId: 6/); + assert.strictEqual(regex.test(output), true); }); // query_with_array_of_struct_param @@ -507,7 +500,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /SingerId: 6\nSingerId: 7/); + const regex = new RegExp(/SingerId: 6\nSingerId: 7/); + assert.strictEqual(regex.test(output), true); }); // query_with_struct_field_param @@ -517,7 +511,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /SingerId: 6/); + const regex = new RegExp(/SingerId: 6/); + assert.strictEqual(regex.test(output), true); }); // query_with_nested_struct_param @@ -527,10 +522,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, + const regex = new RegExp( /SingerId: 6, SongName: Imagination\nSingerId: 9, SongName: Imagination/ ); + assert.strictEqual(regex.test(output), true); }); // dml_standard_insert @@ -540,10 +535,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, + const regex = new RegExp( /Successfully inserted 1 record into the Singers table/ ); + assert.strictEqual(regex.test(output), true); }); // dml_standard_update @@ -553,7 +548,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Successfully updated 1 record/); + const regex = new RegExp(/Successfully updated 1 record/); + assert.strictEqual(regex.test(output), true); }); // dml_standard_delete @@ -563,7 +559,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Successfully deleted 1 record\./); + const regex = new RegExp(/Successfully deleted 1 record\./); + assert.strictEqual(regex.test(output), true); }); // dml_standard_update_with_timestamp @@ -573,7 +570,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Successfully updated 2 records/); + const regex = new RegExp(/Successfully updated 2 records/); + assert.strictEqual(regex.test(output), true); }); // dml_write_then_read @@ -583,7 +581,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Timothy Campbell/); + const regex = new RegExp(/Timothy Campbell/); + assert.strictEqual(regex.test(output), true); }); // dml_structs @@ -593,7 +592,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Successfully updated 1 record/); + const regex = new RegExp(/Successfully updated 1 record/); + assert.strictEqual(regex.test(output), true); }); // dml_getting_started_insert @@ -603,7 +603,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /4 records inserted/); + const regex = new RegExp(/4 records inserted/); + assert.strictEqual(regex.test(output), true); }); // dml_getting_started_update @@ -613,10 +614,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual( - output, + const regex = new RegExp( /Successfully executed read-write transaction using DML to transfer 200000 from Album 1 to Album 2/ ); + assert.strictEqual(regex.test(output), true); }); // dml_partitioned_update @@ -626,7 +627,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Successfully updated 3 records/); + const regex = new RegExp(/Successfully updated 3 records/); + assert.strictEqual(regex.test(output), true); }); // dml_partitioned_delete @@ -636,7 +638,8 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - assert.deepStrictEqual(output, /Successfully deleted 5 records/); + const regex = new RegExp(/Successfully deleted 5 records/); + assert.strictEqual(regex.test(output), true); }); }); From 1ffb42466d5dda9f529a4cca9761e2b54ff92725 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Tue, 30 Oct 2018 16:07:12 +0530 Subject: [PATCH 14/18] Added new line --- samples/system-test/quickstart.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index f332b09f2..e896c5258 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -72,6 +72,7 @@ describe('QuickStart', () => { assert.deepStrictEqual(spannerMock.instance.getCall(0).args, [ `my-instance`, ]); + assert.deepStrictEqual(instanceMock.database.getCall(0).args, [ `my-database`, ]); From bfd13f9896ec284b20c919530c724b8c71744e10 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Wed, 31 Oct 2018 19:23:20 +0530 Subject: [PATCH 15/18] Refactor the code --- samples/system-test/quickstart.test.js | 42 +-- samples/system-test/spanner.test.js | 354 ++++++++++++++++--------- 2 files changed, 238 insertions(+), 158 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index e896c5258..a000e5e9c 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -15,45 +15,28 @@ 'use strict'; -const proxyquire = require(`proxyquire`); +const proxyquire = require(`proxyquire`).noPreserveCache(); const sinon = require(`sinon`); -const assert = require('assert'); +const assert = require(`assert`); const tools = require(`@google-cloud/nodejs-repo-tools`); describe('QuickStart', () => { - let output; - const write = process.stdout.write; - before(() => tools.stubConsole); + after(() => tools.restoreConsole); - beforeEach(() => { - output = ''; - process.stdout.write = str => { - output = str; - }; - }); - - afterEach(() => { - tools.restoreConsole; - process.stdout.write = write; - }); - - it(`should query a table`, done => { + it(`should query a table`, async () => { const databaseMock = { - run: _query => { + run: async _query => { assert.deepStrictEqual(_query, { sql: `SELECT 1`, }); - setTimeout(() => { - try { - const regex = new RegExp(`test`); - assert.strictEqual(regex.test(output), true); - done(); - } catch (err) { - done(err); - } - }, 200); - return Promise.resolve([['test']]); + + await new Promise(r => setTimeout(r, 200)); + try { + assert.deepStrictEqual(console.log.getCall(0).args, [`test`]); + } catch (err) {} + + return await Promise.resolve([['test']]); }, }; const instanceMock = { @@ -72,7 +55,6 @@ describe('QuickStart', () => { assert.deepStrictEqual(spannerMock.instance.getCall(0).args, [ `my-instance`, ]); - assert.deepStrictEqual(instanceMock.database.getCall(0).args, [ `my-database`, ]); diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 36f5030e3..c9b58e050 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -120,14 +120,18 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp( - `Waiting for operation on ${DATABASE_ID} to complete...` + assert.strictEqual( + new RegExp(`Waiting for operation on ${DATABASE_ID} to complete...`).test( + output + ), + true ); - assert.strictEqual(regex.test(output), true); - regex = new RegExp( - `Created database ${DATABASE_ID} on instance ${INSTANCE_ID}.` + assert.strictEqual( + new RegExp( + `Created database ${DATABASE_ID} on instance ${INSTANCE_ID}.` + ).test(output), + true ); - assert.strictEqual(regex.test(output), true); }); // insert_data @@ -137,8 +141,7 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Inserted data\./); - assert.strictEqual(regex.test(output), true); + assert.strictEqual(new RegExp(/Inserted data\./).test(output), true); }); // query_data @@ -148,8 +151,12 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/).test( + output + ), + true + ); }); // read_data @@ -159,8 +166,12 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/).test( + output + ), + true + ); }); // add_column @@ -170,10 +181,14 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp(/Waiting for operation to complete\.\.\./); - assert.strictEqual(regex.test(output), true); - regex = new RegExp(/Added the MarketingBudget column\./); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/Waiting for operation to complete\.\.\./).test(output), + true + ); + assert.strictEqual( + new RegExp(/Added the MarketingBudget column\./).test(output), + true + ); }); // update_data @@ -183,13 +198,11 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Updated data\./); - assert.strictEqual(regex.test(output), true); + assert.strictEqual(new RegExp(/Updated data\./).test(output), true); }); // read_stale_data it(`should read stale data from an example table`, () => { - //t.plan(2); // read-stale-data reads data that is exactly 15 seconds old. So, make sure // 15 seconds have elapsed since the update_data test. return new Promise(resolve => setTimeout(resolve, 16000)).then(async () => { @@ -198,14 +211,18 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp( - /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget: 100000/ + assert.strictEqual( + new RegExp( + /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget: 100000/ + ).test(output), + true ); - assert.strictEqual(regex.test(output), true); - regex = new RegExp( - /SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold your Peace, MarketingBudget: 500000/ + assert.strictEqual( + new RegExp( + /SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold your Peace, MarketingBudget: 500000/ + ).test(output), + true ); - assert.strictEqual(regex.test(output), true); }); }); @@ -216,10 +233,18 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp(/SingerId: 1, AlbumId: 1, MarketingBudget: 100000/); - assert.strictEqual(regex.test(output), true); - regex = new RegExp(/SingerId: 2, AlbumId: 2, MarketingBudget: 500000/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/SingerId: 1, AlbumId: 1, MarketingBudget: 100000/).test( + output + ), + true + ); + assert.strictEqual( + new RegExp(/SingerId: 2, AlbumId: 2, MarketingBudget: 500000/).test( + output + ), + true + ); }); // create_index @@ -229,10 +254,14 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp(/Waiting for operation to complete\.\.\./); - assert.strictEqual(regex.test(output), true); - regex = new RegExp(/Added the AlbumsByAlbumTitle index\./); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/Waiting for operation to complete\.\.\./).test(output), + true + ); + assert.strictEqual( + new RegExp(/Added the AlbumsByAlbumTitle index\./).test(output), + true + ); }); // create_storing_index @@ -242,10 +271,14 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp(/Waiting for operation to complete\.\.\./); - assert.strictEqual(regex.test(output), true); - regex = new RegExp(/Added the AlbumsByAlbumTitle2 index\./); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/Waiting for operation to complete\.\.\./).test(output), + true + ); + assert.strictEqual( + new RegExp(/Added the AlbumsByAlbumTitle2 index\./).test(output), + true + ); }); // query_data_with_index @@ -255,10 +288,12 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp( - /AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/ + assert.strictEqual( + new RegExp(/AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/).test( + output + ), + true ); - assert.strictEqual(regex.test(output), true); assert.deepStrictEqual( output.includes(`AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:`), false @@ -271,12 +306,18 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp( - /AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:/ + assert.strictEqual( + new RegExp(/AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:/).test( + output + ), + true + ); + assert.strictEqual( + new RegExp(/AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/).test( + output + ), + true ); - assert.strictEqual(regex.test(output), true); - regex = new RegExp(/AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:/); - assert.strictEqual(regex.test(output), true); }); // read_data_with_index @@ -286,8 +327,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/AlbumId: 1, AlbumTitle: Total Junk/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/AlbumId: 1, AlbumTitle: Total Junk/).test(output), + true + ); }); // read_data_with_storing_index @@ -297,8 +340,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/AlbumId: 1, AlbumTitle: Total Junk/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/AlbumId: 1, AlbumTitle: Total Junk/).test(output), + true + ); }); // read_only_transaction @@ -308,10 +353,16 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp(/SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/); - assert.strictEqual(regex.test(output), true); - regex = new RegExp(/Successfully executed read-only transaction\./); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk/).test( + output + ), + true + ); + assert.strictEqual( + new RegExp(/Successfully executed read-only transaction\./).test(output), + true + ); }); // read_write_transaction @@ -321,24 +372,38 @@ describe('Spanner', () => { cwd ); let output = results.stdout + results.stderr; - let regex = new RegExp(/The first album's marketing budget: 100000/); - assert.strictEqual(regex.test(output), true); - regex = new RegExp(/The second album's marketing budget: 500000/); - assert.strictEqual(regex.test(output), true); - regex = new RegExp( - /Successfully executed read-write transaction to transfer 200000 from Album 2 to Album 1./ - ); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/The first album's marketing budget: 100000/).test(output), + true + ); + assert.strictEqual( + new RegExp(/The second album's marketing budget: 500000/).test(output), + true + ); + assert.strictEqual( + new RegExp( + /Successfully executed read-write transaction to transfer 200000 from Album 2 to Album 1./ + ).test(output), + true + ); results = await tools.runAsyncWithIO( `${schemaCmd} queryNewColumn ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, cwd ); output = results.stdout + results.stderr; - regex = new RegExp(/SingerId: 1, AlbumId: 1, MarketingBudget: 300000/); - assert.strictEqual(regex.test(output), true); - regex = new RegExp(/SingerId: 2, AlbumId: 2, MarketingBudget: 300000/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/SingerId: 1, AlbumId: 1, MarketingBudget: 300000/).test( + output + ), + true + ); + assert.strictEqual( + new RegExp(/SingerId: 2, AlbumId: 2, MarketingBudget: 300000/).test( + output + ), + true + ); }); // create_query_partitions @@ -355,8 +420,10 @@ describe('Spanner', () => { const output = results.stdout + results.stderr; - const regex = new RegExp(/Successfully created \d query partitions\./); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/Successfully created \d query partitions\./).test(output), + true + ); await transaction.close(); }); @@ -379,10 +446,12 @@ describe('Spanner', () => { const output = results.stdout + results.stderr; - const regex = new RegExp( - /Successfully received \d from executed partition\./ + assert.strictEqual( + new RegExp(/Successfully received \d from executed partition\./).test( + output + ), + true ); - assert.strictEqual(regex.test(output), true); await transaction.close(); }); @@ -394,12 +463,16 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp(/Waiting for operation to complete\.\.\./); - assert.strictEqual(regex.test(output), true); - regex = new RegExp( - /Added LastUpdateTime as a commit timestamp column in Albums table\./ + assert.strictEqual( + new RegExp(/Waiting for operation to complete\.\.\./).test(output), + true + ); + assert.strictEqual( + new RegExp( + /Added LastUpdateTime as a commit timestamp column in Albums table\./ + ).test(output), + true ); - assert.strictEqual(regex.test(output), true); }); // update_data_with_timestamp_column @@ -409,8 +482,7 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Updated data\./); - assert.strictEqual(regex.test(output), true); + assert.strictEqual(new RegExp(/Updated data\./).test(output), true); }); // query_data_with_timestamp_column @@ -420,14 +492,18 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp( - /SingerId: 1, AlbumId: 1, MarketingBudget: 1000000, LastUpdateTime:/ + assert.strictEqual( + new RegExp( + /SingerId: 1, AlbumId: 1, MarketingBudget: 1000000, LastUpdateTime:/ + ).test(output), + true ); - assert.strictEqual(regex.test(output), true); - regex = new RegExp( - /SingerId: 2, AlbumId: 2, MarketingBudget: 750000, LastUpdateTime:/ + assert.strictEqual( + new RegExp( + /SingerId: 2, AlbumId: 2, MarketingBudget: 750000, LastUpdateTime:/ + ).test(output), + true ); - assert.strictEqual(regex.test(output), true); }); // create_table_with_timestamp_column @@ -437,14 +513,18 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp( - `Waiting for operation on ${DATABASE_ID} to complete...` + assert.strictEqual( + new RegExp(`Waiting for operation on ${DATABASE_ID} to complete...`).test( + output + ), + true ); - assert.strictEqual(regex.test(output), true); - regex = new RegExp( - `Created table Performances in database ${DATABASE_ID}.` + assert.strictEqual( + new RegExp(`Created table Performances in database ${DATABASE_ID}.`).test( + output + ), + true ); - assert.strictEqual(regex.test(output), true); }); // insert_data_with_timestamp @@ -454,8 +534,7 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Inserted data\./); - assert.strictEqual(regex.test(output), true); + assert.strictEqual(new RegExp(/Inserted data\./).test(output), true); }); // query_new_table_with_timestamp @@ -465,10 +544,14 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - let regex = new RegExp(/SingerId: 1, VenueId: 4, EventDate:/); - assert.strictEqual(regex.test(output), true); - regex = new RegExp(/Revenue: 15000, LastUpdateTime:/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/SingerId: 1, VenueId: 4, EventDate:/).test(output), + true + ); + assert.strictEqual( + new RegExp(/Revenue: 15000, LastUpdateTime:/).test(output), + true + ); }); // write_data_for_struct_queries @@ -478,8 +561,7 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Inserted data\./); - assert.strictEqual(regex.test(output), true); + assert.strictEqual(new RegExp(/Inserted data\./).test(output), true); }); // query_with_struct_param @@ -489,8 +571,7 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/SingerId: 6/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual(new RegExp(/SingerId: 6/).test(output), true); }); // query_with_array_of_struct_param @@ -500,8 +581,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/SingerId: 6\nSingerId: 7/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/SingerId: 6\nSingerId: 7/).test(output), + true + ); }); // query_with_struct_field_param @@ -511,8 +594,7 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/SingerId: 6/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual(new RegExp(/SingerId: 6/).test(output), true); }); // query_with_nested_struct_param @@ -522,10 +604,12 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp( - /SingerId: 6, SongName: Imagination\nSingerId: 9, SongName: Imagination/ + assert.strictEqual( + new RegExp( + /SingerId: 6, SongName: Imagination\nSingerId: 9, SongName: Imagination/ + ).test(output), + true ); - assert.strictEqual(regex.test(output), true); }); // dml_standard_insert @@ -535,10 +619,12 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp( - /Successfully inserted 1 record into the Singers table/ + assert.strictEqual( + new RegExp(/Successfully inserted 1 record into the Singers table/).test( + output + ), + true ); - assert.strictEqual(regex.test(output), true); }); // dml_standard_update @@ -548,8 +634,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Successfully updated 1 record/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/Successfully updated 1 record/).test(output), + true + ); }); // dml_standard_delete @@ -559,8 +647,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Successfully deleted 1 record\./); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/Successfully deleted 1 record\./).test(output), + true + ); }); // dml_standard_update_with_timestamp @@ -570,8 +660,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Successfully updated 2 records/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/Successfully updated 2 records/).test(output), + true + ); }); // dml_write_then_read @@ -581,8 +673,7 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Timothy Campbell/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual(new RegExp(/Timothy Campbell/).test(output), true); }); // dml_structs @@ -592,8 +683,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Successfully updated 1 record/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/Successfully updated 1 record/).test(output), + true + ); }); // dml_getting_started_insert @@ -603,8 +696,7 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/4 records inserted/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual(new RegExp(/4 records inserted/).test(output), true); }); // dml_getting_started_update @@ -614,10 +706,12 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp( - /Successfully executed read-write transaction using DML to transfer 200000 from Album 1 to Album 2/ + assert.strictEqual( + new RegExp( + /Successfully executed read-write transaction using DML to transfer 200000 from Album 1 to Album 2/ + ).test(output), + true ); - assert.strictEqual(regex.test(output), true); }); // dml_partitioned_update @@ -627,8 +721,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Successfully updated 3 records/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/Successfully updated 3 records/).test(output), + true + ); }); // dml_partitioned_delete @@ -638,8 +734,10 @@ describe('Spanner', () => { cwd ); const output = results.stdout + results.stderr; - const regex = new RegExp(/Successfully deleted 5 records/); - assert.strictEqual(regex.test(output), true); + assert.strictEqual( + new RegExp(/Successfully deleted 5 records/).test(output), + true + ); }); }); From 5a623a3a4b4214274a21ca55f76db1c6d376ee9c Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Thu, 1 Nov 2018 19:15:22 +0530 Subject: [PATCH 16/18] Refactor the code --- samples/system-test/quickstart.test.js | 7 +-- samples/system-test/spanner.test.js | 77 +++++++++++++------------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index a000e5e9c..333bad1ea 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -32,11 +32,8 @@ describe('QuickStart', () => { }); await new Promise(r => setTimeout(r, 200)); - try { - assert.deepStrictEqual(console.log.getCall(0).args, [`test`]); - } catch (err) {} - - return await Promise.resolve([['test']]); + assert.deepStrictEqual(console.log.getCall(0).args, [`test`]); + return [['test']]; }, }; const instanceMock = { diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index c9b58e050..3f46ce0ff 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -77,23 +77,27 @@ describe('Spanner', () => { filter: 'labels.gcloud-sample-tests:true', }); - instances.forEach(async instance => { - const {operations} = await getOperations(instance.metadata.name); - - operations - .filter(operation => { - return operation.metadata['@type'].includes('CreateInstance'); - }) - .filter(operation => { - const yesterday = new Date(); - yesterday.setHours(-24); - - const instanceCreated = new Date(operation.metadata.startTime); - - return instanceCreated < yesterday; - }) - .forEach(async () => await instance.delete()); - }); + await Promise.all( + instances.map(async instance => { + const {operations} = await getOperations(instance.metadata.name); + + await Promise.all( + operations + .filter(operation => { + return operation.metadata['@type'].includes('CreateInstance'); + }) + .filter(operation => { + const yesterday = new Date(); + yesterday.setHours(-24); + + const instanceCreated = new Date(operation.metadata.startTime); + + return instanceCreated < yesterday; + }) + .map(instance.delete) + ); + }) + ); }); after(async () => { @@ -202,28 +206,27 @@ describe('Spanner', () => { }); // read_stale_data - it(`should read stale data from an example table`, () => { + it(`should read stale data from an example table`, async () => { // read-stale-data reads data that is exactly 15 seconds old. So, make sure // 15 seconds have elapsed since the update_data test. - return new Promise(resolve => setTimeout(resolve, 16000)).then(async () => { - const results = await tools.runAsyncWithIO( - `${crudCmd} read-stale ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, - cwd - ); - const output = results.stdout + results.stderr; - assert.strictEqual( - new RegExp( - /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget: 100000/ - ).test(output), - true - ); - assert.strictEqual( - new RegExp( - /SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold your Peace, MarketingBudget: 500000/ - ).test(output), - true - ); - }); + await new Promise(r => setTimeout(r, 16000)); + const results = await tools.runAsyncWithIO( + `${crudCmd} read-stale ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`, + cwd + ); + const output = results.stdout + results.stderr; + assert.strictEqual( + new RegExp( + /SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget: 100000/ + ).test(output), + true + ); + assert.strictEqual( + new RegExp( + /SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold your Peace, MarketingBudget: 500000/ + ).test(output), + true + ); }); // query_data_with_new_column From 4984fbe911aa29f20567442859fd39ebcde929f0 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Mon, 12 Nov 2018 15:11:26 +0530 Subject: [PATCH 17/18] Removed try/catch --- samples/system-test/quickstart.test.js | 4 +-- samples/system-test/spanner.test.js | 39 ++++---------------------- 2 files changed, 8 insertions(+), 35 deletions(-) diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index 333bad1ea..e4415f7cb 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -21,8 +21,8 @@ const assert = require(`assert`); const tools = require(`@google-cloud/nodejs-repo-tools`); describe('QuickStart', () => { - before(() => tools.stubConsole); - after(() => tools.restoreConsole); + before(tools.stubConsole); + after(tools.restoreConsole); it(`should query a table`, async () => { const databaseMock = { diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 3f46ce0ff..2f0140f7d 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -47,17 +47,8 @@ describe('Spanner', () => { const instance = spanner.instance(INSTANCE_ID); const database = instance.database(DATABASE_ID); - try { - await instance.delete(); - } catch (err) { - // Ignore error - } - - try { - await database.delete(); - } catch (err) { - // Ignore error - } + await instance.delete(); + await database.delete(); const [, operation] = await instance.create({ config: 'regional-us-central1', @@ -67,11 +58,7 @@ describe('Spanner', () => { }, }); - try { - await operation.promise(); - } catch (err) { - // Ignore error - } + await operation.promise(); const [instances] = await spanner.getInstances({ filter: 'labels.gcloud-sample-tests:true', @@ -104,17 +91,8 @@ describe('Spanner', () => { const instance = spanner.instance(INSTANCE_ID); const database = instance.database(DATABASE_ID); - try { - await database.delete(); - } catch (err) { - // Ignore error - } - - try { - await instance.delete(); - } catch (err) { - // Ignore error - } + await database.delete(); + await instance.delete(); }); // create_database @@ -755,12 +733,7 @@ function apiRequest(reqOpts) { return; } - try { - resolve(JSON.parse(response.body)); - } catch (e) { - reject(e); - return; - } + resolve(JSON.parse(response.body)); }); }) .catch(reject); From 5f21f307450747cb4748d516f71753c4ee5bd76f Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Tue, 13 Nov 2018 16:27:49 +0530 Subject: [PATCH 18/18] Try/catch added while deleting instance and database in before hook --- samples/system-test/spanner.test.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 2f0140f7d..6927838b2 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -46,9 +46,16 @@ describe('Spanner', () => { tools.checkCredentials(); const instance = spanner.instance(INSTANCE_ID); const database = instance.database(DATABASE_ID); - - await instance.delete(); - await database.delete(); + try { + await instance.delete(); + } catch (err) { + // Ignore error + } + try { + await database.delete(); + } catch (err) { + // Ignore error + } const [, operation] = await instance.create({ config: 'regional-us-central1',