Skip to content

Commit

Permalink
AppEngine: add region tags (#1504)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri authored and fhinkel committed Oct 4, 2019
1 parent 37ba2f2 commit 866f717
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 342 deletions.
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

0 comments on commit 866f717

Please sign in to comment.