@@ -388,3 +388,152 @@ test.serial(`${currentTest} find table categories with tables`, async (t) => {
388388 t . fail ( ) ;
389389 }
390390} ) ;
391+
392+ test . serial ( `${ currentTest } should preserve table order from database in category response` , async ( t ) => {
393+ try {
394+ const connectionToTestDB = getTestData ( mockFactory ) . connectionToPostgres ;
395+ const firstUserToken = ( await registerUserAndReturnUserInfo ( app ) ) . token ;
396+ const { testTableName : firstTestTableName } = await createTestTable ( connectionToTestDB ) ;
397+ const { testTableName : secondTestTableName } = await createTestTable ( connectionToTestDB ) ;
398+ const { testTableName : thirdTestTableName } = await createTestTable ( connectionToTestDB ) ;
399+
400+ const createConnectionResponse = await request ( app . getHttpServer ( ) )
401+ . post ( '/connection' )
402+ . send ( connectionToTestDB )
403+ . set ( 'Cookie' , firstUserToken )
404+ . set ( 'Content-Type' , 'application/json' )
405+ . set ( 'Accept' , 'application/json' ) ;
406+ const createConnectionRO = JSON . parse ( createConnectionResponse . text ) ;
407+ t . is ( createConnectionResponse . status , 201 ) ;
408+
409+ // intentionally order tables as: third, first, second
410+ const categoriesDTO : Array < CreateTableCategoryDto > = [
411+ {
412+ category_name : 'Ordered Category' ,
413+ category_color : '#AABBCC' ,
414+ tables : [ thirdTestTableName , firstTestTableName , secondTestTableName ] ,
415+ category_id : 'cat-order-001' ,
416+ } ,
417+ ] ;
418+
419+ const createTableCategoriesResponse = await request ( app . getHttpServer ( ) )
420+ . put ( `/table-categories/${ createConnectionRO . id } ` )
421+ . send ( categoriesDTO )
422+ . set ( 'Cookie' , firstUserToken )
423+ . set ( 'Content-Type' , 'application/json' )
424+ . set ( 'Accept' , 'application/json' ) ;
425+ t . is ( createTableCategoriesResponse . status , 200 ) ;
426+
427+ const findTableCategoriesWithTablesResponse = await request ( app . getHttpServer ( ) )
428+ . get ( `/table-categories/v2/${ createConnectionRO . id } ` )
429+ . set ( 'Cookie' , firstUserToken )
430+ . set ( 'Content-Type' , 'application/json' )
431+ . set ( 'Accept' , 'application/json' ) ;
432+
433+ const findTableCategoriesWithTablesRO = JSON . parse ( findTableCategoriesWithTablesResponse . text ) ;
434+ t . is ( findTableCategoriesWithTablesResponse . status , 200 ) ;
435+
436+ // index 0 is "All tables", index 1 is our category
437+ t . is ( findTableCategoriesWithTablesRO [ 1 ] . category_name , 'Ordered Category' ) ;
438+ t . is ( findTableCategoriesWithTablesRO [ 1 ] . tables . length , 3 ) ;
439+
440+ // verify the order matches what was stored in the database: third, first, second
441+ t . is ( findTableCategoriesWithTablesRO [ 1 ] . tables [ 0 ] . table , thirdTestTableName ) ;
442+ t . is ( findTableCategoriesWithTablesRO [ 1 ] . tables [ 1 ] . table , firstTestTableName ) ;
443+ t . is ( findTableCategoriesWithTablesRO [ 1 ] . tables [ 2 ] . table , secondTestTableName ) ;
444+ } catch ( e ) {
445+ console . error ( e ) ;
446+ t . fail ( ) ;
447+ }
448+ } ) ;
449+
450+ test . serial ( `${ currentTest } should preserve table order after creating and updating categories` , async ( t ) => {
451+ try {
452+ const connectionToTestDB = getTestData ( mockFactory ) . connectionToPostgres ;
453+ const firstUserToken = ( await registerUserAndReturnUserInfo ( app ) ) . token ;
454+ const { testTableName : firstTestTableName } = await createTestTable ( connectionToTestDB ) ;
455+ const { testTableName : secondTestTableName } = await createTestTable ( connectionToTestDB ) ;
456+ const { testTableName : thirdTestTableName } = await createTestTable ( connectionToTestDB ) ;
457+
458+ const createConnectionResponse = await request ( app . getHttpServer ( ) )
459+ . post ( '/connection' )
460+ . send ( connectionToTestDB )
461+ . set ( 'Cookie' , firstUserToken )
462+ . set ( 'Content-Type' , 'application/json' )
463+ . set ( 'Accept' , 'application/json' ) ;
464+ const createConnectionRO = JSON . parse ( createConnectionResponse . text ) ;
465+ t . is ( createConnectionResponse . status , 201 ) ;
466+
467+ // create category with initial order: third, first, second
468+ const initialCategoriesDTO : Array < CreateTableCategoryDto > = [
469+ {
470+ category_name : 'Ordered Category' ,
471+ category_color : '#AABBCC' ,
472+ tables : [ thirdTestTableName , firstTestTableName , secondTestTableName ] ,
473+ category_id : 'cat-order-001' ,
474+ } ,
475+ ] ;
476+
477+ const createResponse = await request ( app . getHttpServer ( ) )
478+ . put ( `/table-categories/${ createConnectionRO . id } ` )
479+ . send ( initialCategoriesDTO )
480+ . set ( 'Cookie' , firstUserToken )
481+ . set ( 'Content-Type' , 'application/json' )
482+ . set ( 'Accept' , 'application/json' ) ;
483+ const createRO = JSON . parse ( createResponse . text ) ;
484+ t . is ( createResponse . status , 200 ) ;
485+
486+ // verify order in creation response
487+ t . deepEqual ( createRO [ 0 ] . tables , [ thirdTestTableName , firstTestTableName , secondTestTableName ] ) ;
488+
489+ // verify order in v2 GET after creation
490+ const getAfterCreateResponse = await request ( app . getHttpServer ( ) )
491+ . get ( `/table-categories/v2/${ createConnectionRO . id } ` )
492+ . set ( 'Cookie' , firstUserToken )
493+ . set ( 'Content-Type' , 'application/json' )
494+ . set ( 'Accept' , 'application/json' ) ;
495+ const getAfterCreateRO = JSON . parse ( getAfterCreateResponse . text ) ;
496+ t . is ( getAfterCreateResponse . status , 200 ) ;
497+ t . is ( getAfterCreateRO [ 1 ] . tables [ 0 ] . table , thirdTestTableName ) ;
498+ t . is ( getAfterCreateRO [ 1 ] . tables [ 1 ] . table , firstTestTableName ) ;
499+ t . is ( getAfterCreateRO [ 1 ] . tables [ 2 ] . table , secondTestTableName ) ;
500+
501+ // update category with new order: second, third, first
502+ const updatedCategoriesDTO : Array < CreateTableCategoryDto > = [
503+ {
504+ category_name : 'Ordered Category' ,
505+ category_color : '#AABBCC' ,
506+ tables : [ secondTestTableName , thirdTestTableName , firstTestTableName ] ,
507+ category_id : 'cat-order-001' ,
508+ } ,
509+ ] ;
510+
511+ const updateResponse = await request ( app . getHttpServer ( ) )
512+ . put ( `/table-categories/${ createConnectionRO . id } ` )
513+ . send ( updatedCategoriesDTO )
514+ . set ( 'Cookie' , firstUserToken )
515+ . set ( 'Content-Type' , 'application/json' )
516+ . set ( 'Accept' , 'application/json' ) ;
517+ const updateRO = JSON . parse ( updateResponse . text ) ;
518+ t . is ( updateResponse . status , 200 ) ;
519+
520+ // verify order in update response
521+ t . deepEqual ( updateRO [ 0 ] . tables , [ secondTestTableName , thirdTestTableName , firstTestTableName ] ) ;
522+
523+ // verify order in v2 GET after update
524+ const getAfterUpdateResponse = await request ( app . getHttpServer ( ) )
525+ . get ( `/table-categories/v2/${ createConnectionRO . id } ` )
526+ . set ( 'Cookie' , firstUserToken )
527+ . set ( 'Content-Type' , 'application/json' )
528+ . set ( 'Accept' , 'application/json' ) ;
529+ const getAfterUpdateRO = JSON . parse ( getAfterUpdateResponse . text ) ;
530+ t . is ( getAfterUpdateResponse . status , 200 ) ;
531+ t . is ( getAfterUpdateRO [ 1 ] . tables . length , 3 ) ;
532+ t . is ( getAfterUpdateRO [ 1 ] . tables [ 0 ] . table , secondTestTableName ) ;
533+ t . is ( getAfterUpdateRO [ 1 ] . tables [ 1 ] . table , thirdTestTableName ) ;
534+ t . is ( getAfterUpdateRO [ 1 ] . tables [ 2 ] . table , firstTestTableName ) ;
535+ } catch ( e ) {
536+ console . error ( e ) ;
537+ t . fail ( ) ;
538+ }
539+ } ) ;
0 commit comments