Files
maraphon-app/tests/Marathon.Application.Tests/UseCases/PullUpcomingEventsUseCaseTests.cs
T
alexei.dolgolyov 9c5d3df1f2 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.
2026-05-09 15:10:27 +03:00

127 lines
5.2 KiB
C#

using FluentAssertions;
using Marathon.Application.Abstractions;
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;
public sealed class PullUpcomingEventsUseCaseTests
{
private readonly IOddsScraper _scraper = Substitute.For<IOddsScraper>();
private readonly IEventRepository _eventRepo = Substitute.For<IEventRepository>();
private readonly ISnapshotRepository _snapshotRepo = Substitute.For<ISnapshotRepository>();
private PullUpcomingEventsUseCase CreateSut() =>
new(_scraper, _eventRepo, _snapshotRepo,
NullLogger<PullUpcomingEventsUseCase>.Instance);
[Fact]
public async Task Should_PersistNewEventsAndCaptureSnapshots_When_ScraperReturnsEvents()
{
// Arrange: scraper returns 2 events, neither exists in DB
var ev1 = TestFixtures.MakeEvent("11111111");
var ev2 = TestFixtures.MakeEvent("22222222");
var events = new List<Event> { ev1, ev2 }.AsReadOnly();
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>()).Returns(events);
_eventRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>()).Returns((Event?)null);
_scraper.ScrapeEventOddsAsync(Arg.Any<Event>(), OddsSource.PreMatch, Arg.Any<CancellationToken>())
.Returns(ci => TestFixtures.MakeSnapshot(ci.Arg<Event>().Id));
var sut = CreateSut();
// Act
var (processed, newEvents, snapshots) = await sut.ExecuteAsync(CancellationToken.None);
// Assert
processed.Should().Be(2);
newEvents.Should().Be(2);
snapshots.Should().Be(2);
await _eventRepo.Received(2).AddAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>());
await _snapshotRepo.Received(2).AddAsync(Arg.Any<OddsSnapshot>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Should_SkipExistingEvents_When_EventAlreadyInDatabase()
{
// Arrange: 3 events from scraper — 1 already in DB, 2 new
var ev1 = TestFixtures.MakeEvent("11111111"); // already in DB
var ev2 = TestFixtures.MakeEvent("22222222"); // new
var ev3 = TestFixtures.MakeEvent("33333333"); // new
var events = new List<Event> { ev1, ev2, ev3 }.AsReadOnly();
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>()).Returns(events);
// ev1 exists, ev2/ev3 do not
_eventRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>()).Returns(ev1);
_eventRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>()).Returns((Event?)null);
_eventRepo.GetAsync(ev3.Id, Arg.Any<CancellationToken>()).Returns((Event?)null);
_scraper.ScrapeEventOddsAsync(Arg.Any<Event>(), OddsSource.PreMatch, Arg.Any<CancellationToken>())
.Returns(ci => TestFixtures.MakeSnapshot(ci.Arg<Event>().Id));
var sut = CreateSut();
// Act
var (processed, newEvents, snapshots) = await sut.ExecuteAsync(CancellationToken.None);
// Assert
processed.Should().Be(3);
newEvents.Should().Be(2, "ev1 was already in the database");
snapshots.Should().Be(3, "snapshots are captured for all events regardless of duplicate status");
await _eventRepo.Received(2).AddAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>());
await _eventRepo.DidNotReceive().AddAsync(ev1, Arg.Any<CancellationToken>());
}
[Fact]
public async Task Should_ContinueProcessing_When_SnapshotCaptureFailsForOneEvent()
{
// Arrange: 2 events — snapshot for first throws, second succeeds
var ev1 = TestFixtures.MakeEvent("11111111");
var ev2 = TestFixtures.MakeEvent("22222222");
var events = new List<Event> { ev1, ev2 }.AsReadOnly();
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>()).Returns(events);
_eventRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>()).Returns((Event?)null);
_scraper.ScrapeEventOddsAsync(
Arg.Is<Event>(e => e.Id == ev1.Id), OddsSource.PreMatch, Arg.Any<CancellationToken>())
.ThrowsAsync(new HttpRequestException("site down"));
_scraper.ScrapeEventOddsAsync(
Arg.Is<Event>(e => e.Id == ev2.Id), OddsSource.PreMatch, Arg.Any<CancellationToken>())
.Returns(TestFixtures.MakeSnapshot(ev2.Id));
var sut = CreateSut();
// Act — should not throw
var (processed, newEvents, snapshots) = await sut.ExecuteAsync(CancellationToken.None);
// Assert
processed.Should().Be(2);
newEvents.Should().Be(2);
snapshots.Should().Be(1, "only ev2 snapshot succeeded");
}
[Fact]
public async Task Should_ReturnZeros_When_ScraperReturnsNoEvents()
{
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
.Returns(Array.Empty<Event>());
var sut = CreateSut();
var (processed, newEvents, snapshots) = await sut.ExecuteAsync(CancellationToken.None);
processed.Should().Be(0);
newEvents.Should().Be(0);
snapshots.Should().Be(0);
}
}