@@ -11,8 +11,6 @@ describe("Connect DB", () => {
11
11
12
12
test ( "GET - /api/" , async ( ) => {
13
13
const res = await request . get ( "/api/" ) . send ( ) ;
14
- const body = res . body ;
15
- const message = body . message ;
16
14
expect ( res . statusCode ) . toBe ( 200 ) ;
17
15
expect ( res . text ) . toBe ( "Hello from question service!" ) ;
18
16
} ) ;
@@ -264,6 +262,218 @@ describe("Test Get All", () => {
264
262
expect ( sampleQuestion ) . toHaveProperty ( "category" , [ "General" ] ) ;
265
263
expect ( sampleQuestion ) . toHaveProperty ( "complexity" , "Easy" ) ;
266
264
} ) ;
265
+
266
+ test ( "POST /api/all - filter questions" , async ( ) => {
267
+ const searchQuestionTitle = {
268
+ title : "Sample Question" ,
269
+ } ;
270
+
271
+ const searchQuestionComplexity = {
272
+ complexity : [ "Hard" ] ,
273
+ } ;
274
+
275
+ const searchQuestionCategory = {
276
+ category : [ "Dynamic Programming" ] ,
277
+ } ;
278
+
279
+ const newQuestion = {
280
+ title : "Another Question" ,
281
+ description : "This is another sample question" ,
282
+ category : [ "Dynamic Programming" ] ,
283
+ complexity : "Hard" ,
284
+ } ;
285
+ await request . post ( "/api/create" ) . send ( newQuestion ) ;
286
+ const res = await request . post ( "/api/all" ) . send ( searchQuestionTitle ) ;
287
+ const sampleQuestion = res . body . questions [ 0 ] ;
288
+ expect ( res . statusCode ) . toBe ( 200 ) ;
289
+ expect ( Array . isArray ( res . body . questions ) ) . toBe ( true ) ;
290
+ expect ( res . body . questions . length ) . toBe ( 1 ) ;
291
+ expect ( res . body . currentPage ) . toBe ( 1 ) ;
292
+ expect ( res . body . totalPages ) . toBe ( 1 ) ;
293
+ expect ( res . body . totalQuestions ) . toBe ( 1 ) ;
294
+ expect ( sampleQuestion ) . toHaveProperty ( "title" , "Sample Question" ) ;
295
+ expect ( sampleQuestion ) . toHaveProperty (
296
+ "description" ,
297
+ "This is a sample question"
298
+ ) ;
299
+ expect ( sampleQuestion ) . toHaveProperty ( "category" , [ "General" ] ) ;
300
+ expect ( sampleQuestion ) . toHaveProperty ( "complexity" , "Easy" ) ;
301
+
302
+ const res2 = await request . post ( "/api/all" ) . send ( searchQuestionComplexity ) ;
303
+ const sampleQuestion2 = res2 . body . questions [ 0 ] ;
304
+ expect ( res2 . statusCode ) . toBe ( 200 ) ;
305
+ expect ( Array . isArray ( res2 . body . questions ) ) . toBe ( true ) ;
306
+ expect ( res2 . body . questions . length ) . toBe ( 1 ) ;
307
+ expect ( res2 . body . currentPage ) . toBe ( 1 ) ;
308
+ expect ( res2 . body . totalPages ) . toBe ( 1 ) ;
309
+ expect ( res2 . body . totalQuestions ) . toBe ( 1 ) ;
310
+ expect ( sampleQuestion2 ) . toHaveProperty ( "title" , "Another Question" ) ;
311
+ expect ( sampleQuestion2 ) . toHaveProperty (
312
+ "description" ,
313
+ "This is another sample question"
314
+ ) ;
315
+ expect ( sampleQuestion2 ) . toHaveProperty ( "category" , [ "Dynamic Programming" ] ) ;
316
+ expect ( sampleQuestion2 ) . toHaveProperty ( "complexity" , "Hard" ) ;
317
+
318
+ const res3 = await request . post ( "/api/all" ) . send ( searchQuestionCategory ) ;
319
+ const sampleQuestion3 = res3 . body . questions [ 0 ] ;
320
+ expect ( res3 . statusCode ) . toBe ( 200 ) ;
321
+ expect ( Array . isArray ( res3 . body . questions ) ) . toBe ( true ) ;
322
+ expect ( res3 . body . questions . length ) . toBe ( 1 ) ;
323
+ expect ( res3 . body . currentPage ) . toBe ( 1 ) ;
324
+ expect ( res3 . body . totalPages ) . toBe ( 1 ) ;
325
+ expect ( res3 . body . totalQuestions ) . toBe ( 1 ) ;
326
+ expect ( sampleQuestion3 ) . toHaveProperty ( "title" , "Another Question" ) ;
327
+ expect ( sampleQuestion3 ) . toHaveProperty (
328
+ "description" ,
329
+ "This is another sample question"
330
+ ) ;
331
+ expect ( sampleQuestion3 ) . toHaveProperty ( "category" , [ "Dynamic Programming" ] ) ;
332
+ expect ( sampleQuestion3 ) . toHaveProperty ( "complexity" , "Hard" ) ;
333
+ } ) ;
334
+
335
+ test ( "POST /api/all - No questions found" , async ( ) => {
336
+ const searchQuestionTitle = {
337
+ title : "Sample Question that doesn't exist" ,
338
+ } ;
339
+
340
+ const searchQuestionComplexity = {
341
+ complexity : [ "Do not exist" ] ,
342
+ } ;
343
+
344
+ const searchQuestionCategory = {
345
+ category : [ "Do not exist" ] ,
346
+ } ;
347
+
348
+ const res = await request . post ( "/api/all" ) . send ( searchQuestionTitle ) ;
349
+ const res2 = await request . post ( "/api/all" ) . send ( searchQuestionComplexity ) ;
350
+ const res3 = await request . post ( "/api/all" ) . send ( searchQuestionCategory ) ;
351
+ expect ( res . statusCode ) . toBe ( 200 ) ;
352
+ expect ( res . body . questions . length ) . toBe ( 0 ) ;
353
+ expect ( res . body . totalPages ) . toBe ( 0 ) ;
354
+ expect ( res . body . totalQuestions ) . toBe ( 0 ) ;
355
+
356
+ expect ( res2 . statusCode ) . toBe ( 200 ) ;
357
+ expect ( res2 . body . questions . length ) . toBe ( 0 ) ;
358
+ expect ( res2 . body . totalPages ) . toBe ( 0 ) ;
359
+ expect ( res2 . body . totalQuestions ) . toBe ( 0 ) ;
360
+
361
+ expect ( res3 . statusCode ) . toBe ( 200 ) ;
362
+ expect ( res3 . body . questions . length ) . toBe ( 0 ) ;
363
+ expect ( res3 . body . totalPages ) . toBe ( 0 ) ;
364
+ expect ( res3 . body . totalQuestions ) . toBe ( 0 ) ;
365
+ } ) ;
366
+ } ) ;
367
+
368
+ // Test /api/pick-question
369
+ describe ( "Test pick question" , ( ) => {
370
+ // Valid pick question
371
+ test ( "POST - pick question" , async ( ) => {
372
+ const pickQuestionFilter = {
373
+ complexity : [ "Hard" ] ,
374
+ category : [ "Dynamic Programming" ] ,
375
+ } ;
376
+ const res = await request
377
+ . post ( `/api/pick-question` )
378
+ . send ( pickQuestionFilter ) ;
379
+ const sampleQuestion = res . body ;
380
+ expect ( res . statusCode ) . toBe ( 200 ) ;
381
+ expect ( sampleQuestion ) . toHaveProperty ( "title" , "Another Question" ) ;
382
+ expect ( sampleQuestion ) . toHaveProperty (
383
+ "description" ,
384
+ "This is another sample question"
385
+ ) ;
386
+ expect ( sampleQuestion ) . toHaveProperty ( "category" , [ "Dynamic Programming" ] ) ;
387
+ expect ( sampleQuestion ) . toHaveProperty ( "complexity" , "Hard" ) ;
388
+ } ) ;
389
+
390
+ test ( "POST - pick another question" , async ( ) => {
391
+ const pickQuestionFilter = {
392
+ complexity : [ "Easy" ] ,
393
+ category : [ "General" ] ,
394
+ } ;
395
+ const res = await request
396
+ . post ( `/api/pick-question` )
397
+ . send ( pickQuestionFilter ) ;
398
+ const sampleQuestion = res . body ;
399
+ expect ( res . statusCode ) . toBe ( 200 ) ;
400
+ expect ( sampleQuestion ) . toHaveProperty ( "title" , "Sample Question" ) ;
401
+ expect ( sampleQuestion ) . toHaveProperty (
402
+ "description" ,
403
+ "This is a sample question"
404
+ ) ;
405
+ expect ( sampleQuestion ) . toHaveProperty ( "category" , [ "General" ] ) ;
406
+ expect ( sampleQuestion ) . toHaveProperty ( "complexity" , "Easy" ) ;
407
+ } ) ;
408
+
409
+ // No question exists
410
+ test ( "POST - pick question that doesn't exist" , async ( ) => {
411
+ const pickQuestionFilter = {
412
+ complexity : [ "Medium" ] ,
413
+ category : [ "General" ] ,
414
+ } ;
415
+ const res = await request
416
+ . post ( `/api/pick-question` )
417
+ . send ( pickQuestionFilter ) ;
418
+ expect ( res . statusCode ) . toBe ( 404 ) ;
419
+ expect ( res . body . message ) . toBe ( "No questions found" ) ;
420
+ } ) ;
421
+
422
+ // Empty complexity
423
+ test ( "POST - empty complexity" , async ( ) => {
424
+ const pickQuestionFilter = {
425
+ complexity : [ ] ,
426
+ category : [ "General" ] ,
427
+ } ;
428
+ const res = await request
429
+ . post ( `/api/pick-question` )
430
+ . send ( pickQuestionFilter ) ;
431
+ expect ( res . statusCode ) . toBe ( 400 ) ;
432
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Complexity must be a non-empty array" ) ;
433
+ } ) ;
434
+
435
+ // Invalid complexity
436
+ test ( "POST - invalid complexity" , async ( ) => {
437
+ const pickQuestionFilter = {
438
+ complexity : [ "" ] ,
439
+ category : [ "General" ] ,
440
+ } ;
441
+ const res = await request
442
+ . post ( `/api/pick-question` )
443
+ . send ( pickQuestionFilter ) ;
444
+ expect ( res . statusCode ) . toBe ( 400 ) ;
445
+ expect ( res . body . errors [ 0 ] . msg ) . toBe (
446
+ "Complexity must contain only non-empty strings"
447
+ ) ;
448
+ } ) ;
449
+
450
+ // Empty category
451
+ test ( "POST - empty category" , async ( ) => {
452
+ const pickQuestionFilter = {
453
+ complexity : [ "Easy" ] ,
454
+ category : [ ] ,
455
+ } ;
456
+ const res = await request
457
+ . post ( `/api/pick-question` )
458
+ . send ( pickQuestionFilter ) ;
459
+ expect ( res . statusCode ) . toBe ( 400 ) ;
460
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must be a non-empty array" ) ;
461
+ } ) ;
462
+
463
+ // Invalid category
464
+ test ( "POST - invalid category" , async ( ) => {
465
+ const pickQuestionFilter = {
466
+ complexity : [ "Easy" ] ,
467
+ category : [ "" ] ,
468
+ } ;
469
+ const res = await request
470
+ . post ( `/api/pick-question` )
471
+ . send ( pickQuestionFilter ) ;
472
+ expect ( res . statusCode ) . toBe ( 400 ) ;
473
+ expect ( res . body . errors [ 0 ] . msg ) . toBe (
474
+ "Category must contain only non-empty strings"
475
+ ) ;
476
+ } ) ;
267
477
} ) ;
268
478
269
479
// Test /api/{id}
@@ -475,5 +685,5 @@ describe("Test Delete", () => {
475
685
afterAll ( async ( ) => {
476
686
await db . clearDatabase ( ) ;
477
687
await db . closeDatabase ( ) ;
478
- await server . close ( ) ;
688
+ server . close ( ) ;
479
689
} ) ;
0 commit comments