feat(bp): season + reward importers, idempotent + authoritative-per-season
This commit is contained in:
105
SVSim.UnitTests/Importers/BattlePassRewardImporterTests.cs
Normal file
105
SVSim.UnitTests/Importers/BattlePassRewardImporterTests.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SVSim.Bootstrap.Importers;
|
||||
using SVSim.Database;
|
||||
using SVSim.Database.Enums;
|
||||
using SVSim.UnitTests.Infrastructure;
|
||||
|
||||
namespace SVSim.UnitTests.Importers;
|
||||
|
||||
public class BattlePassRewardImporterTests
|
||||
{
|
||||
private static string SeedDir => Path.Combine(AppContext.BaseDirectory, "Data", "seeds");
|
||||
|
||||
private static async Task SeedSeason23(SVSimDbContext db)
|
||||
{
|
||||
db.BattlePassSeasons.Add(new SVSim.Database.Models.BattlePassSeasonEntry
|
||||
{
|
||||
Id = 23, Name = "Season 23", MaxLevel = 100,
|
||||
StartDate = DateTimeOffset.Parse("2026-04-01T02:00:00+09:00"),
|
||||
EndDate = DateTimeOffset.Parse("2026-07-01T01:59:59+09:00"),
|
||||
CanPurchase = true, PriceCrystal = 980, Description = "",
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Imports_normal_and_premium_tracks()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
await SeedSeason23(db);
|
||||
|
||||
await new BattlePassRewardImporter().ImportAsync(db, SeedDir);
|
||||
|
||||
int normal = await db.BattlePassRewards.CountAsync(r => r.SeasonId == 23 && r.Track == BattlePassTrack.Normal);
|
||||
int premium = await db.BattlePassRewards.CountAsync(r => r.SeasonId == 23 && r.Track == BattlePassTrack.Premium);
|
||||
Assert.That(normal, Is.EqualTo(44), "captured normal track count for Season 23");
|
||||
Assert.That(premium, Is.EqualTo(99), "captured premium track count for Season 23");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Is_idempotent_on_rerun()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
await SeedSeason23(db);
|
||||
|
||||
await new BattlePassRewardImporter().ImportAsync(db, SeedDir);
|
||||
int before = await db.BattlePassRewards.CountAsync();
|
||||
await new BattlePassRewardImporter().ImportAsync(db, SeedDir);
|
||||
int after = await db.BattlePassRewards.CountAsync();
|
||||
|
||||
Assert.That(after, Is.EqualTo(before));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Authoritatively_removes_orphan_rows_for_seeded_season()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
await SeedSeason23(db);
|
||||
db.BattlePassRewards.Add(new SVSim.Database.Models.BattlePassRewardEntry
|
||||
{
|
||||
SeasonId = 23, Track = BattlePassTrack.Normal, Level = 99, RewardType = 9,
|
||||
RewardDetailId = 0, RewardNumber = 9999, IsAppealExclusion = false,
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
await new BattlePassRewardImporter().ImportAsync(db, SeedDir);
|
||||
|
||||
bool orphanPresent = await db.BattlePassRewards.AnyAsync(
|
||||
r => r.SeasonId == 23 && r.Track == BattlePassTrack.Normal && r.Level == 99);
|
||||
Assert.That(orphanPresent, Is.False, "orphan reward not in seed must be deleted");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Leaves_other_seasons_untouched()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
await SeedSeason23(db);
|
||||
db.BattlePassSeasons.Add(new SVSim.Database.Models.BattlePassSeasonEntry
|
||||
{
|
||||
Id = 22, Name = "Season 22", MaxLevel = 100,
|
||||
StartDate = DateTimeOffset.Parse("2026-01-01T02:00:00+09:00"),
|
||||
EndDate = DateTimeOffset.Parse("2026-04-01T01:59:59+09:00"),
|
||||
CanPurchase = false, PriceCrystal = 980, Description = "",
|
||||
});
|
||||
db.BattlePassRewards.Add(new SVSim.Database.Models.BattlePassRewardEntry
|
||||
{
|
||||
SeasonId = 22, Track = BattlePassTrack.Normal, Level = 1, RewardType = 9,
|
||||
RewardDetailId = 0, RewardNumber = 100, IsAppealExclusion = false,
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
await new BattlePassRewardImporter().ImportAsync(db, SeedDir);
|
||||
|
||||
bool s22Preserved = await db.BattlePassRewards.AnyAsync(r => r.SeasonId == 22 && r.Level == 1);
|
||||
Assert.That(s22Preserved, Is.True, "rewards for unseeded seasons must be left intact");
|
||||
}
|
||||
}
|
||||
85
SVSim.UnitTests/Importers/BattlePassSeasonImporterTests.cs
Normal file
85
SVSim.UnitTests/Importers/BattlePassSeasonImporterTests.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SVSim.Bootstrap.Importers;
|
||||
using SVSim.Database;
|
||||
using SVSim.UnitTests.Infrastructure;
|
||||
|
||||
namespace SVSim.UnitTests.Importers;
|
||||
|
||||
public class BattlePassSeasonImporterTests
|
||||
{
|
||||
private static string SeedDir => Path.Combine(AppContext.BaseDirectory, "Data", "seeds");
|
||||
|
||||
[Test]
|
||||
public async Task Imports_season_from_seed_file()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
await new BattlePassSeasonImporter().ImportAsync(db, SeedDir);
|
||||
|
||||
var season = await db.BattlePassSeasons.SingleAsync(s => s.Id == 23);
|
||||
Assert.That(season.Name, Does.Contain("Season"));
|
||||
Assert.That(season.MaxLevel, Is.EqualTo(100));
|
||||
Assert.That(season.CanPurchase, Is.True);
|
||||
Assert.That(season.PriceCrystal, Is.EqualTo(980));
|
||||
Assert.That(season.StartDate.Offset, Is.EqualTo(TimeSpan.FromHours(9)),
|
||||
"JST offset (+09:00) must round-trip through DateTimeOffset");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Is_idempotent_on_rerun()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
await new BattlePassSeasonImporter().ImportAsync(db, SeedDir);
|
||||
int before = await db.BattlePassSeasons.CountAsync();
|
||||
await new BattlePassSeasonImporter().ImportAsync(db, SeedDir);
|
||||
int after = await db.BattlePassSeasons.CountAsync();
|
||||
|
||||
Assert.That(after, Is.EqualTo(before));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Updates_existing_row_when_seed_changes()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
db.BattlePassSeasons.Add(new SVSim.Database.Models.BattlePassSeasonEntry
|
||||
{
|
||||
Id = 23, Name = "stale", MaxLevel = 0, PriceCrystal = 0,
|
||||
StartDate = DateTimeOffset.MinValue, EndDate = DateTimeOffset.MinValue,
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
await new BattlePassSeasonImporter().ImportAsync(db, SeedDir);
|
||||
|
||||
var refreshed = await db.BattlePassSeasons.FindAsync(23);
|
||||
Assert.That(refreshed!.Name, Is.Not.EqualTo("stale"));
|
||||
Assert.That(refreshed.PriceCrystal, Is.EqualTo(980));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Empty_seed_is_no_op()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
string tmp = Path.Combine(Path.GetTempPath(), $"seed-{Guid.NewGuid()}");
|
||||
Directory.CreateDirectory(tmp);
|
||||
try
|
||||
{
|
||||
File.WriteAllText(Path.Combine(tmp, "battle-pass-seasons.json"), "[]");
|
||||
await new BattlePassSeasonImporter().ImportAsync(db, tmp);
|
||||
int count = await db.BattlePassSeasons.CountAsync();
|
||||
Assert.That(count, Is.EqualTo(0));
|
||||
}
|
||||
finally { Directory.Delete(tmp, true); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user