Skip to content

Commit 306de05

Browse files
committed
✅ Update test with /patch/:id
modified: back/test/quizz.test.js
1 parent 70d8ebc commit 306de05

File tree

2 files changed

+105
-21
lines changed

2 files changed

+105
-21
lines changed

back/routes/quizz.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,34 +86,40 @@ router
8686

8787
.delete('/:id/delete',
8888
async (req, res) => {
89-
const result = await pool.query('DELETE FROM quizz WHERE id_quizz = $1', [req.params.id], (err, res) => {
90-
if (err) return;
91-
else return res;
92-
});
93-
if (result === undefined) {
89+
const result = await pool.query('DELETE FROM quizz WHERE id_quizz = $1', [req.params.id]);
90+
if (result.rowCount === 0) {
9491
return res.status(404)
9592
.send({
9693
error: 'Quizz can\'t be deleted because it doesn\'t exist'
9794
});
9895
}
99-
res.status(204).send(res);
96+
res.status(204).end();
10097
})
10198

10299
.patch('/:id',
103100
upload.single('file'), async (req, res) => {
104101

105-
if (req.body.title !== '') {
106-
await pool.query('UPDATE quizz SET title = $1 WHERE id_quizz=$2', [req.body.title, req.params.id]);
102+
let result = undefined;
103+
104+
if (req.body.path_file && req.file) {
105+
result = await pool.query('UPDATE quizz SET path_file=$1 WHERE id_quizz=$2', [req.body.path_file, req.params.id]);
106+
} else if(req.body.path_file && !req.file) {
107+
return res.status(500).send({error:'You cant modify the path_file without providing an image'});
107108
}
108109

109-
if (req.body.path_file !== '') {
110-
await pool.query('UPDATE quizz SET path_file=$1 WHERE id_quizz=$2', [req.body.path_file, req.params.id]);
110+
if (req.body.title) {
111+
result = await pool.query('UPDATE quizz SET title = $1 WHERE id_quizz=$2', [req.body.title, req.params.id]);
111112
}
112-
113-
if (req.body.difficulty !== '') {
114-
await pool.query('UPDATE quizz SET difficulty=$1 WHERE id_quizz=$2', [req.body.difficulty, req.params.id]);
113+
114+
if (req.body.difficulty) {
115+
result = await pool.query('UPDATE quizz SET difficulty=$1 WHERE id_quizz=$2', [req.body.difficulty, req.params.id]);
115116
}
116-
117+
118+
if(result === undefined) {
119+
return res.status(500).send({error:'You didnt provide info for one or more field nor quizz doesnt exist'});
120+
}else if(result.rowCount === 0) {
121+
return res.status(404).send({error: 'Quizz not found and can\'t be patched'});
122+
}
117123
res.status(204).end();
118124

119125
})

back/test/quizz.test.js

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,14 @@ describe('Quizzes', () => {
185185
done();
186186
});
187187
});
188-
describe('DELETE /:id', () => {
189-
it('should return 404 if the quizz doesn\'t exist and therefore can\'t be deleted', function(done) {
188+
describe('DELETE /:id', () => {
189+
it('should return 404 if the quizz doesn\'t exist and therefore can\'t be deleted', function (done) {
190190
chai
191-
.request(app)
192-
.delete('/quizzes/1024/delete')
193-
.end((err,res) => {
194-
res.should.have.status(404);
195-
});
191+
.request(app)
192+
.delete('/quizzes/1024/delete')
193+
.end((err, res) => {
194+
res.should.have.status(404);
195+
});
196196
done();
197197
});
198198
it('should return 204 if deleted successfully', function (done) {
@@ -205,4 +205,82 @@ describe('Quizzes', () => {
205205
done();
206206
});
207207
});
208+
describe('PATCH /:id', () => {
209+
it('should return 204 if we try to modify both the path_file and add a new image', function (done) {
210+
chai
211+
.request(app)
212+
.patch('/quizzes/1')
213+
.field('path_file', 'matrix.jpg')
214+
.attach('file', fs.readFileSync(path.join(__dirname, '/assets/matrix.jpg')), 'matrix.jpg')
215+
.end((err, res) => {
216+
res.should.have.status(204);
217+
});
218+
done();
219+
});
220+
it('should return 404 if the quizz doesn\'t exist and therefore can\'t be patched', function (done) {
221+
chai
222+
.request(app)
223+
.patch('/quizzes/1024')
224+
.field('title', 'Totoro')
225+
.end((err, res) => {
226+
res.should.have.status(404);
227+
});
228+
done();
229+
});
230+
it('should return 204 and patch title', function (done) {
231+
chai
232+
.request(app)
233+
.patch('/quizzes/1')
234+
.field('title', "les animaux d''afrique")
235+
.end((err, res) => {
236+
res.should.have.status(204);
237+
});
238+
done();
239+
});
240+
it('should return 204 and patch difficulty', function (done) {
241+
chai
242+
.request(app)
243+
.patch('/quizzes/1')
244+
.field('difficulty', 3)
245+
.end((err, res) => {
246+
res.should.have.status(204);
247+
});
248+
done();
249+
});
250+
it('should return 500 if we try to modify the path_file without a new fille associated', function (done) {
251+
chai
252+
.request(app)
253+
.patch('/quizzes/1')
254+
.field('path_file', 'test.jpg')
255+
.end((err, res) => {
256+
res.should.have.status(500);
257+
});
258+
done();
259+
});
260+
it('should return 500 if we try to modify the path_file and another field without a new fille associated', function (done) {
261+
chai
262+
.request(app)
263+
.patch('/quizzes/1')
264+
.field('path_file', 'test.jpg')
265+
.field('title', 'Matrix')
266+
.end((err, res) => {
267+
res.should.have.status(500);
268+
});
269+
done();
270+
});
271+
it('should return 204 if we try to modify all infos of a quizz but also the image', function (done) {
272+
chai
273+
.request(app)
274+
.patch('/quizzes/4')
275+
.field('title', 'Matrix')
276+
.field('difficulty', 2)
277+
.field('id_creator', 1)
278+
.field('path_file', 'matrix.jpg')
279+
.attach('file', fs.readFileSync(path.join(__dirname, '/assets/matrix.jpg')), 'matrix.jpg')
280+
.end((err, res) => {
281+
res.should.have.status(204);
282+
});
283+
done();
284+
});
285+
});
208286
});

0 commit comments

Comments
 (0)