Skip to content

Commit

Permalink
Address further comments (from jdobry + vikask)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri committed May 3, 2017
1 parent 678af97 commit f74cd88
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
14 changes: 10 additions & 4 deletions functions/spanner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}</br>`);
});
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]
12 changes: 7 additions & 5 deletions functions/spanner/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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</br>`));
t.true(mocks.res.end.called);
});

0 comments on commit f74cd88

Please sign in to comment.