Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Translate samples and bring up to standard. #228

Merged
merged 1 commit into from
Oct 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions appengine/errorreporting/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ describe('appengine/errorreporting/app.js', function () {
.expect(500)
.expect(function (response) {
assert(sample.mocks.winston.error.calledOnce);
var payload = sample.mocks.winston.error.firstCall.args[0];
assert.deepEqual(payload.serviceContext, { service: 'myapp' });
assert.equal(response.text, expectedResult);
})
.end(done);
Expand Down
15 changes: 9 additions & 6 deletions bigquery/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@

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

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

describe(`bigquery:quickstart`, () => {
describe.only(`bigquery:quickstart`, () => {
let bigqueryMock, BigqueryMock;

after((done) => {
bigquery.dataset(datasetName).delete(() => {
bigquery.dataset(datasetId).delete(() => {
// Ignore any error, the dataset might not have been created
done();
});
});

it(`should create a dataset`, (done) => {
bigqueryMock = {
createDataset: (_datasetName, _callback) => {
assert.equal(_datasetName, datasetName);
createDataset: (_datasetId, _callback) => {
assert.equal(_datasetId, expectedDatasetId);
assert.equal(typeof _callback, 'function');

bigquery.createDataset(datasetName, (err, dataset, apiResponse) => {
bigquery.createDataset(datasetId, (err, dataset, apiResponse) => {
_callback(err, dataset, apiResponse);
assert.ifError(err);
assert.notEqual(dataset, undefined);
Expand Down
120 changes: 47 additions & 73 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,66 @@
// Copyright 2015-2016, 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
//
// 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.
/**
* Copyright 2016, 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
*
* 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';

var ISO6391 = require('iso-639-1');
var program = require('../translate');
const Translate = require(`@google-cloud/translate`);
const path = require(`path`);
const run = require(`../../utils`).run;

var text = 'Hello world!';
var toLang = 'ru';
const cwd = path.join(__dirname, `..`);
const cmd = `node translate.js`;
const text = `Hello world!`;
const toLang = `ru`;

describe('translate:translate', function () {
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');
process.stdout.write(`Skipping Translate API tests...\n`);
return;
}

describe('detectLanguage', function () {
it('should detect language', function (done) {
program.detectLanguage(text, function (err, result, apiResponse) {
assert.equal(err, null);
assert.notEqual(result, undefined);
assert.equal(result.language, 'en', 'should have detected english');
assert.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, ['Detected language(s):', result]);
assert.notEqual(apiResponse, undefined);
done();
});
it(`should detect language`, (done) => {
const output = run(`${cmd} detect "${text}"`, cwd);
translate.detect(text, (err, result) => {
assert.ifError(err);
assert.equal(output, `Detected: ${JSON.stringify(result)}`);
done();
});
});

describe('listLanguages', function () {
it('should list languages', function (done) {
program.listLanguages(function (err, languages, apiResponse) {
assert.equal(err, null);
assert.equal(Array.isArray(languages), true);
assert.equal(languages.length > 0, true);
var matchingLanguages = languages.filter(function (language) {
return language.code === 'af' && language.name === 'Afrikaans';
});
assert.equal(matchingLanguages.length, 1, 'found language with name in English');
assert.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]);
assert.notEqual(apiResponse, undefined);
done();
});
});
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);
});

describe('listLanguagesWithTarget', function () {
it('should list languages with a target', function (done) {
program.listLanguagesWithTarget('es', function (err, languages, apiResponse) {
assert.equal(err, null);
assert.equal(Array.isArray(languages), true);
assert.equal(languages.length > 0, true);
var matchingLanguages = languages.filter(function (language) {
return language.code === 'af' && language.name === 'afrikáans';
});
assert.equal(matchingLanguages.length, 1, 'found language with name in Spanish');
assert.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]);
assert.notEqual(apiResponse, undefined);
done();
});
});
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);
});

describe('translateText', function () {
it('should translate text', function (done) {
var expected = 'Привет мир!';

program.translateText(text, toLang, undefined, function (err, translation, apiResponse) {
assert.equal(err, null);
assert.equal(translation, expected);
assert.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, ['Translated to %s:', ISO6391.getName(toLang)]);
assert.notEqual(apiResponse, undefined);
done();
});
it(`should translate text`, (done) => {
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();
});
});
});
Loading