From d9842817ecffa8b9f4a7a9e92242d28a88aa82cf Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 23 Jul 2018 11:33:34 -0700 Subject: [PATCH 1/2] Add node8 unit tests --- functions/node8/package.json | 8 ++- .../node8/test/sample.unit.pubsub.test.js | 46 +++++++++++++ .../node8/test/sample.unit.storage.test.js | 68 +++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 functions/node8/test/sample.unit.pubsub.test.js create mode 100644 functions/node8/test/sample.unit.storage.test.js diff --git a/functions/node8/package.json b/functions/node8/package.json index 1d786a3470..d834d97d54 100644 --- a/functions/node8/package.json +++ b/functions/node8/package.json @@ -3,8 +3,11 @@ "version": "0.0.1", "description": "Google Cloud Functions samples for Node.js 8", "scripts": { + "test": "ava test/*" + }, + "engines": { + "node": "^8" }, - "engines": { "node": "^8"}, "repository": { "type": "git", "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" @@ -13,5 +16,8 @@ "license": "Apache-2.0", "bugs": { "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples/issues" + }, + "dependencies": { + "ava": "^0.25.0" } } diff --git a/functions/node8/test/sample.unit.pubsub.test.js b/functions/node8/test/sample.unit.pubsub.test.js new file mode 100644 index 0000000000..497e5fd4b3 --- /dev/null +++ b/functions/node8/test/sample.unit.pubsub.test.js @@ -0,0 +1,46 @@ +/** + * Copyright 2018, 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. + */ + +// [START functions_pubsub_unit_test] +const test = require(`ava`); +const uuid = require(`uuid`); +const sinon = require(`sinon`); + +const helloPubSub = require(`..`).helloPubSub; +const consoleLog = sinon.stub(console, 'log'); + +test(`helloPubSub: should print a name`, async t => { + + // Initialize mocks + const name = uuid.v4(); + const event = { + data: Buffer.from(name).toString(`base64`) + }; + + // Call tested function and verify its behavior + await helloPubSub(event); + t.true(consoleLog.calledWith(`Hello, ${name}!`)); +}); + +test(`helloPubSub: should print hello world`, async t => { + + // Initialize mocks + const event = {}; + + // Call tested function and verify its behavior + await helloPubSub(event); + t.true(consoleLog.calledWith(`Hello, World!`)); +}); +// [END functions_pubsub_unit_test] diff --git a/functions/node8/test/sample.unit.storage.test.js b/functions/node8/test/sample.unit.storage.test.js new file mode 100644 index 0000000000..c8e82dcfdd --- /dev/null +++ b/functions/node8/test/sample.unit.storage.test.js @@ -0,0 +1,68 @@ +/** + * Copyright 2018, 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. + */ + +// [START functions_storage_unit_test] +const test = require(`ava`); +const uuid = require(`uuid`); +const sinon = require(`sinon`); + +const helloGCS = require(`..`).helloGCS; +const consoleLog = sinon.stub(console, 'log'); + +test(`helloGCS: should print uploaded message`, async t => { + + // Initialize mocks + const filename = uuid.v4(); + const event = { + name: filename, + resourceState: 'exists', + metageneration: '1' + }; + + // Call tested function and verify its behavior + await helloGCS(event); + t.true(consoleLog.calledWith(`File ${filename} uploaded.`)); +}); + +test(`helloGCS: should print metadata updated message`, async t => { + + // Initialize mocks + const filename = uuid.v4(); + const event = { + name: filename, + resourceState: 'exists', + metageneration: '2' + }; + + // Call tested function and verify its behavior + await helloGCS(event); + t.true(consoleLog.calledWith(`File ${filename} metadata updated.`)); +}); + +test(`helloGCS: should print deleted message`, async t => { + + // Initialize mocks + const filename = uuid.v4(); + const event = { + name: filename, + resourceState: 'not_exists', + metageneration: '3' + }; + + // Call tested function and verify its behavior + await helloGCS(event); + t.true(consoleLog.calledWith(`File ${filename} deleted.`)); +}); +// [END functions_storage_unit_test] From 4976d12069ebb753b9d839512b178ea224db8653 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 23 Jul 2018 11:46:14 -0700 Subject: [PATCH 2/2] Fix lint --- functions/node8/package.json | 4 ++++ functions/node8/test/sample.unit.pubsub.test.js | 2 -- functions/node8/test/sample.unit.storage.test.js | 3 --- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/functions/node8/package.json b/functions/node8/package.json index d834d97d54..665830e98e 100644 --- a/functions/node8/package.json +++ b/functions/node8/package.json @@ -3,6 +3,7 @@ "version": "0.0.1", "description": "Google Cloud Functions samples for Node.js 8", "scripts": { + "pretest": "semistandard *.js test/*.js", "test": "ava test/*" }, "engines": { @@ -19,5 +20,8 @@ }, "dependencies": { "ava": "^0.25.0" + }, + "devDependencies": { + "semistandard": "^12.0.1" } } diff --git a/functions/node8/test/sample.unit.pubsub.test.js b/functions/node8/test/sample.unit.pubsub.test.js index 497e5fd4b3..d85f9e55cd 100644 --- a/functions/node8/test/sample.unit.pubsub.test.js +++ b/functions/node8/test/sample.unit.pubsub.test.js @@ -22,7 +22,6 @@ const helloPubSub = require(`..`).helloPubSub; const consoleLog = sinon.stub(console, 'log'); test(`helloPubSub: should print a name`, async t => { - // Initialize mocks const name = uuid.v4(); const event = { @@ -35,7 +34,6 @@ test(`helloPubSub: should print a name`, async t => { }); test(`helloPubSub: should print hello world`, async t => { - // Initialize mocks const event = {}; diff --git a/functions/node8/test/sample.unit.storage.test.js b/functions/node8/test/sample.unit.storage.test.js index c8e82dcfdd..595c6dff7f 100644 --- a/functions/node8/test/sample.unit.storage.test.js +++ b/functions/node8/test/sample.unit.storage.test.js @@ -22,7 +22,6 @@ const helloGCS = require(`..`).helloGCS; const consoleLog = sinon.stub(console, 'log'); test(`helloGCS: should print uploaded message`, async t => { - // Initialize mocks const filename = uuid.v4(); const event = { @@ -37,7 +36,6 @@ test(`helloGCS: should print uploaded message`, async t => { }); test(`helloGCS: should print metadata updated message`, async t => { - // Initialize mocks const filename = uuid.v4(); const event = { @@ -52,7 +50,6 @@ test(`helloGCS: should print metadata updated message`, async t => { }); test(`helloGCS: should print deleted message`, async t => { - // Initialize mocks const filename = uuid.v4(); const event = {