Skip to content

Commit e746d13

Browse files
committed
Fix level creation in unit tests
1 parent c838d8e commit e746d13

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

Refresh.Database/GameDatabaseContext.Levels.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public GameLevel AddLevel(ISerializedPublishLevel createInfo, TokenGame game, Ga
5252
EnforceMinMaxPlayers = createInfo.EnforceMinMaxPlayers,
5353
SameScreenGame = createInfo.SameScreenGame,
5454
BackgroundGuid = createInfo.BackgroundGuid,
55-
Publisher = publisher,
55+
PublisherUserId = publisher.UserId,
5656
GameVersion = game,
5757
PublishDate = timestamp,
5858
UpdateDate = timestamp,
@@ -71,14 +71,21 @@ public GameLevel AddLevel(ISerializedPublishLevel createInfo, TokenGame game, Ga
7171

7272
this.SaveChanges();
7373

74-
if (level.Publisher != null)
74+
if (publisher != null)
7575
{
76-
this.WriteEnsuringStatistics(level.Publisher, () =>
76+
this.WriteEnsuringStatistics(publisher, () =>
7777
{
78-
level.Publisher.Statistics!.LevelCount++;
78+
// even if the level count does get updated, ChangeTracker.HasChanges() will return false anyways
79+
// (in unit tests atleast), failing the assert
80+
this.GameUsers.Update(publisher);
81+
publisher.Statistics!.LevelCount++;
7982
});
8083
}
8184

85+
// setting Publisher on an untracked object prevents EF from unconditionally trying to INSERT the publisher into the
86+
// GameUsers table on the next SaveChanges() call in unit tests, causing a duplicate key exception
87+
level = level.Clone();
88+
level.Publisher = publisher;
8289
return level;
8390
}
8491

RefreshTests.GameServer/Tests/Levels/UploadTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ public void CanCreateLevelDirectly()
2424
Assert.That(context.Database.GetLevelById(1), Is.Not.Null);
2525
}
2626

27+
[Test]
28+
public void CanCreateTwoLevelsWhileRefreshing()
29+
{
30+
using TestContext context = this.GetServer(false);
31+
GameUser user = context.CreateUser();
32+
context.Database.Refresh();
33+
GameLevel level1 = context.CreateLevel(user);
34+
context.Database.Refresh();
35+
GameLevel level2 = context.CreateLevel(user);
36+
context.Database.Refresh();
37+
}
38+
2739
[Test]
2840
public void CanUpdateLevel()
2941
{

0 commit comments

Comments
 (0)