DB Cleanup
This commit is contained in:
@@ -248,4 +248,33 @@ public class RewardGrantServiceTests
|
||||
Assert.ThrowsAsync<NotSupportedException>(async () =>
|
||||
await svc.ApplyAsync(viewer, UserGoodsType.SpotCardOnlyLatestCardPack, 1L, 1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OwnedCardEntry_unique_index_blocks_duplicate_viewer_card_row()
|
||||
{
|
||||
// Schema-level safety net: any code that forgets to .Include(v => v.Cards) before doing
|
||||
// a find-or-add OwnedCardEntry would silently insert a duplicate row otherwise. The
|
||||
// unique index on (ViewerId, CardId) makes that crash loudly at SaveChanges instead.
|
||||
using var factory = new SVSimTestFactory();
|
||||
long viewerId = await factory.SeedViewerAsync();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var ctx = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
const long testCardId = 999_003_001L;
|
||||
var card = new ShadowverseCardEntry { Id = testCardId, Name = "UniqueIdxTest", Rarity = Rarity.Bronze };
|
||||
ctx.Cards.Add(card);
|
||||
var viewer = await ctx.Viewers.Include(v => v.Cards).ThenInclude(c => c.Card).FirstAsync(v => v.Id == viewerId);
|
||||
viewer.Cards.Add(new OwnedCardEntry { Card = card, Count = 1, IsProtected = false });
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
// Simulate the bug: a fresh viewer load WITHOUT .Include(v => v.Cards), then a manual
|
||||
// Add of a second row for the same (Viewer, Card). The unique index must reject this.
|
||||
using var scope2 = factory.Services.CreateScope();
|
||||
var ctx2 = scope2.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
var unloadedViewer = await ctx2.Viewers.FirstAsync(v => v.Id == viewerId);
|
||||
var sameCard = await ctx2.Cards.FirstAsync(c => c.Id == testCardId);
|
||||
unloadedViewer.Cards.Add(new OwnedCardEntry { Card = sameCard, Count = 1, IsProtected = false });
|
||||
|
||||
Assert.ThrowsAsync<DbUpdateException>(async () => await ctx2.SaveChangesAsync());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user