Skip to content

Commit bf65a0b

Browse files
authored
Switch from Mocha to Ava for faster tests (#289)
* Switch from Mocha to Ava * Concurrency: 5
1 parent 1fa0733 commit bf65a0b

File tree

7 files changed

+292
-226
lines changed

7 files changed

+292
-226
lines changed

cloud-language/snippets/slackbot/demo_bot.js

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ function handleEntitiesReply (bot, message) {
146146

147147
// Query the database for the top N entities in the past week
148148
const queryTs = Math.floor(Date.now() / 1000) - SEVEN_DAYS_AGO;
149+
// const entitiesWeekSql = `select * from entities`;
149150
const entitiesWeekSql = `${ENTITIES_BASE_SQL} WHERE ts > ${queryTs}${ENTITIES_SQL}`;
150151
db.all(entitiesWeekSql, (err, topEntities) => {
151152
if (err) {

cloud-language/snippets/slackbot/demo_bot.test.js

-156
This file was deleted.

cloud-language/snippets/slackbot/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"sqlite3": "^3.1.8"
1313
},
1414
"scripts": {
15-
"test": "cd ../..; npm run st -- language/slackbot/*.test.js"
15+
"test": "cd ../..; npm run st -- language/slackbot/system-test/*.test.js"
1616
},
1717
"engines": {
1818
"node": ">=4.3.2"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright 2016, 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+
require(`../../../system-test/_setup`);
19+
20+
const fs = require(`fs`);
21+
const path = require(`path`);
22+
const proxyquire = require(`proxyquire`).noCallThru();
23+
24+
const SLACK_TOKEN_PATH = path.join(__dirname, `../.token`);
25+
26+
let controllerMock, botkitMock, program, originalToken;
27+
28+
test.before((t) => {
29+
originalToken = process.env.SLACK_TOKEN_PATH;
30+
controllerMock = {
31+
spawn: sinon.stub().returnsThis(),
32+
startRTM: sinon.stub().returnsThis(),
33+
hears: sinon.stub().returnsThis(),
34+
on: sinon.stub().returnsThis()
35+
};
36+
botkitMock = {
37+
slackbot: sinon.stub().returns(controllerMock)
38+
};
39+
program = proxyquire(`../demo_bot`, {
40+
botkit: botkitMock,
41+
sqlite3: {
42+
verbose: sinon.stub().returns({
43+
cached: {
44+
Database: sinon.stub().returns({
45+
run: sinon.stub()
46+
})
47+
}
48+
})
49+
}
50+
});
51+
});
52+
53+
test.after((t) => {
54+
process.env.SLACK_TOKEN_PATH = originalToken;
55+
try {
56+
fs.unlinkSync(SLACK_TOKEN_PATH);
57+
} catch (err) {
58+
// Ignore error
59+
}
60+
});
61+
62+
test(`should check SLACK_TOKEN_PATH`, (t) => {
63+
process.env.SLACK_TOKEN_PATH = ``;
64+
65+
t.throws(() => {
66+
program.startController();
67+
}, Error, `Please set the SLACK_TOKEN_PATH environment variable!`);
68+
});
69+
70+
test(`should start the controller`, (t) => {
71+
let controller;
72+
73+
fs.writeFileSync(SLACK_TOKEN_PATH, `test`, { encoding: `utf8` });
74+
process.env.SLACK_TOKEN_PATH = SLACK_TOKEN_PATH;
75+
76+
controller = program.startController();
77+
78+
t.is(controller === controllerMock, true);
79+
t.is(controllerMock.spawn.callCount, 1);
80+
t.is(controllerMock.startRTM.callCount, 1);
81+
t.is(controllerMock.hears.callCount, 2);
82+
t.is(controllerMock.on.callCount, 2);
83+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* Copyright 2016, 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+
require(`../../../system-test/_setup`);
19+
20+
const fs = require(`fs`);
21+
const path = require(`path`);
22+
const proxyquire = require(`proxyquire`).noCallThru();
23+
const sqlite3 = require(`sqlite3`).verbose();
24+
25+
const DB_PATH = path.join(__dirname, `../slackDB.db`);
26+
const SLACK_TOKEN_PATH = path.join(__dirname, `../.token`);
27+
const text = `President Obama is speaking at the White House.`;
28+
29+
let db, controllerMock, botkitMock, botMock, program;
30+
31+
test.before.cb((t) => {
32+
fs.unlink(DB_PATH, (err) => {
33+
if (err && err.code !== `ENOENT`) {
34+
t.end(err);
35+
return;
36+
}
37+
38+
db = new sqlite3.cached.Database(DB_PATH);
39+
controllerMock = {
40+
spawn: sinon.stub().returnsThis(),
41+
startRTM: sinon.stub().returnsThis(),
42+
hears: sinon.stub().returnsThis(),
43+
on: sinon.stub().returnsThis()
44+
};
45+
46+
botkitMock = {
47+
slackbot: sinon.stub().returns(controllerMock)
48+
};
49+
50+
botMock = {
51+
reply: sinon.stub()
52+
};
53+
54+
program = proxyquire(`../demo_bot`, {
55+
botkit: botkitMock
56+
});
57+
58+
db.run(program.TABLE_SQL, t.end);
59+
});
60+
});
61+
62+
test.after.cb((t) => {
63+
fs.unlink(DB_PATH, (err) => {
64+
if (err) {
65+
t.end(err);
66+
return;
67+
}
68+
try {
69+
fs.unlinkSync(SLACK_TOKEN_PATH);
70+
} catch (err) {
71+
// Ignore error
72+
}
73+
t.end();
74+
});
75+
});
76+
77+
test.serial(`should analyze sentiment in text`, async (t) => {
78+
const sentiment = await program.analyzeSentiment(text);
79+
t.is(sentiment > 0, true);
80+
});
81+
82+
test.serial(`should analyze entities in text`, async (t) => {
83+
const entities = await program.analyzeEntities(text, Date.now());
84+
t.is(entities.some((entity) => entity.name === `Obama`), true);
85+
t.is(entities.some((entity) => entity.name === `White House`), true);
86+
87+
await new Promise((resolve, reject) => {
88+
setTimeout(() => {
89+
db.all(`select * from entities`, (err, entities) => {
90+
if (err) {
91+
reject(err);
92+
return;
93+
}
94+
t.is(entities.some((entity) => entity.name === `Obama`), true);
95+
t.is(entities.some((entity) => entity.name === `White House`), true);
96+
resolve();
97+
});
98+
}, 1000);
99+
});
100+
});
101+
102+
test.serial(`should reply to simple hello message`, (t) => {
103+
const message = {};
104+
105+
program.handleSimpleReply(botMock, message);
106+
107+
t.is(botMock.reply.callCount, 1);
108+
t.deepEqual(botMock.reply.getCall(0).args, [message, `Hello.`]);
109+
});
110+
111+
test.cb.serial(`should reply to entities message`, (t) => {
112+
const message = {};
113+
114+
program.handleEntitiesReply(botMock, message);
115+
116+
setTimeout(() => {
117+
try {
118+
t.is(botMock.reply.callCount, 3);
119+
t.deepEqual(botMock.reply.getCall(1).args, [message, `Top entities: `]);
120+
t.deepEqual(botMock.reply.getCall(2).args, [message, `entity: *Obama*, type: PERSON, count: 1\nentity: *White House*, type: LOCATION, count: 1\n`]);
121+
t.end();
122+
} catch (err) {
123+
t.end(err);
124+
}
125+
}, 1000);
126+
});

0 commit comments

Comments
 (0)