Skip to content

Commit

Permalink
New quickstarts. (#226)
Browse files Browse the repository at this point in the history
* New quickstarts.

* Address comments.
  • Loading branch information
jmdobry authored and Ace Nassri committed Oct 3, 2016
1 parent 11f01a6 commit 0f4e2e8
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cloud-language/snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"devDependencies": {
"mocha": "^3.0.2",
"node-uuid": "^1.4.7"
},
"engines": {
"node": ">=4.3.2"
}
}
43 changes: 43 additions & 0 deletions cloud-language/snippets/quickstart.js
Original file line number Diff line number Diff line change
@@ -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]
51 changes: 51 additions & 0 deletions cloud-language/snippets/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -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
});
});
});
44 changes: 44 additions & 0 deletions cloud-language/snippets/test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -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]);
});
});

0 comments on commit 0f4e2e8

Please sign in to comment.