Skip to content

Commit 796751f

Browse files
committed
Different attempt at fixing level test fails
1 parent 63a69ed commit 796751f

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

Refresh.Database/GameDatabaseContext.Levels.cs

Lines changed: 12 additions & 19 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-
PublisherUserId = publisher.UserId,
55+
Publisher = publisher,
5656
GameVersion = game,
5757
PublishDate = timestamp,
5858
UpdateDate = timestamp,
@@ -61,31 +61,24 @@ public GameLevel AddLevel(ISerializedPublishLevel createInfo, TokenGame game, Ga
6161
this.ApplyLevelMetadataFromAttributes(level);
6262
this.GameLevels.Add(level);
6363

64-
this.SaveChanges();
65-
66-
this.CreateRevisionForLevel(level, level.Publisher);
67-
this.GameLevelStatistics.Add(level.Statistics = new GameLevelStatistics
68-
{
69-
LevelId = level.LevelId,
70-
});
64+
// Seems unnecessary, but prevents EF from trying to INSERT both unconditionally in unit tests
65+
this.GameUsers.Update(publisher);
66+
if (publisher.Statistics != null)
67+
this.GameUserStatistics.Update(publisher.Statistics);
7168

7269
this.SaveChanges();
7370

74-
if (publisher != null)
71+
this.WriteEnsuringStatistics(publisher, () =>
7572
{
76-
this.WriteEnsuringStatistics(publisher, () =>
73+
this.GameLevelStatistics.Add(level.Statistics = new GameLevelStatistics
7774
{
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++;
75+
LevelId = level.LevelId,
8276
});
83-
}
8477

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;
78+
this.CreateRevisionForLevel(level, level.Publisher);
79+
publisher.Statistics!.LevelCount++;
80+
});
81+
8982
return level;
9083
}
9184

RefreshTests.GameServer/Tests/Levels/UploadTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ public void CanCreateLevelDirectly()
2525
}
2626

2727
[Test]
28-
public void CanCreateTwoLevelsWhileRefreshing()
28+
public void CanCreateMultipleLevelsWhileRefreshing()
2929
{
3030
using TestContext context = this.GetServer(false);
3131
GameUser user = context.CreateUser();
32+
3233
context.Database.Refresh();
3334
GameLevel level1 = context.CreateLevel(user);
3435
context.Database.Refresh();
3536
GameLevel level2 = context.CreateLevel(user);
3637
context.Database.Refresh();
38+
GameLevel level3 = context.CreateLevel(user);
39+
context.Database.Refresh();
3740
}
3841

3942
[Test]

0 commit comments

Comments
 (0)