Skip to content

Commit

Permalink
Update Translate samples, with some minor tweaks to Language samples. (
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 b5f2fb0 commit ff93e43
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 267 deletions.
8 changes: 2 additions & 6 deletions translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
"license": "Apache Version 2.0",
"author": "Google Inc.",
"scripts": {
"test": "mocha -R spec --require intelli-espower-loader ../test/_setup.js test/*.test.js",
"system-test": "mocha -R spec --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
"test": "cd ..; npm run st -- translate/system-test/*.test.js"
},
"dependencies": {
"@google-cloud/translate": "^0.4.0",
"@google-cloud/translate": "^0.5.0",
"yargs": "^6.4.0"
},
"devDependencies": {
"mocha": "^3.1.2"
},
"engines": {
"node": ">=4.3.2"
}
Expand Down
20 changes: 9 additions & 11 deletions translate/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
// Imports the Google Cloud client library
const Translate = require('@google-cloud/translate');

// Your Translate API key
const apiKey = 'YOUR_API_KEY';
// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';

// Instantiates a client
const translateClient = Translate({
key: apiKey
projectId: projectId
});

// The text to translate
Expand All @@ -33,13 +33,11 @@ const text = 'Hello, world!';
const target = 'ru';

// Translates some text into Russian
translateClient.translate(text, target, (err, translation) => {
if (err) {
console.error(err);
return;
}
translateClient.translate(text, target)
.then((results) => {
const translation = results[0];

console.log(`Text: ${text}`);
console.log(`Translation: ${translation}`);
});
console.log(`Text: ${text}`);
console.log(`Translation: ${translation}`);
});
// [END translate_quickstart]
44 changes: 21 additions & 23 deletions translate/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,37 @@
'use strict';

const proxyquire = require(`proxyquire`).noPreserveCache();
const translate = proxyquire(`@google-cloud/translate`, {})({
key: process.env.TRANSLATE_API_KEY
});
const string = `Hello, world!`;
const expectedTranslation = `Привет мир!`;
const targetLanguage = `ru`;
const translate = proxyquire(`@google-cloud/translate`, {})();

describe(`translate:quickstart`, () => {
let translateMock, TranslateMock;

it(`should translate a string`, (done) => {
translateMock = {
translate: (_string, _targetLanguage, _callback) => {
const string = `Hello, world!`;
const expectedTranslation = `Привет мир!`;
const targetLanguage = `ru`;
const translateMock = {
translate: (_string, _targetLanguage) => {
assert.equal(_string, string);
assert.equal(_targetLanguage, targetLanguage);
assert.equal(typeof _callback, 'function');

translate.translate(_string, _targetLanguage, (err, translation, apiResponse) => {
_callback(err, translation, apiResponse);
assert.ifError(err);
assert.equal(translation, expectedTranslation);
assert.notEqual(apiResponse, undefined);
assert.equal(console.log.calledTwice, true);
assert.deepEqual(console.log.firstCall.args, [`Text: ${string}`]);
assert.deepEqual(console.log.secondCall.args, [`Translation: ${expectedTranslation}`]);
done();
});
return translate.translate(_string, _targetLanguage)
.then((results) => {
const translation = results[0];
assert.equal(translation, expectedTranslation);

setTimeout(() => {
assert.equal(console.log.callCount, 2);
assert.deepEqual(console.log.getCall(0).args, [`Text: ${string}`]);
assert.deepEqual(console.log.getCall(1).args, [`Translation: ${expectedTranslation}`]);
done();
}, 200);

return results;
});
}
};
TranslateMock = sinon.stub().returns(translateMock);

proxyquire(`../quickstart`, {
'@google-cloud/translate': TranslateMock
'@google-cloud/translate': sinon.stub().returns(translateMock)
});
});
});
60 changes: 36 additions & 24 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,57 @@ const run = require(`../../utils`).run;
const cwd = path.join(__dirname, `..`);
const cmd = `node translate.js`;
const text = `Hello world!`;
const text2 = `Goodbye!`;
const toLang = `ru`;

describe(`translate:translate`, () => {
const translate = Translate({
key: process.env.TRANSLATE_API_KEY
});
if (!process.env.TRANSLATE_API_KEY) {
process.stdout.write(`Skipping Translate API tests...\n`);
return;
}
const translate = Translate();

it(`should detect language`, (done) => {
it(`should detect language of a single string`, () => {
const output = run(`${cmd} detect "${text}"`, cwd);
translate.detect(text, (err, result) => {
assert.ifError(err);
assert.equal(output, `Detected: ${JSON.stringify(result)}`);
done();
});
return translate.detect(text)
.then((results) => {
const expected = `Detections:\n${text} => ${results[0].language}`;
assert.equal(output, expected);
});
});

it(`should detect language of multiple strings`, () => {
const output = run(`${cmd} detect "${text}" "${text2}"`, cwd);
return translate.detect([text, text2])
.then((results) => {
const expected = `Detections:\n${text} => ${results[0][0].language}\n${text2} => ${results[0][1].language}`;
assert.equal(output, expected);
});
});

it(`should list languages`, () => {
const output = run(`${cmd} list`, cwd);
assert.notEqual(output.indexOf(`Languages:`), -1);
assert.notEqual(output.indexOf(`{ code: 'af', name: 'Afrikaans' }`), -1);
assert.equal(output.includes(`Languages:`), true);
assert.equal(output.includes(`{ code: 'af', name: 'Afrikaans' }`), true);
});

it(`should list languages with a target`, () => {
const output = run(`${cmd} list es`, cwd);
assert.notEqual(output.indexOf(`Languages:`), -1);
assert.notEqual(output.indexOf(`{ code: 'af', name: 'afrikáans' }`), -1);
assert.equal(output.includes(`Languages:`), true);
assert.equal(output.includes(`{ code: 'af', name: 'afrikáans' }`), true);
});

it(`should translate text`, (done) => {
it(`should translate a single string`, () => {
const output = run(`${cmd} translate ${toLang} "${text}"`, cwd);
translate.translate(text, toLang, (err, translation) => {
assert.ifError(err);
assert.notEqual(output.indexOf(`Text: ["${text}"]`), -1);
assert.notEqual(output.indexOf(`Translation: "${translation}"`), -1);
done();
});
return translate.translate(text, toLang)
.then((results) => {
const expected = `Translations:\n${text} => (${toLang}) ${results[0]}`;
assert.equal(output, expected);
});
});

it(`should translate multiple strings`, () => {
const output = run(`${cmd} translate ${toLang} "${text}" "${text2}"`, cwd);
return translate.translate([text, text2], toLang)
.then((results) => {
const expected = `Translations:\n${text} => (${toLang}) ${results[0][0]}\n${text2} => (${toLang}) ${results[0][1]}`;
assert.equal(output, expected);
});
});
});
43 changes: 0 additions & 43 deletions translate/test/quickstart.test.js

This file was deleted.

44 changes: 0 additions & 44 deletions translate/test/translate.test.js

This file was deleted.

Loading

0 comments on commit ff93e43

Please sign in to comment.