@@ -97,7 +97,9 @@ describe("Test Question API", () => {
97
97
98
98
const res = await request . post ( "/api/create" ) . send ( newQuestion ) ;
99
99
expect ( res . statusCode ) . toBe ( 400 ) ;
100
- expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
100
+ expect ( res . body . errors [ 0 ] . msg ) . toBe (
101
+ "Category must contain only non-empty strings"
102
+ ) ;
101
103
expect ( res . body . errors [ 0 ] . path ) . toBe ( "category" ) ;
102
104
} ) ;
103
105
@@ -172,7 +174,9 @@ describe("Test Question API", () => {
172
174
173
175
const res = await request . post ( "/api/create" ) . send ( newQuestion ) ;
174
176
expect ( res . statusCode ) . toBe ( 400 ) ;
175
- expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
177
+ expect ( res . body . errors [ 0 ] . msg ) . toBe (
178
+ "Category must contain only non-empty strings"
179
+ ) ;
176
180
expect ( res . body . errors [ 0 ] . path ) . toBe ( "category" ) ;
177
181
} ) ;
178
182
@@ -187,7 +191,9 @@ describe("Test Question API", () => {
187
191
188
192
const res = await request . post ( "/api/create" ) . send ( newQuestion ) ;
189
193
expect ( res . statusCode ) . toBe ( 400 ) ;
190
- expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
194
+ expect ( res . body . errors [ 0 ] . msg ) . toBe (
195
+ "Category must contain only non-empty strings"
196
+ ) ;
191
197
expect ( res . body . errors [ 0 ] . path ) . toBe ( "category" ) ;
192
198
} ) ;
193
199
@@ -202,7 +208,9 @@ describe("Test Question API", () => {
202
208
203
209
const res = await request . post ( "/api/create" ) . send ( newQuestion ) ;
204
210
expect ( res . statusCode ) . toBe ( 400 ) ;
205
- expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
211
+ expect ( res . body . errors [ 0 ] . msg ) . toBe (
212
+ "Category must contain only non-empty strings"
213
+ ) ;
206
214
expect ( res . body . errors [ 0 ] . path ) . toBe ( "category" ) ;
207
215
} ) ;
208
216
@@ -220,6 +228,20 @@ describe("Test Question API", () => {
220
228
expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Invalid value" ) ;
221
229
expect ( res . body . errors [ 0 ] . path ) . toBe ( "complexity" ) ;
222
230
} ) ;
231
+
232
+ // Duplicate question
233
+ test ( "POST /api/create - duplicate question" , async ( ) => {
234
+ const newQuestion = {
235
+ title : "Sample Question" ,
236
+ description : "This is a sample question" ,
237
+ category : [ "General" ] ,
238
+ complexity : "test" ,
239
+ } ;
240
+
241
+ const res = await request . post ( "/api/create" ) . send ( newQuestion ) ;
242
+ expect ( res . statusCode ) . toBe ( 400 ) ;
243
+ expect ( res . body . message ) . toBe ( "A question with this title already exists" ) ;
244
+ } ) ;
223
245
} ) ;
224
246
225
247
// Test /api/all
@@ -326,7 +348,7 @@ describe("Test Update", () => {
326
348
// Update with invalid category
327
349
test ( "POST - empty category" , async ( ) => {
328
350
const updateQuestion = {
329
- category : [ ]
351
+ category : [ ] ,
330
352
} ;
331
353
const questionId = 1090 ;
332
354
const res = await request
@@ -347,7 +369,9 @@ describe("Test Update", () => {
347
369
. post ( `/api/${ questionId } /update` )
348
370
. send ( updateQuestion ) ;
349
371
expect ( res . statusCode ) . toBe ( 400 ) ;
350
- expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
372
+ expect ( res . body . errors [ 0 ] . msg ) . toBe (
373
+ "Category must contain only non-empty strings"
374
+ ) ;
351
375
} ) ;
352
376
353
377
// Update with invalid category
@@ -362,13 +386,15 @@ describe("Test Update", () => {
362
386
. post ( `/api/${ questionId } /update` )
363
387
. send ( updateQuestion ) ;
364
388
expect ( res . statusCode ) . toBe ( 400 ) ;
365
- expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
389
+ expect ( res . body . errors [ 0 ] . msg ) . toBe (
390
+ "Category must contain only non-empty strings"
391
+ ) ;
366
392
} ) ;
367
393
368
394
// Negative id
369
395
test ( "POST - negative id update" , async ( ) => {
370
396
const updateQuestion = {
371
- title : "Update Title" ,
397
+ title : "Update Title Again " ,
372
398
description : "Update Description" ,
373
399
category : [ "Update Category" ] ,
374
400
complexity : "Update Complexity" ,
@@ -384,7 +410,7 @@ describe("Test Update", () => {
384
410
// Non-existent id
385
411
test ( "POST - non-existent id update" , async ( ) => {
386
412
const updateQuestion = {
387
- title : "Update Title" ,
413
+ title : "Update Title Again " ,
388
414
description : "Update Description" ,
389
415
category : [ "Update Category" ] ,
390
416
complexity : "Update Complexity" ,
@@ -396,6 +422,22 @@ describe("Test Update", () => {
396
422
expect ( res . statusCode ) . toBe ( 404 ) ;
397
423
expect ( res . body ) . toBe ( "Document not found" ) ;
398
424
} ) ;
425
+
426
+ // Duplicate question
427
+ test ( "POST - non-existent id update" , async ( ) => {
428
+ const updateQuestion = {
429
+ title : "Update Title" ,
430
+ description : "Update Description" ,
431
+ category : [ "Update Category" ] ,
432
+ complexity : "Update Complexity" ,
433
+ } ;
434
+ const questionId = 999999 ;
435
+ const res = await request
436
+ . post ( `/api/${ questionId } /update` )
437
+ . send ( updateQuestion ) ;
438
+ expect ( res . statusCode ) . toBe ( 400 ) ;
439
+ expect ( res . body . message ) . toBe ( "A question with this title already exists" ) ;
440
+ } ) ;
399
441
} ) ;
400
442
401
443
// Test /api/{id}/delete
0 commit comments