From 6268f56597c23ce17abc8982541ca287310ea43b Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 19 Sep 2016 15:15:08 -0700 Subject: [PATCH 1/3] First draft of new tests --- 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 ++++++++++++++ translate/system-test/quickstart.test.js | 46 ++++++++++++++++ translate/test/quickstart.test.js | 38 +++++++++++++ 6 files changed, 280 insertions(+) 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..5be6703d89 --- /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 Datastore 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/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 300d5d0ed6aaf07c8ef3602a3279a3f7ef31ab8b Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 19 Sep 2016 15:30:30 -0700 Subject: [PATCH 2/3] Fix failing tests --- logging/test/quickstart.test.js | 2 +- pubsub/test/quickstart.test.js | 2 +- storage/test/quickstart.test.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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]); }); From 4ef67b87f8177520365eaf905f0a54c5a2634dbf Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Tue, 20 Sep 2016 09:55:37 -0700 Subject: [PATCH 3/3] Fix comments --- datastore/system-test/quickstart.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datastore/system-test/quickstart.test.js b/datastore/system-test/quickstart.test.js index 5be6703d89..4a0266f9d1 100644 --- a/datastore/system-test/quickstart.test.js +++ b/datastore/system-test/quickstart.test.js @@ -36,7 +36,7 @@ describe('datastore:quickstart', function () { after(function (done) { datastore.delete(key, function () { - // Ignore any error, the Datastore might not have been created + // Ignore any error, the entity might not have been created done(); }); });