Skip to content

Commit 4d0e7a9

Browse files
sarafordAce Nassriaverikitsch
authored
feat(functions/helloPubSub): use new FF testing layer (GoogleCloudPlatform#2645)
* updated helloPubSub to use new FF testing layer * removed console from integration test & renames * updated integration test * fixed lint * updated region tag in test * removed unused dependencies * Copyright year Co-authored-by: Averi Kitsch <akitsch@google.com> Co-authored-by: Ace Nassri <anassri@google.com> Co-authored-by: Averi Kitsch <akitsch@google.com>
1 parent dfa1e95 commit 4d0e7a9

File tree

3 files changed

+68
-25
lines changed

3 files changed

+68
-25
lines changed

functions/v2/helloPubSub/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
"node": ">=12.0.0"
1313
},
1414
"scripts": {
15-
"test": "mocha test/index.test.js"
15+
"test": "mocha test/*.test.js --timeout=60000"
1616
},
1717
"dependencies": {
1818
"@google-cloud/functions-framework": "^3.0.0"
1919
},
2020
"devDependencies": {
2121
"mocha": "^9.1.3",
22-
"p-retry": "^4.6.1",
2322
"sinon": "^13.0.0",
2423
"supertest": "^6.0.0",
2524
"uuid": "^8.3.2"

functions/v2/helloPubSub/test/index.test.js renamed to functions/v2/helloPubSub/test/integration.test.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,25 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const assert = require('assert');
16-
const sinon = require('sinon');
1715
const supertest = require('supertest');
18-
1916
const functionsFramework = require('@google-cloud/functions-framework/testing');
2017

21-
beforeEach(() => {
22-
// require the module that includes the functions we are testing
23-
require('../index');
24-
25-
// stub the console so we can use it for side effect assertions
26-
sinon.stub(console, 'log');
27-
sinon.stub(console, 'error');
28-
});
29-
30-
afterEach(() => {
31-
// restore the console stub
32-
console.log.restore();
33-
console.error.restore();
34-
});
18+
require('../index');
3519

3620
describe('functions_cloudevent_pubsub', () => {
3721
it('should process a CloudEvent', async () => {
38-
const event = {
39-
data: {
40-
message: 'd29ybGQ=', // 'World' in base 64
41-
},
22+
const cloudEventData = {data: {message: {}}};
23+
24+
const name = 'Cecil';
25+
cloudEventData.data.message = {
26+
data: Buffer.from(name).toString('base64'),
4227
};
4328

4429
const server = functionsFramework.getTestServer('helloPubSub');
4530
await supertest(server)
4631
.post('/')
47-
.send(event)
32+
.send(cloudEventData)
4833
.set('Content-Type', 'application/json')
4934
.expect(204);
50-
assert(console.log.calledWith('Hello, World!'));
5135
});
5236
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
const {getFunction} = require('@google-cloud/functions-framework/testing');
16+
require('..');
17+
18+
describe('functions_cloudevent_pubsub', () => {
19+
const assert = require('assert');
20+
const uuid = require('uuid');
21+
const sinon = require('sinon');
22+
23+
const stubConsole = function () {
24+
sinon.stub(console, 'error');
25+
sinon.stub(console, 'log');
26+
};
27+
28+
const restoreConsole = function () {
29+
console.log.restore();
30+
console.error.restore();
31+
};
32+
33+
beforeEach(stubConsole);
34+
afterEach(restoreConsole);
35+
36+
it('should print a name from the pubsub message', () => {
37+
// Create mock Pub/Sub event
38+
const cloudEvent = {data: {message: {}}};
39+
const name = uuid.v4();
40+
cloudEvent.data.message = {
41+
data: Buffer.from(name).toString('base64'),
42+
};
43+
44+
// Call tested function and verify its behavior
45+
const helloPubSub = getFunction('helloPubSub');
46+
helloPubSub(cloudEvent);
47+
assert.ok(console.log.calledWith(`Hello, ${name}!`));
48+
});
49+
50+
it('helloPubSub: should print hello world', () => {
51+
// Create mock Pub/Sub event, in the event where a
52+
// PubSub message is empty but message has an attribute
53+
const cloudEvent = {data: {message: {data: null}}};
54+
55+
// Call tested function and verify its behavior
56+
const helloPubSub = getFunction('helloPubSub');
57+
helloPubSub(cloudEvent);
58+
assert.ok(console.log.calledWith('Hello, World!'));
59+
});
60+
});

0 commit comments

Comments
 (0)