@@ -406,6 +406,19 @@ describe('MigrationsModule', () => {
406406 await assert . rejects ( ( ) => inst . runMigrations ( ) , { message : 'boom' } )
407407 assert . equal ( inst . executeMigration . mock . callCount ( ) , 1 )
408408 } )
409+
410+ it ( 'should skip completed check in dryRun mode' , async ( ) => {
411+ const inst = createInstance ( )
412+ inst . executeMigration = mock . fn ( )
413+ inst . discoverMigrations = mock . fn ( async ( ) => [ createMigration ( ) ] )
414+ inst . getCompletedMigrations = mock . fn ( async ( ) => [
415+ { module : 'mod-a' , version : '1.0.0' }
416+ ] )
417+ await inst . runMigrations ( { dryRun : true } )
418+
419+ assert . equal ( inst . getCompletedMigrations . mock . callCount ( ) , 0 )
420+ assert . equal ( inst . executeMigration . mock . callCount ( ) , 1 )
421+ } )
409422 } )
410423
411424 describe ( 'executeMigration' , ( ) => {
@@ -650,4 +663,70 @@ describe('MigrationsModule', () => {
650663 assert . ok ( doc . completedAt instanceof Date )
651664 } )
652665 } )
666+
667+ describe ( 'resetHandler' , ( ) => {
668+ function createRes ( ) {
669+ const res = {
670+ statusCode : 200 ,
671+ status : mock . fn ( function ( code ) { res . statusCode = code ; return res } ) ,
672+ json : mock . fn ( )
673+ }
674+ return res
675+ }
676+
677+ it ( 'should return 400 when module is missing' , async ( ) => {
678+ const inst = createInstance ( )
679+ inst . resetHandler = proto . resetHandler
680+ const res = createRes ( )
681+ await inst . resetHandler ( { body : { version : '1.0.0' } } , res , mock . fn ( ) )
682+
683+ assert . equal ( res . statusCode , 400 )
684+ } )
685+
686+ it ( 'should return 400 when version is missing' , async ( ) => {
687+ const inst = createInstance ( )
688+ inst . resetHandler = proto . resetHandler
689+ const res = createRes ( )
690+ await inst . resetHandler ( { body : { module : 'mod-a' } } , res , mock . fn ( ) )
691+
692+ assert . equal ( res . statusCode , 400 )
693+ } )
694+
695+ it ( 'should return 404 when no record found' , async ( ) => {
696+ const deleteOneMock = mock . fn ( async ( ) => ( { deletedCount : 0 } ) )
697+ const inst = createInstance ( {
698+ db : { collection : mock . fn ( ( ) => ( { deleteOne : deleteOneMock } ) ) }
699+ } )
700+ inst . resetHandler = proto . resetHandler
701+ const res = createRes ( )
702+ await inst . resetHandler ( { body : { module : 'mod-a' , version : '1.0.0' } } , res , mock . fn ( ) )
703+
704+ assert . equal ( res . statusCode , 404 )
705+ } )
706+
707+ it ( 'should delete the record and return success' , async ( ) => {
708+ const deleteOneMock = mock . fn ( async ( ) => ( { deletedCount : 1 } ) )
709+ const collectionMock = mock . fn ( ( ) => ( { deleteOne : deleteOneMock } ) )
710+ const inst = createInstance ( { db : { collection : collectionMock } } )
711+ inst . resetHandler = proto . resetHandler
712+ const res = createRes ( )
713+ await inst . resetHandler ( { body : { module : 'mod-a' , version : '1.0.0' } } , res , mock . fn ( ) )
714+
715+ assert . equal ( collectionMock . mock . calls [ 0 ] . arguments [ 0 ] , 'migrations' )
716+ assert . deepEqual ( deleteOneMock . mock . calls [ 0 ] . arguments [ 0 ] , { module : 'mod-a' , version : '1.0.0' } )
717+ assert . equal ( res . statusCode , 200 )
718+ } )
719+
720+ it ( 'should call next on error' , async ( ) => {
721+ const inst = createInstance ( {
722+ db : { collection : mock . fn ( ( ) => { throw new Error ( 'db error' ) } ) }
723+ } )
724+ inst . resetHandler = proto . resetHandler
725+ const next = mock . fn ( )
726+ await inst . resetHandler ( { body : { module : 'mod-a' , version : '1.0.0' } } , createRes ( ) , next )
727+
728+ assert . equal ( next . mock . callCount ( ) , 1 )
729+ assert . equal ( next . mock . calls [ 0 ] . arguments [ 0 ] . message , 'db error' )
730+ } )
731+ } )
653732} )
0 commit comments