From f87553e21f88cd9b3ad83f5fc02d0705b79f31a0 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. --- speech/package.json | 4 +- speech/quickstart.js | 48 ++++++++++++++++++++++ speech/system-test/quickstart.test.js | 58 +++++++++++++++++++++++++++ speech/test/quickstart.test.js | 49 ++++++++++++++++++++++ 4 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 speech/quickstart.js create mode 100644 speech/system-test/quickstart.test.js create mode 100644 speech/test/quickstart.test.js diff --git a/speech/package.json b/speech/package.json index 37cafd9fdd..b2a31de679 100644 --- a/speech/package.json +++ b/speech/package.json @@ -11,10 +11,10 @@ "dependencies": { "@google-cloud/speech": "^0.1.1", "node-record-lpcm16": "^0.1.4", - "yargs": "^5.0.0" + "yargs": "^6.0.0" }, "devDependencies": { - "mocha": "^3.0.2" + "mocha": "^3.1.0" }, "engines": { "node": ">=4.3.2" diff --git a/speech/quickstart.js b/speech/quickstart.js new file mode 100644 index 0000000000..fa28527ca3 --- /dev/null +++ b/speech/quickstart.js @@ -0,0 +1,48 @@ +/** + * 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 speech_quickstart] +// Imports the Google Cloud client library +const Speech = require('@google-cloud/speech'); + +// Your Google Cloud Platform project ID +const projectId = 'YOUR_PROJECT_ID'; + +// Instantiates a client +const speechClient = Speech({ + projectId: projectId +}); + +// The name of the audio file to transcribe +const fileName = './resources/audio.raw'; + +// The audio file's encoding and sample rate +const options = { + encoding: 'LINEAR16', + sampleRate: 16000 +}; + +// Detects speech in the audio file +speechClient.recognize(fileName, options, (err, result) => { + if (err) { + console.error(err); + return; + } + + console.log(`Transcription: ${result}`); +}); +// [END speech_quickstart] diff --git a/speech/system-test/quickstart.test.js b/speech/system-test/quickstart.test.js new file mode 100644 index 0000000000..1dbc4b5af1 --- /dev/null +++ b/speech/system-test/quickstart.test.js @@ -0,0 +1,58 @@ +/** + * 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 path = require(`path`); +const proxyquire = require(`proxyquire`).noPreserveCache(); +const speech = proxyquire(`@google-cloud/speech`, {})(); + +const fileName = path.join(__dirname, `../resources/audio.raw`); +const config = { + encoding: `LINEAR16`, + sampleRate: 16000 +}; + +describe(`speech:quickstart`, () => { + let speechMock, SpeechMock; + + it(`should detect speech`, (done) => { + const expectedFileName = `./resources/audio.raw`; + const expectedText = `how old is the Brooklyn Bridge`; + + speechMock = { + recognize: (_fileName, _config, _callback) => { + assert.equal(_fileName, expectedFileName); + assert.deepEqual(_config, config); + assert.equal(typeof _callback, `function`); + + speech.recognize(fileName, config, (err, transcription, apiResponse) => { + _callback(err, transcription, apiResponse); + assert.ifError(err); + assert.equal(transcription, expectedText); + assert.notEqual(apiResponse, undefined); + assert.equal(console.log.calledOnce, true); + assert.deepEqual(console.log.firstCall.args, [`Transcription: ${expectedText}`]); + done(); + }); + } + }; + SpeechMock = sinon.stub().returns(speechMock); + + proxyquire(`../quickstart`, { + '@google-cloud/speech': SpeechMock + }); + }); +}); diff --git a/speech/test/quickstart.test.js b/speech/test/quickstart.test.js new file mode 100644 index 0000000000..807e23e507 --- /dev/null +++ b/speech/test/quickstart.test.js @@ -0,0 +1,49 @@ +/** + * 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(); + +const config = { + encoding: 'LINEAR16', + sampleRate: 16000 +}; + +describe(`speech:quickstart`, () => { + let speechMock, SpeechMock; + const error = new Error(`error`); + const fileName = `./resources/audio.raw`; + + before(() => { + speechMock = { + recognize: sinon.stub().yields(error) + }; + SpeechMock = sinon.stub().returns(speechMock); + }); + + it(`should handle error`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/speech': SpeechMock + }); + + assert.equal(SpeechMock.calledOnce, true); + assert.deepEqual(SpeechMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(speechMock.recognize.calledOnce, true); + assert.deepEqual(speechMock.recognize.firstCall.args.slice(0, -1), [fileName, config]); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); + }); +});