Skip to content

Commit 2504ec1

Browse files
committed
Test entity deletion during migration
1 parent 31eccf6 commit 2504ec1

2 files changed

Lines changed: 168 additions & 1 deletion

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Refresh.Database.Models.Levels;
2+
using Refresh.Workers;
3+
4+
namespace RefreshTests.GameServer.GameServer;
5+
6+
public class TestMigrationJobDeletingLevels : MigrationJob<GameLevel>
7+
{
8+
protected override int BatchCount => 6;
9+
10+
// Deletes every second level
11+
protected override int Migrate(WorkContext context, GameLevel[] batch)
12+
{
13+
int entitiesLeft = batch.Length;
14+
int index = 0;
15+
foreach (GameLevel level in batch)
16+
{
17+
if (index % 2 == 1)
18+
{
19+
context.Database.DeleteLevel(level);
20+
entitiesLeft--;
21+
}
22+
else
23+
{
24+
level.Title += " test";
25+
}
26+
27+
index++;
28+
}
29+
30+
return entitiesLeft;
31+
}
32+
33+
protected override IQueryable<GameLevel> SortAndFilter(IQueryable<GameLevel> query)
34+
{
35+
return query.OrderBy(l => l.LevelId);
36+
}
37+
}

RefreshTests.GameServer/Tests/Workers/MigrationTests.cs

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Refresh.Database.Models.Authentication;
22
using Refresh.Database.Models.Levels;
3+
using Refresh.Database.Models.Pins;
34
using Refresh.Database.Models.Users;
4-
using Refresh.Database.Query;
5+
using Refresh.Database.Models.Workers;
6+
using Refresh.Interfaces.Workers.Migrations;
57
using Refresh.Workers.State;
68

79
namespace 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

Comments
 (0)