From 60cde2dc18b570f2bdfa7be3028237bc9956fe0f Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 13 Sep 2016 18:33:18 -0700 Subject: [PATCH 1/8] Add some simple "quickstart" samples. --- bigquery/quickstart.js | 26 +++++++++++ datastore/quickstart.js | 28 ++++++++++++ logging/quickstart.js | 29 ++++++++++++ logging/system-test/quickstart.test.js | 62 ++++++++++++++++++++++++++ logging/test/quickstart.test.js | 50 +++++++++++++++++++++ pubsub/quickstart.js | 26 +++++++++++ pubsub/system-test/quickstart.test.js | 56 +++++++++++++++++++++++ pubsub/test/quickstart.test.js | 40 +++++++++++++++++ storage/quickstart.js | 26 +++++++++++ storage/system-test/quickstart.test.js | 54 ++++++++++++++++++++++ storage/test/quickstart.test.js | 40 +++++++++++++++++ translate/quickstart.js | 26 +++++++++++ 12 files changed, 463 insertions(+) create mode 100644 bigquery/quickstart.js create mode 100644 datastore/quickstart.js create mode 100644 logging/quickstart.js create mode 100644 logging/system-test/quickstart.test.js create mode 100644 logging/test/quickstart.test.js create mode 100644 pubsub/quickstart.js create mode 100644 pubsub/system-test/quickstart.test.js create mode 100644 pubsub/test/quickstart.test.js create mode 100644 storage/quickstart.js create mode 100644 storage/system-test/quickstart.test.js create mode 100644 storage/test/quickstart.test.js create mode 100644 translate/quickstart.js diff --git a/bigquery/quickstart.js b/bigquery/quickstart.js new file mode 100644 index 0000000000..bf1f07d190 --- /dev/null +++ b/bigquery/quickstart.js @@ -0,0 +1,26 @@ +// 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 quickstart] +var bigquery = require('@google-cloud/bigquery')({ + projectId: 'YOUR_PROJECT_ID' +}); + +bigquery.createDataset('my_new_dataset', function (err, dataset, apiResponse) { + if (!err) { + // The dataset was created successfully. + } +}); +// [END quickstart] diff --git a/datastore/quickstart.js b/datastore/quickstart.js new file mode 100644 index 0000000000..5f7304f87a --- /dev/null +++ b/datastore/quickstart.js @@ -0,0 +1,28 @@ +// 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 quickstart] +var datastore = require('@google-cloud/datastore')({ + projectId: 'YOUR_PROJECT_ID' +}); + +var taskKey = datastore.key(['Task', 1234]); + +datastore.get(taskKey, function (err, entity, apiResponse) { + if (!err) { + // The entity was retrieved successfully. + } +}); +// [END quickstart] diff --git a/logging/quickstart.js b/logging/quickstart.js new file mode 100644 index 0000000000..6f292ecb67 --- /dev/null +++ b/logging/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 quickstart] +var logging = require('@google-cloud/logging')({ + projectId: 'YOUR_PROJECT_ID' +}); + +var log = logging.log('my-log'); +var entry = log.entry({ type: 'global' }, 'Hello, world!'); + +log.write(entry, function (err, apiResponse) { + if (!err) { + // The entry was logged successfully. + } +}); +// [END quickstart] diff --git a/logging/system-test/quickstart.test.js b/logging/system-test/quickstart.test.js new file mode 100644 index 0000000000..9bedfeaa72 --- /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'; + +var proxyquire = require('proxyquire').noPreserveCache(); +var logging = proxyquire('@google-cloud/logging', {})(); +var uuid = require('node-uuid'); + +var logName = 'nodejs-docs-samples-test-' + uuid.v4(); + +describe.only('logging:quickstart', function () { + var logMock, loggingMock, LoggingMock; + + after(function (done) { + logging.log(logName).delete(function () { + // Ignore any error, the topic might not have been created + done(); + }); + }); + + it('should log an entry', function (done) { + var expectedlogName = 'my-log'; + + logMock = { + entry: sinon.stub().returns({}), + write: function (_entry) { + assert.deepEqual(_entry, {}); + + var log = logging.log(logName); + var entry = log.entry({ type: 'global' }, 'Hello, world!'); + log.write(entry, function (err, apiResponse) { + assert.ifError(err); + assert.notEqual(apiResponse, undefined); + // Logs are eventually consistent + setTimeout(done, 5000); + }); + } + }; + loggingMock = { + log: function (_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..c9ce31acf1 --- /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'; + +var proxyquire = require('proxyquire').noCallThru(); + +describe('logging:quickstart', function () { + var logMock, loggingMock, LoggingMock; + + var expectedLogName = 'my-log'; + var expectedResource = { type: 'global' }; + var expectedMessage = 'Hello, world!'; + + before(function () { + 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', function () { + proxyquire('../quickstart', { + '@google-cloud/logging': LoggingMock + }); + + assert.equal(LoggingMock.calledOnce, true); + assert.deepEqual(LoggingMock.firstCall.args, []); + 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..bda48960e4 --- /dev/null +++ b/pubsub/quickstart.js @@ -0,0 +1,26 @@ +// 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 quickstart] +var pubsub = require('@google-cloud/pubsub')({ + projectId: 'YOUR_PROJECT_ID' +}); + +pubsub.createTopic('my-new-topic', function (err, topic, apiResponse) { + if (!err) { + // The topic was created successfully. + } +}); +// [END quickstart] diff --git a/pubsub/system-test/quickstart.test.js b/pubsub/system-test/quickstart.test.js new file mode 100644 index 0000000000..3bb77f2ec9 --- /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'; + +var proxyquire = require('proxyquire').noPreserveCache(); +var pubsub = proxyquire('@google-cloud/pubsub', {})(); +var uuid = require('node-uuid'); + +var topicName = 'nodejs-docs-samples-test-' + uuid.v4(); +var projectId = process.env.GCLOUD_PROJECT; +var fullTopicName = 'projects/' + projectId + '/topics/' + topicName; + +describe('pubsub:quickstart', function () { + var pubsubMock, PubSubMock; + + after(function (done) { + pubsub.topic(topicName).delete(function () { + // Ignore any error, the topic might not have been created + done(); + }); + }); + + it('should create a topic', function (done) { + var expectedTopicName = 'my-new-topic'; + + pubsubMock = { + createTopic: function (_topicName) { + assert.equal(_topicName, expectedTopicName); + + pubsub.createTopic(topicName, function (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..cdd3a49273 --- /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'; + +var proxyquire = require('proxyquire').noCallThru(); + +describe('pubsub:quickstart', function () { + var pubsubMock, PubSubMock; + + var expectedTopicName = 'my-new-topic'; + + before(function () { + pubsubMock = { + createTopic: sinon.stub().yields(null, {}, {}) + }; + PubSubMock = sinon.stub().returns(pubsubMock); + }); + + it('should create a topic', function () { + proxyquire('../quickstart', { + '@google-cloud/pubsub': PubSubMock + }); + + assert.equal(PubSubMock.calledOnce, true); + assert.deepEqual(PubSubMock.firstCall.args, []); + 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..43e846fb1e --- /dev/null +++ b/storage/quickstart.js @@ -0,0 +1,26 @@ +// 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 quickstart] +var storage = require('@google-cloud/storage')({ + projectId: 'YOUR_PROJECT_ID' +}); + +storage.createBucket('my-new-bucket', function (err, bucket, apiResponse) { + if (!err) { + // The bucket was created successfully. + } +}); +// [END quickstart] diff --git a/storage/system-test/quickstart.test.js b/storage/system-test/quickstart.test.js new file mode 100644 index 0000000000..a60a0cdf44 --- /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'; + +var proxyquire = require('proxyquire').noPreserveCache(); +var storage = proxyquire('@google-cloud/storage', {})(); +var uuid = require('node-uuid'); + +var bucketName = 'nodejs-docs-samples-test-' + uuid.v4(); + +describe('storage:quickstart', function () { + var storageMock, StorageMock; + + after(function (done) { + storage.bucket(bucketName).delete(function () { + // Ignore any error, the topic might not have been created + done(); + }); + }); + + it('should create a topic', function (done) { + var expectedBucketName = 'my-new-bucket'; + + storageMock = { + createBucket: function (_bucketName) { + assert.equal(_bucketName, expectedBucketName); + + storage.createBucket(bucketName, function (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..809e0017aa --- /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'; + +var proxyquire = require('proxyquire').noCallThru(); + +describe('storage:quickstart', function () { + var storageMock, StorageMock; + + var expectedBucketName = 'my-new-bucket'; + + before(function () { + storageMock = { + createBucket: sinon.stub().yields(null, {}, {}) + }; + StorageMock = sinon.stub().returns(storageMock); + }); + + it('should create a bucket', function () { + proxyquire('../quickstart', { + '@google-cloud/storage': StorageMock + }); + + assert.equal(StorageMock.calledOnce, true); + assert.deepEqual(StorageMock.firstCall.args, []); + 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..732cc739fa --- /dev/null +++ b/translate/quickstart.js @@ -0,0 +1,26 @@ +// 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 quickstart] +var translate = require('@google-cloud/translate')({ + key: 'YOUR_API_KEY' +}); + +translate.translate('Hello, world!', 'ru', function (err, translation, apiResponse) { + if (!err) { + // The text was translated successfully. + } +}); +// [END quickstart] From 735260d75dcd37fd46748b64bbdc02ba39c84be2 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Tue, 20 Sep 2016 14:06:16 -0700 Subject: [PATCH 2/8] Add quickstart tests (#215) * First draft of new tests * Fix failing tests * Fix comments --- bigquery/system-test/quickstart.test.js | 50 +++++++++++++++++ bigquery/test/quickstart.test.js | 38 +++++++++++++ datastore/system-test/quickstart.test.js | 69 ++++++++++++++++++++++++ datastore/test/quickstart.test.js | 39 ++++++++++++++ logging/test/quickstart.test.js | 2 +- pubsub/test/quickstart.test.js | 2 +- storage/test/quickstart.test.js | 2 +- translate/system-test/quickstart.test.js | 46 ++++++++++++++++ translate/test/quickstart.test.js | 38 +++++++++++++ 9 files changed, 283 insertions(+), 3 deletions(-) create mode 100644 bigquery/system-test/quickstart.test.js create mode 100644 bigquery/test/quickstart.test.js create mode 100644 datastore/system-test/quickstart.test.js create mode 100644 datastore/test/quickstart.test.js create mode 100644 translate/system-test/quickstart.test.js create mode 100644 translate/test/quickstart.test.js diff --git a/bigquery/system-test/quickstart.test.js b/bigquery/system-test/quickstart.test.js new file mode 100644 index 0000000000..11225a22e9 --- /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'; + +var proxyquire = require('proxyquire').noPreserveCache(); +var bigquery = proxyquire('@google-cloud/bigquery', {})(); + +var datasetName = 'my_new_dataset'; + +describe('bigquery:quickstart', function () { + var bigqueryMock, BigqueryMock; + + after(function (done) { + bigquery.dataset(datasetName).delete(function () { + // Ignore any error, the dataset might not have been created + done(); + }); + }); + + it('should create a dataset', function (done) { + bigqueryMock = { + createDataset: function (_datasetName) { + assert.equal(_datasetName, datasetName); + + bigquery.createDataset(datasetName, function (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..409b1c87c9 --- /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'; + +var proxyquire = require('proxyquire').noCallThru(); + +describe('bigquery:quickstart', function () { + var bigqueryMock, BigqueryMock; + + before(function () { + bigqueryMock = { + createDataset: sinon.stub().yields(null, {}, {}) + }; + BigqueryMock = sinon.stub().returns(bigqueryMock); + }); + + it('should create a dataset', function () { + 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/system-test/quickstart.test.js b/datastore/system-test/quickstart.test.js new file mode 100644 index 0000000000..4a0266f9d1 --- /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'; + +var proxyquire = require('proxyquire').noPreserveCache(); +var datastore = proxyquire('@google-cloud/datastore', {})(); +var kind = 'Task'; +var message = 'Buy milk'; +var key = datastore.key(kind); + +describe('datastore:quickstart', function () { + var datastoreMock, DatastoreMock; + + before(function (done) { + datastore.save({ + key: key, + data: { + message: message + } + }, function () { + // Datastore is eventually consistent + setTimeout(done, 5000); + }); + }); + + after(function (done) { + datastore.delete(key, function () { + // Ignore any error, the entity might not have been created + done(); + }); + }); + + it('should get a task from Datastore', function (done) { + datastoreMock = { + key: function () { + return key; + }, + + get: function (_key) { + assert.equal(_key, key); + + datastore.get(_key, function (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..a81dff883b --- /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'; + +var proxyquire = require('proxyquire').noPreserveCache(); + +describe('datastore:quickstart', function () { + var datastoreMock, DatastoreMock; + + before(function () { + 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', function () { + 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/test/quickstart.test.js b/logging/test/quickstart.test.js index c9ce31acf1..23214d7e59 100644 --- a/logging/test/quickstart.test.js +++ b/logging/test/quickstart.test.js @@ -39,7 +39,7 @@ describe('logging:quickstart', function () { }); assert.equal(LoggingMock.calledOnce, true); - assert.deepEqual(LoggingMock.firstCall.args, []); + 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); diff --git a/pubsub/test/quickstart.test.js b/pubsub/test/quickstart.test.js index cdd3a49273..e809d968fb 100644 --- a/pubsub/test/quickstart.test.js +++ b/pubsub/test/quickstart.test.js @@ -33,7 +33,7 @@ describe('pubsub:quickstart', function () { }); assert.equal(PubSubMock.calledOnce, true); - assert.deepEqual(PubSubMock.firstCall.args, []); + 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/test/quickstart.test.js b/storage/test/quickstart.test.js index 809e0017aa..623634c317 100644 --- a/storage/test/quickstart.test.js +++ b/storage/test/quickstart.test.js @@ -33,7 +33,7 @@ describe('storage:quickstart', function () { }); assert.equal(StorageMock.calledOnce, true); - assert.deepEqual(StorageMock.firstCall.args, []); + 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/system-test/quickstart.test.js b/translate/system-test/quickstart.test.js new file mode 100644 index 0000000000..3e772411ae --- /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'; + +var proxyquire = require('proxyquire').noPreserveCache(); +var translate = proxyquire('@google-cloud/translate', {})({ + key: process.env.TRANSLATE_API_KEY +}); +var string = 'Hello, world!'; +var targetLanguage = 'ru'; + +describe('translate:quickstart', function () { + var translateMock, TranslateMock; + + it('should translate a string', function (done) { + translateMock = { + translate: function (_string, _targetLanguage) { + assert.equal(_string, string); + assert.equal(_targetLanguage, targetLanguage); + + translate.translate(_string, _targetLanguage, function (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..35f2dda6aa --- /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'; + +var proxyquire = require('proxyquire').noCallThru(); + +describe('translate:quickstart', function () { + var translateMock, TranslateMock; + + before(function () { + translateMock = { + translate: sinon.stub().yields(null, 'Привет мир!', {}) + }; + TranslateMock = sinon.stub().returns(translateMock); + }); + + it('should translate a string', function () { + 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']); + }); +}); From 6f199414f7d5504c3e307f63b6bbfba95c6877c4 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 20 Sep 2016 16:34:33 -0700 Subject: [PATCH 3/8] Add comments. --- bigquery/quickstart.js | 2 ++ datastore/quickstart.js | 2 ++ logging/quickstart.js | 6 +++++- pubsub/quickstart.js | 2 ++ storage/quickstart.js | 2 ++ 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bigquery/quickstart.js b/bigquery/quickstart.js index bf1f07d190..9c0d67a44c 100644 --- a/bigquery/quickstart.js +++ b/bigquery/quickstart.js @@ -14,10 +14,12 @@ 'use strict'; // [START quickstart] +// Import and instantiate the Google BigQuery client library var bigquery = require('@google-cloud/bigquery')({ projectId: 'YOUR_PROJECT_ID' }); +// Create a new BigQuery dataset bigquery.createDataset('my_new_dataset', function (err, dataset, apiResponse) { if (!err) { // The dataset was created successfully. diff --git a/datastore/quickstart.js b/datastore/quickstart.js index 5f7304f87a..78b5fad59f 100644 --- a/datastore/quickstart.js +++ b/datastore/quickstart.js @@ -14,12 +14,14 @@ 'use strict'; // [START quickstart] +// Import and instantiate the Google Cloud Datastore client library var datastore = require('@google-cloud/datastore')({ projectId: 'YOUR_PROJECT_ID' }); var taskKey = datastore.key(['Task', 1234]); +// Retrieve an entity from Cloud Datastore datastore.get(taskKey, function (err, entity, apiResponse) { if (!err) { // The entity was retrieved successfully. diff --git a/logging/quickstart.js b/logging/quickstart.js index 6f292ecb67..1deb612cd2 100644 --- a/logging/quickstart.js +++ b/logging/quickstart.js @@ -1,4 +1,4 @@ -// Copyright 2015-2016, Google, Inc. +// 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 @@ -14,13 +14,17 @@ 'use strict'; // [START quickstart] +// Import and instantiate the Stackdriver Logging client library var logging = require('@google-cloud/logging')({ projectId: 'YOUR_PROJECT_ID' }); +// Select the log to write to var log = logging.log('my-log'); +// Prepare a log entry var entry = log.entry({ type: 'global' }, 'Hello, world!'); +// Write the log entry log.write(entry, function (err, apiResponse) { if (!err) { // The entry was logged successfully. diff --git a/pubsub/quickstart.js b/pubsub/quickstart.js index bda48960e4..7a39d3e0c6 100644 --- a/pubsub/quickstart.js +++ b/pubsub/quickstart.js @@ -14,10 +14,12 @@ 'use strict'; // [START quickstart] +// Import and instantiate the Google Cloud Pub/Sub client library var pubsub = require('@google-cloud/pubsub')({ projectId: 'YOUR_PROJECT_ID' }); +// Create a new Cloud Pub/Sub topic pubsub.createTopic('my-new-topic', function (err, topic, apiResponse) { if (!err) { // The topic was created successfully. diff --git a/storage/quickstart.js b/storage/quickstart.js index 43e846fb1e..95ed1fd4aa 100644 --- a/storage/quickstart.js +++ b/storage/quickstart.js @@ -14,10 +14,12 @@ 'use strict'; // [START quickstart] +// Import and instantiate the Google Cloud Storage client library var storage = require('@google-cloud/storage')({ projectId: 'YOUR_PROJECT_ID' }); +// Create a new Cloud Storage bucket storage.createBucket('my-new-bucket', function (err, bucket, apiResponse) { if (!err) { // The bucket was created successfully. From 44d06788dee18c012641245593e5826af2b41c5d Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 20 Sep 2016 16:39:36 -0700 Subject: [PATCH 4/8] Update comments. --- bigquery/quickstart.js | 3 ++- datastore/quickstart.js | 3 ++- logging/quickstart.js | 3 ++- pubsub/quickstart.js | 3 ++- storage/quickstart.js | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/bigquery/quickstart.js b/bigquery/quickstart.js index 9c0d67a44c..ade92da894 100644 --- a/bigquery/quickstart.js +++ b/bigquery/quickstart.js @@ -14,7 +14,8 @@ 'use strict'; // [START quickstart] -// Import and instantiate the Google BigQuery client library +// Import and instantiate the Google Cloud client library +// for Google BigQuery var bigquery = require('@google-cloud/bigquery')({ projectId: 'YOUR_PROJECT_ID' }); diff --git a/datastore/quickstart.js b/datastore/quickstart.js index 78b5fad59f..3cb9662b81 100644 --- a/datastore/quickstart.js +++ b/datastore/quickstart.js @@ -14,7 +14,8 @@ 'use strict'; // [START quickstart] -// Import and instantiate the Google Cloud Datastore client library +// Import and instantiate the Google Cloud client library +// for Google Cloud Datastore var datastore = require('@google-cloud/datastore')({ projectId: 'YOUR_PROJECT_ID' }); diff --git a/logging/quickstart.js b/logging/quickstart.js index 1deb612cd2..c0e141e93c 100644 --- a/logging/quickstart.js +++ b/logging/quickstart.js @@ -14,7 +14,8 @@ 'use strict'; // [START quickstart] -// Import and instantiate the Stackdriver Logging client library +// Import and instantiate the Google Cloud client library +// for Stackdriver Logging var logging = require('@google-cloud/logging')({ projectId: 'YOUR_PROJECT_ID' }); diff --git a/pubsub/quickstart.js b/pubsub/quickstart.js index 7a39d3e0c6..0800276133 100644 --- a/pubsub/quickstart.js +++ b/pubsub/quickstart.js @@ -14,7 +14,8 @@ 'use strict'; // [START quickstart] -// Import and instantiate the Google Cloud Pub/Sub client library +// Import and instantiate the Google Cloud client library +// for Google Cloud Pub/Sub var pubsub = require('@google-cloud/pubsub')({ projectId: 'YOUR_PROJECT_ID' }); diff --git a/storage/quickstart.js b/storage/quickstart.js index 95ed1fd4aa..624023ec2c 100644 --- a/storage/quickstart.js +++ b/storage/quickstart.js @@ -14,7 +14,8 @@ 'use strict'; // [START quickstart] -// Import and instantiate the Google Cloud Storage client library +// Import and instantiate the Google Cloud client library +// for Google Cloud Storage var storage = require('@google-cloud/storage')({ projectId: 'YOUR_PROJECT_ID' }); From 2d8fbce7af9c3f9533158c9f093fcc7878d0cdc6 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 20 Sep 2016 16:47:48 -0700 Subject: [PATCH 5/8] Add another comment. --- translate/quickstart.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/translate/quickstart.js b/translate/quickstart.js index 732cc739fa..51b38f788d 100644 --- a/translate/quickstart.js +++ b/translate/quickstart.js @@ -14,10 +14,13 @@ 'use strict'; // [START quickstart] +// Import and instantiate the Google Cloud client library +// for the Google Translate API var translate = require('@google-cloud/translate')({ key: 'YOUR_API_KEY' }); +// Translate some text into Russian translate.translate('Hello, world!', 'ru', function (err, translation, apiResponse) { if (!err) { // The text was translated successfully. From 35115997826095aa7d1fdc55ca872b7dd352be7b Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 27 Sep 2016 11:46:54 -0700 Subject: [PATCH 6/8] Cleanup. --- bigquery/quickstart.js | 8 +++--- bigquery/system-test/quickstart.test.js | 22 ++++++++-------- bigquery/test/quickstart.test.js | 12 ++++----- datastore/quickstart.js | 10 ++++---- datastore/system-test/quickstart.test.js | 32 ++++++++++++------------ datastore/test/quickstart.test.js | 16 ++++++------ logging/quickstart.js | 16 ++++++------ logging/system-test/quickstart.test.js | 32 ++++++++++++------------ logging/test/quickstart.test.js | 18 ++++++------- pubsub/quickstart.js | 8 +++--- pubsub/system-test/quickstart.test.js | 30 +++++++++++----------- pubsub/test/quickstart.test.js | 14 +++++------ storage/quickstart.js | 8 +++--- storage/system-test/quickstart.test.js | 26 +++++++++---------- storage/test/quickstart.test.js | 14 +++++------ translate/quickstart.js | 8 +++--- translate/system-test/quickstart.test.js | 22 ++++++++-------- translate/test/quickstart.test.js | 14 +++++------ 18 files changed, 155 insertions(+), 155 deletions(-) diff --git a/bigquery/quickstart.js b/bigquery/quickstart.js index ade92da894..d641a2c837 100644 --- a/bigquery/quickstart.js +++ b/bigquery/quickstart.js @@ -16,14 +16,14 @@ // [START quickstart] // Import and instantiate the Google Cloud client library // for Google BigQuery -var bigquery = require('@google-cloud/bigquery')({ +const bigquery = require('@google-cloud/bigquery')({ projectId: 'YOUR_PROJECT_ID' }); -// Create a new BigQuery dataset -bigquery.createDataset('my_new_dataset', function (err, dataset, apiResponse) { +// Creates a new dataset +bigquery.createDataset('my_new_dataset', (err, dataset) => { if (!err) { - // The dataset was created successfully. + // The dataset was created successfully } }); // [END quickstart] diff --git a/bigquery/system-test/quickstart.test.js b/bigquery/system-test/quickstart.test.js index 11225a22e9..9da54b1ebc 100644 --- a/bigquery/system-test/quickstart.test.js +++ b/bigquery/system-test/quickstart.test.js @@ -13,27 +13,27 @@ 'use strict'; -var proxyquire = require('proxyquire').noPreserveCache(); -var bigquery = proxyquire('@google-cloud/bigquery', {})(); +const proxyquire = require(`proxyquire`).noPreserveCache(); +const bigquery = proxyquire(`@google-cloud/bigquery`, {})(); -var datasetName = 'my_new_dataset'; +const datasetName = `my_new_dataset`; -describe('bigquery:quickstart', function () { - var bigqueryMock, BigqueryMock; +describe(`bigquery:quickstart`, () => { + let bigqueryMock, BigqueryMock; - after(function (done) { - bigquery.dataset(datasetName).delete(function () { + after((done) => { + bigquery.dataset(datasetName).delete(() => { // Ignore any error, the dataset might not have been created done(); }); }); - it('should create a dataset', function (done) { + it(`should create a dataset`, (done) => { bigqueryMock = { - createDataset: function (_datasetName) { + createDataset: (_datasetName) => { assert.equal(_datasetName, datasetName); - bigquery.createDataset(datasetName, function (err, dataset, apiResponse) { + bigquery.createDataset(datasetName, (err, dataset, apiResponse) => { assert.ifError(err); assert.notEqual(dataset, undefined); assert.notEqual(apiResponse, undefined); @@ -43,7 +43,7 @@ describe('bigquery:quickstart', function () { }; BigqueryMock = sinon.stub().returns(bigqueryMock); - proxyquire('../quickstart', { + proxyquire(`../quickstart`, { '@google-cloud/bigquery': BigqueryMock }); }); diff --git a/bigquery/test/quickstart.test.js b/bigquery/test/quickstart.test.js index 409b1c87c9..487d32208c 100644 --- a/bigquery/test/quickstart.test.js +++ b/bigquery/test/quickstart.test.js @@ -13,20 +13,20 @@ 'use strict'; -var proxyquire = require('proxyquire').noCallThru(); +const proxyquire = require(`proxyquire`).noCallThru(); -describe('bigquery:quickstart', function () { - var bigqueryMock, BigqueryMock; +describe(`bigquery:quickstart`, () => { + let bigqueryMock, BigqueryMock; - before(function () { + before(() => { bigqueryMock = { createDataset: sinon.stub().yields(null, {}, {}) }; BigqueryMock = sinon.stub().returns(bigqueryMock); }); - it('should create a dataset', function () { - proxyquire('../quickstart', { + it(`should create a dataset`, () => { + proxyquire(`../quickstart`, { '@google-cloud/bigquery': BigqueryMock }); diff --git a/datastore/quickstart.js b/datastore/quickstart.js index 3cb9662b81..4f3dfed5e4 100644 --- a/datastore/quickstart.js +++ b/datastore/quickstart.js @@ -16,16 +16,16 @@ // [START quickstart] // Import and instantiate the Google Cloud client library // for Google Cloud Datastore -var datastore = require('@google-cloud/datastore')({ +const datastore = require('@google-cloud/datastore')({ projectId: 'YOUR_PROJECT_ID' }); -var taskKey = datastore.key(['Task', 1234]); +const taskKey = datastore.key(['Task', 1234]); -// Retrieve an entity from Cloud Datastore -datastore.get(taskKey, function (err, entity, apiResponse) { +// Retrieves an entity +datastore.get(taskKey, (err, entity) => { if (!err) { - // The entity was retrieved successfully. + // The entity was retrieved successfully } }); // [END quickstart] diff --git a/datastore/system-test/quickstart.test.js b/datastore/system-test/quickstart.test.js index 4a0266f9d1..2eb1ac9d65 100644 --- a/datastore/system-test/quickstart.test.js +++ b/datastore/system-test/quickstart.test.js @@ -13,44 +13,44 @@ 'use strict'; -var proxyquire = require('proxyquire').noPreserveCache(); -var datastore = proxyquire('@google-cloud/datastore', {})(); -var kind = 'Task'; -var message = 'Buy milk'; -var key = datastore.key(kind); +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', function () { - var datastoreMock, DatastoreMock; +describe(`datastore:quickstart`, () => { + let datastoreMock, DatastoreMock; - before(function (done) { + before((done) => { datastore.save({ key: key, data: { message: message } - }, function () { + }, () => { // Datastore is eventually consistent setTimeout(done, 5000); }); }); - after(function (done) { - datastore.delete(key, function () { + after((done) => { + datastore.delete(key, () => { // Ignore any error, the entity might not have been created done(); }); }); - it('should get a task from Datastore', function (done) { + it(`should get a task from Datastore`, (done) => { datastoreMock = { - key: function () { + key: () => { return key; }, - get: function (_key) { + get: (_key) => { assert.equal(_key, key); - datastore.get(_key, function (err, entity) { + datastore.get(_key, (err, entity) => { assert.ifError(err); assert.notEqual(entity, undefined); assert.notEqual(entity.key, undefined); @@ -62,7 +62,7 @@ describe('datastore:quickstart', function () { }; DatastoreMock = sinon.stub().returns(datastoreMock); - proxyquire('../quickstart', { + proxyquire(`../quickstart`, { '@google-cloud/datastore': DatastoreMock }); }); diff --git a/datastore/test/quickstart.test.js b/datastore/test/quickstart.test.js index a81dff883b..e793399f1b 100644 --- a/datastore/test/quickstart.test.js +++ b/datastore/test/quickstart.test.js @@ -13,21 +13,21 @@ 'use strict'; -var proxyquire = require('proxyquire').noPreserveCache(); +const proxyquire = require(`proxyquire`).noPreserveCache(); -describe('datastore:quickstart', function () { - var datastoreMock, DatastoreMock; +describe(`datastore:quickstart`, () => { + let datastoreMock, DatastoreMock; - before(function () { + before(() => { datastoreMock = { - get: sinon.stub().yields(null, {key: 1234}), - key: sinon.stub.returns('task/1234') + 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', function () { - proxyquire('../quickstart', { + it(`should get a task from Datastore`, () => { + proxyquire(`../quickstart`, { '@google-cloud/datastore': DatastoreMock }); diff --git a/logging/quickstart.js b/logging/quickstart.js index c0e141e93c..c7e0ebbe7c 100644 --- a/logging/quickstart.js +++ b/logging/quickstart.js @@ -16,19 +16,19 @@ // [START quickstart] // Import and instantiate the Google Cloud client library // for Stackdriver Logging -var logging = require('@google-cloud/logging')({ +const logging = require('@google-cloud/logging')({ projectId: 'YOUR_PROJECT_ID' }); -// Select the log to write to -var log = logging.log('my-log'); -// Prepare a log entry -var entry = log.entry({ type: 'global' }, 'Hello, world!'); +// Selects the log to write to +const log = logging.log('my-log'); +// Prepares a log entry +const entry = log.entry({ type: 'global' }, 'Hello, world!'); -// Write the log entry -log.write(entry, function (err, apiResponse) { +// Writes the log entry +log.write(entry, (err) => { if (!err) { - // The entry was logged successfully. + // The entry was logged successfully } }); // [END quickstart] diff --git a/logging/system-test/quickstart.test.js b/logging/system-test/quickstart.test.js index 9bedfeaa72..179493c449 100644 --- a/logging/system-test/quickstart.test.js +++ b/logging/system-test/quickstart.test.js @@ -13,33 +13,33 @@ 'use strict'; -var proxyquire = require('proxyquire').noPreserveCache(); -var logging = proxyquire('@google-cloud/logging', {})(); -var uuid = require('node-uuid'); +const proxyquire = require(`proxyquire`).noPreserveCache(); +const logging = proxyquire(`@google-cloud/logging`, {})(); +const uuid = require(`node-uuid`); -var logName = 'nodejs-docs-samples-test-' + uuid.v4(); +const logName = `nodejs-docs-samples-test-${uuid.v4()}`; -describe.only('logging:quickstart', function () { - var logMock, loggingMock, LoggingMock; +describe(`logging:quickstart`, () => { + let logMock, loggingMock, LoggingMock; - after(function (done) { - logging.log(logName).delete(function () { + after((done) => { + logging.log(logName).delete(() => { // Ignore any error, the topic might not have been created done(); }); }); - it('should log an entry', function (done) { - var expectedlogName = 'my-log'; + it(`should log an entry`, (done) => { + const expectedlogName = `my-log`; logMock = { entry: sinon.stub().returns({}), - write: function (_entry) { + write: (_entry) => { assert.deepEqual(_entry, {}); - var log = logging.log(logName); - var entry = log.entry({ type: 'global' }, 'Hello, world!'); - log.write(entry, function (err, apiResponse) { + 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 @@ -48,14 +48,14 @@ describe.only('logging:quickstart', function () { } }; loggingMock = { - log: function (_logName) { + log: (_logName) => { assert.equal(_logName, expectedlogName); return logMock; } }; LoggingMock = sinon.stub().returns(loggingMock); - proxyquire('../quickstart', { + proxyquire(`../quickstart`, { '@google-cloud/logging': LoggingMock }); }); diff --git a/logging/test/quickstart.test.js b/logging/test/quickstart.test.js index 23214d7e59..c5f4a7b953 100644 --- a/logging/test/quickstart.test.js +++ b/logging/test/quickstart.test.js @@ -13,16 +13,16 @@ 'use strict'; -var proxyquire = require('proxyquire').noCallThru(); +const proxyquire = require(`proxyquire`).noCallThru(); -describe('logging:quickstart', function () { - var logMock, loggingMock, LoggingMock; +describe(`logging:quickstart`, () => { + let logMock, loggingMock, LoggingMock; - var expectedLogName = 'my-log'; - var expectedResource = { type: 'global' }; - var expectedMessage = 'Hello, world!'; + const expectedLogName = `my-log`; + const expectedResource = { type: `global` }; + const expectedMessage = `Hello, world!`; - before(function () { + before(() => { logMock = { entry: sinon.stub().returns({}), write: sinon.stub().yields(null, {}) @@ -33,8 +33,8 @@ describe('logging:quickstart', function () { LoggingMock = sinon.stub().returns(loggingMock); }); - it('should log an entry', function () { - proxyquire('../quickstart', { + it(`should log an entry`, () => { + proxyquire(`../quickstart`, { '@google-cloud/logging': LoggingMock }); diff --git a/pubsub/quickstart.js b/pubsub/quickstart.js index 0800276133..1b5c609df4 100644 --- a/pubsub/quickstart.js +++ b/pubsub/quickstart.js @@ -16,14 +16,14 @@ // [START quickstart] // Import and instantiate the Google Cloud client library // for Google Cloud Pub/Sub -var pubsub = require('@google-cloud/pubsub')({ +const pubsub = require('@google-cloud/pubsub')({ projectId: 'YOUR_PROJECT_ID' }); -// Create a new Cloud Pub/Sub topic -pubsub.createTopic('my-new-topic', function (err, topic, apiResponse) { +// Creates a new topic +pubsub.createTopic('my-new-topic', (err, topic) => { if (!err) { - // The topic was created successfully. + // The topic was created successfully } }); // [END quickstart] diff --git a/pubsub/system-test/quickstart.test.js b/pubsub/system-test/quickstart.test.js index 3bb77f2ec9..858b6abe54 100644 --- a/pubsub/system-test/quickstart.test.js +++ b/pubsub/system-test/quickstart.test.js @@ -13,32 +13,32 @@ 'use strict'; -var proxyquire = require('proxyquire').noPreserveCache(); -var pubsub = proxyquire('@google-cloud/pubsub', {})(); -var uuid = require('node-uuid'); +const proxyquire = require(`proxyquire`).noPreserveCache(); +const pubsub = proxyquire(`@google-cloud/pubsub`, {})(); +const uuid = require(`node-uuid`); -var topicName = 'nodejs-docs-samples-test-' + uuid.v4(); -var projectId = process.env.GCLOUD_PROJECT; -var fullTopicName = 'projects/' + projectId + '/topics/' + topicName; +const topicName = `nodejs-docs-samples-test-${uuid.v4()}`; +const projectId = process.env.GCLOUD_PROJECT; +const fullTopicName = `projects/${projectId}/topics/${topicName}`; -describe('pubsub:quickstart', function () { - var pubsubMock, PubSubMock; +describe(`pubsub:quickstart`, () => { + let pubsubMock, PubSubMock; - after(function (done) { - pubsub.topic(topicName).delete(function () { + after((done) => { + pubsub.topic(topicName).delete(() => { // Ignore any error, the topic might not have been created done(); }); }); - it('should create a topic', function (done) { - var expectedTopicName = 'my-new-topic'; + it(`should create a topic`, (done) => { + const expectedTopicName = `my-new-topic`; pubsubMock = { - createTopic: function (_topicName) { + createTopic: (_topicName) => { assert.equal(_topicName, expectedTopicName); - pubsub.createTopic(topicName, function (err, topic, apiResponse) { + pubsub.createTopic(topicName, (err, topic, apiResponse) => { assert.ifError(err); assert.notEqual(topic, undefined); assert.equal(topic.name, fullTopicName); @@ -49,7 +49,7 @@ describe('pubsub:quickstart', function () { }; PubSubMock = sinon.stub().returns(pubsubMock); - proxyquire('../quickstart', { + proxyquire(`../quickstart`, { '@google-cloud/pubsub': PubSubMock }); }); diff --git a/pubsub/test/quickstart.test.js b/pubsub/test/quickstart.test.js index e809d968fb..9bd1f639b6 100644 --- a/pubsub/test/quickstart.test.js +++ b/pubsub/test/quickstart.test.js @@ -13,22 +13,22 @@ 'use strict'; -var proxyquire = require('proxyquire').noCallThru(); +const proxyquire = require(`proxyquire`).noCallThru(); -describe('pubsub:quickstart', function () { - var pubsubMock, PubSubMock; +describe(`pubsub:quickstart`, () => { + let pubsubMock, PubSubMock; - var expectedTopicName = 'my-new-topic'; + const expectedTopicName = `my-new-topic`; - before(function () { + before(() => { pubsubMock = { createTopic: sinon.stub().yields(null, {}, {}) }; PubSubMock = sinon.stub().returns(pubsubMock); }); - it('should create a topic', function () { - proxyquire('../quickstart', { + it(`should create a topic`, () => { + proxyquire(`../quickstart`, { '@google-cloud/pubsub': PubSubMock }); diff --git a/storage/quickstart.js b/storage/quickstart.js index 624023ec2c..44cab63801 100644 --- a/storage/quickstart.js +++ b/storage/quickstart.js @@ -16,14 +16,14 @@ // [START quickstart] // Import and instantiate the Google Cloud client library // for Google Cloud Storage -var storage = require('@google-cloud/storage')({ +const storage = require('@google-cloud/storage')({ projectId: 'YOUR_PROJECT_ID' }); -// Create a new Cloud Storage bucket -storage.createBucket('my-new-bucket', function (err, bucket, apiResponse) { +// Creates a new bucket +storage.createBucket('my-new-bucket', (err, bucket) => { if (!err) { - // The bucket was created successfully. + // The bucket was created successfully } }); // [END quickstart] diff --git a/storage/system-test/quickstart.test.js b/storage/system-test/quickstart.test.js index a60a0cdf44..bf2a022ffc 100644 --- a/storage/system-test/quickstart.test.js +++ b/storage/system-test/quickstart.test.js @@ -13,30 +13,30 @@ 'use strict'; -var proxyquire = require('proxyquire').noPreserveCache(); -var storage = proxyquire('@google-cloud/storage', {})(); -var uuid = require('node-uuid'); +const proxyquire = require(`proxyquire`).noPreserveCache(); +const storage = proxyquire(`@google-cloud/storage`, {})(); +const uuid = require(`node-uuid`); -var bucketName = 'nodejs-docs-samples-test-' + uuid.v4(); +const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; -describe('storage:quickstart', function () { - var storageMock, StorageMock; +describe(`storage:quickstart`, () => { + let storageMock, StorageMock; - after(function (done) { - storage.bucket(bucketName).delete(function () { + after((done) => { + storage.bucket(bucketName).delete(() => { // Ignore any error, the topic might not have been created done(); }); }); - it('should create a topic', function (done) { - var expectedBucketName = 'my-new-bucket'; + it(`should create a topic`, (done) => { + const expectedBucketName = `my-new-bucket`; storageMock = { - createBucket: function (_bucketName) { + createBucket: (_bucketName) => { assert.equal(_bucketName, expectedBucketName); - storage.createBucket(bucketName, function (err, bucket, apiResponse) { + storage.createBucket(bucketName, (err, bucket, apiResponse) => { assert.ifError(err); assert.notEqual(bucket, undefined); assert.equal(bucket.name, bucketName); @@ -47,7 +47,7 @@ describe('storage:quickstart', function () { }; StorageMock = sinon.stub().returns(storageMock); - proxyquire('../quickstart', { + proxyquire(`../quickstart`, { '@google-cloud/storage': StorageMock }); }); diff --git a/storage/test/quickstart.test.js b/storage/test/quickstart.test.js index 623634c317..7e37dd84e5 100644 --- a/storage/test/quickstart.test.js +++ b/storage/test/quickstart.test.js @@ -13,22 +13,22 @@ 'use strict'; -var proxyquire = require('proxyquire').noCallThru(); +const proxyquire = require(`proxyquire`).noCallThru(); -describe('storage:quickstart', function () { - var storageMock, StorageMock; +describe(`storage:quickstart`, () => { + let storageMock, StorageMock; - var expectedBucketName = 'my-new-bucket'; + const expectedBucketName = `my-new-bucket`; - before(function () { + before(() => { storageMock = { createBucket: sinon.stub().yields(null, {}, {}) }; StorageMock = sinon.stub().returns(storageMock); }); - it('should create a bucket', function () { - proxyquire('../quickstart', { + it(`should create a bucket`, () => { + proxyquire(`../quickstart`, { '@google-cloud/storage': StorageMock }); diff --git a/translate/quickstart.js b/translate/quickstart.js index 51b38f788d..05e451c5ff 100644 --- a/translate/quickstart.js +++ b/translate/quickstart.js @@ -16,14 +16,14 @@ // [START quickstart] // Import and instantiate the Google Cloud client library // for the Google Translate API -var translate = require('@google-cloud/translate')({ +const translate = require('@google-cloud/translate')({ key: 'YOUR_API_KEY' }); -// Translate some text into Russian -translate.translate('Hello, world!', 'ru', function (err, translation, apiResponse) { +// Translates some text into Russian +translate.translate('Hello, world!', 'ru', (err, translation, apiResponse) => { if (!err) { - // The text was translated successfully. + // The text was translated successfully } }); // [END quickstart] diff --git a/translate/system-test/quickstart.test.js b/translate/system-test/quickstart.test.js index 3e772411ae..3936592c5b 100644 --- a/translate/system-test/quickstart.test.js +++ b/translate/system-test/quickstart.test.js @@ -13,25 +13,25 @@ 'use strict'; -var proxyquire = require('proxyquire').noPreserveCache(); -var translate = proxyquire('@google-cloud/translate', {})({ +const proxyquire = require(`proxyquire`).noPreserveCache(); +const translate = proxyquire(`@google-cloud/translate`, {})({ key: process.env.TRANSLATE_API_KEY }); -var string = 'Hello, world!'; -var targetLanguage = 'ru'; +const string = `Hello, world!`; +const targetLanguage = `ru`; -describe('translate:quickstart', function () { - var translateMock, TranslateMock; +describe(`translate:quickstart`, () => { + let translateMock, TranslateMock; - it('should translate a string', function (done) { + it(`should translate a string`, (done) => { translateMock = { - translate: function (_string, _targetLanguage) { + translate: (_string, _targetLanguage) => { assert.equal(_string, string); assert.equal(_targetLanguage, targetLanguage); - translate.translate(_string, _targetLanguage, function (err, translation, apiResponse) { + translate.translate(_string, _targetLanguage, (err, translation, apiResponse) => { assert.ifError(err); - assert.equal(translation, 'Привет мир!'); + assert.equal(translation, `Привет мир!`); assert.notEqual(apiResponse, undefined); done(); }); @@ -39,7 +39,7 @@ describe('translate:quickstart', function () { }; TranslateMock = sinon.stub().returns(translateMock); - proxyquire('../quickstart', { + proxyquire(`../quickstart`, { '@google-cloud/translate': TranslateMock }); }); diff --git a/translate/test/quickstart.test.js b/translate/test/quickstart.test.js index 35f2dda6aa..85c20267b6 100644 --- a/translate/test/quickstart.test.js +++ b/translate/test/quickstart.test.js @@ -13,20 +13,20 @@ 'use strict'; -var proxyquire = require('proxyquire').noCallThru(); +const proxyquire = require(`proxyquire`).noCallThru(); -describe('translate:quickstart', function () { - var translateMock, TranslateMock; +describe(`translate:quickstart`, () => { + let translateMock, TranslateMock; - before(function () { + before(() => { translateMock = { - translate: sinon.stub().yields(null, 'Привет мир!', {}) + translate: sinon.stub().yields(null, `Привет мир!`, {}) }; TranslateMock = sinon.stub().returns(translateMock); }); - it('should translate a string', function () { - proxyquire('../quickstart', { + it(`should translate a string`, () => { + proxyquire(`../quickstart`, { '@google-cloud/translate': TranslateMock }); From bf8e80ef264bf242f1f7bd3cb3eddd8cde143e09 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 27 Sep 2016 13:21:11 -0700 Subject: [PATCH 7/8] Fix region tags. --- bigquery/quickstart.js | 4 ++-- datastore/quickstart.js | 4 ++-- logging/quickstart.js | 4 ++-- pubsub/quickstart.js | 4 ++-- storage/quickstart.js | 4 ++-- translate/quickstart.js | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bigquery/quickstart.js b/bigquery/quickstart.js index d641a2c837..8fdf6c1339 100644 --- a/bigquery/quickstart.js +++ b/bigquery/quickstart.js @@ -13,7 +13,7 @@ 'use strict'; -// [START quickstart] +// [START bigquery_quickstart] // Import and instantiate the Google Cloud client library // for Google BigQuery const bigquery = require('@google-cloud/bigquery')({ @@ -26,4 +26,4 @@ bigquery.createDataset('my_new_dataset', (err, dataset) => { // The dataset was created successfully } }); -// [END quickstart] +// [END bigquery_quickstart] diff --git a/datastore/quickstart.js b/datastore/quickstart.js index 4f3dfed5e4..4d56c5eee0 100644 --- a/datastore/quickstart.js +++ b/datastore/quickstart.js @@ -13,7 +13,7 @@ 'use strict'; -// [START quickstart] +// [START datastore_quickstart] // Import and instantiate the Google Cloud client library // for Google Cloud Datastore const datastore = require('@google-cloud/datastore')({ @@ -28,4 +28,4 @@ datastore.get(taskKey, (err, entity) => { // The entity was retrieved successfully } }); -// [END quickstart] +// [END datastore_quickstart] diff --git a/logging/quickstart.js b/logging/quickstart.js index c7e0ebbe7c..b9f69c1f34 100644 --- a/logging/quickstart.js +++ b/logging/quickstart.js @@ -13,7 +13,7 @@ 'use strict'; -// [START quickstart] +// [START logging_quickstart] // Import and instantiate the Google Cloud client library // for Stackdriver Logging const logging = require('@google-cloud/logging')({ @@ -31,4 +31,4 @@ log.write(entry, (err) => { // The entry was logged successfully } }); -// [END quickstart] +// [END logging_quickstart] diff --git a/pubsub/quickstart.js b/pubsub/quickstart.js index 1b5c609df4..3853cfe46d 100644 --- a/pubsub/quickstart.js +++ b/pubsub/quickstart.js @@ -13,7 +13,7 @@ 'use strict'; -// [START quickstart] +// [START pubsub_quickstart] // Import and instantiate the Google Cloud client library // for Google Cloud Pub/Sub const pubsub = require('@google-cloud/pubsub')({ @@ -26,4 +26,4 @@ pubsub.createTopic('my-new-topic', (err, topic) => { // The topic was created successfully } }); -// [END quickstart] +// [END pubsub_quickstart] diff --git a/storage/quickstart.js b/storage/quickstart.js index 44cab63801..56ab0069a8 100644 --- a/storage/quickstart.js +++ b/storage/quickstart.js @@ -13,7 +13,7 @@ 'use strict'; -// [START quickstart] +// [START storage_quickstart] // Import and instantiate the Google Cloud client library // for Google Cloud Storage const storage = require('@google-cloud/storage')({ @@ -26,4 +26,4 @@ storage.createBucket('my-new-bucket', (err, bucket) => { // The bucket was created successfully } }); -// [END quickstart] +// [END storage_quickstart] diff --git a/translate/quickstart.js b/translate/quickstart.js index 05e451c5ff..0b7a70d39f 100644 --- a/translate/quickstart.js +++ b/translate/quickstart.js @@ -13,7 +13,7 @@ 'use strict'; -// [START quickstart] +// [START translate_quickstart] // Import and instantiate the Google Cloud client library // for the Google Translate API const translate = require('@google-cloud/translate')({ @@ -26,4 +26,4 @@ translate.translate('Hello, world!', 'ru', (err, translation, apiResponse) => { // The text was translated successfully } }); -// [END quickstart] +// [END translate_quickstart] From e1ef553cebe848cdafe9ede9f7e922491727674a Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 27 Sep 2016 13:25:43 -0700 Subject: [PATCH 8/8] Fix comments. --- bigquery/quickstart.js | 2 +- datastore/quickstart.js | 2 +- logging/quickstart.js | 2 +- pubsub/quickstart.js | 2 +- storage/quickstart.js | 2 +- translate/quickstart.js | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bigquery/quickstart.js b/bigquery/quickstart.js index 8fdf6c1339..af2dac581f 100644 --- a/bigquery/quickstart.js +++ b/bigquery/quickstart.js @@ -14,7 +14,7 @@ 'use strict'; // [START bigquery_quickstart] -// Import and instantiate the Google Cloud client library +// Imports and instantiates the Google Cloud client library // for Google BigQuery const bigquery = require('@google-cloud/bigquery')({ projectId: 'YOUR_PROJECT_ID' diff --git a/datastore/quickstart.js b/datastore/quickstart.js index 4d56c5eee0..05feb6dc0a 100644 --- a/datastore/quickstart.js +++ b/datastore/quickstart.js @@ -14,7 +14,7 @@ 'use strict'; // [START datastore_quickstart] -// Import and instantiate the Google Cloud client library +// Imports and instantiates the Google Cloud client library // for Google Cloud Datastore const datastore = require('@google-cloud/datastore')({ projectId: 'YOUR_PROJECT_ID' diff --git a/logging/quickstart.js b/logging/quickstart.js index b9f69c1f34..0257750986 100644 --- a/logging/quickstart.js +++ b/logging/quickstart.js @@ -14,7 +14,7 @@ 'use strict'; // [START logging_quickstart] -// Import and instantiate the Google Cloud client library +// Imports and instantiates the Google Cloud client library // for Stackdriver Logging const logging = require('@google-cloud/logging')({ projectId: 'YOUR_PROJECT_ID' diff --git a/pubsub/quickstart.js b/pubsub/quickstart.js index 3853cfe46d..c6fd49246b 100644 --- a/pubsub/quickstart.js +++ b/pubsub/quickstart.js @@ -14,7 +14,7 @@ 'use strict'; // [START pubsub_quickstart] -// Import and instantiate the Google Cloud client library +// Imports and instantiates the Google Cloud client library // for Google Cloud Pub/Sub const pubsub = require('@google-cloud/pubsub')({ projectId: 'YOUR_PROJECT_ID' diff --git a/storage/quickstart.js b/storage/quickstart.js index 56ab0069a8..012344afe3 100644 --- a/storage/quickstart.js +++ b/storage/quickstart.js @@ -14,7 +14,7 @@ 'use strict'; // [START storage_quickstart] -// Import and instantiate the Google Cloud client library +// Imports and instantiates the Google Cloud client library // for Google Cloud Storage const storage = require('@google-cloud/storage')({ projectId: 'YOUR_PROJECT_ID' diff --git a/translate/quickstart.js b/translate/quickstart.js index 0b7a70d39f..46ce2607ca 100644 --- a/translate/quickstart.js +++ b/translate/quickstart.js @@ -14,7 +14,7 @@ 'use strict'; // [START translate_quickstart] -// Import and instantiate the Google Cloud client library +// Imports and instantiates the Google Cloud client library // for the Google Translate API const translate = require('@google-cloud/translate')({ key: 'YOUR_API_KEY'