test+chore: real-SQLite query coverage, batch detect writes, finish date centralization

Review follow-ups:
- (HIGH) Add real-SQLite round-trip tests for the new query methods so the load-bearing
  lexical O-format date ordering is verified, not just mocked: Anomaly
  ListByDateRange/CountSince, Snapshot CountSince/ListByEvents grouping, Event Query/GetMany.
- (MED) DetectAnomaliesUseCase: one SaveChanges per event instead of per anomaly.
- (LOW) Route PlacedBetRepository + ExcelExporter date bounds through SqliteDateText.
- (LOW) Backtest: reject a one-sided date range (was silently ignored).
- (LOW) Refresh stale comments after the detector fan-out.
This commit is contained in:
2026-05-29 01:25:25 +03:00
parent c9eee9f907
commit b67030ae7f
5 changed files with 107 additions and 11 deletions
@@ -1,4 +1,5 @@
using FluentAssertions;
using Marathon.Application.Storage;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
@@ -279,6 +280,92 @@ public sealed class RoundTripTests : IDisposable
exception.Should().BeNull("PRAGMA journal_mode=WAL should execute without error");
}
// ── New query methods — verified against real SQLite (lexical O-format ordering) ──
[Fact]
public async Task Anomaly_DateRangeAndCountSince_FilterByDetectedAt()
{
DateTimeOffset At(int day) => new(2026, 5, day, 12, 0, 0, MoscowOffset);
await _eventRepo.AddAsync(BuildEvent("D100"));
await _eventRepo.SaveChangesAsync();
async Task AddAnomaly(DateTimeOffset at, decimal score) =>
await _anomalyRepo.AddAsync(new Anomaly(
Guid.NewGuid(), new EventId("D100"), at, AnomalyKind.SuspensionFlip, score, "{}"));
await AddAnomaly(At(1), 0.50m);
await AddAnomaly(At(5), 0.60m);
await AddAnomaly(At(10), 0.70m);
await _anomalyRepo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
// Inclusive [day3..day7] → only the day-5 anomaly.
var inRange = await _anomalyRepo.ListByDateRangeAsync(At(3), At(7));
inRange.Should().ContainSingle();
inRange[0].Score.Should().Be(0.60m);
// Open-ended returns newest-first.
var all = await _anomalyRepo.ListByDateRangeAsync(null, null);
all.Select(a => a.DetectedAt).Should().BeInDescendingOrder();
// CountSinceAsync is strictly-after → only day-10.
(await _anomalyRepo.CountSinceAsync(At(5))).Should().Be(1);
}
[Fact]
public async Task Snapshot_CountSinceAndListByEvents_FilterAndGroup()
{
DateTimeOffset At(int day) => new(2026, 5, day, 12, 0, 0, MoscowOffset);
await _eventRepo.AddAsync(BuildEvent("S100"));
await _eventRepo.AddAsync(BuildEvent("S200"));
await _eventRepo.SaveChangesAsync();
OddsSnapshot Snap(string id, DateTimeOffset at) =>
new(new EventId(id), at, OddsSource.Live,
new List<Bet> { new(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(1.90m)) });
await _snapshotRepo.AddAsync(Snap("S100", At(1)));
await _snapshotRepo.AddAsync(Snap("S100", At(5)));
await _snapshotRepo.AddAsync(Snap("S200", At(6)));
await _snapshotRepo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
// Inclusive >= day3 → day5 (S100) + day6 (S200).
(await _snapshotRepo.CountSinceAsync(At(3))).Should().Be(2);
var byEvent = await _snapshotRepo.ListByEventsAsync(
new[] { new EventId("S100"), new EventId("S200") }, At(1).AddDays(-1), At(10));
byEvent[new EventId("S100")].Should().HaveCount(2);
byEvent[new EventId("S200")].Should().HaveCount(1);
}
[Fact]
public async Task Event_QueryAndGetMany_FilterAndBatch()
{
DateTimeOffset At(int day) => new(2026, 5, day, 12, 0, 0, MoscowOffset);
await _eventRepo.AddAsync(BuildEvent("Q1", At(1))); // sport 11
await _eventRepo.AddAsync(BuildEvent("Q2", At(5))); // sport 11, in range
await _eventRepo.AddAsync(new Event(
new EventId("Q3"), new SportCode(6), "England", "premier-league", "", At(5), "Home", "Away"));
await _eventRepo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
// QueryAsync: [day3..day7] + sport 11 → only Q2 (Q3 is sport 6).
var queried = await _eventRepo.QueryAsync(
new EventQuery(new DateRange(At(3), At(7)), SportCodes: new[] { 11 }));
queried.Should().ContainSingle();
queried[0].Id.Value.Should().Be("Q2");
// GetManyAsync: batched; unknown id is simply absent.
var many = await _eventRepo.GetManyAsync(
new[] { new EventId("Q1"), new EventId("Q3"), new EventId("missing") });
many.Should().HaveCount(2);
many.Keys.Select(k => k.Value).Should().BeEquivalentTo(new[] { "Q1", "Q3" });
}
// ── Helpers ─────────────────────────────────────────────────────────────
private static Event BuildEvent(string id, DateTimeOffset? scheduledAt = null) =>