Skip to content

Commit

Permalink
Adds 6 quickstart examples (#218)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
jmdobry authored and Ace Nassri committed Nov 17, 2022
1 parent e7416cf commit cc472b9
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
29 changes: 29 additions & 0 deletions translate/quickstart.js
Original file line number Diff line number Diff line change
@@ -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]
46 changes: 46 additions & 0 deletions translate/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -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
});
});
});
38 changes: 38 additions & 0 deletions translate/test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -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']);
});
});

0 comments on commit cc472b9

Please sign in to comment.