From 3f260eab07fb47962a5cd1c2a90d2e3d8be8f640 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 27 Sep 2016 22:02:05 -0700 Subject: [PATCH] Adds 6 quickstart examples (#218) * Add some simple "quickstart" samples. * Add quickstart tests (#215) * First draft of new tests * Fix failing tests * Fix comments * Add comments. * Update comments. * Add another comment. * Cleanup. * Fix region tags. * Fix comments. --- translate/quickstart.js | 29 +++++++++++++++ translate/system-test/quickstart.test.js | 46 ++++++++++++++++++++++++ translate/test/quickstart.test.js | 38 ++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 translate/quickstart.js create mode 100644 translate/system-test/quickstart.test.js create mode 100644 translate/test/quickstart.test.js diff --git a/translate/quickstart.js b/translate/quickstart.js new file mode 100644 index 0000000000..46ce2607ca --- /dev/null +++ b/translate/quickstart.js @@ -0,0 +1,29 @@ +// 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. + +'use strict'; + +// [START translate_quickstart] +// Imports and instantiates the Google Cloud client library +// for the Google Translate API +const translate = require('@google-cloud/translate')({ + key: 'YOUR_API_KEY' +}); + +// Translates some text into Russian +translate.translate('Hello, world!', 'ru', (err, translation, apiResponse) => { + if (!err) { + // The text was translated successfully + } +}); +// [END translate_quickstart] diff --git a/translate/system-test/quickstart.test.js b/translate/system-test/quickstart.test.js new file mode 100644 index 0000000000..3936592c5b --- /dev/null +++ b/translate/system-test/quickstart.test.js @@ -0,0 +1,46 @@ +// 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. + +'use strict'; + +const proxyquire = require(`proxyquire`).noPreserveCache(); +const translate = proxyquire(`@google-cloud/translate`, {})({ + key: process.env.TRANSLATE_API_KEY +}); +const string = `Hello, world!`; +const targetLanguage = `ru`; + +describe(`translate:quickstart`, () => { + let translateMock, TranslateMock; + + it(`should translate a string`, (done) => { + translateMock = { + translate: (_string, _targetLanguage) => { + assert.equal(_string, string); + assert.equal(_targetLanguage, targetLanguage); + + translate.translate(_string, _targetLanguage, (err, translation, apiResponse) => { + assert.ifError(err); + assert.equal(translation, `Привет мир!`); + assert.notEqual(apiResponse, undefined); + done(); + }); + } + }; + TranslateMock = sinon.stub().returns(translateMock); + + proxyquire(`../quickstart`, { + '@google-cloud/translate': TranslateMock + }); + }); +}); diff --git a/translate/test/quickstart.test.js b/translate/test/quickstart.test.js new file mode 100644 index 0000000000..85c20267b6 --- /dev/null +++ b/translate/test/quickstart.test.js @@ -0,0 +1,38 @@ +// 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'; + +const proxyquire = require(`proxyquire`).noCallThru(); + +describe(`translate:quickstart`, () => { + let translateMock, TranslateMock; + + before(() => { + translateMock = { + translate: sinon.stub().yields(null, `Привет мир!`, {}) + }; + TranslateMock = sinon.stub().returns(translateMock); + }); + + it(`should translate a string`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/translate': TranslateMock + }); + + assert.equal(TranslateMock.calledOnce, true); + assert.deepEqual(TranslateMock.firstCall.args, [{ key: 'YOUR_API_KEY' }]); + assert.equal(translateMock.translate.calledOnce, true); + assert.deepEqual(translateMock.translate.firstCall.args.slice(0, -1), ['Hello, world!', 'ru']); + }); +});