Skip to content

Commit

Permalink
Switch from Mocha to Ava for faster tests (#289)
Browse files Browse the repository at this point in the history
* Switch from Mocha to Ava

* Concurrency: 5
  • Loading branch information
jmdobry authored Jan 6, 2017
1 parent 1fa0733 commit bf65a0b
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 226 deletions.
1 change: 1 addition & 0 deletions cloud-language/snippets/slackbot/demo_bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ function handleEntitiesReply (bot, message) {

// Query the database for the top N entities in the past week
const queryTs = Math.floor(Date.now() / 1000) - SEVEN_DAYS_AGO;
// const entitiesWeekSql = `select * from entities`;
const entitiesWeekSql = `${ENTITIES_BASE_SQL} WHERE ts > ${queryTs}${ENTITIES_SQL}`;
db.all(entitiesWeekSql, (err, topEntities) => {
if (err) {
Expand Down
156 changes: 0 additions & 156 deletions cloud-language/snippets/slackbot/demo_bot.test.js

This file was deleted.

2 changes: 1 addition & 1 deletion cloud-language/snippets/slackbot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"sqlite3": "^3.1.8"
},
"scripts": {
"test": "cd ../..; npm run st -- language/slackbot/*.test.js"
"test": "cd ../..; npm run st -- language/slackbot/system-test/*.test.js"
},
"engines": {
"node": ">=4.3.2"
Expand Down
83 changes: 83 additions & 0 deletions cloud-language/snippets/slackbot/system-test/controller.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright 2016, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

require(`../../../system-test/_setup`);

const fs = require(`fs`);
const path = require(`path`);
const proxyquire = require(`proxyquire`).noCallThru();

const SLACK_TOKEN_PATH = path.join(__dirname, `../.token`);

let controllerMock, botkitMock, program, originalToken;

test.before((t) => {
originalToken = process.env.SLACK_TOKEN_PATH;
controllerMock = {
spawn: sinon.stub().returnsThis(),
startRTM: sinon.stub().returnsThis(),
hears: sinon.stub().returnsThis(),
on: sinon.stub().returnsThis()
};
botkitMock = {
slackbot: sinon.stub().returns(controllerMock)
};
program = proxyquire(`../demo_bot`, {
botkit: botkitMock,
sqlite3: {
verbose: sinon.stub().returns({
cached: {
Database: sinon.stub().returns({
run: sinon.stub()
})
}
})
}
});
});

test.after((t) => {
process.env.SLACK_TOKEN_PATH = originalToken;
try {
fs.unlinkSync(SLACK_TOKEN_PATH);
} catch (err) {
// Ignore error
}
});

test(`should check SLACK_TOKEN_PATH`, (t) => {
process.env.SLACK_TOKEN_PATH = ``;

t.throws(() => {
program.startController();
}, Error, `Please set the SLACK_TOKEN_PATH environment variable!`);
});

test(`should start the controller`, (t) => {
let controller;

fs.writeFileSync(SLACK_TOKEN_PATH, `test`, { encoding: `utf8` });
process.env.SLACK_TOKEN_PATH = SLACK_TOKEN_PATH;

controller = program.startController();

t.is(controller === controllerMock, true);
t.is(controllerMock.spawn.callCount, 1);
t.is(controllerMock.startRTM.callCount, 1);
t.is(controllerMock.hears.callCount, 2);
t.is(controllerMock.on.callCount, 2);
});
126 changes: 126 additions & 0 deletions cloud-language/snippets/slackbot/system-test/demo_bot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* Copyright 2016, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

require(`../../../system-test/_setup`);

const fs = require(`fs`);
const path = require(`path`);
const proxyquire = require(`proxyquire`).noCallThru();
const sqlite3 = require(`sqlite3`).verbose();

const DB_PATH = path.join(__dirname, `../slackDB.db`);
const SLACK_TOKEN_PATH = path.join(__dirname, `../.token`);
const text = `President Obama is speaking at the White House.`;

let db, controllerMock, botkitMock, botMock, program;

test.before.cb((t) => {
fs.unlink(DB_PATH, (err) => {
if (err && err.code !== `ENOENT`) {
t.end(err);
return;
}

db = new sqlite3.cached.Database(DB_PATH);
controllerMock = {
spawn: sinon.stub().returnsThis(),
startRTM: sinon.stub().returnsThis(),
hears: sinon.stub().returnsThis(),
on: sinon.stub().returnsThis()
};

botkitMock = {
slackbot: sinon.stub().returns(controllerMock)
};

botMock = {
reply: sinon.stub()
};

program = proxyquire(`../demo_bot`, {
botkit: botkitMock
});

db.run(program.TABLE_SQL, t.end);
});
});

test.after.cb((t) => {
fs.unlink(DB_PATH, (err) => {
if (err) {
t.end(err);
return;
}
try {
fs.unlinkSync(SLACK_TOKEN_PATH);
} catch (err) {
// Ignore error
}
t.end();
});
});

test.serial(`should analyze sentiment in text`, async (t) => {
const sentiment = await program.analyzeSentiment(text);
t.is(sentiment > 0, true);
});

test.serial(`should analyze entities in text`, async (t) => {
const entities = await program.analyzeEntities(text, Date.now());
t.is(entities.some((entity) => entity.name === `Obama`), true);
t.is(entities.some((entity) => entity.name === `White House`), true);

await new Promise((resolve, reject) => {
setTimeout(() => {
db.all(`select * from entities`, (err, entities) => {
if (err) {
reject(err);
return;
}
t.is(entities.some((entity) => entity.name === `Obama`), true);
t.is(entities.some((entity) => entity.name === `White House`), true);
resolve();
});
}, 1000);
});
});

test.serial(`should reply to simple hello message`, (t) => {
const message = {};

program.handleSimpleReply(botMock, message);

t.is(botMock.reply.callCount, 1);
t.deepEqual(botMock.reply.getCall(0).args, [message, `Hello.`]);
});

test.cb.serial(`should reply to entities message`, (t) => {
const message = {};

program.handleEntitiesReply(botMock, message);

setTimeout(() => {
try {
t.is(botMock.reply.callCount, 3);
t.deepEqual(botMock.reply.getCall(1).args, [message, `Top entities: `]);
t.deepEqual(botMock.reply.getCall(2).args, [message, `entity: *Obama*, type: PERSON, count: 1\nentity: *White House*, type: LOCATION, count: 1\n`]);
t.end();
} catch (err) {
t.end(err);
}
}, 1000);
});
Loading

0 comments on commit bf65a0b

Please sign in to comment.