Skip to content
Permalink
Browse files
Merge pull request reisborw#33 from 5001CEM-1920SEPJAN/fix-showUserNa…
…meOnAnswers

Fix show user name on answers
  • Loading branch information
reisborw committed Nov 28, 2019
2 parents be467f8 + a2e725d commit c064f2aaf5a44cadae289b07e3960b80a5facbc8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
@@ -27,12 +27,14 @@ module.exports = class Answer {
}

async getAnswersByQuestion(id) {
try {
const sql = `SELECT * FROM answers WHERE question_id = "${id}";`
const answers = await this.db.all(sql)
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() {
await this.db.run(table.createUsersTable())
await this.db.run('INSERT INTO users(name, username, password) VALUES("Wallef", "username", "password");')
}
}
@@ -18,7 +18,7 @@
<article style="background-color: white;">
<p>{{ body }}</p>
<p>{{ date }}</p>
<p>{{ user_id }}</p>
<p>{{ user_name }}</p>
</article>
<hr>
{{/each}}
@@ -28,28 +28,29 @@ describe('Create()', () => {

test('Empty answer', async done => {
expect.assertions(1)
//Arrange
// Arrange
const answer = await new Answer()
const request = {
body: {body: ''},
parameters: {question_id: 1},
session: {user: {id: 1}}
}
//Act & Assert
// Act & Assert
await expect(answer.createAnswer(request, '21/11/2019')).rejects.toEqual(Error('Answer cannot be empty!'))
done()
})
})

describe('getAnswersByQuestion()', () => {
test('Get all answers from a question', async done => {
expect.assertions(4)
//Arrange
expect.assertions(5)
// Arrange
const answer = await new Answer()
await answer.__testData()
const request = {
body: {body: 'Getting Answers'},
parameters: {question_id: 3},
session: {user: {id: 2}}
session: {user: {id: 1}}
}
// Act
await answer.createAnswer(request, '21/11/2019')
@@ -58,7 +59,8 @@ describe('getAnswersByQuestion()', () => {
expect(data[0].body).toBe('Getting Answers')
expect(data[0].date).toBe('21/11/2019')
expect(data[0].question_id).toBe(3)
expect(data[0].user_id).toBe(2)
expect(data[0].user_id).toBe(1)
expect(data[0].user_name).toBe('Wallef')
done()
})
})

0 comments on commit c064f2a

Please sign in to comment.