Skip to content

Commit

Permalink
GCF: Add Node 8 background function samples (#779)
Browse files Browse the repository at this point in the history
* GCF: Add Node 8 background function samples

* Add necessary dependencies to package.json

* GCF: Add Node 8 background function samples

* Add necessary dependencies to package.json

* Add tests + switch to requestPromiseNative/mocking

* Fix lint bugs caused by merge
  • Loading branch information
ericschmidtatwork authored and Ace Nassri committed Nov 13, 2018
1 parent eef741b commit cccf576
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
6 changes: 3 additions & 3 deletions functions/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ exports.helloWorld = (event, callback) => {
// [END functions_background_helloworld]

// [START functions_background_promise]
const requestPromiseNative = require('request-promise-native');

/**
* Background Cloud Function that returns a Promise. Note that we don't pass
* a "callback" argument to the function.
Expand All @@ -45,9 +47,7 @@ exports.helloWorld = (event, callback) => {
* @returns {Promise}
*/
exports.helloPromise = (event) => {
const request = require('request-promise');

return request({
return requestPromiseNative({
uri: event.data.endpoint
});
};
Expand Down
2 changes: 1 addition & 1 deletion functions/background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"dependencies": {
"request": "2.83.0",
"request-promise": "4.2.2"
"request-promise-native": "^1.0.5"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
Expand Down
8 changes: 4 additions & 4 deletions functions/background/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

function getSample () {
const requestPromise = sinon.stub().returns(Promise.resolve(`test`));
const requestPromiseNative = sinon.stub().returns(Promise.resolve(`test`));

return {
program: proxyquire(`../`, {
'request-promise': requestPromise
'request-promise-native': requestPromiseNative
}),
mocks: {
requestPromise: requestPromise
requestPromiseNative: requestPromiseNative
}
};
}
Expand Down Expand Up @@ -73,7 +73,7 @@ test.serial(`should make a promise request`, (t) => {

return sample.program.helloPromise(event)
.then((result) => {
t.deepEqual(sample.mocks.requestPromise.firstCall.args, [{ uri: `foo.com` }]);
t.deepEqual(sample.mocks.requestPromiseNative.firstCall.args, [{ uri: `foo.com` }]);
t.is(result, `test`);
});
});
Expand Down
34 changes: 34 additions & 0 deletions functions/node8/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,40 @@ exports.helloAnalytics = (data, context) => {
};
// [END functions_firebase_analytics_node8]

// [START functions_background_promise_node8]
const requestPromiseNative = require('request-promise-native');

/**
* Background Cloud Function that returns a Promise. Note that we don't pass
* a "callback" argument to the function.
*
* @param {object} data The Cloud Functions event data.
* @returns {Promise}
*/
exports.helloPromise = (data) => {
return requestPromiseNative({
uri: data.endpoint
});
};
// [END functions_background_promise_node8]

// [START functions_background_synchronous_node8]
/**
* Background Cloud Function that returns synchronously. Note that we don't pass
* a "callback" argument to the function.
*
* @param {object} data The Cloud Functions event data.
*/
exports.helloSynchronous = (data) => {
// This function returns synchronously
if (data.something === true) {
return 'Something is true!';
} else {
throw new Error('Something was not true!');
}
};
// [END functions_background_synchronous_node8]

// [START functions_firebase_reactive_node8]
const Firestore = require('@google-cloud/firestore');

Expand Down
2 changes: 2 additions & 0 deletions functions/node8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"uuid": "^3.3.2"
},
"dependencies": {
"request": "^2.88.0",
"request-promise-native": "^1.0.5",
"@google-cloud/firestore": "^0.18.0"
}
}
35 changes: 33 additions & 2 deletions functions/node8/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@

'use strict';

const sinon = require('sinon');
const sinon = require(`sinon`);
const uuid = require('uuid');
const test = require('ava');
const utils = require('@google-cloud/nodejs-repo-tools');
const proxyquire = require(`proxyquire`).noCallThru();

function getSample () {
const requestPromiseNative = sinon.stub().returns(Promise.resolve(`test`));

const firestoreMock = {
doc: sinon.stub().returnsThis(),
set: sinon.stub()
};

return {
program: proxyquire(`../`, {
'request-promise-native': requestPromiseNative,
'@google-cloud/firestore': sinon.stub().returns(firestoreMock)
}),
mocks: {
firestore: firestoreMock
firestore: firestoreMock,
requestPromiseNative: requestPromiseNative
}
};
}
Expand Down Expand Up @@ -155,6 +159,33 @@ test.serial('should monitor Analytics', t => {
t.is(console.log.args[4][0], `Location: London, UK`);
});

test.serial(`should make a promise request`, (t) => {
const sample = getSample();
const data = {
endpoint: `foo.com`
};

return sample.program.helloPromise(data)
.then((result) => {
t.deepEqual(sample.mocks.requestPromiseNative.firstCall.args, [{ uri: `foo.com` }]);
t.is(result, `test`);
});
});

test.serial(`should return synchronously`, (t) => {
t.is(getSample().program.helloSynchronous({
something: true
}), `Something is true!`);
});

test.serial(`should throw an error`, (t) => {
t.throws(() => {
getSample().program.helloSynchronous({
something: false
});
}, Error, `Something was not true!`);
});

test(`should update data in response to Firestore events`, t => {
const sample = getSample();

Expand Down

0 comments on commit cccf576

Please sign in to comment.