|
| 1 | +/** |
| 2 | + * Copyright 2017, Google, Inc. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +const proxyquire = require(`proxyquire`).noCallThru(); |
| 19 | +const sinon = require(`sinon`); |
| 20 | +const test = require(`ava`); |
| 21 | + |
| 22 | +const entities = [ |
| 23 | + { |
| 24 | + SingerId: { value: 1 }, |
| 25 | + AlbumId: { value: 1 }, |
| 26 | + AlbumTitle: 'Go, Go, Go' |
| 27 | + }, |
| 28 | + { |
| 29 | + SingerId: { value: 1 }, |
| 30 | + AlbumId: { value: 2 }, |
| 31 | + AlbumTitle: 'Total Junk' |
| 32 | + } |
| 33 | +]; |
| 34 | + |
| 35 | +const query = { |
| 36 | + sql: 'SELECT * FROM Albums' |
| 37 | +}; |
| 38 | + |
| 39 | +function getSample () { |
| 40 | + const resultsMock = entities.map((row) => { |
| 41 | + return { toJSON: sinon.stub().returns(row) }; |
| 42 | + }); |
| 43 | + const databaseMock = { |
| 44 | + run: sinon.stub().returns(Promise.resolve([resultsMock])) |
| 45 | + }; |
| 46 | + const instanceMock = { |
| 47 | + database: sinon.stub().returns(databaseMock) |
| 48 | + }; |
| 49 | + const spannerMock = { |
| 50 | + instance: sinon.stub().returns(instanceMock) |
| 51 | + }; |
| 52 | + |
| 53 | + const SpannerMock = sinon.stub().returns(spannerMock); |
| 54 | + |
| 55 | + return { |
| 56 | + program: proxyquire(`../`, { |
| 57 | + '@google-cloud/spanner': SpannerMock |
| 58 | + }), |
| 59 | + mocks: { |
| 60 | + spanner: spannerMock, |
| 61 | + database: databaseMock, |
| 62 | + instance: instanceMock, |
| 63 | + results: resultsMock, |
| 64 | + res: { |
| 65 | + status: sinon.stub().returnsThis(), |
| 66 | + send: sinon.stub().returnsThis(), |
| 67 | + end: sinon.stub().returnsThis(), |
| 68 | + write: sinon.stub().returnsThis() |
| 69 | + } |
| 70 | + } |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +test(`get: Gets albums`, async (t) => { |
| 75 | + const sample = getSample(); |
| 76 | + const mocks = sample.mocks; |
| 77 | + |
| 78 | + await sample.program.get(mocks.req, mocks.res); |
| 79 | + t.true(mocks.spanner.instance.called); |
| 80 | + t.true(mocks.instance.database.called); |
| 81 | + t.true(mocks.database.run.calledWith(query)); |
| 82 | + t.true(mocks.results[0].toJSON.called); |
| 83 | + t.true(mocks.res.write.calledWith(`SingerId: 1, AlbumId: 2, AlbumTitle: Total Junk\n`)); |
| 84 | + t.true(mocks.res.end.called); |
| 85 | +}); |
0 commit comments