-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch from Mocha to Ava for faster tests (#289)
* Switch from Mocha to Ava * Concurrency: 5
- Loading branch information
Showing
7 changed files
with
292 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
cloud-language/snippets/slackbot/system-test/controller.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
126
cloud-language/snippets/slackbot/system-test/demo_bot.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.