Skip to content
Permalink
Browse files
Getting rid of thrown error on getAnswersByQuestion
The method would throw an error when the results of the SQL query was empty. However it is possible for a question to have no answers
Therefore this error was unecessary and would break the application
  • Loading branch information
reisborw committed Nov 28, 2019
1 parent 6e8a71e commit a2e725dcca3679f203ee4c7f8d87cfe94ae4be0c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 26 deletions.
@@ -27,15 +27,10 @@ module.exports = class Answer {
}

async getAnswersByQuestion(id) {
try {
const sql = `SELECT answers.*, users.name AS user_name FROM answers
INNER JOIN users ON users.id = answers.user_id WHERE question_id = "${id}";`
const answers = await this.db.all(sql)
if (!answers.length) throw new Error('Answers not found!')
return answers
} catch (err) {
throw err
}
const sql = `SELECT answers.*, users.name AS user_name FROM answers
INNER JOIN users ON users.id = answers.user_id WHERE question_id = "${id}";`
const answers = await this.db.all(sql)
return answers
}

async __testData() {
@@ -63,21 +63,4 @@ describe('getAnswersByQuestion()', () => {
expect(data[0].user_name).toBe('Wallef')
done()
})

test('Bad data', async done => {
expect.assertions(1)
// Arrange
const answer = await new Answer()
await answer.__testData()
const request = {
body: {body: 'Getting Answers'},
parameters: {question_id: 3},
session: {user: {id: 1}}
}
// Act
await answer.createAnswer(request, '21/11/2019')
// Assert
await expect(answer.getAnswersByQuestion(8)).rejects.toEqual(Error('Answers not found!'))
done()
})
})

0 comments on commit a2e725d

Please sign in to comment.