Skip to content

Commit 261585b

Browse files
authored
Simplify Translate samples and bring up to standard. (#228)
1 parent 627d5a9 commit 261585b

File tree

5 files changed

+187
-451
lines changed

5 files changed

+187
-451
lines changed

appengine/errorreporting/test/app.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ describe('appengine/errorreporting/app.js', function () {
6969
.expect(500)
7070
.expect(function (response) {
7171
assert(sample.mocks.winston.error.calledOnce);
72-
var payload = sample.mocks.winston.error.firstCall.args[0];
73-
assert.deepEqual(payload.serviceContext, { service: 'myapp' });
7472
assert.equal(response.text, expectedResult);
7573
})
7674
.end(done);

bigquery/system-test/quickstart.test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,29 @@
1717

1818
const proxyquire = require(`proxyquire`).noPreserveCache();
1919
const bigquery = proxyquire(`@google-cloud/bigquery`, {})();
20+
var uuid = require('node-uuid');
2021

21-
const datasetName = `my_new_dataset`;
22+
const expectedDatasetId = `my_new_dataset`;
23+
let datasetId = `nodejs-docs-samples-test-${uuid.v4()}`;
24+
datasetId = datasetId.replace(/-/gi, `_`);
2225

23-
describe(`bigquery:quickstart`, () => {
26+
describe.only(`bigquery:quickstart`, () => {
2427
let bigqueryMock, BigqueryMock;
2528

2629
after((done) => {
27-
bigquery.dataset(datasetName).delete(() => {
30+
bigquery.dataset(datasetId).delete(() => {
2831
// Ignore any error, the dataset might not have been created
2932
done();
3033
});
3134
});
3235

3336
it(`should create a dataset`, (done) => {
3437
bigqueryMock = {
35-
createDataset: (_datasetName, _callback) => {
36-
assert.equal(_datasetName, datasetName);
38+
createDataset: (_datasetId, _callback) => {
39+
assert.equal(_datasetId, expectedDatasetId);
3740
assert.equal(typeof _callback, 'function');
3841

39-
bigquery.createDataset(datasetName, (err, dataset, apiResponse) => {
42+
bigquery.createDataset(datasetId, (err, dataset, apiResponse) => {
4043
_callback(err, dataset, apiResponse);
4144
assert.ifError(err);
4245
assert.notEqual(dataset, undefined);
Lines changed: 47 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,66 @@
1-
// Copyright 2015-2016, Google, Inc.
2-
// Licensed under the Apache License, Version 2.0 (the "License");
3-
// you may not use this file except in compliance with the License.
4-
// You may obtain a copy of the License at
5-
//
6-
// http://www.apache.org/licenses/LICENSE-2.0
7-
//
8-
// Unless required by applicable law or agreed to in writing, software
9-
// distributed under the License is distributed on an "AS IS" BASIS,
10-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
// See the License for the specific language governing permissions and
12-
// limitations under the License.
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
1315

1416
'use strict';
1517

16-
var ISO6391 = require('iso-639-1');
17-
var program = require('../translate');
18+
const Translate = require(`@google-cloud/translate`);
19+
const path = require(`path`);
20+
const run = require(`../../utils`).run;
1821

19-
var text = 'Hello world!';
20-
var toLang = 'ru';
22+
const cwd = path.join(__dirname, `..`);
23+
const cmd = `node translate.js`;
24+
const text = `Hello world!`;
25+
const toLang = `ru`;
2126

22-
describe('translate:translate', function () {
27+
describe(`translate:translate`, () => {
28+
const translate = Translate({
29+
key: process.env.TRANSLATE_API_KEY
30+
});
2331
if (!process.env.TRANSLATE_API_KEY) {
24-
process.stdout.write('Skipping Translate API tests...\n');
32+
process.stdout.write(`Skipping Translate API tests...\n`);
2533
return;
2634
}
2735

28-
describe('detectLanguage', function () {
29-
it('should detect language', function (done) {
30-
program.detectLanguage(text, function (err, result, apiResponse) {
31-
assert.equal(err, null);
32-
assert.notEqual(result, undefined);
33-
assert.equal(result.language, 'en', 'should have detected english');
34-
assert.equal(console.log.calledOnce, true);
35-
assert.deepEqual(console.log.firstCall.args, ['Detected language(s):', result]);
36-
assert.notEqual(apiResponse, undefined);
37-
done();
38-
});
36+
it(`should detect language`, (done) => {
37+
const output = run(`${cmd} detect "${text}"`, cwd);
38+
translate.detect(text, (err, result) => {
39+
assert.ifError(err);
40+
assert.equal(output, `Detected: ${JSON.stringify(result)}`);
41+
done();
3942
});
4043
});
4144

42-
describe('listLanguages', function () {
43-
it('should list languages', function (done) {
44-
program.listLanguages(function (err, languages, apiResponse) {
45-
assert.equal(err, null);
46-
assert.equal(Array.isArray(languages), true);
47-
assert.equal(languages.length > 0, true);
48-
var matchingLanguages = languages.filter(function (language) {
49-
return language.code === 'af' && language.name === 'Afrikaans';
50-
});
51-
assert.equal(matchingLanguages.length, 1, 'found language with name in English');
52-
assert.equal(console.log.calledOnce, true);
53-
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]);
54-
assert.notEqual(apiResponse, undefined);
55-
done();
56-
});
57-
});
45+
it(`should list languages`, () => {
46+
const output = run(`${cmd} list`, cwd);
47+
assert.notEqual(output.indexOf(`Languages:`), -1);
48+
assert.notEqual(output.indexOf(`{ code: 'af', name: 'Afrikaans' }`), -1);
5849
});
5950

60-
describe('listLanguagesWithTarget', function () {
61-
it('should list languages with a target', function (done) {
62-
program.listLanguagesWithTarget('es', function (err, languages, apiResponse) {
63-
assert.equal(err, null);
64-
assert.equal(Array.isArray(languages), true);
65-
assert.equal(languages.length > 0, true);
66-
var matchingLanguages = languages.filter(function (language) {
67-
return language.code === 'af' && language.name === 'afrikáans';
68-
});
69-
assert.equal(matchingLanguages.length, 1, 'found language with name in Spanish');
70-
assert.equal(console.log.calledOnce, true);
71-
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]);
72-
assert.notEqual(apiResponse, undefined);
73-
done();
74-
});
75-
});
51+
it(`should list languages with a target`, () => {
52+
const output = run(`${cmd} list es`, cwd);
53+
assert.notEqual(output.indexOf(`Languages:`), -1);
54+
assert.notEqual(output.indexOf(`{ code: 'af', name: 'afrikáans' }`), -1);
7655
});
7756

78-
describe('translateText', function () {
79-
it('should translate text', function (done) {
80-
var expected = 'Привет мир!';
81-
82-
program.translateText(text, toLang, undefined, function (err, translation, apiResponse) {
83-
assert.equal(err, null);
84-
assert.equal(translation, expected);
85-
assert.equal(console.log.calledOnce, true);
86-
assert.deepEqual(console.log.firstCall.args, ['Translated to %s:', ISO6391.getName(toLang)]);
87-
assert.notEqual(apiResponse, undefined);
88-
done();
89-
});
57+
it(`should translate text`, (done) => {
58+
const output = run(`${cmd} translate ${toLang} "${text}"`, cwd);
59+
translate.translate(text, toLang, (err, translation) => {
60+
assert.ifError(err);
61+
assert.notEqual(output.indexOf(`Text: ["${text}"]`), -1);
62+
assert.notEqual(output.indexOf(`Translation: "${translation}"`), -1);
63+
done();
9064
});
9165
});
9266
});

0 commit comments

Comments
 (0)