Skip to content

Commit

Permalink
Cleanup App Engine samples and re-work tests. (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry authored and Ace Nassri committed Nov 17, 2022
1 parent 4c81df1 commit 99d189a
Show file tree
Hide file tree
Showing 6 changed files with 3,262 additions and 94 deletions.
29 changes: 23 additions & 6 deletions translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@
"name": "nodejs-docs-samples-translate",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"license": "Apache-2.0",
"author": "Google Inc.",
"scripts": {
"test": "cd ..; npm run st -- --verbose translate/system-test/*.test.js"
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"dependencies": {
"@google-cloud/translate": "0.8.0",
"yargs": "7.0.2"
"cloud": {
"requiresKeyFile": true,
"requiresProjectId": true
},
"engines": {
"node": ">=4.3.2"
},
"scripts": {
"lint": "samples lint",
"pretest": "npm run lint",
"system-test": "ava -T 20s --verbose system-test/*.test.js",
"test": "npm run system-test"
},
"dependencies": {
"@google-cloud/translate": "0.8.1",
"yargs": "7.1.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "1.3.1",
"ava": "0.19.1",
"proxyquire": "1.7.11",
"sinon": "2.1.0"
}
}
2 changes: 1 addition & 1 deletion translate/quickstart.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, Google, Inc.
* Copyright 2017, Google, Inc.
* 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
Expand Down
12 changes: 7 additions & 5 deletions translate/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, Google, Inc.
* Copyright 2017, Google, Inc.
* 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
Expand All @@ -15,13 +15,15 @@

'use strict';

require(`../../system-test/_setup`);

const proxyquire = require(`proxyquire`).noPreserveCache();
const sinon = require(`sinon`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);
const translate = proxyquire(`@google-cloud/translate`, {})();

test.before(stubConsole);
test.after.always(restoreConsole);
test.before(tools.checkCredentials);
test.before(tools.stubConsole);
test.after.always(tools.restoreConsole);

test.cb(`should translate a string`, (t) => {
const string = `Hello, world!`;
Expand Down
26 changes: 14 additions & 12 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, Google, Inc.
* Copyright 2017, Google, Inc.
* 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
Expand All @@ -15,10 +15,10 @@

'use strict';

require(`../../system-test/_setup`);

const translate = require(`@google-cloud/translate`)();
const path = require(`path`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);
const translate = require(`@google-cloud/translate`)();

const cwd = path.join(__dirname, `..`);
const cmd = `node translate.js`;
Expand All @@ -27,55 +27,57 @@ const text2 = `Goodbye!`;
const model = `nmt`;
const toLang = `ru`;

test.before(tools.checkCredentials);

test(`should detect language of a single string`, async (t) => {
const output = await runAsync(`${cmd} detect "${text}"`, cwd);
const output = await tools.runAsync(`${cmd} detect "${text}"`, cwd);
const [detection] = await translate.detect(text);
const expected = `Detections:\n${text} => ${detection.language}`;
t.is(output, expected);
});

test(`should detect language of multiple strings`, async (t) => {
const output = await runAsync(`${cmd} detect "${text}" "${text2}"`, cwd);
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}`;
t.is(output, expected);
});

test(`should list languages`, async (t) => {
const output = await runAsync(`${cmd} list`, cwd);
const output = await tools.runAsync(`${cmd} list`, cwd);
t.true(output.includes(`Languages:`));
t.true(output.includes(`{ code: 'af', name: 'Afrikaans' }`));
});

test(`should list languages with a target`, async (t) => {
const output = await runAsync(`${cmd} list es`, cwd);
const output = await tools.runAsync(`${cmd} list es`, cwd);
t.true(output.includes(`Languages:`));
t.true(output.includes(`{ code: 'af', name: 'afrikáans' }`));
});

test(`should translate a single string`, async (t) => {
const output = await runAsync(`${cmd} translate ${toLang} "${text}"`, cwd);
const output = await tools.runAsync(`${cmd} translate ${toLang} "${text}"`, cwd);
const [translation] = await translate.translate(text, toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
t.is(output, expected);
});

test(`should translate multiple strings`, async (t) => {
const output = await runAsync(`${cmd} translate ${toLang} "${text}" "${text2}"`, cwd);
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]}`;
t.is(output, expected);
});

test(`should translate a single string with a model`, async (t) => {
const output = await runAsync(`${cmd} translate-with-model ${toLang} ${model} "${text}"`, cwd);
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}`;
t.is(output, expected);
});

test(`should translate multiple strings with a model`, async (t) => {
const output = await runAsync(`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`, cwd);
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]}`;
t.is(output, expected);
Expand Down
4 changes: 2 additions & 2 deletions translate/translate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, Google, Inc.
* Copyright 2017, Google, Inc.
* 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
Expand Down Expand Up @@ -169,7 +169,7 @@ function translateTextWithModel (text, target, model) {
// [END translate_text_with_model]
}

require(`yargs`)
require(`yargs`) // eslint-disable-line
.demand(1)
.command(
`detect <text..>`,
Expand Down
Loading

0 comments on commit 99d189a

Please sign in to comment.