diff --git a/bigquery/quickstart.js b/bigquery/quickstart.js new file mode 100644 index 0000000000..af2dac581f --- /dev/null +++ b/bigquery/quickstart.js @@ -0,0 +1,29 @@ +// 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 bigquery_quickstart] +// Imports and instantiates the Google Cloud client library +// for Google BigQuery +const bigquery = require('@google-cloud/bigquery')({ + projectId: 'YOUR_PROJECT_ID' +}); + +// Creates a new dataset +bigquery.createDataset('my_new_dataset', (err, dataset) => { + if (!err) { + // The dataset was created successfully + } +}); +// [END bigquery_quickstart] diff --git a/bigquery/system-test/quickstart.test.js b/bigquery/system-test/quickstart.test.js new file mode 100644 index 0000000000..9da54b1ebc --- /dev/null +++ b/bigquery/system-test/quickstart.test.js @@ -0,0 +1,50 @@ +// 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 bigquery = proxyquire(`@google-cloud/bigquery`, {})(); + +const datasetName = `my_new_dataset`; + +describe(`bigquery:quickstart`, () => { + let bigqueryMock, BigqueryMock; + + after((done) => { + bigquery.dataset(datasetName).delete(() => { + // Ignore any error, the dataset might not have been created + done(); + }); + }); + + it(`should create a dataset`, (done) => { + bigqueryMock = { + createDataset: (_datasetName) => { + assert.equal(_datasetName, datasetName); + + bigquery.createDataset(datasetName, (err, dataset, apiResponse) => { + assert.ifError(err); + assert.notEqual(dataset, undefined); + assert.notEqual(apiResponse, undefined); + done(); + }); + } + }; + BigqueryMock = sinon.stub().returns(bigqueryMock); + + proxyquire(`../quickstart`, { + '@google-cloud/bigquery': BigqueryMock + }); + }); +}); diff --git a/bigquery/test/quickstart.test.js b/bigquery/test/quickstart.test.js new file mode 100644 index 0000000000..487d32208c --- /dev/null +++ b/bigquery/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(`bigquery:quickstart`, () => { + let bigqueryMock, BigqueryMock; + + before(() => { + bigqueryMock = { + createDataset: sinon.stub().yields(null, {}, {}) + }; + BigqueryMock = sinon.stub().returns(bigqueryMock); + }); + + it(`should create a dataset`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/bigquery': BigqueryMock + }); + + assert.equal(BigqueryMock.calledOnce, true); + assert.deepEqual(BigqueryMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(bigqueryMock.createDataset.calledOnce, true); + assert.deepEqual(bigqueryMock.createDataset.firstCall.args.slice(0, -1), ['my_new_dataset']); + }); +}); diff --git a/datastore/quickstart.js b/datastore/quickstart.js new file mode 100644 index 0000000000..05feb6dc0a --- /dev/null +++ b/datastore/quickstart.js @@ -0,0 +1,31 @@ +// 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 datastore_quickstart] +// Imports and instantiates the Google Cloud client library +// for Google Cloud Datastore +const datastore = require('@google-cloud/datastore')({ + projectId: 'YOUR_PROJECT_ID' +}); + +const taskKey = datastore.key(['Task', 1234]); + +// Retrieves an entity +datastore.get(taskKey, (err, entity) => { + if (!err) { + // The entity was retrieved successfully + } +}); +// [END datastore_quickstart] diff --git a/datastore/system-test/quickstart.test.js b/datastore/system-test/quickstart.test.js new file mode 100644 index 0000000000..2eb1ac9d65 --- /dev/null +++ b/datastore/system-test/quickstart.test.js @@ -0,0 +1,69 @@ +// 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 datastore = proxyquire(`@google-cloud/datastore`, {})(); +const kind = `Task`; +const message = `Buy milk`; +const key = datastore.key(kind); + +describe(`datastore:quickstart`, () => { + let datastoreMock, DatastoreMock; + + before((done) => { + datastore.save({ + key: key, + data: { + message: message + } + }, () => { + // Datastore is eventually consistent + setTimeout(done, 5000); + }); + }); + + after((done) => { + datastore.delete(key, () => { + // Ignore any error, the entity might not have been created + done(); + }); + }); + + it(`should get a task from Datastore`, (done) => { + datastoreMock = { + key: () => { + return key; + }, + + get: (_key) => { + assert.equal(_key, key); + + datastore.get(_key, (err, entity) => { + assert.ifError(err); + assert.notEqual(entity, undefined); + assert.notEqual(entity.key, undefined); + assert.equal(entity.key.kind, kind); + assert.deepEqual(entity.data, { message: message }); + done(); + }); + } + }; + DatastoreMock = sinon.stub().returns(datastoreMock); + + proxyquire(`../quickstart`, { + '@google-cloud/datastore': DatastoreMock + }); + }); +}); diff --git a/datastore/test/quickstart.test.js b/datastore/test/quickstart.test.js new file mode 100644 index 0000000000..e793399f1b --- /dev/null +++ b/datastore/test/quickstart.test.js @@ -0,0 +1,39 @@ +// 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(); + +describe(`datastore:quickstart`, () => { + let datastoreMock, DatastoreMock; + + before(() => { + datastoreMock = { + get: sinon.stub().yields(null, { key: 1234 }), + key: sinon.stub.returns(`task/1234`) + }; + DatastoreMock = sinon.stub().returns(datastoreMock); + }); + + it(`should get a task from Datastore`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/datastore': DatastoreMock + }); + + assert.equal(DatastoreMock.calledOnce, true); + assert.deepEqual(DatastoreMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(datastoreMock.get.calledOnce, true); + assert.deepEqual(datastoreMock.get.firstCall.args.slice(0, -1), [datastoreMock.key(['Task', 1234])]); + }); +}); diff --git a/logging/quickstart.js b/logging/quickstart.js new file mode 100644 index 0000000000..0257750986 --- /dev/null +++ b/logging/quickstart.js @@ -0,0 +1,34 @@ +// 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 logging_quickstart] +// Imports and instantiates the Google Cloud client library +// for Stackdriver Logging +const logging = require('@google-cloud/logging')({ + projectId: 'YOUR_PROJECT_ID' +}); + +// Selects the log to write to +const log = logging.log('my-log'); +// Prepares a log entry +const entry = log.entry({ type: 'global' }, 'Hello, world!'); + +// Writes the log entry +log.write(entry, (err) => { + if (!err) { + // The entry was logged successfully + } +}); +// [END logging_quickstart] diff --git a/logging/system-test/quickstart.test.js b/logging/system-test/quickstart.test.js new file mode 100644 index 0000000000..179493c449 --- /dev/null +++ b/logging/system-test/quickstart.test.js @@ -0,0 +1,62 @@ +// 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 logging = proxyquire(`@google-cloud/logging`, {})(); +const uuid = require(`node-uuid`); + +const logName = `nodejs-docs-samples-test-${uuid.v4()}`; + +describe(`logging:quickstart`, () => { + let logMock, loggingMock, LoggingMock; + + after((done) => { + logging.log(logName).delete(() => { + // Ignore any error, the topic might not have been created + done(); + }); + }); + + it(`should log an entry`, (done) => { + const expectedlogName = `my-log`; + + logMock = { + entry: sinon.stub().returns({}), + write: (_entry) => { + assert.deepEqual(_entry, {}); + + const log = logging.log(logName); + const entry = log.entry({ type: `global` }, `Hello, world!`); + log.write(entry, (err, apiResponse) => { + assert.ifError(err); + assert.notEqual(apiResponse, undefined); + // Logs are eventually consistent + setTimeout(done, 5000); + }); + } + }; + loggingMock = { + log: (_logName) => { + assert.equal(_logName, expectedlogName); + return logMock; + } + }; + LoggingMock = sinon.stub().returns(loggingMock); + + proxyquire(`../quickstart`, { + '@google-cloud/logging': LoggingMock + }); + }); +}); diff --git a/logging/test/quickstart.test.js b/logging/test/quickstart.test.js new file mode 100644 index 0000000000..c5f4a7b953 --- /dev/null +++ b/logging/test/quickstart.test.js @@ -0,0 +1,50 @@ +// 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(`logging:quickstart`, () => { + let logMock, loggingMock, LoggingMock; + + const expectedLogName = `my-log`; + const expectedResource = { type: `global` }; + const expectedMessage = `Hello, world!`; + + before(() => { + logMock = { + entry: sinon.stub().returns({}), + write: sinon.stub().yields(null, {}) + }; + loggingMock = { + log: sinon.stub().returns(logMock) + }; + LoggingMock = sinon.stub().returns(loggingMock); + }); + + it(`should log an entry`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/logging': LoggingMock + }); + + assert.equal(LoggingMock.calledOnce, true); + assert.deepEqual(LoggingMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(loggingMock.log.calledOnce, true); + assert.deepEqual(loggingMock.log.firstCall.args, [expectedLogName]); + assert.equal(logMock.entry.calledOnce, true); + assert.deepEqual(logMock.entry.firstCall.args, [expectedResource, expectedMessage]); + assert.equal(logMock.write.calledOnce, true); + assert.deepEqual(logMock.write.firstCall.args.slice(0, -1), [{}]); + }); +}); diff --git a/pubsub/quickstart.js b/pubsub/quickstart.js new file mode 100644 index 0000000000..c6fd49246b --- /dev/null +++ b/pubsub/quickstart.js @@ -0,0 +1,29 @@ +// 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 pubsub_quickstart] +// Imports and instantiates the Google Cloud client library +// for Google Cloud Pub/Sub +const pubsub = require('@google-cloud/pubsub')({ + projectId: 'YOUR_PROJECT_ID' +}); + +// Creates a new topic +pubsub.createTopic('my-new-topic', (err, topic) => { + if (!err) { + // The topic was created successfully + } +}); +// [END pubsub_quickstart] diff --git a/pubsub/system-test/quickstart.test.js b/pubsub/system-test/quickstart.test.js new file mode 100644 index 0000000000..858b6abe54 --- /dev/null +++ b/pubsub/system-test/quickstart.test.js @@ -0,0 +1,56 @@ +// 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 pubsub = proxyquire(`@google-cloud/pubsub`, {})(); +const uuid = require(`node-uuid`); + +const topicName = `nodejs-docs-samples-test-${uuid.v4()}`; +const projectId = process.env.GCLOUD_PROJECT; +const fullTopicName = `projects/${projectId}/topics/${topicName}`; + +describe(`pubsub:quickstart`, () => { + let pubsubMock, PubSubMock; + + after((done) => { + pubsub.topic(topicName).delete(() => { + // Ignore any error, the topic might not have been created + done(); + }); + }); + + it(`should create a topic`, (done) => { + const expectedTopicName = `my-new-topic`; + + pubsubMock = { + createTopic: (_topicName) => { + assert.equal(_topicName, expectedTopicName); + + pubsub.createTopic(topicName, (err, topic, apiResponse) => { + assert.ifError(err); + assert.notEqual(topic, undefined); + assert.equal(topic.name, fullTopicName); + assert.notEqual(apiResponse, undefined); + done(); + }); + } + }; + PubSubMock = sinon.stub().returns(pubsubMock); + + proxyquire(`../quickstart`, { + '@google-cloud/pubsub': PubSubMock + }); + }); +}); diff --git a/pubsub/test/quickstart.test.js b/pubsub/test/quickstart.test.js new file mode 100644 index 0000000000..9bd1f639b6 --- /dev/null +++ b/pubsub/test/quickstart.test.js @@ -0,0 +1,40 @@ +// 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(`pubsub:quickstart`, () => { + let pubsubMock, PubSubMock; + + const expectedTopicName = `my-new-topic`; + + before(() => { + pubsubMock = { + createTopic: sinon.stub().yields(null, {}, {}) + }; + PubSubMock = sinon.stub().returns(pubsubMock); + }); + + it(`should create a topic`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/pubsub': PubSubMock + }); + + assert.equal(PubSubMock.calledOnce, true); + assert.deepEqual(PubSubMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(pubsubMock.createTopic.calledOnce, true); + assert.deepEqual(pubsubMock.createTopic.firstCall.args.slice(0, -1), [expectedTopicName]); + }); +}); diff --git a/storage/quickstart.js b/storage/quickstart.js new file mode 100644 index 0000000000..012344afe3 --- /dev/null +++ b/storage/quickstart.js @@ -0,0 +1,29 @@ +// 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 storage_quickstart] +// Imports and instantiates the Google Cloud client library +// for Google Cloud Storage +const storage = require('@google-cloud/storage')({ + projectId: 'YOUR_PROJECT_ID' +}); + +// Creates a new bucket +storage.createBucket('my-new-bucket', (err, bucket) => { + if (!err) { + // The bucket was created successfully + } +}); +// [END storage_quickstart] diff --git a/storage/system-test/quickstart.test.js b/storage/system-test/quickstart.test.js new file mode 100644 index 0000000000..bf2a022ffc --- /dev/null +++ b/storage/system-test/quickstart.test.js @@ -0,0 +1,54 @@ +// 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 storage = proxyquire(`@google-cloud/storage`, {})(); +const uuid = require(`node-uuid`); + +const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; + +describe(`storage:quickstart`, () => { + let storageMock, StorageMock; + + after((done) => { + storage.bucket(bucketName).delete(() => { + // Ignore any error, the topic might not have been created + done(); + }); + }); + + it(`should create a topic`, (done) => { + const expectedBucketName = `my-new-bucket`; + + storageMock = { + createBucket: (_bucketName) => { + assert.equal(_bucketName, expectedBucketName); + + storage.createBucket(bucketName, (err, bucket, apiResponse) => { + assert.ifError(err); + assert.notEqual(bucket, undefined); + assert.equal(bucket.name, bucketName); + assert.notEqual(apiResponse, undefined); + done(); + }); + } + }; + StorageMock = sinon.stub().returns(storageMock); + + proxyquire(`../quickstart`, { + '@google-cloud/storage': StorageMock + }); + }); +}); diff --git a/storage/test/quickstart.test.js b/storage/test/quickstart.test.js new file mode 100644 index 0000000000..7e37dd84e5 --- /dev/null +++ b/storage/test/quickstart.test.js @@ -0,0 +1,40 @@ +// 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(`storage:quickstart`, () => { + let storageMock, StorageMock; + + const expectedBucketName = `my-new-bucket`; + + before(() => { + storageMock = { + createBucket: sinon.stub().yields(null, {}, {}) + }; + StorageMock = sinon.stub().returns(storageMock); + }); + + it(`should create a bucket`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/storage': StorageMock + }); + + assert.equal(StorageMock.calledOnce, true); + assert.deepEqual(StorageMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(storageMock.createBucket.calledOnce, true); + assert.deepEqual(storageMock.createBucket.firstCall.args.slice(0, -1), [expectedBucketName]); + }); +}); 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']); + }); +});