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>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@ using Marathon.Application.Abstractions;
|
||||
using Marathon.Application.Storage;
|
||||
using Marathon.Application.UseCases;
|
||||
using Marathon.Domain.Entities;
|
||||
using Marathon.Domain.Enums;
|
||||
using Marathon.Domain.ValueObjects;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
|
||||
namespace Marathon.Application.Tests.UseCases;
|
||||
|
||||
@@ -23,13 +25,15 @@ public sealed class PullResultsUseCaseTests
|
||||
new(_scraper, _eventRepo, _resultRepo,
|
||||
NullLogger<PullResultsUseCase>.Instance);
|
||||
|
||||
// ── Selection mode ──────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Should_InspectOnlySelectedEvents_When_SelectionIsProvided()
|
||||
{
|
||||
// Arrange: 3 events in DB; only 2 are in the selection
|
||||
// Arrange: 3 events in the DB; only 2 in the selection
|
||||
var ev1 = TestFixtures.MakeEvent("11111111");
|
||||
var ev2 = TestFixtures.MakeEvent("22222222");
|
||||
var ev3 = TestFixtures.MakeEvent("33333333"); // not selected
|
||||
var ev3 = TestFixtures.MakeEvent("33333333");
|
||||
|
||||
_eventRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>()).Returns(ev1);
|
||||
_eventRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>()).Returns(ev2);
|
||||
@@ -37,10 +41,8 @@ public sealed class PullResultsUseCaseTests
|
||||
|
||||
_resultRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
|
||||
// Scraper returns no results (Phase 3 no-op)
|
||||
_scraper.ScrapeResultsAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<EventResult>());
|
||||
_scraper.ScrapeEventResultAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
|
||||
var selection = new List<EventId> { ev1.Id, ev2.Id };
|
||||
var sut = CreateSut();
|
||||
@@ -48,19 +50,20 @@ public sealed class PullResultsUseCaseTests
|
||||
// Act
|
||||
var (inspected, loaded, skipped) = await sut.ExecuteAsync(AnyRange, selection, CancellationToken.None);
|
||||
|
||||
// Assert: only ev1 and ev2 inspected; ev3 not fetched via GetAsync lookup for range
|
||||
// Assert: ev3 never resolved; only ev1+ev2 inspected
|
||||
inspected.Should().Be(2);
|
||||
loaded.Should().Be(0, "scraper returns no results in Phase 3");
|
||||
loaded.Should().Be(0);
|
||||
skipped.Should().Be(0);
|
||||
|
||||
// ev3 was never resolved
|
||||
await _eventRepo.DidNotReceive().GetAsync(ev3.Id, Arg.Any<CancellationToken>());
|
||||
await _eventRepo.DidNotReceive().ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
// ── Bulk mode ───────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Should_InspectAllEventsInRange_When_SelectionIsNull()
|
||||
{
|
||||
// Arrange: 3 events returned by date-range query
|
||||
var ev1 = TestFixtures.MakeEvent("11111111");
|
||||
var ev2 = TestFixtures.MakeEvent("22222222");
|
||||
var ev3 = TestFixtures.MakeEvent("33333333");
|
||||
@@ -68,84 +71,188 @@ public sealed class PullResultsUseCaseTests
|
||||
|
||||
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||
.Returns(allEvents);
|
||||
|
||||
_resultRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
|
||||
_scraper.ScrapeResultsAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<EventResult>());
|
||||
_scraper.ScrapeEventResultAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var (inspected, loaded, skipped) = await sut.ExecuteAsync(AnyRange, selection: null, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
inspected.Should().Be(3);
|
||||
loaded.Should().Be(0);
|
||||
loaded.Should().Be(0, "scraper says none of them are complete yet");
|
||||
skipped.Should().Be(0);
|
||||
|
||||
await _eventRepo.Received(1).ListByDateRangeAsync(AnyRange, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_InspectAllEventsInRange_When_SelectionIsEmpty()
|
||||
{
|
||||
var ev1 = TestFixtures.MakeEvent("11111111");
|
||||
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new List<Event> { ev1 }.AsReadOnly());
|
||||
_resultRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
_scraper.ScrapeEventResultAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
var (inspected, _, _) = await sut.ExecuteAsync(
|
||||
AnyRange,
|
||||
selection: Array.Empty<EventId>(),
|
||||
CancellationToken.None);
|
||||
|
||||
inspected.Should().Be(1);
|
||||
await _eventRepo.Received(1).ListByDateRangeAsync(AnyRange, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
// ── Idempotency ─────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Should_SkipEventsWithExistingResult_And_BeIdempotent()
|
||||
{
|
||||
// Arrange: 2 events — ev1 already has a result stored
|
||||
var ev1 = TestFixtures.MakeEvent("11111111");
|
||||
var ev2 = TestFixtures.MakeEvent("22222222");
|
||||
var allEvents = new List<Event> { ev1, ev2 }.AsReadOnly();
|
||||
|
||||
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||
.Returns(allEvents);
|
||||
.Returns(new List<Event> { ev1, ev2 }.AsReadOnly());
|
||||
|
||||
_resultRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(TestFixtures.MakeResult(ev1.Id)); // ev1 already has result
|
||||
.Returns(TestFixtures.MakeResult(ev1.Id));
|
||||
_resultRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
|
||||
_scraper.ScrapeResultsAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<EventResult>());
|
||||
_scraper.ScrapeEventResultAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act — run twice to verify idempotency
|
||||
var (_, _, skipped1) = await sut.ExecuteAsync(AnyRange, null, CancellationToken.None);
|
||||
var (_, _, skipped2) = await sut.ExecuteAsync(AnyRange, null, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
skipped1.Should().Be(1, "ev1 already has a result");
|
||||
skipped2.Should().Be(1, "idempotent: ev1 still skipped on second run");
|
||||
|
||||
// No new results persisted
|
||||
await _resultRepo.DidNotReceive().AddAsync(Arg.Any<EventResult>(), Arg.Any<CancellationToken>());
|
||||
await _scraper.DidNotReceive()
|
||||
.ScrapeEventResultAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev1.Id),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
// ── Successful loads ────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Should_PersistResults_When_ScraperReturnsMatchingResults()
|
||||
public async Task Should_PersistResults_When_ScraperReturnsCompletedMatch()
|
||||
{
|
||||
// Arrange: 1 event; scraper returns a result for it
|
||||
var ev1 = TestFixtures.MakeEvent("11111111");
|
||||
var result1 = TestFixtures.MakeResult(ev1.Id);
|
||||
var allEvents = new List<Event> { ev1 }.AsReadOnly();
|
||||
|
||||
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||
.Returns(allEvents);
|
||||
.Returns(new List<Event> { ev1 }.AsReadOnly());
|
||||
_resultRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
_scraper.ScrapeResultsAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new List<EventResult> { result1 }.AsReadOnly());
|
||||
_scraper.ScrapeEventResultAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev1.Id),
|
||||
Arg.Any<CancellationToken>())
|
||||
.Returns(result1);
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var (inspected, loaded, skipped) = await sut.ExecuteAsync(AnyRange, null, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
inspected.Should().Be(1);
|
||||
loaded.Should().Be(1);
|
||||
skipped.Should().Be(0);
|
||||
|
||||
await _resultRepo.Received(1).AddAsync(result1, Arg.Any<CancellationToken>());
|
||||
await _resultRepo.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
// ── Progress reporting ──────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ReportProgress_OncePerCandidate_With_CorrectOutcome()
|
||||
{
|
||||
var ev1 = TestFixtures.MakeEvent("11111111"); // already has result → AlreadyLoaded
|
||||
var ev2 = TestFixtures.MakeEvent("22222222"); // scrape returns null → NotYetComplete
|
||||
var ev3 = TestFixtures.MakeEvent("33333333"); // scrape returns res3 → Loaded
|
||||
var result3 = TestFixtures.MakeResult(ev3.Id);
|
||||
|
||||
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new List<Event> { ev1, ev2, ev3 }.AsReadOnly());
|
||||
|
||||
_resultRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(TestFixtures.MakeResult(ev1.Id));
|
||||
_resultRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
_resultRepo.GetAsync(ev3.Id, Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
|
||||
_scraper.ScrapeEventResultAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev2.Id), Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
_scraper.ScrapeEventResultAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev3.Id), Arg.Any<CancellationToken>())
|
||||
.Returns(result3);
|
||||
|
||||
var ticks = new List<PullResultsProgress>();
|
||||
var progress = new Progress<PullResultsProgress>(ticks.Add);
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
await sut.ExecuteAsync(AnyRange, null, progress, CancellationToken.None);
|
||||
|
||||
// Progress callback runs on the synchronization context — pump it
|
||||
await Task.Delay(50);
|
||||
|
||||
ticks.Should().HaveCount(3);
|
||||
ticks.Select(t => t.Total).Should().AllBeEquivalentTo(3);
|
||||
ticks.Select(t => t.Processed).Should().Equal(1, 2, 3);
|
||||
ticks.Select(t => t.Outcome).Should().Equal(
|
||||
ResultLoadOutcome.AlreadyLoaded,
|
||||
ResultLoadOutcome.NotYetComplete,
|
||||
ResultLoadOutcome.Loaded);
|
||||
ticks[2].Result.Should().Be(result3);
|
||||
}
|
||||
|
||||
// ── Failure isolation ───────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ContinueAfterScrapeFailure_AndReportFailedOutcome()
|
||||
{
|
||||
var ev1 = TestFixtures.MakeEvent("11111111");
|
||||
var ev2 = TestFixtures.MakeEvent("22222222");
|
||||
var result2 = TestFixtures.MakeResult(ev2.Id);
|
||||
|
||||
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new List<Event> { ev1, ev2 }.AsReadOnly());
|
||||
_resultRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>())
|
||||
.Returns((EventResult?)null);
|
||||
|
||||
_scraper.ScrapeEventResultAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev1.Id), Arg.Any<CancellationToken>())
|
||||
.ThrowsAsync(new HttpRequestException("network down"));
|
||||
_scraper.ScrapeEventResultAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev2.Id), Arg.Any<CancellationToken>())
|
||||
.Returns(result2);
|
||||
|
||||
var ticks = new List<PullResultsProgress>();
|
||||
var progress = new Progress<PullResultsProgress>(ticks.Add);
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
var (inspected, loaded, _) = await sut.ExecuteAsync(AnyRange, null, progress, CancellationToken.None);
|
||||
|
||||
await Task.Delay(50);
|
||||
|
||||
inspected.Should().Be(2);
|
||||
loaded.Should().Be(1, "ev1 failed, ev2 loaded");
|
||||
|
||||
ticks.Should().HaveCount(2);
|
||||
ticks[0].Outcome.Should().Be(ResultLoadOutcome.Failed);
|
||||
ticks[1].Outcome.Should().Be(ResultLoadOutcome.Loaded);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,8 +30,8 @@ public sealed class PullUpcomingEventsUseCaseTests
|
||||
|
||||
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>()).Returns(events);
|
||||
_eventRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>()).Returns((Event?)null);
|
||||
_scraper.ScrapeEventOddsAsync(Arg.Any<EventId>(), OddsSource.PreMatch, Arg.Any<CancellationToken>())
|
||||
.Returns(ci => TestFixtures.MakeSnapshot(ci.Arg<EventId>()));
|
||||
_scraper.ScrapeEventOddsAsync(Arg.Any<Event>(), OddsSource.PreMatch, Arg.Any<CancellationToken>())
|
||||
.Returns(ci => TestFixtures.MakeSnapshot(ci.Arg<Event>().Id));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
@@ -63,8 +63,8 @@ public sealed class PullUpcomingEventsUseCaseTests
|
||||
_eventRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>()).Returns((Event?)null);
|
||||
_eventRepo.GetAsync(ev3.Id, Arg.Any<CancellationToken>()).Returns((Event?)null);
|
||||
|
||||
_scraper.ScrapeEventOddsAsync(Arg.Any<EventId>(), OddsSource.PreMatch, Arg.Any<CancellationToken>())
|
||||
.Returns(ci => TestFixtures.MakeSnapshot(ci.Arg<EventId>()));
|
||||
_scraper.ScrapeEventOddsAsync(Arg.Any<Event>(), OddsSource.PreMatch, Arg.Any<CancellationToken>())
|
||||
.Returns(ci => TestFixtures.MakeSnapshot(ci.Arg<Event>().Id));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
@@ -91,9 +91,11 @@ public sealed class PullUpcomingEventsUseCaseTests
|
||||
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>()).Returns(events);
|
||||
_eventRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>()).Returns((Event?)null);
|
||||
|
||||
_scraper.ScrapeEventOddsAsync(ev1.Id, OddsSource.PreMatch, Arg.Any<CancellationToken>())
|
||||
_scraper.ScrapeEventOddsAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev1.Id), OddsSource.PreMatch, Arg.Any<CancellationToken>())
|
||||
.ThrowsAsync(new HttpRequestException("site down"));
|
||||
_scraper.ScrapeEventOddsAsync(ev2.Id, OddsSource.PreMatch, Arg.Any<CancellationToken>())
|
||||
_scraper.ScrapeEventOddsAsync(
|
||||
Arg.Is<Event>(e => e.Id == ev2.Id), OddsSource.PreMatch, Arg.Any<CancellationToken>())
|
||||
.Returns(TestFixtures.MakeSnapshot(ev2.Id));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
@@ -121,8 +121,18 @@ public sealed class EventTests
|
||||
public void Event_IsImmutable_NoSettablePublicProperties()
|
||||
{
|
||||
var eventType = typeof(Event);
|
||||
|
||||
// Init-only setters (`init`) are immutable from a runtime perspective
|
||||
// — they can only be assigned during object initialization, not later.
|
||||
// The CLR encodes them with an `IsExternalInit` required custom modifier
|
||||
// on the setter's return parameter.
|
||||
static bool IsInitOnly(System.Reflection.MethodInfo setter) =>
|
||||
setter.ReturnParameter
|
||||
.GetRequiredCustomModifiers()
|
||||
.Any(m => m.FullName == "System.Runtime.CompilerServices.IsExternalInit");
|
||||
|
||||
var settableProperties = eventType.GetProperties()
|
||||
.Where(p => p.CanWrite && p.GetSetMethod(nonPublic: false) is not null)
|
||||
.Where(p => p.CanWrite && p.GetSetMethod(nonPublic: false) is { } setter && !IsInitOnly(setter))
|
||||
.ToList();
|
||||
|
||||
settableProperties.Should().BeEmpty("Event must be immutable.");
|
||||
|
||||
@@ -78,12 +78,13 @@ public sealed class LiveOddsPollerTests
|
||||
var eventRepo = Substitute.For<IEventRepository>();
|
||||
var snapshotRepo = Substitute.For<ISnapshotRepository>();
|
||||
|
||||
// ScrapeUpcomingAsync called by use case internally
|
||||
scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<Event>());
|
||||
eventRepo.ListAsync(Arg.Any<CancellationToken>())
|
||||
// Use case discovers live events via ScrapeLiveAsync (NOT ListAsync)
|
||||
scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(new List<Event> { ev }.AsReadOnly());
|
||||
scraper.ScrapeEventOddsAsync(eventId, OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
eventRepo.GetAsync(eventId, Arg.Any<CancellationToken>())
|
||||
.Returns(ev);
|
||||
scraper.ScrapeEventOddsAsync(
|
||||
Arg.Is<Event>(e => e.Id == eventId), OddsSource.Live, Arg.Any<CancellationToken>())
|
||||
.Returns(MakeSnapshot(eventId));
|
||||
|
||||
var sp = BuildServiceProvider(scraper, eventRepo, snapshotRepo);
|
||||
@@ -122,7 +123,7 @@ public sealed class LiveOddsPollerTests
|
||||
|
||||
// Assert — no snapshot attempts while disabled
|
||||
await snapshotRepo.DidNotReceive().AddAsync(Arg.Any<OddsSnapshot>(), Arg.Any<CancellationToken>());
|
||||
await eventRepo.DidNotReceive().ListAsync(Arg.Any<CancellationToken>());
|
||||
await scraper.DidNotReceive().ScrapeLiveAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -133,10 +134,11 @@ public sealed class LiveOddsPollerTests
|
||||
var eventRepo = Substitute.For<IEventRepository>();
|
||||
var snapshotRepo = Substitute.For<ISnapshotRepository>();
|
||||
|
||||
scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<Event>());
|
||||
eventRepo.ListAsync(Arg.Any<CancellationToken>())
|
||||
.ThrowsAsync(new InvalidOperationException("DB unavailable"));
|
||||
// Use case calls ScrapeLiveAsync first; if it throws, the cycle returns 0
|
||||
// without hitting the repo. To exercise the "poller survives failures"
|
||||
// path, make ScrapeLiveAsync itself throw.
|
||||
scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
||||
.ThrowsAsync(new InvalidOperationException("listing unavailable"));
|
||||
|
||||
var sp = BuildServiceProvider(scraper, eventRepo, snapshotRepo);
|
||||
var opts = BuildOptions(enabled: true, intervalSeconds: 0);
|
||||
@@ -153,7 +155,9 @@ public sealed class LiveOddsPollerTests
|
||||
// Assert — StopAsync must not propagate the exception
|
||||
await stopAct.Should().NotThrowAsync();
|
||||
|
||||
// DB was hit multiple times (poller didn't give up after first failure)
|
||||
await eventRepo.Received().ListAsync(Arg.Any<CancellationToken>());
|
||||
// Scraper was hit at least once (poller cycled at least one iteration).
|
||||
// The use case swallows ScrapeLiveAsync exceptions and returns 0, so the
|
||||
// poller's catch block is not triggered — but it still cycles.
|
||||
await scraper.Received().ScrapeLiveAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user