Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cloud-sql/postgres/knex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=8"
},
"scripts": {
"unit-test": "ava --verbose test/*.test.js",
"unit-test": "mocha test/*.test.js --timeout=60000 --exit",
"start-proxy": "! pgrep cloud_sql_proxy > /dev/null && cloud_sql_proxy -dir=/cloudsql -instances=$CLOUD_SQL_INSTANCE_NAME &",
"system-test": "repo-tools test app -- server.js",
"system-test-proxy": "npm run start-proxy; npm run system-test",
Expand All @@ -30,8 +30,8 @@
"winston": "^3.1.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.2.0",
"ava": "^0.25.0",
"@google-cloud/nodejs-repo-tools": "3.2.0",
"mocha": "^6.0.0",
"proxyquire": "^2.1.0",
"supertest": "^4.0.0",
"sinon": "^7.1.1"
Expand Down
92 changes: 46 additions & 46 deletions cloud-sql/postgres/knex/test/createTable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

'use strict';

const test = require(`ava`);
const path = require(`path`);
const proxyquire = require(`proxyquire`).noPreserveCache();
const sinon = require(`sinon`);
const tools = require(`@google-cloud/nodejs-repo-tools`);
const assert = require('assert');
const path = require('path');
const proxyquire = require('proxyquire').noPreserveCache();
const sinon = require('sinon');
const tools = require('@google-cloud/nodejs-repo-tools');

const SAMPLE_PATH = path.join(__dirname, `../createTables.js`);
const SAMPLE_PATH = path.join(__dirname, '../createTable.js');

const exampleConfig = [`user`, `password`, `database`];
const exampleConfig = ['user', 'password', 'database'];

function getSample() {
const configMock = exampleConfig;
Expand Down Expand Up @@ -58,10 +58,10 @@ function getSample() {
};
}

test.beforeEach(tools.stubConsole);
test.afterEach.always(tools.restoreConsole);
beforeEach(tools.stubConsole);
afterEach(tools.restoreConsole);

test.cb.serial(`should create a table`, t => {
it('should create a table', async () => {
const sample = getSample();
const expectedResult = `Successfully created 'votes' table.`;

Expand All @@ -70,30 +70,34 @@ test.cb.serial(`should create a table`, t => {
prompt: sample.mocks.prompt,
});

t.true(sample.mocks.prompt.start.calledOnce);
t.true(sample.mocks.prompt.get.calledOnce);
t.deepEqual(sample.mocks.prompt.get.firstCall.args[0], exampleConfig);

setTimeout(() => {
t.true(sample.mocks.Knex.calledOnce);
t.deepEqual(sample.mocks.Knex.firstCall.args, [
{
client: 'pg',
connection: exampleConfig,
},
]);

t.true(sample.mocks.knex.schema.createTable.calledOnce);
t.is(sample.mocks.knex.schema.createTable.firstCall.args[0], 'votes');

t.true(console.log.calledWith(expectedResult));
t.true(sample.mocks.knex.destroy.calledOnce);
t.end();
}, 10);
assert.ok(sample.mocks.prompt.start.calledOnce);
assert.ok(sample.mocks.prompt.get.calledOnce);
assert.deepStrictEqual(
sample.mocks.prompt.get.firstCall.args[0],
exampleConfig
);

await new Promise(r => setTimeout(r, 10));
assert.ok(sample.mocks.Knex.calledOnce);
assert.deepStrictEqual(sample.mocks.Knex.firstCall.args, [
{
client: 'pg',
connection: exampleConfig,
},
]);

assert.ok(sample.mocks.knex.schema.createTable.calledOnce);
assert.strictEqual(
sample.mocks.knex.schema.createTable.firstCall.args[0],
'votes'
);

assert.ok(console.log.calledWith(expectedResult));
assert.ok(sample.mocks.knex.destroy.calledOnce);
});

test.cb.serial(`should handle prompt error`, t => {
const error = new Error(`error`);
it('should handle prompt error', async () => {
const error = new Error('error');
const sample = getSample();
sample.mocks.prompt.get = sinon.stub().yields(error);

Expand All @@ -102,16 +106,14 @@ test.cb.serial(`should handle prompt error`, t => {
prompt: sample.mocks.prompt,
});

setTimeout(() => {
t.true(console.error.calledOnce);
t.true(console.error.calledWith(error));
t.true(sample.mocks.Knex.notCalled);
t.end();
}, 10);
await new Promise(r => setTimeout(r, 10));
assert.ok(console.error.calledOnce);
assert.ok(console.error.calledWith(error));
assert.ok(sample.mocks.Knex.notCalled);
});

test.cb.serial(`should handle knex creation error`, t => {
const error = new Error(`error`);
it('should handle knex creation error', async () => {
const error = new Error('error');
const sample = getSample();
sample.mocks.knex.schema.createTable = sinon
.stub()
Expand All @@ -122,10 +124,8 @@ test.cb.serial(`should handle knex creation error`, t => {
prompt: sample.mocks.prompt,
});

setTimeout(() => {
t.true(console.error.calledOnce);
t.true(console.error.calledWith(`Failed to create 'votes' table:`, error));
t.true(sample.mocks.knex.destroy.calledOnce);
t.end();
}, 10);
await new Promise(r => setTimeout(r, 10));
assert.ok(console.error.calledOnce);
assert.ok(console.error.calledWith(`Failed to create 'votes' table:`, error));
assert.ok(sample.mocks.knex.destroy.calledOnce);
});
54 changes: 26 additions & 28 deletions cloud-sql/postgres/knex/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@

'use strict';

const express = require(`express`);
const path = require(`path`);
const proxyquire = require(`proxyquire`).noCallThru();
const request = require(`supertest`);
const sinon = require(`sinon`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);
const express = require('express');
const path = require('path');
const proxyquire = require('proxyquire').noCallThru();
const request = require('supertest');
const sinon = require('sinon');
const assert = require('assert');
const tools = require('@google-cloud/nodejs-repo-tools');

const SAMPLE_PATH = path.join(__dirname, `../server.js`);
const SAMPLE_PATH = path.join(__dirname, '../server.js');

function getSample() {
const testApp = express();
sinon.stub(testApp, `listen`).yields();
sinon.stub(testApp, 'listen').yields();
const expressMock = sinon.stub().returns(testApp);
const timestamp = new Date();
const resultsMock = [
Expand Down Expand Up @@ -75,15 +75,15 @@ function getSample() {
};
}

test.beforeEach(tools.stubConsole);
test.afterEach.always(tools.restoreConsole);
beforeEach(tools.stubConsole);
afterEach(tools.restoreConsole);

test(`should set up sample in Postgres`, t => {
it('should set up sample in Postgres', () => {
const sample = getSample();

t.true(sample.mocks.express.calledOnce);
t.true(sample.mocks.Knex.calledOnce);
t.deepEqual(sample.mocks.Knex.firstCall.args, [
assert.ok(sample.mocks.express.calledOnce);
assert.ok(sample.mocks.Knex.calledOnce);
assert.deepStrictEqual(sample.mocks.Knex.firstCall.args, [
{
client: 'pg',
connection: {
Expand All @@ -95,30 +95,28 @@ test(`should set up sample in Postgres`, t => {
]);
});

test.cb(`should display the default page`, t => {
it('should display the default page', async () => {
const sample = getSample();
const expectedResult = `Tabs VS Spaces`;
const expectedResult = 'Tabs VS Spaces';

request(sample.app)
.get(`/`)
await request(sample.app)
.get('/')
.expect(200)
.expect(response => {
t.is(response.text, expectedResult);
})
.end(t.end);
assert.strictEqual(response.text, expectedResult);
});
});

test.cb(`should handle insert error`, t => {
it('should handle insert error', async () => {
const sample = getSample();
const expectedResult = 'Invalid team specified';

sample.mocks.knex.limit.returns(Promise.reject());

request(sample.app)
.post(`/`)
await request(sample.app)
.post('/')
.expect(400)
.expect(response => {
t.is(response.text.includes(expectedResult), true);
})
.end(t.end);
assert.ok(response.text.includes(expectedResult));
});
});