Skip to content

Commit

Permalink
Add (missing) Node 8 unit tests (#695)
Browse files Browse the repository at this point in the history
* Add node8 unit tests

* Fix lint
  • Loading branch information
Ace Nassri authored Jul 23, 2018
1 parent 3b9d623 commit 29f1230
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
12 changes: 11 additions & 1 deletion functions/node8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
"version": "0.0.1",
"description": "Google Cloud Functions samples for Node.js 8",
"scripts": {
"pretest": "semistandard *.js test/*.js",
"test": "ava test/*"
},
"engines": {
"node": "^8"
},
"engines": { "node": "^8"},
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
Expand All @@ -13,5 +17,11 @@
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples/issues"
},
"dependencies": {
"ava": "^0.25.0"
},
"devDependencies": {
"semistandard": "^12.0.1"
}
}
44 changes: 44 additions & 0 deletions functions/node8/test/sample.unit.pubsub.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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]
65 changes: 65 additions & 0 deletions functions/node8/test/sample.unit.storage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* 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]

0 comments on commit 29f1230

Please sign in to comment.