From 730346269ca4287462cb1de2644bde797e588ab6 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Mon, 17 Dec 2018 16:36:06 -0800 Subject: [PATCH] refactor: modernize sample tests (#199) --- translate/automl/automlTranslationDataset.js | 108 ++++--------- translate/automl/automlTranslationModel.js | 74 ++++----- translate/automl/automlTranslationPredict.js | 24 ++- translate/automl/package.json | 26 ---- translate/automl/system-test/.eslintrc.yml | 5 - .../system-test/automlTranslation.test.js | 140 ----------------- translate/package.json | 15 +- translate/quickstart.js | 43 +++--- .../system-test/automlTranslation.test.js | 146 ++++++++++++++++++ translate/system-test/quickstart.test.js | 49 ++---- translate/system-test/translate.test.js | 134 ++++++++-------- 11 files changed, 313 insertions(+), 451 deletions(-) mode change 100755 => 100644 translate/automl/automlTranslationDataset.js mode change 100755 => 100644 translate/automl/automlTranslationModel.js mode change 100755 => 100644 translate/automl/automlTranslationPredict.js delete mode 100644 translate/automl/package.json delete mode 100644 translate/automl/system-test/.eslintrc.yml delete mode 100644 translate/automl/system-test/automlTranslation.test.js create mode 100644 translate/system-test/automlTranslation.test.js diff --git a/translate/automl/automlTranslationDataset.js b/translate/automl/automlTranslationDataset.js old mode 100755 new mode 100644 index 0528a45d52c..e76d91904cd --- a/translate/automl/automlTranslationDataset.js +++ b/translate/automl/automlTranslationDataset.js @@ -23,26 +23,15 @@ `use strict`; -async function createDataset( - projectId, - computeRegion, - datasetName, - source, - target -) { +async function createDataset(projectId) { // [START automl_translation_create_dataset] - const automl = require(`@google-cloud/automl`).v1beta1; + const automl = require(`@google-cloud/automl`); const client = new automl.AutoMlClient(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const datasetName = `name of the dataset to create, e.g. “myDataset”`; - // const source = `source language code, e.g. "en" `; - // const target = `target language code, e.g. "ja" `; + const computeRegion = 'us-central1'; + const datasetName = 'myDataset'; + const source = 'en'; + const target = 'ja'; // A resource that represents Google Cloud Platform location. const projectLocation = client.locationPath(projectId, computeRegion); @@ -90,8 +79,7 @@ async function createDataset( async function listDatasets(projectId, computeRegion, filter) { // [START automl_translation_list_datasets] const automl = require(`@google-cloud/automl`); - - const client = new automl.v1beta1.AutoMlClient(); + const client = new automl.AutoMlClient(); /** * TODO(developer): Uncomment the following line before running the sample. @@ -110,6 +98,10 @@ async function listDatasets(projectId, computeRegion, filter) { }); // Display the dataset information. + if (datasets.length === 0) { + console.log('No datasets found!'); + return; + } console.log(`List of datasets:`); datasets.forEach(dataset => { console.log(`Dataset name: ${dataset.name}`); @@ -131,14 +123,12 @@ async function listDatasets(projectId, computeRegion, filter) { console.log(`\tseconds: ${dataset.createTime.seconds}`); console.log(`\tnanos: ${dataset.createTime.nanos}`); }); - // [END automl_translation_list_datasets] } async function getDataset(projectId, computeRegion, datasetId) { // [START automl_translation_get_dataset] - const automl = require(`@google-cloud/automl`).v1beta1; - + const automl = require(`@google-cloud/automl`); const client = new automl.AutoMlClient(); /** @@ -179,7 +169,7 @@ async function getDataset(projectId, computeRegion, datasetId) { async function importData(projectId, computeRegion, datasetId, path) { // [START automl_translation_import_data] - const automl = require(`@google-cloud/automl`).v1beta1; + const automl = require(`@google-cloud/automl`); const client = new automl.AutoMlClient(); @@ -219,8 +209,7 @@ async function importData(projectId, computeRegion, datasetId, path) { async function deleteDataset(projectId, computeRegion, datasetId) { // [START automl_translation_delete_dataset] - const automl = require(`@google-cloud/automl`).v1beta1; - + const automl = require(`@google-cloud/automl`); const client = new automl.AutoMlClient(); /** @@ -248,7 +237,7 @@ require(`yargs`) computeRegion: { alias: `c`, type: `string`, - default: process.env.REGION_NAME, + default: 'us-central1', requiresArg: true, description: `region name e.g. "us-central1"`, }, @@ -315,61 +304,26 @@ require(`yargs`) description: `The target language to be translated to`, }, }) - .command( - `createDataset`, - `creates a new Dataset`, - {}, - async opts => - await createDataset( - opts.projectId, - opts.computeRegion, - opts.datasetName, - opts.source, - opts.target - ).catch(console.error) + .command(`createDataset`, `creates a new Dataset`, {}, opts => + createDataset( + opts.projectId, + opts.computeRegion, + opts.datasetName, + opts.source, + opts.target + ) ) - .command( - `list-datasets`, - `list all Datasets`, - {}, - async opts => - await listDatasets(opts.projectId, opts.computeRegion, opts.filter).catch( - console.error - ) + .command(`list-datasets`, `list all Datasets`, {}, opts => + listDatasets(opts.projectId, opts.computeRegion, opts.filter) ) - .command( - `get-dataset`, - `Get a Dataset`, - {}, - async opts => - await getDataset( - opts.projectId, - opts.computeRegion, - opts.datasetId - ).catch(console.error) + .command(`get-dataset`, `Get a Dataset`, {}, opts => + getDataset(opts.projectId, opts.computeRegion, opts.datasetId) ) - .command( - `delete-dataset`, - `Delete a dataset`, - {}, - async opts => - await deleteDataset( - opts.projectId, - opts.computeRegion, - opts.datasetId - ).catch(console.error) + .command(`delete-dataset`, `Delete a dataset`, {}, opts => + deleteDataset(opts.projectId, opts.computeRegion, opts.datasetId) ) - .command( - `import-data`, - `Import labeled items into dataset`, - {}, - async opts => - await importData( - opts.projectId, - opts.computeRegion, - opts.datasetId, - opts.path - ).catch(console.error) + .command(`import-data`, `Import labeled items into dataset`, {}, opts => + importData(opts.projectId, opts.computeRegion, opts.datasetId, opts.path) ) .example(`node $0 create-dataset -n "newDataSet" -s "en" -t "ja"`) .example(`node $0 list-datasets -f "translationDatasetMetadata:*"`) diff --git a/translate/automl/automlTranslationModel.js b/translate/automl/automlTranslationModel.js old mode 100755 new mode 100644 index b3df6301d95..d59693a7d99 --- a/translate/automl/automlTranslationModel.js +++ b/translate/automl/automlTranslationModel.js @@ -25,7 +25,7 @@ async function createModel(projectId, computeRegion, datasetId, modelName) { // [START automl_translation_create_model] - const automl = require(`@google-cloud/automl`).v1beta1; + const automl = require(`@google-cloud/automl`); const client = new automl.AutoMlClient(); @@ -80,7 +80,7 @@ async function createModel(projectId, computeRegion, datasetId, modelName) { async function listModels(projectId, computeRegion, filter) { // [START automl_translation_list_models] - const automl = require(`@google-cloud/automl`).v1beta1; + const automl = require(`@google-cloud/automl`); const client = new automl.AutoMlClient(); @@ -122,7 +122,7 @@ async function listModels(projectId, computeRegion, filter) { async function getModel(projectId, computeRegion, modelId) { // [START automl_translation_get_model] - const automl = require(`@google-cloud/automl`).v1beta1; + const automl = require(`@google-cloud/automl`); const client = new automl.AutoMlClient(); @@ -191,7 +191,7 @@ async function getModel(projectId, computeRegion, modelId) { async function listModelEvaluations(projectId, computeRegion, modelId, filter) { // [START automl_translation_list_model_evaluations] - const automl = require(`@google-cloud/automl`).v1beta1; + const automl = require(`@google-cloud/automl`); const client = new automl.AutoMlClient(); @@ -227,8 +227,7 @@ async function getModelEvaluation( ) { // [START automl_translation_get_model_evaluation] const automl = require(`@google-cloud/automl`); - - const client = new automl.v1beta1.AutoMlClient(); + const client = new automl.AutoMlClient(); /** * TODO(developer): Uncomment the following line before running the sample. @@ -257,7 +256,7 @@ async function getModelEvaluation( async function deleteModel(projectId, computeRegion, modelId) { // [START automl_translation_delete_model] - const automl = require(`@google-cloud/automl`).v1beta1; + const automl = require(`@google-cloud/automl`); const client = new automl.AutoMlClient(); @@ -282,7 +281,7 @@ async function deleteModel(projectId, computeRegion, modelId) { async function getOperationStatus(operationFullId) { // [START automl_translation_get_operation_status] - const automl = require(`@google-cloud/automl`).v1beta1; + const automl = require(`@google-cloud/automl`); const client = new automl.AutoMlClient(); @@ -305,7 +304,7 @@ require(`yargs`) computeRegion: { alias: `c`, type: `string`, - default: process.env.REGION_NAME, + default: 'us-central1', requiresArg: true, description: `region name e.g. "us-central1"`, }, @@ -370,51 +369,32 @@ require(`yargs`) `get-operation-status`, `Gets status of current operation`, {}, - async opts => - await getOperationStatus(opts.operationFullId).catch(console.error) + opts => getOperationStatus(opts.operationFullId) ) - .command( - `list-models`, - `list all Models`, - {}, - async opts => - await listModels(opts.projectId, opts.computeRegion, opts.filter).catch( - console.error - ) + .command(`list-models`, `list all Models`, {}, opts => + listModels(opts.projectId, opts.computeRegion, opts.filter) ) .command(`get-model`, `Get a Model`, {}, opts => getModel(opts.projectId, opts.computeRegion, opts.modelId) ) - .command( - `list-model-evaluations`, - `List model evaluations`, - {}, - async opts => - await listModelEvaluations( - opts.projectId, - opts.computeRegion, - opts.modelId, - opts.filter - ).catch(console.error) + .command(`list-model-evaluations`, `List model evaluations`, {}, opts => + listModelEvaluations( + opts.projectId, + opts.computeRegion, + opts.modelId, + opts.filter + ) ) - .command( - `get-model-evaluation`, - `Get model evaluation`, - {}, - async opts => - await getModelEvaluation( - opts.projectId, - opts.computeRegion, - opts.modelId, - opts.modelEvaluationId - ) + .command(`get-model-evaluation`, `Get model evaluation`, {}, opts => + getModelEvaluation( + opts.projectId, + opts.computeRegion, + opts.modelId, + opts.modelEvaluationId + ) ) - .command( - `delete-model`, - `Delete a Model`, - {}, - async opts => - await deleteModel(opts.projectId, opts.computeRegion, opts.modelId) + .command(`delete-model`, `Delete a Model`, {}, opts => + deleteModel(opts.projectId, opts.computeRegion, opts.modelId) ) .example(`node $0 create-model -i "DatasetID" -m "myModelName"`) .example(`node $0 get-operation-status -i "datasetId" -o "OperationFullID"`) diff --git a/translate/automl/automlTranslationPredict.js b/translate/automl/automlTranslationPredict.js old mode 100755 new mode 100644 index e9e0e38ac74..f88265a9623 --- a/translate/automl/automlTranslationPredict.js +++ b/translate/automl/automlTranslationPredict.js @@ -31,7 +31,7 @@ async function predict( translationAllowFallback ) { // [START automl_translation_predict] - const automl = require(`@google-cloud/automl`).v1beta1; + const automl = require(`@google-cloud/automl`); const fs = require(`fs`); // Create client for prediction service. @@ -89,7 +89,7 @@ require(`yargs`) computeRegion: { alias: `c`, type: `string`, - default: process.env.REGION_NAME, + default: 'us-central1', requiresArg: true, description: `region name e.g. "us-central1"`, }, @@ -124,18 +124,14 @@ require(`yargs`) `serve the request. Use false to not use Google translation model.`, }, }) - .command( - `predict`, - `classify the content`, - {}, - async opts => - await predict( - opts.projectId, - opts.computeRegion, - opts.modelId, - opts.filePath, - opts.translationAllowFallback - ) + .command(`predict`, `classify the content`, {}, opts => + predict( + opts.projectId, + opts.computeRegion, + opts.modelId, + opts.filePath, + opts.translationAllowFallback + ) ) .example( `node $0 predict -i "modelId" -f "./resources/testInput.txt" -t "False"` diff --git a/translate/automl/package.json b/translate/automl/package.json deleted file mode 100644 index 2dc018c5c26..00000000000 --- a/translate/automl/package.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "name": "automl", - "version": "1.0.0", - "description": "", - "main": "automlTranslationDataset.js", - "scripts": { - "test": "ava -T 1m --verbose system-test/*.test.js" - }, - "engines": { - "node": ">=8" - }, - "author": "", - "license": "ISC", - "dependencies": { - "@google-cloud/automl": "^0.1.1", - "@google-cloud/translate": "^2.0.0", - "yargs": "^12.0.1", - "mathjs": "^5.0.4" - }, - "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.0.0", - "ava": "^0.25.0", - "proxyquire": "^2.0.1", - "sinon": "^7.0.0" - } -} diff --git a/translate/automl/system-test/.eslintrc.yml b/translate/automl/system-test/.eslintrc.yml deleted file mode 100644 index 8b2fa88f8e1..00000000000 --- a/translate/automl/system-test/.eslintrc.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -rules: - node/no-unpublished-require: off - node/no-unsupported-features: off - no-empty: off \ No newline at end of file diff --git a/translate/automl/system-test/automlTranslation.test.js b/translate/automl/system-test/automlTranslation.test.js deleted file mode 100644 index 931f6a5a7ad..00000000000 --- a/translate/automl/system-test/automlTranslation.test.js +++ /dev/null @@ -1,140 +0,0 @@ -/** - * Copyright 2018, Google, LLC. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); - -const cmdDataset = `node automlTranslationDataset.js`; -const cmdModel = `node automlTranslationModel.js`; -const cmdPredict = `node automlTranslationPredict.js`; - -const testDataSetName = `testDataSet`; -const dummyDataSet = `dummyDataSet`; -const testModelName = 'dummyModel'; -const sampleText = `./resources/testInput.txt`; - -const donotdeleteModelId = `TRL188026453969732486`; - -// Skipped because it's been taking too long to delete datasets -test.skip(`should create a create, list, and delete a dataset`, async t => { - // Check to see that this dataset does not yet exist - let output = await tools.runAsync(`${cmdDataset} list-datasets`); - t.false(output.includes(testDataSetName)); - - // Create dataset - output = await tools.runAsync( - `${cmdDataset} create-dataset -n "${testDataSetName}"` - ); - const dataSetId = output - .split(`\n`)[1] - .split(`:`)[1] - .trim(); - t.true(output.includes(`Dataset display name: ${testDataSetName}`)); - - // Delete dataset - output = await tools.runAsync( - `${cmdDataset} delete-dataset -i "${dataSetId}"` - ); - t.true(output.includes(`Dataset deleted.`)); -}); - -// We make two models running this test, see hard-coded workaround below -test.skip(`should create a dataset, import data, and start making a model`, async t => { - // Check to see that this dataset does not yet exist - let output = await tools.runAsync(`${cmdDataset} list-datasets`); - t.false(output.includes(dummyDataSet)); - - // Create dataset - output = await tools.runAsync( - `${cmdDataset} create-dataset -n "${dummyDataSet}"` - ); - const dataSetId = output - .split(`\n`)[1] - .split(`:`)[1] - .trim(); - t.true(output.includes(`Dataset display name: ${dummyDataSet}`)); - - // Import Data - output = await tools.runAsync( - `${cmdDataset} import-data -i "${dataSetId}" -p "gs://nodejs-docs-samples-vcm/flowerTraindata20lines.csv"` - ); - t.true(output.includes(`Data imported.`)); - - // Check to make sure model doesn't already exist - output = await tools.runAsync(`${cmdModel} list-models`); - t.false(output.includes(`${testModelName}`)); - - // Begin training dataset, getting operation ID for next operation - output = await tools.runAsync( - `${cmdModel} create-model -i "${dataSetId}" -m "${testModelName}" -t "2"` - ); - const operationName = output - .split(`\n`)[0] - .split(`:`)[1] - .trim(); - t.true(output.includes(`Training started...`)); - - // Poll operation status, here confirming that operation is not complete yet - output = await tools.runAsync( - `${cmdModel} get-operation-status -i "${dataSetId}" -o "${operationName}"` - ); - t.true(output.includes(`done: false`)); -}); - -test(`should run get model (from a prexisting model)`, async t => { - // Confirm dataset exists - let output = await tools.runAsync(`${cmdDataset} list-datasets`); - t.true(output.includes(`me_do_not_delete`)); - - // List model evaluations, confirm model exists - output = await tools.runAsync( - `${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"` - ); - t.true(output.includes(`translationEvaluationMetrics:`)); - - // Get model evaluation - output = await tools.runAsync( - `${cmdModel} get-model -a "${donotdeleteModelId}"` - ); - t.true(output.includes(`Model deployment state: DEPLOYED`)); -}); - -test(`should run Prediction from prexisting model`, async t => { - // Confirm dataset exists - let output = await tools.runAsync(`${cmdDataset} list-datasets`); - t.true(output.includes(`me_do_not_delete`)); - - // List model evaluations, confirm model exists - output = await tools.runAsync( - `${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"` - ); - t.true(output.includes(`translationEvaluationMetrics:`)); - - // Run prediction on 'testImage.jpg' in resources folder - output = await tools.runAsync( - `${cmdPredict} predict -i "${donotdeleteModelId}" -f "${sampleText}" -t "False"` - ); - t.true( - output.includes(`Translated Content: これがどのように終わるか教えて`) - ); -}); - -// List datasets -test(`should list datasets`, async t => { - const output = await tools.runAsync(`${cmdDataset} list-datasets`); - t.true(output.includes(`List of datasets:`)); -}); diff --git a/translate/package.json b/translate/package.json index 3279abb22c5..a9371ede36a 100644 --- a/translate/package.json +++ b/translate/package.json @@ -1,6 +1,5 @@ { "name": "nodejs-docs-samples-translate", - "version": "0.0.1", "private": true, "license": "Apache-2.0", "author": "Google Inc.", @@ -9,19 +8,17 @@ "node": ">=8" }, "scripts": { - "cover": "nyc --reporter=lcov --cache mocha ./system-test/*.test.js --timeout=60000 && nyc report", - "test": "npm run cover" + "test": "mocha system-test" }, "dependencies": { "@google-cloud/translate": "^2.1.3", - "@google-cloud/automl": "^0.1.1", - "mathjs": "^5.1.0", + "@google-cloud/automl": "^0.1.2", "yargs": "^12.0.1" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.0.0", - "ava": "^0.25.0", - "proxyquire": "^2.0.1", - "sinon": "^7.0.0" + "chai": "^4.2.0", + "execa": "^1.0.0", + "mocha": "^5.2.0", + "uuid": "^3.3.2" } } diff --git a/translate/quickstart.js b/translate/quickstart.js index d56235c2401..1798e4316c6 100644 --- a/translate/quickstart.js +++ b/translate/quickstart.js @@ -16,32 +16,27 @@ 'use strict'; // [START translate_quickstart] -// Imports the Google Cloud client library -const {Translate} = require('@google-cloud/translate'); +async function quickstart( + projectId = 'YOUR_PROJECT_ID' // Your GCP Project Id +) { + // Imports the Google Cloud client library + const {Translate} = require('@google-cloud/translate'); -// Your Google Cloud Platform project ID -const projectId = 'YOUR_PROJECT_ID'; + // Instantiates a client + const translate = new Translate({projectId}); -// Instantiates a client -const translate = new Translate({ - projectId: projectId, -}); + // The text to translate + const text = 'Hello, world!'; -// The text to translate -const text = 'Hello, world!'; -// The target language -const target = 'ru'; + // The target language + const target = 'ru'; -// Translates some text into Russian -translate - .translate(text, target) - .then(results => { - const translation = results[0]; - - console.log(`Text: ${text}`); - console.log(`Translation: ${translation}`); - }) - .catch(err => { - console.error('ERROR:', err); - }); + // Translates some text into Russian + const [translation] = await translate.translate(text, target); + console.log(`Text: ${text}`); + console.log(`Translation: ${translation}`); +} // [END translate_quickstart] + +const args = process.argv.slice(2); +quickstart(...args).catch(console.error); diff --git a/translate/system-test/automlTranslation.test.js b/translate/system-test/automlTranslation.test.js new file mode 100644 index 00000000000..2ce9e5c5c92 --- /dev/null +++ b/translate/system-test/automlTranslation.test.js @@ -0,0 +1,146 @@ +/** + * Copyright 2018, Google, LLC. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'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 cmdDataset = 'node automlTranslationDataset.js'; +const cmdModel = 'node automlTranslationModel.js'; +const cmdPredict = 'node automlTranslationPredict.js'; + +const testDataSetName = 'testDataSet'; +const dummyDataSet = 'dummyDataSet'; +const testModelName = 'dummyModel'; +const sampleText = './resources/testInput.txt'; +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`); + assert.match(output, new RegExp(testDataSetName)); + + // Create dataset + output = await exec(`${cmdDataset} create-dataset -n "${testDataSetName}"`); + const dataSetId = output + .split(`\n`)[1] + .split(`:`)[1] + .trim(); + assert.match( + output, + new RegExp(`Dataset display name: ${testDataSetName}`) + ); + + // Delete dataset + output = await exec(`${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`); + assert.notMatch(output, new RegExp(dummyDataSet)); + + // Create dataset + output = await exec(`${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( + `${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`); + assert.notMatch(output, testModelName); + + // Begin training dataset, getting operation ID for next operation + output = await exec( + `${cmdModel} create-model -i "${dataSetId}" -m "${testModelName}" -t "2"` + ); + const operationName = output + .split(`\n`)[0] + .split(`:`)[1] + .trim(); + assert.match(output, `Training started...`); + + // Poll operation status, here confirming that operation is not complete yet + output = await exec( + `${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`); + assert.match(output, /me_do_not_delete/); + + // List model evaluations, confirm model exists + output = await exec( + `${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"` + ); + assert.match(output, /translationEvaluationMetrics:/); + + // Get model evaluation + output = await exec(`${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`); + assert.match(output, /me_do_not_delete/); + + // List model evaluations, confirm model exists + output = await exec( + `${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"` + ); + assert.match(output, `translationEvaluationMetrics:`); + + // Run prediction on 'testImage.jpg' in resources folder + output = await exec( + `${cmdPredict} predict -i "${donotdeleteModelId}" -f "${sampleText}" -t "False"` + ); + assert.match( + output, + /Translated Content: {2}これがどのように終わるか教えて/ + ); + }); + + // List datasets + it(`should list datasets`, async () => { + const output = await exec(`${cmdDataset} list-datasets`); + assert.match(output, /List of datasets:/); + }); +}); diff --git a/translate/system-test/quickstart.test.js b/translate/system-test/quickstart.test.js index bf892b1cbd4..8e1688cfb91 100644 --- a/translate/system-test/quickstart.test.js +++ b/translate/system-test/quickstart.test.js @@ -15,47 +15,16 @@ 'use strict'; -const proxyquire = require('proxyquire').noPreserveCache(); -const sinon = require('sinon'); -const assert = require('assert'); -const tools = require('@google-cloud/nodejs-repo-tools'); -const {Translate} = proxyquire('@google-cloud/translate', {}); -const translate = new Translate(); +const {assert} = require('chai'); +const execa = require('execa'); +const path = require('path'); -before(tools.checkCredentials); -before(tools.stubConsole); -after(tools.restoreConsole); +const cwd = path.join(__dirname, '..'); +const projectId = process.env.GCLOUD_PROJECT; -it('should translate a string', () => { - const string = 'Hello, world!'; - const expectedTranslation = 'Привет, мир!'; - const targetLanguage = 'ru'; - const translateMock = { - translate: (_string, _targetLanguage) => { - assert.strictEqual(_string, string); - assert.strictEqual(_targetLanguage, targetLanguage); - - return translate - .translate(_string, _targetLanguage) - .then(async ([translation]) => { - assert.strictEqual(translation, expectedTranslation); - await new Promise(r => setTimeout(r, 200)); - assert.strictEqual(console.log.callCount, 2); - assert.deepStrictEqual(console.log.getCall(0).args, [ - `Text: ${string}`, - ]); - assert.deepStrictEqual(console.log.getCall(1).args, [ - `Translation: ${expectedTranslation}`, - ]); - - return [translation]; - }); - }, - }; - - proxyquire('../quickstart', { - '@google-cloud/translate': { - Translate: sinon.stub().returns(translateMock), - }, +describe('quickstart sample tests', () => { + it('should translate a string', async () => { + const {stdout} = await execa.shell(`node quickstart ${projectId}`, {cwd}); + assert.match(stdout, new RegExp('Translation: Привет, мир!')); }); }); diff --git a/translate/system-test/translate.test.js b/translate/system-test/translate.test.js index 31b77e8abb8..4cf521434f7 100644 --- a/translate/system-test/translate.test.js +++ b/translate/system-test/translate.test.js @@ -16,9 +16,10 @@ 'use strict'; const path = require('path'); -const assert = require('assert'); -const tools = require('@google-cloud/nodejs-repo-tools'); +const {assert} = require('chai'); const {Translate} = require('@google-cloud/translate'); +const execa = require('execa'); + const translate = new Translate(); const cwd = path.join(__dirname, '..'); @@ -28,79 +29,74 @@ const text2 = 'Goodbye!'; const model = 'nmt'; const toLang = 'ru'; -before(tools.checkCredentials); +const exec = async cmd => { + return (await execa.shell(cmd, {cwd})).stdout; +}; -it('should detect language of a single string', async () => { - const output = await tools.runAsync(`${cmd} detect "${text}"`, cwd); - const [detection] = await translate.detect(text); - const expected = `Detections:\n${text} => ${detection.language}`; - assert.strictEqual(output, expected); -}); +describe('translate sample tests', () => { + it('should detect language of a single string', async () => { + const output = await exec(`${cmd} detect "${text}"`); + const [detection] = await translate.detect(text); + const expected = `Detections:\n${text} => ${detection.language}`; + assert.strictEqual(output, expected); + }); -it('should detect language of multiple strings', async () => { - const output = await tools.runAsync( - `${cmd} detect "${text}" "${text2}"`, - cwd - ); - const [detections] = await translate.detect([text, text2]); - const expected = `Detections:\n${text} => ${ - detections[0].language - }\n${text2} => ${detections[1].language}`; - assert.strictEqual(output, expected); -}); + it('should detect language of multiple strings', async () => { + const output = await exec(`${cmd} detect "${text}" "${text2}"`, cwd); + const [detections] = await translate.detect([text, text2]); + const expected = `Detections:\n${text} => ${ + detections[0].language + }\n${text2} => ${detections[1].language}`; + assert.strictEqual(output, expected); + }); -it('should list languages', async () => { - const output = await tools.runAsync(`${cmd} list`, cwd); - assert.ok(output.includes('Languages:')); - assert.ok(output.includes(`{ code: 'af', name: 'Afrikaans' }`)); -}); + it('should list languages', async () => { + const output = await exec(`${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 tools.runAsync(`${cmd} list es`, cwd); - assert.ok(output.includes('Languages:')); - assert.ok(output.includes(`{ code: 'af', name: 'afrikáans' }`)); -}); + it('should list languages with a target', async () => { + const output = await exec(`${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 tools.runAsync( - `${cmd} translate ${toLang} "${text}"`, - cwd - ); - const [translation] = await translate.translate(text, toLang); - const expected = `Translations:\n${text} => (${toLang}) ${translation}`; - assert.strictEqual(output, expected); -}); + it('should translate a single string', async () => { + const output = await exec(`${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 tools.runAsync( - `${cmd} translate ${toLang} "${text}" "${text2}"`, - cwd - ); - const [translations] = await translate.translate([text, text2], toLang); - const expected = `Translations:\n${text} => (${toLang}) ${ - translations[0] - }\n${text2} => (${toLang}) ${translations[1]}`; - assert.strictEqual(output, expected); -}); + it('should translate multiple strings', async () => { + const output = await exec( + `${cmd} translate ${toLang} "${text}" "${text2}"` + ); + const [translations] = await translate.translate([text, text2], toLang); + const expected = `Translations:\n${text} => (${toLang}) ${ + translations[0] + }\n${text2} => (${toLang}) ${translations[1]}`; + assert.strictEqual(output, expected); + }); -it('should translate a single string with a model', async () => { - const output = await tools.runAsync( - `${cmd} translate-with-model ${toLang} ${model} "${text}"`, - cwd - ); - const [translation] = await translate.translate(text, toLang); - const expected = `Translations:\n${text} => (${toLang}) ${translation}`; - assert.strictEqual(output, expected); -}); + it('should translate a single string with a model', async () => { + const output = await exec( + `${cmd} translate-with-model ${toLang} ${model} "${text}"` + ); + const [translation] = await translate.translate(text, toLang); + const expected = `Translations:\n${text} => (${toLang}) ${translation}`; + assert.strictEqual(output, expected); + }); -it('should translate multiple strings with a model', async () => { - const output = await tools.runAsync( - `${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`, - cwd - ); - const [translations] = await translate.translate([text, text2], toLang); - const expected = `Translations:\n${text} => (${toLang}) ${ - translations[0] - }\n${text2} => (${toLang}) ${translations[1]}`; - assert.strictEqual(output, expected); + it('should translate multiple strings with a model', async () => { + const output = await exec( + `${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"` + ); + const [translations] = await translate.translate([text, text2], toLang); + const expected = `Translations:\n${text} => (${toLang}) ${ + translations[0] + }\n${text2} => (${toLang}) ${translations[1]}`; + assert.strictEqual(output, expected); + }); });