From f74cd88bc6e19d09a6fc7a1a27aa727df45bca88 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Wed, 3 May 2017 10:49:24 -0700 Subject: [PATCH] Address further comments (from jdobry + vikask) --- functions/spanner/index.js | 14 ++++++++++---- functions/spanner/test/index.test.js | 12 +++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/functions/spanner/index.js b/functions/spanner/index.js index 48c80db124..9ee02d4a3e 100644 --- a/functions/spanner/index.js +++ b/functions/spanner/index.js @@ -47,12 +47,18 @@ exports.get = (req, res) => { // Execute the query database.run(query) .then((results) => { - const rows = results[0]; - res.send(rows.map((row) => row.toJSON())); + const rows = results[0].map((row) => row.toJSON()); + rows.forEach((row) => { + res.write(`SingerId: ${row.SingerId.value}, AlbumId: ${row.AlbumId.value}, AlbumTitle: ${row.AlbumTitle}
`); + }); + res + .status(200) + .end(); }) .catch((err) => { - res.status(500); - res.send(`Error querying Spanner: ${err}`); + res + .status(500) + .send(`Error querying Spanner: ${err}`); }); }; // [END spanner_functions_quickstart] diff --git a/functions/spanner/test/index.test.js b/functions/spanner/test/index.test.js index bc89b66d0d..d8bf8231af 100644 --- a/functions/spanner/test/index.test.js +++ b/functions/spanner/test/index.test.js @@ -63,21 +63,23 @@ function getSample () { results: resultsMock, res: { status: sinon.stub().returnsThis(), - send: sinon.stub().returnsThis() + send: sinon.stub().returnsThis(), + end: sinon.stub().returnsThis(), + write: sinon.stub().returnsThis() } } }; } -test(`get: Gets albums`, (t) => { +test(`get: Gets albums`, async (t) => { const sample = getSample(); const mocks = sample.mocks; - const err = sample.program.get(mocks.req, mocks.res); + 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.send.called); - t.true(mocks.res.send.calledWith(entities)); + t.true(mocks.res.write.calledWith(`SingerId: 1, AlbumId: 2, AlbumTitle: Total Junk
`)); + t.true(mocks.res.end.called); });