Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
"name": "Launch Program",
"program": "${workspaceFolder}/index.js",
"smartStep": true
},
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}
54 changes: 6 additions & 48 deletions __mocks__/db.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,13 @@
var SequelizeMock = require('sequelize-mock');
var dbMock = new SequelizeMock();

const Questions = dbMock.define(
'questions',
[
{
id: 1,
question: '12345',
subjectId: 1,
level: 1,
},
{
id: 2,
question: '456789',
subjectId: 2,
level: 2,
},
],
{ timestamps: false }
);
const Questions = dbMock.define('questions', {}, { timestamps: false });
const Answers = dbMock.define('answers', {}, { timestamps: false });

const Answers = dbMock.define(
'answers',
[
{
id: 1,
answer: '1234',
isCorrect: 1,
questionId: 1,
},
{
id: 2,
answer: '1234',
isCorrect: 0,
questionId: 1,
},
{
id: 3,
answer: '1234',
isCorrect: 0,
questionId: 1,
},
{
id: 4,
answer: '1234',
isCorrect: 0,
questionId: 1,
},
],
{ timestamps: false }
);
Questions.hasMany(Answers);
Answers.belongsTo(Questions, {
foreignKey: 'questionId',
});

module.exports.db = dbMock;
module.exports.Questions = Questions;
Expand Down
3 changes: 3 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use('/api/v1', require('./routes/api'));
app.use(function(err, req, res, next) {
res.status(422).json(err);
});

module.exports = app;
1 change: 0 additions & 1 deletion config/dev.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
HOST=localhost
PORT=1234
SUBJECTS_AMOUNT=3
HASH=$2b$10$PjqecawtIkC0tAPJhKjHGOH7N8KyZuhMNoQtP79fE.zGnoHnbjuxe
DB_HOST=localhost
DB_NAME=quiz
Expand Down
1 change: 0 additions & 1 deletion config/test.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
ENV=testing
HOST=localhost
PORT=1234
SUBJECTS_AMOUNT=3
HASH=$2b$10$PjqecawtIkC0tAPJhKjHGOH7N8KyZuhMNoQtP79fE.zGnoHnbjuxe
DB_HOST=localhost
DB_NAME=testquiz
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
"test": "env-cmd -f ./config/test.env jest --watch"
},
"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"collectCoverage": true,
"collectCoverageFrom": [
"routes/api.js",
"validation/answers.js",
"!**/node_modules/**"
]
},
"repository": {
"type": "git",
Expand Down
17 changes: 9 additions & 8 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ validate.options({
router.post('/questions', validate(questionValidation), function(req, res) {
bcrypt.compare(req.body.password, HASH).then(function(pass) {
if (pass) {
Questions.create(req.body, {
Questions.findOrCreate(req.body, {
include: [Answers],
})
.then(_ => {
Expand All @@ -41,7 +41,7 @@ router.post('/questions', validate(questionValidation), function(req, res) {
})
.catch(e => {
res.status(500).send('Server error');
console.error('Error: ', e);
// console.error('Error: ', e);
return;
});
} else {
Expand All @@ -66,7 +66,7 @@ router.get('/questions/:id', function(req, res) {
})
.catch(e => {
res.status(500).send('Server error');
console.error('Error: ', e);
// console.error('Error: ', e);
return;
});
});
Expand Down Expand Up @@ -102,7 +102,7 @@ router.get('/questions', validate(queryValidation), function(req, res) {
})
.catch(e => {
res.status(500).send('Server error');
console.error('Error: ', e);
// console.error('Error: ', e);
return;
});
});
Expand All @@ -119,7 +119,7 @@ router.put('/questions', validate(updateValidation), function(req, res) {
try {
validateAnswersUpdate(question.answers, req.body.answers);
} catch (e) {
console.log(e.message);
//console.log(e.message);
res.status(422).send(e.message);
return;
}
Expand All @@ -129,21 +129,22 @@ router.put('/questions', validate(updateValidation), function(req, res) {
if (updatedQuestion) {
updatedQuestion.answers.forEach(answer => {
answer.update(answer.dataValues).catch(e => {
// console.error('Error: ', e);
res.status(500).send('Server error');
console.error('Error: ', e);
return;
});
});
res.status(200).send('Ok');
return;
} else {
// console.log('Answers updating server error');
res.status(500).send('Server error');
return;
}
})
.catch(e => {
// console.log('Question updating server error', e);
res.status(500).send('Server error');
console.error('Error: ', e);
return;
});
} else {
Expand Down Expand Up @@ -188,7 +189,7 @@ router.get('/start', validate(startValidation), function(req, res) {
})
.catch(e => {
res.status(500).send('Server error');
console.error('Error: ', e);
// console.error('Error: ', e);
return;
});
});
Expand Down
133 changes: 0 additions & 133 deletions test/api.testwithmocha.js

This file was deleted.

Loading