Skip to content

Commit

Permalink
refactor: use execSync for tests (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and Ace Nassri committed Nov 17, 2022
1 parent 8c6dd74 commit 167937a
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 87 deletions.
1 change: 1 addition & 0 deletions translate/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
rules:
no-console: off
node/no-missing-require: off
3 changes: 1 addition & 2 deletions translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"node": ">=8"
},
"scripts": {
"test": "mocha system-test --recursive --timeout 90000"
"test": "mocha --recursive --timeout 90000"
},
"dependencies": {
"@google-cloud/translate": "^3.0.0",
Expand All @@ -17,7 +17,6 @@
},
"devDependencies": {
"chai": "^4.2.0",
"execa": "^1.0.0",
"@google-cloud/storage": "^2.4.3",
"mocha": "^6.0.0",
"uuid": "^3.3.2"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,9 @@
'use strict';

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

const cwd = path.join(__dirname, '..', 'automl');
const exec = async cmd => {
const res = await execa.shell(cmd, {cwd});
if (res.stderr) {
throw new Error(res.stderr);
}
return res.stdout;
};
const cp = require('child_process');

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

const cmdDataset = 'node automlTranslationDataset.js';
const cmdModel = 'node automlTranslationModel.js';
Expand All @@ -41,11 +33,11 @@ const donotdeleteModelId = 'TRL188026453969732486';
describe.skip('automl sample tests', () => {
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`);
assert.match(output, new RegExp(testDataSetName));

// 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]
Expand All @@ -56,36 +48,36 @@ describe.skip('automl sample tests', () => {
);

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

// 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, new RegExp(dummyDataSet));

// 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(`Dataset display name: ${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, 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
Expand All @@ -95,41 +87,41 @@ describe.skip('automl sample tests', () => {
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/);
});

it(`should run get model (from a prexisting model)`, async () => {
// Confirm dataset exists
let output = await exec(`${cmdDataset} list-datasets`);
let output = execSync(`${cmdDataset} list-datasets`);
assert.match(output, /me_do_not_delete/);

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

// Get model evaluation
output = await exec(`${cmdModel} get-model -a "${donotdeleteModelId}"`);
output = execSync(`${cmdModel} get-model -a "${donotdeleteModelId}"`);
assert.match(output, /Model deployment state: DEPLOYED/);
});

it(`should run Prediction from prexisting model`, async () => {
// Confirm dataset exists
let output = await exec(`${cmdDataset} list-datasets`);
let output = execSync(`${cmdDataset} list-datasets`);
assert.match(output, /me_do_not_delete/);

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

// Run prediction on 'testImage.jpg' in resources folder
output = await exec(
output = execSync(
`${cmdPredict} predict -i "${donotdeleteModelId}" -f "${sampleText}" -t "False"`
);
assert.match(
Expand All @@ -140,7 +132,7 @@ describe.skip('automl sample tests', () => {

// 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:/);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
'use strict';

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

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

const cwd = path.join(__dirname, '..');
const projectId = process.env.GCLOUD_PROJECT;

describe('quickstart sample tests', () => {
it('should translate a string', async () => {
const {stdout} = await execa.shell(`node quickstart ${projectId}`, {cwd});
const stdout = execSync(`node quickstart ${projectId}`, {cwd});
assert.match(stdout, new RegExp('Translation: Привет, мир!'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,60 @@

'use strict';

const path = require('path');
const {assert} = require('chai');
const {Translate} = require('@google-cloud/translate');
const execa = require('execa');
const cp = require('child_process');

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

const cwd = path.join(__dirname, '..');
const cmd = 'node translate.js';
const text = 'Hello world!';
const text2 = 'Goodbye!';
const model = 'nmt';
const toLang = 'ru';

const exec = async cmd => {
return (await execa.shell(cmd, {cwd})).stdout;
};

describe('translate sample tests', () => {
it('should detect language of a single string', async () => {
const output = await exec(`${cmd} detect "${text}"`);
const output = execSync(`${cmd} detect "${text}"`);
const [detection] = await translate.detect(text);
const expected = `Detections:\n${text} => ${detection.language}`;
assert.strictEqual(output, expected);
const expected = new RegExp(
`Detections:\n${text} => ${detection.language}`
);
assert.match(output, expected);
});

it('should detect language of multiple strings', async () => {
const output = await exec(`${cmd} detect "${text}" "${text2}"`, cwd);
const output = execSync(`${cmd} detect "${text}" "${text2}"`);
const [detections] = await translate.detect([text, text2]);
const expected = `Detections:\n${text} => ${
detections[0].language
}\n${text2} => ${detections[1].language}`;
assert.strictEqual(output, expected);
const expected = new RegExp(
`Detections:\n${text} => ${detections[0].language}\n${text2} => ${
detections[1].language
}`
);
assert.match(output, expected);
});

it('should list languages', async () => {
const output = await exec(`${cmd} list`);
it('should list languages', () => {
const output = execSync(`${cmd} list`);
assert.match(output, /Languages:/);
assert.match(output, new RegExp(`{ code: 'af', name: 'Afrikaans' }`));
});

it('should list languages with a target', async () => {
const output = await exec(`${cmd} list es`);
it('should list languages with a target', () => {
const output = execSync(`${cmd} list es`);
assert.match(output, /Languages:/);
assert.match(output, new RegExp(`{ code: 'af', name: 'afrikáans' }`));
});

it('should translate a single string', async () => {
const output = await exec(`${cmd} translate ${toLang} "${text}"`);
const output = execSync(`${cmd} translate ${toLang} "${text}"`);
const [translation] = await translate.translate(text, toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
assert.strictEqual(output, expected);
});

it('should translate multiple strings', async () => {
const output = await exec(
`${cmd} translate ${toLang} "${text}" "${text2}"`
);
const output = execSync(`${cmd} translate ${toLang} "${text}" "${text2}"`);
const [translations] = await translate.translate([text, text2], toLang);
const expected = `Translations:\n${text} => (${toLang}) ${
translations[0]
Expand All @@ -81,7 +77,7 @@ describe('translate sample tests', () => {
});

it('should translate a single string with a model', async () => {
const output = await exec(
const output = execSync(
`${cmd} translate-with-model ${toLang} ${model} "${text}"`
);
const [translation] = await translate.translate(text, toLang);
Expand All @@ -90,7 +86,7 @@ describe('translate sample tests', () => {
});

it('should translate multiple strings with a model', async () => {
const output = await exec(
const output = execSync(
`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`
);
const [translations] = await translate.translate([text, text2], toLang);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const {Storage} = require('@google-cloud/storage');
const execa = require('execa');
const cp = require('child_process');
const uuid = require('uuid');
const exec = async cmd => (await execa.shell(cmd)).stdout;

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

const REGION_TAG = 'translate_batch_translate_text_beta';

Expand Down Expand Up @@ -53,7 +54,7 @@ describe(REGION_TAG, () => {
const inputUri = `gs://cloud-samples-data/translation/text.txt`;

const outputUri = `gs://${projectId}/${bucketName}`;
const output = await exec(
const output = execSync(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri}`
);
assert.match(output, /Total Characters: 13/);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const execa = require('execa');
const exec = async cmd => (await execa.shell(cmd)).stdout;
const cp = require('child_process');

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

const REGION_TAG = 'translate_create_glossary_beta';

Expand All @@ -29,7 +30,7 @@ describe(REGION_TAG, () => {
const projectId = await translationClient.getProjectId();
const location = 'us-central1';
const glossaryId = 'test-glossary';
const output = await exec(
const output = execSync(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
);
assert.match(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const execa = require('execa');
const exec = async cmd => (await execa.shell(cmd)).stdout;
const cp = require('child_process');

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

const REGION_TAG = 'translate_delete_glossary_beta';

Expand Down Expand Up @@ -60,7 +61,7 @@ describe(REGION_TAG, () => {
it('should delete a glossary', async () => {
const projectId = await translationClient.getProjectId();

const output = await exec(
const output = execSync(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
);
assert.match(output, /glossary/);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const execa = require('execa');
const exec = async cmd => (await execa.shell(cmd)).stdout;
const cp = require('child_process');

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

const REGION_TAG = 'translate_detect_language_beta';

Expand All @@ -28,7 +29,7 @@ describe(REGION_TAG, () => {
const projectId = await translationClient.getProjectId();
const location = 'global';
const text = `'Hæ sæta'`;
const output = await exec(
const output = execSync(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${text}`
);
assert.match(output, /Language Code: is/);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const execa = require('execa');
const exec = async cmd => (await execa.shell(cmd)).stdout;
const cp = require('child_process');

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

const REGION_TAG = 'translate_get_glossary_beta';

Expand Down Expand Up @@ -59,7 +60,7 @@ describe(REGION_TAG, () => {
it('should get a glossary', async () => {
const projectId = await translationClient.getProjectId();

const output = await exec(
const output = execSync(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
);
assert.match(output, /test-glossary/);
Expand Down
Loading

0 comments on commit 167937a

Please sign in to comment.