11using Refresh . Database . Models . Authentication ;
22using Refresh . Database . Models . Levels ;
3+ using Refresh . Database . Models . Pins ;
34using Refresh . Database . Models . Users ;
4- using Refresh . Database . Query ;
5+ using Refresh . Database . Models . Workers ;
6+ using Refresh . Interfaces . Workers . Migrations ;
57using Refresh . Workers . State ;
68
79namespace RefreshTests . GameServer . Tests . Workers ;
@@ -29,4 +31,132 @@ public void MigrationJobWorks()
2931 allLevels = context . Database . GetNewestLevels ( 100 , 0 , null , new ( TokenGame . Website ) ) . Items ;
3032 Assert . That ( allLevels . All ( l => l . Title == "Level test" ) , Is . True ) ;
3133 }
34+
35+ [ Test ]
36+ public void WebsitePinMigrationDoesNotBreakPagination ( )
37+ {
38+ using TestContext context = this . GetServer ( ) ;
39+
40+ for ( int i = 0 ; i < 50 ; i ++ )
41+ {
42+ GameUser user = context . CreateUser ( ) ;
43+ context . Database . AddPinProgress ( new ( )
44+ {
45+ PinId = ( long ) ServerPins . SignIntoWebsite ,
46+ Progress = i + 1 ,
47+ Publisher = user ,
48+ FirstPublished = new ( ) ,
49+ LastUpdated = new ( ) ,
50+ IsBeta = false ,
51+ Platform = TokenPlatform . RPCS3 ,
52+ } , false ) ;
53+
54+ context . Database . AddPinProgress ( new ( )
55+ {
56+ PinId = ( long ) ServerPins . SignIntoWebsite ,
57+ Progress = i + 1 ,
58+ Publisher = user ,
59+ FirstPublished = new ( ) ,
60+ LastUpdated = new ( ) ,
61+ IsBeta = true ,
62+ Platform = TokenPlatform . RPCS3 ,
63+ } , false ) ;
64+ }
65+ context . Database . SaveChanges ( ) ;
66+ Assert . That ( context . Database . GetTotalPinProgresses ( ) , Is . EqualTo ( 100 ) ) ;
67+
68+ // Prepare migration
69+ CorrectWebsitePinProgressPlatformMigration job = new ( ) ;
70+ context . Database . UpdateOrCreateJobState ( typeof ( CorrectWebsitePinProgressPlatformMigration ) . Name , new MigrationJobState ( )
71+ {
72+ Total = 100 // Since we already have to manually create the state
73+ } , WorkerClass . Refresh ) ;
74+ context . Database . Refresh ( ) ;
75+
76+ object ? stateObject = context . Database . GetJobState ( typeof ( CorrectWebsitePinProgressPlatformMigration ) . Name , typeof ( MigrationJobState ) , WorkerClass . Refresh ) ;
77+ Assert . That ( stateObject , Is . Not . Null ) ;
78+ job . JobState = stateObject ! ;
79+
80+ // Migrate
81+ job . ExecuteJob ( context . GetWorkContext ( ) ) ;
82+ context . Database . Refresh ( ) ;
83+
84+ stateObject = context . Database . GetJobState ( typeof ( CorrectWebsitePinProgressPlatformMigration ) . Name , typeof ( MigrationJobState ) , WorkerClass . Refresh ) ;
85+ Assert . That ( stateObject , Is . Not . Null ) ;
86+
87+ // While we're not actually testing pagination here (batch count can't be edited + don't want to create over 1000 users here, would take too long),
88+ // it should be enough to simply ensure that both the total and processed counts are adjusted to be less than 100 (as 50 pins were deleted)
89+ MigrationJobState jobState = ( MigrationJobState ) stateObject ! ;
90+ Assert . That ( jobState . Processed , Is . EqualTo ( 50 ) ) ;
91+ Assert . That ( jobState . Total , Is . EqualTo ( 50 ) ) ;
92+ Assert . That ( jobState . Complete , Is . True ) ;
93+
94+ Assert . That ( context . Database . GetTotalPinProgresses ( ) , Is . EqualTo ( 50 ) ) ;
95+ }
96+
97+ [ Test ]
98+ public void DeletingEntitiesDuringMigrationJobDoesNotBreakPagination ( )
99+ {
100+ using TestContext context = this . GetServer ( ) ;
101+ GameUser user = context . CreateUser ( ) ;
102+
103+ // 3 cycles (6 levels per cycle)
104+ for ( int i = 0 ; i < 18 ; i ++ )
105+ {
106+ context . CreateLevel ( user ) ;
107+ }
108+ context . Database . Refresh ( ) ;
109+ Assert . That ( context . Database . GetTotalLevelCount ( ) , Is . EqualTo ( 18 ) ) ;
110+
111+ // Prepare migration
112+ TestMigrationJobDeletingLevels job = new ( ) ;
113+ context . Database . UpdateOrCreateJobState ( typeof ( TestMigrationJobDeletingLevels ) . Name , new MigrationJobState ( )
114+ {
115+ Total = 18
116+ } , WorkerClass . Refresh ) ;
117+ context . Database . Refresh ( ) ;
118+
119+ object ? stateObject = context . Database . GetJobState ( typeof ( TestMigrationJobDeletingLevels ) . Name , typeof ( MigrationJobState ) , WorkerClass . Refresh ) ;
120+ Assert . That ( stateObject , Is . Not . Null ) ;
121+ job . JobState = stateObject ! ;
122+
123+ // Migrate - First cycle
124+ job . ExecuteJob ( context . GetWorkContext ( ) ) ;
125+ context . Database . Refresh ( ) ;
126+
127+ // Ensure only 3 levels were actually migrated, and 3 levels were deleted
128+ Assert . That ( context . Database . GetTotalLevelCount ( ) , Is . EqualTo ( 15 ) ) ;
129+ Assert . That ( context . Database . GetNewestLevels ( 20 , 0 , null , new ( TokenGame . Website ) ) . Items . Count ( l => l . Title . EndsWith ( " test" ) ) , Is . EqualTo ( 3 ) ) ;
130+
131+ MigrationJobState jobState = ( MigrationJobState ) stateObject ! ;
132+ Assert . That ( jobState . Processed , Is . EqualTo ( 3 ) ) ;
133+ Assert . That ( jobState . Total , Is . EqualTo ( 15 ) ) ;
134+ Assert . That ( jobState . Complete , Is . False ) ;
135+
136+ // Migrate - Second cycle
137+ job . ExecuteJob ( context . GetWorkContext ( ) ) ;
138+ context . Database . Refresh ( ) ;
139+
140+ // Ensure 3 more levels were deleted, and 3 more levels were migrated
141+ Assert . That ( context . Database . GetTotalLevelCount ( ) , Is . EqualTo ( 12 ) ) ;
142+ Assert . That ( context . Database . GetNewestLevels ( 20 , 0 , null , new ( TokenGame . Website ) ) . Items . Count ( l => l . Title . EndsWith ( " test" ) ) , Is . EqualTo ( 6 ) ) ;
143+
144+ jobState = ( MigrationJobState ) stateObject ! ;
145+ Assert . That ( jobState . Processed , Is . EqualTo ( 6 ) ) ;
146+ Assert . That ( jobState . Total , Is . EqualTo ( 12 ) ) ;
147+ Assert . That ( jobState . Complete , Is . False ) ;
148+
149+ // Migrate - Last cycle
150+ job . ExecuteJob ( context . GetWorkContext ( ) ) ;
151+ context . Database . Refresh ( ) ;
152+
153+ // Ensure 3 more levels were deleted, and 3 more levels were migrated
154+ Assert . That ( context . Database . GetTotalLevelCount ( ) , Is . EqualTo ( 9 ) ) ;
155+ Assert . That ( context . Database . GetNewestLevels ( 20 , 0 , null , new ( TokenGame . Website ) ) . Items . Count ( l => l . Title . EndsWith ( " test" ) ) , Is . EqualTo ( 9 ) ) ;
156+
157+ jobState = ( MigrationJobState ) stateObject ! ;
158+ Assert . That ( jobState . Processed , Is . EqualTo ( 9 ) ) ;
159+ Assert . That ( jobState . Total , Is . EqualTo ( 9 ) ) ;
160+ Assert . That ( jobState . Complete , Is . True ) ;
161+ }
32162}
0 commit comments