Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (missing) Node 8 unit tests #695

Merged
merged 2 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]