Skip to content
Permalink
Browse files
updated unit testing and created some data display
  • Loading branch information
dasilv32 committed Apr 23, 2020
1 parent fb522f7 commit 5577e69b55927d948ea7ba42043556efd03e35e6
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 23 deletions.
@@ -7,22 +7,22 @@ module.exports = class Question {
constructor(dbName = ':memory:'){
return (async() => {
this.db = await sqlite.open(dbName)

//CHECK IF TABLE EXISTS ELSE CREATE
const sql = 'CREATE TABLE IF NOT EXISTS available_questions (\
id INTEGER PRIMARY KEY AUTOINCREMENT,\
question TEXT, correct_answer TEXT\
question TEXT, possible_answer1 TEXT, possible_answer2 TEXT, possible_answer3 TEXT, correct_answer TEXT\
);'
await this.db.run(sql)
return this
})()
}

//INSERT QUESTIONS IN DB
//INSERT QUESTIONS IN DB - BACKOFFICE
async questionz(questions) {
try{
console.log('Question Added')
const sql = `INSERT INTO available_questions(question, correct_answer)\
VALUES("${questions.question}","${questions.correct_answer}")`
const sql = `INSERT INTO available_questions(question, possible_answer1, possible_answer2, possible_answer3, correct_answer)\
VALUES("${questions.question}","${questions.possible_answer1}","${questions.possible_answer2}","${questions.possible_answer3}","${questions.correct_answer}")`
console.log(sql)
await this.db.run(sql)
} catch(err) {
@@ -31,11 +31,11 @@ module.exports = class Question {
}
}

//SELECT ALL QUESTIONS IN DB
//SELECT ALL QUESTIONS IN DB
async all(){
try {
console.log('ALL questions')
const sql = 'SELECT id, question, correct_answer FROM available_questions'
const sql = 'SELECT id, question, possible_answer1, possible_answer2, possible_answer3, correct_answer FROM available_questions'
console.log(sql)
const questiont = await this.db.all(sql)
console.log(questiont)
@@ -45,4 +45,19 @@ module.exports = class Question {
throw err
}
}

//GETS THE ID
async getById(id){
try{
console.log('BY ID')
console.log(id)
const sql =`SELECT * FROM available_questions WHERE id=${id}`
console.log(sql)
const question = await this.db.get(sql)
return question
} catch(err) {
console.log(err.message)
throw err
}
}
}
@@ -13,8 +13,8 @@ const router = new Router({ prefix: '/secure' })
router.get('/', async ctx => {
try {
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure')
const answers = await new Answer(dbName)
ctx.hbs.answert = await answers.all()
//const answers = await new Answer(dbName)
//ctx.hbs.answert = await answers.all()
console.log(ctx.hbs)
await ctx.render('menu.handlebars', ctx.hbs)
//this renders the MENU page
@@ -28,7 +28,7 @@ router.get('/', async ctx => {
//Render the BACKOFFICE page
router.get('/backoffice', async ctx =>{
try{
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure')
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/backoffice')
const questions = await new Question(dbName)
ctx.hbs.questiont = await questions.all()
console.log(ctx.hbs)
@@ -43,7 +43,7 @@ router.get('/backoffice', async ctx =>{
//Render the TEACHING CONTENT page
router.get('/learnGit', async ctx =>{
try{
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure')
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/learnGit')
//const gitcontent = await new GitContent(dbName)
//ctx.hbs.gitselected = await gitcontent.allgt()
console.log(ctx.hbs)
@@ -59,7 +59,6 @@ router.get('/learnGit', async ctx =>{
router.get('/quiz', async ctx => {
try {
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/quiz')
console.log(ctx.hbs)
const questions = await new Question(dbName)
ctx.hbs.questiont = await questions.all()
console.log(ctx.hbs)
@@ -89,7 +88,7 @@ router.post('/quiz', koaBody, async ctx => {
//Gets the QUESTION input from the quizpage
router.post('/questionz', koaBody, async ctx => {
try{
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/quiz')
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/questionz')
const body = ctx.request.body
console.log(body)
const questions = await new Question(dbName)
@@ -104,7 +103,7 @@ router.post('/questionz', koaBody, async ctx => {
//Gets the git content input
router.post('/gitcontentz', koaBody, async ctx => {
try{
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/quiz')
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/gitcontentz')
const body = ctx.request.body
console.log(body)
const gitcontent = await new GitContent(dbName)
@@ -116,5 +115,21 @@ router.post('/gitcontentz', koaBody, async ctx => {
}
})

//Render for the SECOND QUIZ PAGE handlebar
router.get('/quizpage2/:id', async ctx => {
try {
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/quizpage2')
console.log(ctx.hbs)
const questions = await new Question(dbName)
ctx.hbs.questiont = await questions.getById(ctx.params.id)
console.log(ctx.hbs)
await ctx.render('quizpage2', ctx.hbs)
} catch(err) {
ctx.hbs.error = err.message
await ctx.render('error', ctx.hbs)
}
})



module.exports = router
@@ -1,6 +1,109 @@

const Accounts = require('../modules/user.js')

//TEST FOR ALL POSSIBLE ACTIONS -> answers
describe('insert answer in DB()', () =>{

//INSERT DATA WITH SUCCESS
test('insert data', async done =>{
expect.assertions(1)
const account = await new Accounts()
const insert_answer = await account.quiz('this is an answer')
expect(insert_answer).toBe(true)
account.tearDown()
done()
})

//MISSING FIELD IN TEST
test('error if blank answer', async done => {
expect.assertions(1)
const account = await new Accounts()
await expect(account.quiz(''))
.rejects.toEqual( Error('missing field'))
done()
})

//INSERT AN ANSWER ALREADY IN DB
test('insert duplicated data', async done =>{
expect.assertions(1)
const account = await new Accounts()
await account.quiz('this is a duplicated answer')
await expect( account.quiz('this is a duplicated answer') )
.rejects.toEqual( Error('the answer was already given') )
account.tearDown()
done()
})

})

//TEST FOR ALL POSSIBLE ACTIONS -> questions, possible answers and correct answer
describe('insert questions and possible answers', () => {

//INSERT DATA WITH SUCCESS
test('insert data', async done =>{
expect.assertions(1)
const account = await new Accounts()
const insert_answer = await account.questionz('this a question?','possible answer 1','possible answer 2', 'possible answer 3','correct answer')
expect(insert_answer).toBe(true)
account.tearDown()
done()
})

//MISSING FIELD IN TEST
test('error if blank answer', async done => {
expect.assertions(1)
const account = await new Accounts()
await expect(account.questionz(''))
.rejects.toEqual( Error('missing field'))
done()
})

//INSERT AN ANSWER ALREADY IN DB
test('insert duplicated data', async done =>{
expect.assertions(1)
const account = await new Accounts()
await account.questionz('this is a duplicated question','this is a duplicated possible answer 1','this is a duplicated possible answer 2','this is a duplicated possible answer 3','this is a duplicated correct answer')
await expect( account.questionz('this is a duplicated question','this is a duplicated possible answer 1','this is a duplicated possible answer 2','this is a duplicated possible answer 3','this is a duplicated correct answer') )
.rejects.toEqual( Error('duplicated fields') )
account.tearDown()
done()
})
})

//TEST FOR ALL POSSIBLE ACTIONS -> git content
describe('insert git content', () => {

//INSERT DATA WITH SUCCESS
test('insert data', async done =>{
expect.assertions(1)
const account = await new Accounts()
const insert_gitcontent = await account.content('random title','block content test')
expect(insert_gitcontent).toBe(true)
account.tearDown()
done()
})

//MISSING FIELD IN TEST
test('error if blank answer', async done => {
expect.assertions(1)
const account = await new Accounts()
await expect(account.content(''))
.rejects.toEqual( Error('missing field'))
done()
})

//INSERT AN ANSWER ALREADY IN DB
test('insert duplicated data', async done =>{
expect.assertions(1)
const account = await new Accounts()
await account.content('this is a duplicated title','this is a duplicated block content test')
await expect( account.content('this is a duplicated title','this is a duplicated block content test') )
.rejects.toEqual( Error('duplicated fields') )
account.tearDown()
done()
})
})

describe('register()', () => {

test('register a valid account', async done => {
@@ -15,4 +15,12 @@
</table>
</article>

DISPLAY DATA !!!
DISPLAY DATA !!!


<form action="/secure/quiz" enctype="multipart/form-data" method="post">
<p>
<input type="text" name="answer" placeholder="Type in your answer" value="" required>
</p>
<p><input type="submit" value="Submit Answer"></p>
<p><a href="/secure">Secure Page</a></p>
@@ -19,15 +19,9 @@
{{/if}}
<main>
{{#each questiont}}
<p>{{this.question}}</p>
<p>Press the first question at random to start</p>
<p><a href="/secure/quizpage2/{{this.id}}">Question:</a></p>
{{/each}}
<form action="/secure/quiz" enctype="multipart/form-data" method="post">
<p>
<label for="answer">Answer</label><br />
<input type="text" name="answer" placeholder="question answer" value="" required>
</p>
<p><input type="submit" value="Create"></p>
<p><a href="/secure">Secure Page</a></p>
</main>

</body>
@@ -0,0 +1,35 @@
<!doctype html>

<html lang="en">
<head>
<meta charset="utf-8">
<title>Quiz Page</title>
<meta name="description" content="form for the Quiz">
<meta name="author" content="Daniel Dias">
<link href="{{host}}/style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<header>
<h1>Questions</h1>
<a href="/logout">Log out</a>
</header>
{{#if msg}}
<p class="msg">{{msg}}</p>
{{/if}}
<main>
<p>Question: </p>
<p>-> {{questiont.question}}</p>
<tr>
<td>{{questiont.possible_answer1}}</td></br>
</br>
<td>{{questiont.possible_answer2}}</td></br>
</br>
<td>{{questiont.possible_answer3}}</td>
</tr>

<p><a href="/secure/quizpage3/{{this.id}}">Next Question</a></p>
<p><a href="/secure/quiz">Previous Question</a></p>
</main>
</body>

</html>

0 comments on commit 5577e69

Please sign in to comment.