Skip to content

Commit

Permalink
Add spanner sample + test to GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri committed May 1, 2017
1 parent 606b89b commit 34c186b
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
61 changes: 61 additions & 0 deletions functions/spanner/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* 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');

// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';

// Instantiates a client
const spanner = Spanner({
projectId: projectId
});

// 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 = function helloGET (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];
var data = [];
rows.forEach((row) => data.push(row.toJSON()));
res.send(data);
});
};
// [END spanner_functions_quickstart]
84 changes: 84 additions & 0 deletions functions/spanner/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* 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()
}
}
};
}

test(`get: Gets albums`, async (t) => {
const sample = getSample();
const mocks = sample.mocks;

const err = await sample.program.get(mocks.req, mocks.res);
t.falsy(err, null);
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.send.called);
t.true(mocks.res.send.calledWith(entities));
});

0 comments on commit 34c186b

Please sign in to comment.