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 Spanner + Cloud Functions sample #371

Merged
merged 8 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
65 changes: 65 additions & 0 deletions functions/spanner/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2017, 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.
*/

'use strict';

// [START spanner_functions_quickstart]
// Imports the Google Cloud client library
const Spanner = require('@google-cloud/spanner');

// Instantiates a client
const spanner = Spanner();

// Your Cloud Spanner instance ID
const instanceId = 'my-instance';

// Your Cloud Spanner database ID
const databaseId = 'my-database';

/**
* HTTP Cloud Function.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.get = (req, res) => {
// Gets a reference to a Cloud Spanner instance and database
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

// The query to execute
const query = {
sql: 'SELECT * FROM Albums'
};

// Execute the query
database.run(query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To eliminate possible race condition/flakiness in the test:

s/database.run(query)/return database.run(query)/

.then((results) => {
const rows = results[0].map((row) => row.toJSON());
rows.forEach((row) => {
res.write(`SingerId: ${row.SingerId.value}, AlbumId: ${row.AlbumId.value}, AlbumTitle: ${row.AlbumTitle}</br>`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to set the Content-Type response header? (If you're using res.write, I'm wondering if it will correctly detect the content-type as text/html)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, it sets the Content-Type to text/plain - which IMO should be fine unless we're specifically trying to show HTML.

(If I convert the </br>s to \ns, I can get the same desired result with text/plain.)

});
res
.status(200)
.end();
})
.catch((err) => {
res
.status(500)
.send(`Error querying Spanner: ${err}`)
.end();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling.

};
// [END spanner_functions_quickstart]
85 changes: 85 additions & 0 deletions functions/spanner/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright 2017, 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.
*/

'use strict';

const proxyquire = require(`proxyquire`).noCallThru();
const sinon = require(`sinon`);
const test = require(`ava`);

const entities = [
{
SingerId: { value: 1 },
AlbumId: { value: 1 },
AlbumTitle: 'Go, Go, Go'
},
{
SingerId: { value: 1 },
AlbumId: { value: 2 },
AlbumTitle: 'Total Junk'
}
];

const query = {
sql: 'SELECT * FROM Albums'
};

function getSample () {
const resultsMock = entities.map((row) => {
return { toJSON: sinon.stub().returns(row) };
});
const databaseMock = {
run: sinon.stub().returns(Promise.resolve([resultsMock]))
};
const instanceMock = {
database: sinon.stub().returns(databaseMock)
};
const spannerMock = {
instance: sinon.stub().returns(instanceMock)
};

const SpannerMock = sinon.stub().returns(spannerMock);

return {
program: proxyquire(`../`, {
'@google-cloud/spanner': SpannerMock
}),
mocks: {
spanner: spannerMock,
database: databaseMock,
instance: instanceMock,
results: resultsMock,
res: {
status: sinon.stub().returnsThis(),
send: sinon.stub().returnsThis(),
end: sinon.stub().returnsThis(),
write: sinon.stub().returnsThis()
}
}
};
}

test(`get: Gets albums`, async (t) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove async

const sample = getSample();
const mocks = sample.mocks;

await sample.program.get(mocks.req, mocks.res);
t.true(mocks.spanner.instance.called);
t.true(mocks.instance.database.called);
t.true(mocks.database.run.calledWith(query));
t.true(mocks.results[0].toJSON.called);
t.true(mocks.res.write.calledWith(`SingerId: 1, AlbumId: 2, AlbumTitle: Total Junk</br>`));
t.true(mocks.res.end.called);
});