From ac7cbf40ba63bbd51eafdf35918483c085fbdc24 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Mon, 3 Oct 2016 14:44:10 -0700 Subject: [PATCH] New quickstarts. (#226) * New quickstarts. * Address comments. --- cloud-language/snippets/package.json | 3 ++ cloud-language/snippets/quickstart.js | 43 ++++++++++++++++ .../snippets/system-test/quickstart.test.js | 51 +++++++++++++++++++ .../snippets/test/quickstart.test.js | 44 ++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 cloud-language/snippets/quickstart.js create mode 100644 cloud-language/snippets/system-test/quickstart.test.js create mode 100644 cloud-language/snippets/test/quickstart.test.js diff --git a/cloud-language/snippets/package.json b/cloud-language/snippets/package.json index 284be2a837..c5ac342fd0 100644 --- a/cloud-language/snippets/package.json +++ b/cloud-language/snippets/package.json @@ -16,5 +16,8 @@ "devDependencies": { "mocha": "^3.0.2", "node-uuid": "^1.4.7" + }, + "engines": { + "node": ">=4.3.2" } } diff --git a/cloud-language/snippets/quickstart.js b/cloud-language/snippets/quickstart.js new file mode 100644 index 0000000000..ac595aa563 --- /dev/null +++ b/cloud-language/snippets/quickstart.js @@ -0,0 +1,43 @@ +/** + * 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'; + +// [START language_quickstart] +// Imports the Google Cloud client library +const Language = require('@google-cloud/language'); + +// Your Google Cloud Platform project ID +const projectId = 'YOUR_PROJECT_ID'; + +// Instantiates a client +const languageClient = Language({ + projectId: projectId +}); + +// The text to analyze +const text = 'Hello, world!'; + +// Detects the sentiment of the text +languageClient.detectSentiment(text, { verbose: true }, (err, sentiment) => { + if (err) { + console.error(err); + return; + } + + console.log('Text: %s', text); + console.log('Sentiment: %j', sentiment); +}); +// [END language_quickstart] diff --git a/cloud-language/snippets/system-test/quickstart.test.js b/cloud-language/snippets/system-test/quickstart.test.js new file mode 100644 index 0000000000..6c68b79400 --- /dev/null +++ b/cloud-language/snippets/system-test/quickstart.test.js @@ -0,0 +1,51 @@ +/** + * 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`).noPreserveCache(); +const language = proxyquire(`@google-cloud/language`, {})(); + +describe(`language:quickstart`, () => { + let languageMock, LanguageMock; + + it(`should detect sentiment`, (done) => { + const expectedText = `Hello, world!`; + + languageMock = { + detectSentiment: (_text, _config, _callback) => { + assert.equal(_text, expectedText); + assert.deepEqual(_config, { verbose: true }); + assert.equal(typeof _callback, 'function'); + + language.detectSentiment(_text, _config, (err, sentiment, apiResponse) => { + _callback(err, sentiment, apiResponse); + assert.ifError(err); + assert.equal(typeof sentiment, 'object'); + assert.notEqual(apiResponse, undefined); + assert.equal(console.log.calledTwice, true); + assert.deepEqual(console.log.firstCall.args, [`Text: %s`, expectedText]); + assert.deepEqual(console.log.secondCall.args, [`Sentiment: %j`, sentiment]); + done(); + }); + } + }; + LanguageMock = sinon.stub().returns(languageMock); + + proxyquire(`../quickstart`, { + '@google-cloud/language': LanguageMock + }); + }); +}); diff --git a/cloud-language/snippets/test/quickstart.test.js b/cloud-language/snippets/test/quickstart.test.js new file mode 100644 index 0000000000..df0519c73c --- /dev/null +++ b/cloud-language/snippets/test/quickstart.test.js @@ -0,0 +1,44 @@ +/** + * 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(`language:quickstart`, () => { + let languageMock, LanguageMock; + const error = new Error(`error`); + const text = 'Hello, world!'; + + before(() => { + languageMock = { + detectSentiment: sinon.stub().yields(error) + }; + LanguageMock = sinon.stub().returns(languageMock); + }); + + it(`should handle error`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/language': LanguageMock + }); + + assert.equal(LanguageMock.calledOnce, true); + assert.deepEqual(LanguageMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(languageMock.detectSentiment.calledOnce, true); + assert.deepEqual(languageMock.detectSentiment.firstCall.args.slice(0, -1), [text, { verbose: true }]); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); + }); +});