Skip to content

Commit

Permalink
refactor: use execSync for tests
Browse files Browse the repository at this point in the history
refactor: use execSync for tests

#225 automerged by dpebot
  • Loading branch information
JustinBeckwith authored and yoshi-automation committed Apr 4, 2019
1 parent a4a6cac commit 9dcebcb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 54 deletions.
1 change: 0 additions & 1 deletion cloud-language/snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
},
"devDependencies": {
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^6.0.0",
"uuid": "^3.2.1"
}
Expand Down
28 changes: 11 additions & 17 deletions cloud-language/snippets/test/analyze.v1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ const fs = require('fs');
const path = require('path');
const {Storage} = require('@google-cloud/storage');
const {assert} = require('chai');
const execa = require('execa');
const {execSync} = require('child_process');
const uuid = require('uuid');

const exec = async cmd => (await execa.shell(cmd)).stdout;

describe('analyze.v1', () => {
const storage = new Storage();
const cmd = 'node analyze.v1.js';
Expand All @@ -49,33 +47,31 @@ describe('analyze.v1', () => {
});

it('should analyze sentiment in text', async () => {
const output = await exec(`${cmd} sentiment-text "${text}"`);
const output = execSync(`${cmd} sentiment-text "${text}"`);
assert.match(output, /Document sentiment:/);
assert.match(output, new RegExp(`Sentence: ${text}`));
assert.match(output, /Score: 0/);
assert.match(output, /Magnitude: 0/);
});

it('should analyze sentiment in a file', async () => {
const output = await exec(
`${cmd} sentiment-file ${bucketName} ${fileName}`
);
const output = execSync(`${cmd} sentiment-file ${bucketName} ${fileName}`);
assert(output, /Document sentiment:/);
assert.match(output, new RegExp(`Sentence: ${text}`));
assert.match(output, /Score: 0/);
assert.match(output, /Magnitude: 0/);
});

it('should analyze entities in text', async () => {
const output = await exec(`${cmd} entities-text "${text}"`);
const output = execSync(`${cmd} entities-text "${text}"`);
assert.match(output, /Obama/);
assert.match(output, /Type: PERSON/);
assert.match(output, /White House/);
assert.match(output, /Type: LOCATION/);
});

it('should analyze entities in a file', async () => {
const output = await exec(`${cmd} entities-file ${bucketName} ${fileName}`);
const output = execSync(`${cmd} entities-file ${bucketName} ${fileName}`);
assert.match(output, /Entities:/);
assert.match(output, /Obama/);
assert.match(output, /Type: PERSON/);
Expand All @@ -84,7 +80,7 @@ describe('analyze.v1', () => {
});

it('should analyze syntax in text', async () => {
const output = await exec(`${cmd} syntax-text "${text}"`);
const output = execSync(`${cmd} syntax-text "${text}"`);
assert.match(output, /Tokens:/);
assert.match(output, /NOUN:/);
assert.match(output, /President/);
Expand All @@ -94,7 +90,7 @@ describe('analyze.v1', () => {
});

it('should analyze syntax in a file', async () => {
const output = await exec(`${cmd} syntax-file ${bucketName} ${fileName}`);
const output = execSync(`${cmd} syntax-file ${bucketName} ${fileName}`);
assert.match(output, /NOUN:/);
assert.match(output, /President/);
assert.match(output, /Obama/);
Expand All @@ -103,7 +99,7 @@ describe('analyze.v1', () => {
});

it('should analyze entity sentiment in text', async () => {
const output = await exec(`${cmd} entity-sentiment-text "${text}"`);
const output = execSync(`${cmd} entity-sentiment-text "${text}"`);
assert.match(output, /Entities and sentiments:/);
assert.match(output, /Obama/);
assert.match(output, /PERSON/);
Expand All @@ -112,7 +108,7 @@ describe('analyze.v1', () => {
});

it('should analyze entity sentiment in a file', async () => {
const output = await exec(
const output = execSync(
`${cmd} entity-sentiment-file ${bucketName} ${fileName}`
);
assert.match(output, /Entities and sentiments:/);
Expand All @@ -123,15 +119,13 @@ describe('analyze.v1', () => {
});

it('should classify text in a file', async () => {
const output = await exec(
`${cmd} classify-file ${bucketName} ${fileName2}`
);
const output = execSync(`${cmd} classify-file ${bucketName} ${fileName2}`);
assert.match(output, /Name:/);
assert.match(output, /Computers & Electronics/);
});

it('should classify text in text', async () => {
const output = await exec(`${cmd} classify-text "${text2}"`);
const output = execSync(`${cmd} classify-text "${text2}"`);
assert.match(output, /Name:/);
assert.match(output, /Computers & Electronics/);
});
Expand Down
25 changes: 10 additions & 15 deletions cloud-language/snippets/test/analyze.v1beta2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ const fs = require('fs');
const path = require('path');
const {Storage} = require('@google-cloud/storage');
const {assert} = require('chai');
const execa = require('execa');
const {execSync} = require('child_process');
const uuid = require('uuid');

const exec = async cmd => (await execa.shell(cmd)).stdout;
const storage = new Storage();
const cmd = 'node analyze.v1beta2.js';
const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
Expand All @@ -49,41 +48,39 @@ describe('analyze.v1beta2', () => {
});

it(`should analyze sentiment in text`, async () => {
const output = await exec(`${cmd} sentiment-text "${text}"`);
const output = execSync(`${cmd} sentiment-text "${text}"`);
assert.match(output, /Document sentiment:/);
assert.match(output, new RegExp(`Sentence: ${text}`));
assert.match(output, /Score: 0/);
assert.match(output, /Magnitude: 0/);
});

it(`should analyze sentiment in a file`, async () => {
const output = await exec(
`${cmd} sentiment-file ${bucketName} ${fileName}`
);
const output = execSync(`${cmd} sentiment-file ${bucketName} ${fileName}`);
assert.match(output, /Document sentiment:/);
assert.match(output, new RegExp(`Sentence: ${text}`));
assert.match(output, /Score: 0/);
assert.match(output, /Magnitude: 0/);
});

it(`should analyze entities in text`, async () => {
const output = await exec(`${cmd} entities-text "${text}"`);
const output = execSync(`${cmd} entities-text "${text}"`);
assert.match(output, /Obama/);
assert.match(output, /Type: PERSON/);
assert.match(output, /White House/);
assert.match(output, /Type: LOCATION/);
});

it('should analyze entities in a file', async () => {
const output = await exec(`${cmd} entities-file ${bucketName} ${fileName}`);
const output = execSync(`${cmd} entities-file ${bucketName} ${fileName}`);
assert.match(output, /Entities:/);
assert.match(output, /Type: PERSON/);
assert.match(output, /White House/);
assert.match(output, /Type: LOCATION/);
});

it(`should analyze syntax in text`, async () => {
const output = await exec(`${cmd} syntax-text "${text}"`);
const output = execSync(`${cmd} syntax-text "${text}"`);
assert.match(output, /Parts of speech:/);
assert.match(output, /NOUN:/);
assert.match(output, /President/);
Expand All @@ -93,7 +90,7 @@ describe('analyze.v1beta2', () => {
});

it('should analyze syntax in a file', async () => {
const output = await exec(`${cmd} syntax-file ${bucketName} ${fileName}`);
const output = execSync(`${cmd} syntax-file ${bucketName} ${fileName}`);
assert.match(output, /NOUN:/);
assert.match(output, /President/);
assert.match(output, /Obama/);
Expand All @@ -102,23 +99,21 @@ describe('analyze.v1beta2', () => {
});

it('should analyze syntax in a 1.1 language (German)', async () => {
const output = await exec(`${cmd} syntax-text "${germanText}"`);
const output = execSync(`${cmd} syntax-text "${germanText}"`);
assert.match(output, /Parts of speech:/);
assert.match(output, /ADV: Willkommen/);
assert.match(output, /ADP: bei/);
assert.match(output, /NOUN: München/);
});

it('should classify text in a file', async () => {
const output = await exec(
`${cmd} classify-file ${bucketName} ${fileName2}`
);
const output = execSync(`${cmd} classify-file ${bucketName} ${fileName2}`);
assert.match(output, /Name:/);
assert.match(output, /Computers & Electronics/);
});

it('should classify text in text', async () => {
const output = await exec(`${cmd} classify-text "${text2}"`);
const output = execSync(`${cmd} classify-text "${text2}"`);
assert.match(output, /Name:/);
assert.match(output, /Computers & Electronics/);
});
Expand Down
36 changes: 17 additions & 19 deletions cloud-language/snippets/test/automlNaturalLanguage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'use strict';

const {assert} = require('chai');
const execa = require('execa');
const {execSync} = require('child_process');

const cmdDataset = 'node automl/automlNaturalLanguageDataset.js';
const cmdModel = 'node automl/automlNaturalLanguageModel.js';
Expand All @@ -28,36 +28,34 @@ const testModelName = 'dummyModel';
const sampleText = './resources/test.txt';
const projectId = process.env.GCLOUD_PROJECT;

const exec = async cmd => (await execa.shell(cmd)).stdout;

describe.skip('automl', () => {
// Skipped because it's been taking too long to delete datasets
it('should create a create, list, and delete a dataset', async () => {
// Check to see that this dataset does not yet exist
let output = await exec(`${cmdDataset} list-datasets`);
let output = execSync(`${cmdDataset} list-datasets`);
//t.false(output.includes(testDataSetName));
assert.notMatch(output, /testDataset/);

// Create dataset
output = await exec(`${cmdDataset} create-dataset -n "${testDataSetName}"`);
output = execSync(`${cmdDataset} create-dataset -n "${testDataSetName}"`);
const parsedOut = output.split('\n');
const dataSetId = parsedOut[1].split(':')[1].trim();
assert.match(output, /Dataset display name: {2}testDataset/);

// Delete dataset
output = await exec(`${cmdDataset} delete-dataset -i "${dataSetId}"`);
output = execSync(`${cmdDataset} delete-dataset -i "${dataSetId}"`);
assert.match(output, /Dataset deleted./);
});

// See : https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/NaturalLanguage/automl/model_test.py
// We make two models running this test, see hard-coded workaround below
it('should create a dataset, import data, and start making a model', async () => {
// Check to see that this dataset does not yet exist
let output = await exec(`${cmdDataset} list-datasets`);
let output = execSync(`${cmdDataset} list-datasets`);
assert.notMatch(output, /dummyDataset/);

// Create dataset
output = await exec(`${cmdDataset} create-dataset -n "${dummyDataSet}"`);
output = execSync(`${cmdDataset} create-dataset -n "${dummyDataSet}"`);

const dataSetId = output
.split('\n')[1]
Expand All @@ -66,17 +64,17 @@ describe.skip('automl', () => {
assert.match(output, /Dataset display name: {2}dummyDataSet/);

// Import Data
output = await exec(
output = execSync(
`${cmdDataset} import-data -i "${dataSetId}" -p "gs://nodejs-docs-samples-vcm/happiness.csv"`
);
assert.match(output, /Data imported./);

// Check to make sure model doesn't already exist
output = await exec(`${cmdModel} list-models`);
output = execSync(`${cmdModel} list-models`);
assert.notMatch(output, /dummyModel/);

// Begin training dataset, getting operation ID for next operation
output = await exec(
output = execSync(
`${cmdModel} create-model -i "${dataSetId}" -m "${testModelName}" -t "2"`
);
const operationName = output
Expand All @@ -86,7 +84,7 @@ describe.skip('automl', () => {
assert.match(output, /Training started.../);

// Poll operation status, here confirming that operation is not complete yet
output = await exec(
output = execSync(
`${cmdModel} get-operation-status -i "${dataSetId}" -o "${operationName}"`
);
assert.match(output, /done: false/);
Expand All @@ -96,16 +94,16 @@ describe.skip('automl', () => {
const donotdeleteModelId = `TCN4740161257642267869`;

// Confirm dataset exists
let output = await exec(`${cmdDataset} list-datasets`);
let output = execSync(`${cmdDataset} list-datasets`);
assert.match(output, /dummyDb/);

// List model evaluations, confirm model exists
output = await exec(
output = execSync(
`${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"`
);

// Display evaluation
output = await exec(
output = execSync(
`${cmdModel} display-evaluation -a "${donotdeleteModelId}"`
);
assert.match(output, /Model Precision:/);
Expand All @@ -115,25 +113,25 @@ describe.skip('automl', () => {
const donotdeleteModelId = `TCN4740161257642267869`;

// Confirm dataset exists
let output = await exec(`${cmdDataset} list-datasets`);
let output = execSync(`${cmdDataset} list-datasets`);
assert.match(output, /do_not_delete_me/);

// List model evaluations, confirm model exists
output = await exec(
output = execSync(
`${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"`
);
assert.match(output, /classificationEvaluationMetrics:/);

// Run prediction on 'test.txt' in resources folder
output = await exec(
output = execSync(
`${cmdPredict} predict -i "${donotdeleteModelId}" -f "${sampleText}" -s "0.5"`
);
assert.match(output, /Firm_Cheese/);
});

// List datasets
it('should list datasets', async () => {
const output = await exec(`${cmdDataset} list-datasets ${projectId}`);
const output = execSync(`${cmdDataset} list-datasets ${projectId}`);
assert.match(output, /List of datasets:/);
});
});
4 changes: 2 additions & 2 deletions cloud-language/snippets/test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
'use strict';

const {assert} = require('chai');
const execa = require('execa');
const {execSync} = require('child_process');

describe('quickstart', () => {
it('should analyze sentiment in text', async () => {
const {stdout} = await execa.shell('node quickstart.js');
const stdout = execSync('node quickstart.js');
assert(stdout, /Text: Hello, world!/);
assert(stdout, /Sentiment score: /);
assert(stdout, /Sentiment magnitude: /);
Expand Down

0 comments on commit 9dcebcb

Please sign in to comment.