Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Region tags -> tests: AppEngine #1504

Merged
merged 2 commits into from
Oct 4, 2019
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
124 changes: 63 additions & 61 deletions appengine/cloudsql/test/createTables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,72 +61,74 @@ const getSample = () => {
beforeEach(tools.stubConsole);
afterEach(tools.restoreConsole);

it('should create a table', async () => {
const sample = getSample();
const expectedResult = `Successfully created 'visits' table.`;

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
describe('gae_flex_mysql_create_tables', () => {
it('should create a table', async () => {
const sample = getSample();
const expectedResult = `Successfully created 'visits' table.`;

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
});

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: 'mysql',
connection: exampleConfig,
},
]);

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

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

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: 'mysql',
connection: exampleConfig,
},
]);

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

assert.ok(console.log.calledWith(expectedResult));
assert.ok(sample.mocks.knex.destroy.calledOnce);
});
it('should handle prompt error', async () => {
const error = new Error('error');
const sample = getSample();
sample.mocks.prompt.get = sinon.stub().yields(error);

it('should handle prompt error', async () => {
const error = new Error('error');
const sample = getSample();
sample.mocks.prompt.get = sinon.stub().yields(error);
proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
});

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
await new Promise(r => setTimeout(r, 10));
assert.ok(console.error.calledOnce);
assert.ok(console.error.calledWith(error));
assert.ok(sample.mocks.Knex.notCalled);
});

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

it('should handle knex creation error', async () => {
const error = new Error('error');
const sample = getSample();
sample.mocks.knex.schema.createTable = sinon
.stub()
.returns(Promise.reject(error));

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
it('should handle knex creation error', async () => {
const error = new Error('error');
const sample = getSample();
sample.mocks.knex.schema.createTable = sinon
.stub()
.returns(Promise.reject(error));

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
});
await new Promise(r => setTimeout(r, 10));
assert.ok(console.error.calledOnce);
assert.ok(
console.error.calledWith(`Failed to create 'visits' table:`, error)
);
assert.ok(sample.mocks.knex.destroy.calledOnce);
});
await new Promise(r => setTimeout(r, 10));
assert.ok(console.error.calledOnce);
assert.ok(
console.error.calledWith(`Failed to create 'visits' table:`, error)
);
assert.ok(sample.mocks.knex.destroy.calledOnce);
});
96 changes: 50 additions & 46 deletions appengine/cloudsql/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,59 +77,63 @@ const getSample = () => {
beforeEach(tools.stubConsole);
afterEach(tools.restoreConsole);

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

assert.ok(sample.mocks.express.calledOnce);
assert.ok(sample.mocks.Knex.calledOnce);
assert.deepStrictEqual(sample.mocks.Knex.firstCall.args, [
{
client: 'mysql',
connection: {
user: sample.mocks.process.env.SQL_USER,
password: sample.mocks.process.env.SQL_PASSWORD,
database: sample.mocks.process.env.SQL_DATABASE,
describe('gae_flex_mysql_connect', () => {
it('should set up sample in Postgres', () => {
const sample = getSample();

assert.ok(sample.mocks.express.calledOnce);
assert.ok(sample.mocks.Knex.calledOnce);
assert.deepStrictEqual(sample.mocks.Knex.firstCall.args, [
{
client: 'mysql',
connection: {
user: sample.mocks.process.env.SQL_USER,
password: sample.mocks.process.env.SQL_PASSWORD,
database: sample.mocks.process.env.SQL_DATABASE,
},
},
},
]);
]);
});
});

it('should record a visit', async () => {
const sample = getSample();
const expectedResult = 'Last 10 visits:\nTime: 1234, AddrHash: abcd';

await request(sample.app)
.get('/')
.expect(200)
.expect(response => {
assert.strictEqual(response.text, expectedResult);
});
});
describe('gae_flex_mysql_app', () => {
it('should record a visit', async () => {
const sample = getSample();
const expectedResult = 'Last 10 visits:\nTime: 1234, AddrHash: abcd';

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

it('should handle insert error', async () => {
const sample = getSample();
const expectedResult = 'insert_error';
it('should handle insert error', async () => {
const sample = getSample();
const expectedResult = 'insert_error';

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

await request(sample.app)
.get('/')
.expect(500)
.expect(response => {
assert.ok(response.text.includes(expectedResult));
});
});
await request(sample.app)
.get('/')
.expect(500)
.expect(response => {
assert.ok(response.text.includes(expectedResult));
});
});

it('should handle read error', async () => {
const sample = getSample();
const expectedResult = 'read_error';
it('should handle read error', async () => {
const sample = getSample();
const expectedResult = 'read_error';

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

await request(sample.app)
.get('/')
.expect(500)
.expect(response => {
assert.ok(response.text.includes(expectedResult));
});
await request(sample.app)
.get('/')
.expect(500)
.expect(response => {
assert.ok(response.text.includes(expectedResult));
});
});
});
126 changes: 64 additions & 62 deletions appengine/cloudsql_postgresql/test/createTables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,73 +61,75 @@ const getSample = () => {
beforeEach(tools.stubConsole);
afterEach(tools.restoreConsole);

it('should create a table', async () => {
const sample = getSample();
const expectedResult = `Successfully created 'visits' table.`;

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
describe('gae_flex_postgres_create_tables', () => {
it('should create a table', async () => {
const sample = getSample();
const expectedResult = `Successfully created 'visits' table.`;

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
});

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],
'visits'
);

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

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],
'visits'
);

assert.ok(console.log.calledWith(expectedResult));
assert.ok(sample.mocks.knex.destroy.calledOnce);
});
it('should handle prompt error', async () => {
const error = new Error('error');
const sample = getSample();
sample.mocks.prompt.get = sinon.stub().yields(error);

it('should handle prompt error', async () => {
const error = new Error('error');
const sample = getSample();
sample.mocks.prompt.get = sinon.stub().yields(error);
proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
});

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
await new Promise(r => setTimeout(r, 10));
assert.ok(console.error.calledOnce);
assert.ok(console.error.calledWith(error));
assert.ok(sample.mocks.Knex.notCalled);
});

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

it('should handle knex creation error', async () => {
const error = new Error('error');
const sample = getSample();
sample.mocks.knex.schema.createTable = sinon
.stub()
.returns(Promise.reject(error));

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
it('should handle knex creation error', async () => {
const error = new Error('error');
const sample = getSample();
sample.mocks.knex.schema.createTable = sinon
.stub()
.returns(Promise.reject(error));

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt,
});

await new Promise(r => setTimeout(r, 10));
assert.ok(console.error.calledOnce);
assert.ok(
console.error.calledWith(`Failed to create 'visits' table:`, error)
);
assert.ok(sample.mocks.knex.destroy.calledOnce);
});

await new Promise(r => setTimeout(r, 10));
assert.ok(console.error.calledOnce);
assert.ok(
console.error.calledWith(`Failed to create 'visits' table:`, error)
);
assert.ok(sample.mocks.knex.destroy.calledOnce);
});
Loading