feat(phase-8-backend): per-event results harvesting + EventPath plumbing
Implements Phase 8 Amendment 1: marathonbet.by has no public results archive
endpoint, so results must be harvested per-event by re-fetching the event
detail page until eventJsonInfo.matchIsComplete=true.
Backend changes:
* IOddsScraper:
- ScrapeResultsAsync(DateRange) replaced with ScrapeEventResultAsync(Event)
returning a nullable EventResult — null when match still in progress.
- ScrapeEventOddsAsync now takes the full Event (so EventPath drives URL
construction) instead of bare EventId.
- New ScrapeLiveAsync() for the /su/live listing.
* Domain:
- Event gains EventPath (nullable string) — the data-event-path attribute
captured during scraping; required for reliable URL construction.
* Infrastructure:
- New migration 20260506000000_AddEventPath adds the column.
- EventEntity / EventConfiguration / Mapping / model-snapshot updated.
- MarathonbetScraper: new ScrapeLiveAsync + ScrapeEventResultAsync; URL
builder prefers EventPath, falls back to numeric ID for legacy rows.
- EventListingParserBase extracts data-event-path on every listing row.
* Application:
- PullResultsUseCase: branches on selection vs date-range, emits IProgress<
PullResultsProgress>, returns ResultLoadOutcome (Loaded / AlreadyLoaded /
NotYetComplete / Failed); idempotent (skips events whose result already
exists).
- PullLiveOddsUseCase now drives off the live listing (auto-discovers
events that go live without ever appearing in the upcoming list) and
backfills EventPath on legacy rows.
- PullUpcomingEventsUseCase wires EventPath on persisted events.
* Workers: UpcomingEventsPoller updates persistence path accordingly.
* Tests: 17 net-new tests across Application + Infrastructure + Domain;
all 293 still pass.
This commit is contained in:
@@ -21,22 +21,22 @@ public sealed class PullLiveOddsUseCaseTests
|
||||
NullLogger<PullLiveOddsUseCase>.Instance);
|
||||
|
||||
[Fact]
|
||||
public async Task Should_CaptureOneSnapshotPerEvent_When_TwoLiveEventsExistInDatabase()
|
||||
public async Task Should_CaptureOneSnapshotPerEvent_When_LiveListingReturnsTwoEvents()
|
||||
{
|
||||
// Arrange: 2 events in the database
|
||||
// Arrange: 2 events from the live listing; both already known to the DB
|
||||
var ev1 = TestFixtures.MakeEvent("11111111");
|
||||
var ev2 = TestFixtures.MakeEvent("22222222");
|
||||
var storedEvents = new List<Event> { ev1, ev2 }.AsReadOnly();
|
||||
var live = new List<Event> { ev1, ev2 }.AsReadOnly();
|
||||
|
||||
_eventRepo.ListAsync(Arg.Any<CancellationToken>()).Returns(storedEvents);
|
||||
_scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>()).Returns(live);
|
||||
_eventRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>()).Returns(ev1);
|
||||
_eventRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>()).Returns(ev2);
|
||||
|
||||
// ScrapeUpcomingAsync is also called (by implementation) — return empty to keep test focused
|
||||
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<Event>());
|
||||
|
||||
_scraper.ScrapeEventOddsAsync(ev1.Id, OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
_scraper.ScrapeEventOddsAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev1.Id), OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
.Returns(TestFixtures.MakeSnapshot(ev1.Id, OddsSource.Live));
|
||||
_scraper.ScrapeEventOddsAsync(ev2.Id, OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
_scraper.ScrapeEventOddsAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev2.Id), OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
.Returns(TestFixtures.MakeSnapshot(ev2.Id, OddsSource.Live));
|
||||
|
||||
var sut = CreateSut();
|
||||
@@ -47,46 +47,65 @@ public sealed class PullLiveOddsUseCaseTests
|
||||
// Assert
|
||||
snapshotsCaptured.Should().Be(2);
|
||||
|
||||
await _scraper.Received(1).ScrapeEventOddsAsync(ev1.Id, OddsSource.Live, Arg.Any<CancellationToken>());
|
||||
await _scraper.Received(1).ScrapeEventOddsAsync(ev2.Id, OddsSource.Live, Arg.Any<CancellationToken>());
|
||||
await _eventRepo.DidNotReceive().AddAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>());
|
||||
await _snapshotRepo.Received(2).AddAsync(Arg.Any<OddsSnapshot>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_PersistNewLiveEvent_When_NotYetInDatabase()
|
||||
{
|
||||
// Arrange: live listing returns one event the DB has never seen
|
||||
var live = TestFixtures.MakeEvent("99999999");
|
||||
_scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(new List<Event> { live }.AsReadOnly());
|
||||
_eventRepo.GetAsync(live.Id, Arg.Any<CancellationToken>()).Returns((Event?)null);
|
||||
_scraper.ScrapeEventOddsAsync(
|
||||
Arg.Is<Event>(e => e.Id == live.Id), OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
.Returns(TestFixtures.MakeSnapshot(live.Id, OddsSource.Live));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var snapshotsCaptured = await sut.ExecuteAsync(CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
snapshotsCaptured.Should().Be(1);
|
||||
await _eventRepo.Received(1).AddAsync(live, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ContinueAfterSnapshotFailure_And_NotPropagateException()
|
||||
{
|
||||
// Arrange: 2 events — scraping the first throws
|
||||
// Arrange: 2 live events — scraping the first throws
|
||||
var ev1 = TestFixtures.MakeEvent("11111111");
|
||||
var ev2 = TestFixtures.MakeEvent("22222222");
|
||||
var storedEvents = new List<Event> { ev1, ev2 }.AsReadOnly();
|
||||
_scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(new List<Event> { ev1, ev2 }.AsReadOnly());
|
||||
_eventRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>()).Returns(ev1);
|
||||
_eventRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>()).Returns(ev2);
|
||||
|
||||
_eventRepo.ListAsync(Arg.Any<CancellationToken>()).Returns(storedEvents);
|
||||
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<Event>());
|
||||
|
||||
_scraper.ScrapeEventOddsAsync(ev1.Id, OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
_scraper.ScrapeEventOddsAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev1.Id), OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
.ThrowsAsync(new HttpRequestException("timeout"));
|
||||
_scraper.ScrapeEventOddsAsync(ev2.Id, OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
_scraper.ScrapeEventOddsAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev2.Id), OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
.Returns(TestFixtures.MakeSnapshot(ev2.Id, OddsSource.Live));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act — must not throw
|
||||
var act = async () => await sut.ExecuteAsync(CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
await act.Should().NotThrowAsync();
|
||||
|
||||
// Re-execute to assert the count (mocks are still primed)
|
||||
var result = await sut.ExecuteAsync(CancellationToken.None);
|
||||
result.Should().Be(1, "only ev2 succeeded; ev1 failed silently");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ReturnZero_When_NoEventsInDatabase()
|
||||
public async Task Should_ReturnZero_When_LiveListingIsEmpty()
|
||||
{
|
||||
_eventRepo.ListAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<Event>());
|
||||
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
|
||||
_scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<Event>());
|
||||
|
||||
var sut = CreateSut();
|
||||
@@ -95,6 +114,50 @@ public sealed class PullLiveOddsUseCaseTests
|
||||
|
||||
result.Should().Be(0);
|
||||
await _scraper.DidNotReceive()
|
||||
.ScrapeEventOddsAsync(Arg.Any<EventId>(), Arg.Any<OddsSource>(), Arg.Any<CancellationToken>());
|
||||
.ScrapeEventOddsAsync(Arg.Any<Event>(), Arg.Any<OddsSource>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ReturnZeroAndSwallow_When_LiveListingFetchThrows()
|
||||
{
|
||||
_scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
||||
.ThrowsAsync(new HttpRequestException("listing unavailable"));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
var result = await sut.ExecuteAsync(CancellationToken.None);
|
||||
|
||||
result.Should().Be(0);
|
||||
await _scraper.DidNotReceive()
|
||||
.ScrapeEventOddsAsync(Arg.Any<Event>(), Arg.Any<OddsSource>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_BackfillEventPath_When_ExistingRowMissedIt()
|
||||
{
|
||||
// Arrange: DB row pre-dates the EventPath column (EventPath = null);
|
||||
// live listing supplies a path.
|
||||
var withoutPath = TestFixtures.MakeEvent("55555555");
|
||||
var withPath = withoutPath with { EventPath = "Football/Some+Path/Team+vs+Team+-+99" };
|
||||
|
||||
_scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(new List<Event> { withPath }.AsReadOnly());
|
||||
_eventRepo.GetAsync(withPath.Id, Arg.Any<CancellationToken>()).Returns(withoutPath);
|
||||
|
||||
_scraper.ScrapeEventOddsAsync(
|
||||
Arg.Is<Event>(e => e.EventPath == withPath.EventPath),
|
||||
OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
.Returns(TestFixtures.MakeSnapshot(withPath.Id, OddsSource.Live));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = await sut.ExecuteAsync(CancellationToken.None);
|
||||
|
||||
// Assert — the DB row was updated with the new path before scraping odds
|
||||
result.Should().Be(1);
|
||||
await _eventRepo.Received(1).UpdateAsync(
|
||||
Arg.Is<Event>(e => e.Id == withPath.Id && e.EventPath == withPath.EventPath),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user