Skip to content

Commit

Permalink
refactor: use execSync for tests (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Apr 5, 2019
1 parent 003b3cb commit 7b9d1df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
14 changes: 5 additions & 9 deletions automl/package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
{
"name": "@google-cloud/automl-samples",
"description": "Samples for the Cloud AutoML Client Library for Node.js.",
"version": "0.0.1",
"license": "Apache-2.0",
"author": "Google LLC",
"engines": {
"node": ">=8"
},
"files": [
"!test/*"
],
"repository": "googleapis/nodejs-automl",
"private": true,
"nyc": {
"exclude": [
"**/*.test.js"
]
},
"scripts": {
"test": "mocha --timeout 600000"
},
"dependencies": {
"@google-cloud/automl": "^0.2.0",
"chai": "^4.2.0",
"execa": "^1.0.0",
"mathjs": "^5.5.0",
"yargs": "^13.2.1"
},
"devDependencies": {
"mocha": "^6.0.1"
"mocha": "^6.0.1",
"chai": "^4.2.0"
}
}
39 changes: 19 additions & 20 deletions automl/test/automlVision.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

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

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const exec = async cmd => (await execa.shell(cmd)).stdout;
const cmdDataset = `node vision/automlVisionDataset.js`;
const cmdModel = `node vision/automlVisionModel.js`;
const cmdPredict = `node vision/automlVisionPredict.js`;
Expand All @@ -33,49 +34,49 @@ const sampleImage2 = path.join(testImgPath, `testImage2.jpg`);
describe(`auto ml vision`, () => {
it.skip(`should 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`);
assert.strictEqual(output.includes(testDataSetName), false);

// Create dataset
output = await exec(`${cmdDataset} create-dataset -n "${testDataSetName}"`);
output = execSync(`${cmdDataset} create-dataset -n "${testDataSetName}"`);
const dataSetId = output
.split(`\n`)[1]
.split(`:`)[1]
.trim();
assert.match(output, new RegExp(testDataSetName));

// 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/vision/automl/model_test.py
// We make two models running this test, see hard-coded workaround below
it.skip(`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.strictEqual(output.includes(dummyDataSet), false);

// Create dataset
output = await exec(`${cmdDataset} create-dataset -n "${dummyDataSet}"`);
output = execSync(`${cmdDataset} create-dataset -n "${dummyDataSet}"`);
const dataSetId = output
.split(`\n`)[1]
.split(`:`)[1]
.trim();
assert.match(output, new RegExp(dummyDataSet));

// Import Data
output = await exec(
output = execSync(
`${cmdDataset} import-data -i "${dataSetId}" -p "gs://nodejs-docs-samples-vcm/flowerTraindata20lines.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, new RegExp(testModelName));

// 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
.split(`\n`)[0]
Expand All @@ -86,7 +87,7 @@ describe(`auto ml vision`, () => {
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 @@ -97,18 +98,16 @@ describe(`auto ml vision`, () => {
const flowersDisplayName = `flowersTest`;

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

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

// Display evaluation
output = await exec(
`${cmdModel} display-evaluation -a "${flowersModelId}"`
);
output = execSync(`${cmdModel} display-evaluation -a "${flowersModelId}"`);
assert.match(output, /Model Precision/);
});

Expand All @@ -117,23 +116,23 @@ describe(`auto ml vision`, () => {
const flowersDisplayName = `flowers`;

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

// List model evaluations, confirm model exists
output = await exec(
output = execSync(
`${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"`
);
// Run prediction on 'testImage.jpg' in resources folder
output = await exec(
output = execSync(
`${cmdPredict} predict -i "${donotdeleteModelId}" -f "${sampleImage2}" -s "0.5"`
);
assert.match(output, /dandelion/);
});

// List datasets
it(`should list datasets`, async () => {
const output = await exec(`${cmdDataset} list-datasets`);
const output = execSync(`${cmdDataset} list-datasets`);
assert.match(output, /List of datasets:/);
});
});

0 comments on commit 7b9d1df

Please sign in to comment.