Added acceptance tests for the spelling module (#29)

This commit is contained in:
Miguel Serrano 2019-07-09 20:12:30 +02:00 committed by GitHub
parent ba642b9807
commit a28ecc0c39
8 changed files with 199 additions and 21 deletions

View file

@ -14,7 +14,12 @@
"chai-friendly" "chai-friendly"
], ],
"env": { "env": {
"node": true "node": true,
"mocha": true
},
"globals": {
"expect": true,
"define": true
}, },
"rules": { "rules": {
// Add some mocha specific rules // Add some mocha specific rules

View file

@ -34,25 +34,21 @@ server.get('/status', (req, res) => res.send({ status: 'spelling api is up' }))
server.get('/health_check', HealthCheckController.healthCheck) server.get('/health_check', HealthCheckController.healthCheck)
const host = const settings =
__guard__( Settings.internal && Settings.internal.spelling
Settings.internal != null ? Settings.internal.spelling : undefined, ? Settings.internal.spelling
x => x.host : undefined
) || 'localhost' const host = settings && settings.host ? settings.host : 'localhost'
const port = const port = settings && settings.port ? settings.port : 3005
__guard__(
Settings.internal != null ? Settings.internal.spelling : undefined, if (!module.parent) {
x1 => x1.port // application entry point, called directly
) || 3005 server.listen(port, host, function(error) {
server.listen(port, host, function(error) {
if (error != null) { if (error != null) {
throw error throw error
} }
return logger.info(`spelling starting up, listening on ${host}:${port}`) return logger.info(`spelling starting up, listening on ${host}:${port}`)
}) })
function __guard__(value, transform) {
return typeof value !== 'undefined' && value !== null
? transform(value)
: undefined
} }
module.exports = server

View file

@ -0,0 +1,81 @@
const { expect } = require('chai')
const request = require('./helpers/request')
const USER_ID = 101
const checkWord = words =>
request.post({
url: `/user/${USER_ID}/check`,
body: JSON.stringify({
words
})
})
describe('checking words', () => {
let response
describe('on successful response', () => {
beforeEach(async () => {
response = await checkWord(['anather'])
})
it('should return status 200', async () => {
expect(response.statusCode).to.equal(200)
})
it('should return the list of misspellings', async () => {
const body = JSON.parse(response.body)
expect(body).to.deep.equal({
misspellings: [{ index: 0, suggestions: ['anther', 'another'] }]
})
})
})
describe('when multiple words are submitted', () => {
beforeEach(async () => {
response = await checkWord(['anather', 'anather', 'theorie'])
})
it('should return the misspellings for all the words', async () => {
const body = JSON.parse(response.body)
expect(body.misspellings.length).to.equal(3)
})
it('should have misspelling suggestions with consecutive indexes', async () => {
const body = JSON.parse(response.body)
const indexes = body.misspellings.map(mspl => mspl.index)
expect(indexes).to.deep.equal([0, 1, 2])
})
it('should return identical suggestions for the same entry', async () => {
const body = JSON.parse(response.body)
expect(body.misspellings[0].suggestions).to.deep.equal(
body.misspellings[1].suggestions
)
})
})
describe('when a very long list of words if submitted', () => {
beforeEach(async () => {
let words = []
for (let i = 0; i <= 20000; i++) {
words.push('anather')
}
response = await checkWord(words)
})
it('should return misspellings for the first 10K results only', async () => {
const body = JSON.parse(response.body)
expect(body.misspellings.length).to.equal(10000)
})
it('should have misspelling suggestions with consecutive indexes', async () => {
const body = JSON.parse(response.body)
const indexList = body.misspellings.map(mspl => mspl.index)
expect(indexList.length).to.equal(10000) // avoid testing over an incorrect array
for (let i = 0; i < indexList.length - 1; i++) {
expect(indexList[i] + 1).to.equal(indexList[i + 1])
}
})
})
})

View file

@ -0,0 +1,9 @@
const { expect } = require('chai')
const request = require('./helpers/request')
describe('/health_check', () => {
it('should return 200', async () => {
const response = await request.get('/health_check')
expect(response.statusCode).to.equal(200)
})
})

View file

@ -0,0 +1,4 @@
const App = require('../../../app.js')
const { PORT } = require('./helpers/request')
before(done => App.listen(PORT, 'localhost', done))

View file

@ -0,0 +1,53 @@
const { expect } = require('chai')
const request = require('./helpers/request')
const USER_ID = 101
const checkWord = words =>
request.post({
url: `/user/${USER_ID}/check`,
body: JSON.stringify({
words
})
})
const learnWord = word =>
request.post({
url: `/user/${USER_ID}/learn`,
body: JSON.stringify({
word
})
})
const deleteDict = () =>
request.del({
url: `/user/${USER_ID}`
})
describe('learning words', () => {
it('should return status 200 when posting a word successfully', async () => {
const response = await learnWord('abcd')
expect(response.statusCode).to.equal(200)
})
it('should return no misspellings after a word is learnt', async () => {
const response = await checkWord(['abv'])
const responseBody = JSON.parse(response.body)
expect(responseBody.misspellings.length).to.equals(1)
await learnWord('abv')
const response2 = await checkWord(['abv'])
const responseBody2 = JSON.parse(response2.body)
expect(responseBody2.misspellings.length).to.equals(0)
})
it('should return misspellings again after a personal dictionary is deleted', async () => {
await learnWord('bvc')
await deleteDict()
const response = await checkWord(['bvc'])
const responseBody = JSON.parse(response.body)
expect(responseBody.misspellings.length).to.equals(1)
})
})

View file

@ -0,0 +1,9 @@
const { expect } = require('chai')
const request = require('./helpers/request')
describe('/status', () => {
it('should return 200', async () => {
const response = await request.get('/health_check')
expect(response.statusCode).to.equal(200)
})
})

View file

@ -0,0 +1,21 @@
const { promisify } = require('util')
const PORT = 3005
const BASE_URL = `http://${process.env['HTTP_TEST_HOST'] ||
'localhost'}:${PORT}`
const request = require('request').defaults({
baseUrl: BASE_URL,
headers: {
'Content-Type': 'application/json'
},
followRedirect: false
})
module.exports = {
PORT,
get: promisify(request.get),
post: promisify(request.post),
del: promisify(request.del)
}