-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 5 commits
45f38b5
3f33b21
678af97
f74cd88
a62010f
624de84
afc6a6d
e68f8b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
.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>`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to set the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By default, it sets the (If I convert the |
||
}); | ||
res | ||
.status(200) | ||
.end(); | ||
}) | ||
.catch((err) => { | ||
res | ||
.status(500) | ||
.send(`Error querying Spanner: ${err}`) | ||
.end(); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing error handling. |
||
}; | ||
// [END spanner_functions_quickstart] |
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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
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); | ||
}); |
There was a problem hiding this comment.
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)
/