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:
@@ -1,4 +1,3 @@
|
|||||||
using Marathon.Application.Storage;
|
|
||||||
using Marathon.Domain.Entities;
|
using Marathon.Domain.Entities;
|
||||||
using Marathon.Domain.Enums;
|
using Marathon.Domain.Enums;
|
||||||
using Marathon.Domain.ValueObjects;
|
using Marathon.Domain.ValueObjects;
|
||||||
@@ -25,29 +24,52 @@ public interface IOddsScraper
|
|||||||
SportCode? sportFilter,
|
SportCode? sportFilter,
|
||||||
CancellationToken ct);
|
CancellationToken ct);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the list of currently-live events parsed from <c>/su/live</c>.
|
||||||
|
/// Each returned <see cref="Event"/> has its <see cref="Event.EventPath"/>
|
||||||
|
/// populated so the caller can immediately fetch its odds snapshot.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ct">Cancellation token.</param>
|
||||||
|
Task<IReadOnlyList<Event>> ScrapeLiveAsync(CancellationToken ct);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Fetches a full odds snapshot (all markets) for a single event.
|
/// Fetches a full odds snapshot (all markets) for a single event.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">The bookmaker's event identifier.</param>
|
/// <param name="eventInfo">
|
||||||
|
/// The event to scrape — its <see cref="Event.EventPath"/> drives URL construction.
|
||||||
|
/// When the path is null (legacy row), the scraper falls back to the numeric event ID.
|
||||||
|
/// </param>
|
||||||
/// <param name="source">Whether this is a pre-match or live scrape.</param>
|
/// <param name="source">Whether this is a pre-match or live scrape.</param>
|
||||||
/// <param name="ct">Cancellation token.</param>
|
/// <param name="ct">Cancellation token.</param>
|
||||||
Task<OddsSnapshot> ScrapeEventOddsAsync(
|
Task<OddsSnapshot> ScrapeEventOddsAsync(
|
||||||
EventId id,
|
Event eventInfo,
|
||||||
OddsSource source,
|
OddsSource source,
|
||||||
CancellationToken ct);
|
CancellationToken ct);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns completed event results within a date range.
|
/// Fetches the event-detail page for a single event and extracts its final
|
||||||
|
/// result if and only if the bookmaker has flagged the match as complete
|
||||||
|
/// (<c>eventJsonInfo.matchIsComplete = true</c>).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// <b>Interim no-op (Phase 3):</b> marathonbet.by has no public results archive
|
/// marathonbet.by has no public results archive endpoint
|
||||||
/// endpoint (<c>/su/results</c> → 404). This method returns an empty list and
|
/// (<c>/su/results</c> → 404), so results are harvested per-event by
|
||||||
/// logs a warning. Results harvesting is implemented in Phase 8 via polling
|
/// re-fetching the same event-detail HTML used for odds scraping and
|
||||||
/// event-detail pages until <c>matchIsComplete=true</c>.
|
/// parsing the embedded <c>eventJsonInfo</c> JSON.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
Task<IReadOnlyList<EventResult>> ScrapeResultsAsync(
|
/// <param name="eventInfo">
|
||||||
DateRange range,
|
/// The event to query — its <see cref="Event.EventPath"/> drives URL
|
||||||
|
/// construction (with the numeric ID as a best-effort fallback).
|
||||||
|
/// </param>
|
||||||
|
/// <param name="ct">Cancellation token.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// An <see cref="EventResult"/> when the match is complete and the score
|
||||||
|
/// could be parsed, <c>null</c> when the match is still in-progress or
|
||||||
|
/// the score string is unrecognised.
|
||||||
|
/// </returns>
|
||||||
|
Task<EventResult?> ScrapeEventResultAsync(
|
||||||
|
Event eventInfo,
|
||||||
CancellationToken ct);
|
CancellationToken ct);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,21 @@
|
|||||||
using Marathon.Application.Abstractions;
|
using Marathon.Application.Abstractions;
|
||||||
|
using Marathon.Domain.Entities;
|
||||||
using Marathon.Domain.Enums;
|
using Marathon.Domain.Enums;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Marathon.Application.UseCases;
|
namespace Marathon.Application.UseCases;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// For each currently-live event in the database, fetches a fresh odds snapshot
|
/// Discovers currently-live events from the bookmaker's <c>/su/live</c> listing,
|
||||||
/// via the scraper and persists it.
|
/// persists any not yet known to the database, and captures a fresh
|
||||||
|
/// <see cref="OddsSource.Live"/> snapshot for each.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Live discovery is authoritative: events that go live without ever appearing
|
||||||
|
/// in the upcoming list (late-added matches, in-play markets opened on demand)
|
||||||
|
/// are picked up here. Pre-match-only events are NOT scraped by this use case —
|
||||||
|
/// they would just be wasted requests against the bookmaker.
|
||||||
|
/// </remarks>
|
||||||
public sealed class PullLiveOddsUseCase
|
public sealed class PullLiveOddsUseCase
|
||||||
{
|
{
|
||||||
private readonly IOddsScraper _scraper;
|
private readonly IOddsScraper _scraper;
|
||||||
@@ -31,27 +39,80 @@ public sealed class PullLiveOddsUseCase
|
|||||||
/// Executes one live-odds polling cycle.
|
/// Executes one live-odds polling cycle.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="ct">Cancellation token.</param>
|
/// <param name="ct">Cancellation token.</param>
|
||||||
/// <returns>Number of snapshots successfully captured.</returns>
|
/// <returns>Number of live snapshots successfully captured.</returns>
|
||||||
public async Task<int> ExecuteAsync(CancellationToken ct)
|
public async Task<int> ExecuteAsync(CancellationToken ct)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("PullLiveOddsUseCase: cycle started");
|
_logger.LogInformation("PullLiveOddsUseCase: cycle started");
|
||||||
|
|
||||||
// Refresh odds for every event we already track. The "live vs pre-match"
|
IReadOnlyList<Event> liveEvents;
|
||||||
// distinction is recorded by stamping each snapshot with OddsSource.Live.
|
try
|
||||||
// TODO(phase-6/8): once IEventRepository.ListLiveAsync(cutoff) ships, swap
|
{
|
||||||
// this for a filter that only returns currently-live events to avoid
|
liveEvents = await _scraper.ScrapeLiveAsync(ct);
|
||||||
// hammering the scraper with non-live IDs.
|
}
|
||||||
var allEvents = await _eventRepo.ListAsync(ct);
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex,
|
||||||
|
"PullLiveOddsUseCase: failed to fetch live event listing — skipping cycle");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
"PullLiveOddsUseCase: scraper returned {Count} live events",
|
||||||
|
liveEvents.Count);
|
||||||
|
|
||||||
int snapshotsCaptured = 0;
|
int snapshotsCaptured = 0;
|
||||||
|
|
||||||
foreach (var ev in allEvents)
|
foreach (var live in liveEvents)
|
||||||
{
|
{
|
||||||
ct.ThrowIfCancellationRequested();
|
ct.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
// Persist new live events — the upcoming poller may not have seen them
|
||||||
|
// yet (or never will, for matches added after their scheduled start).
|
||||||
|
// The Live page reads from the events table, so a new live row must
|
||||||
|
// exist before its snapshots become visible.
|
||||||
|
Event eventForScrape;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var snapshot = await _scraper.ScrapeEventOddsAsync(ev.Id, OddsSource.Live, ct);
|
var existing = await _eventRepo.GetAsync(live.Id, ct);
|
||||||
|
if (existing is null)
|
||||||
|
{
|
||||||
|
await _eventRepo.AddAsync(live, ct);
|
||||||
|
await _eventRepo.SaveChangesAsync(ct);
|
||||||
|
eventForScrape = live;
|
||||||
|
}
|
||||||
|
else if (existing.EventPath is null && live.EventPath is not null)
|
||||||
|
{
|
||||||
|
// Backfill EventPath on rows persisted before the column existed,
|
||||||
|
// so subsequent scrapes can use the correct URL.
|
||||||
|
var patched = existing with { EventPath = live.EventPath };
|
||||||
|
await _eventRepo.UpdateAsync(patched, ct);
|
||||||
|
await _eventRepo.SaveChangesAsync(ct);
|
||||||
|
eventForScrape = patched;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eventForScrape = existing;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"PullLiveOddsUseCase: failed to persist/lookup live event {EventId} — skipping",
|
||||||
|
live.Id.Value);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var snapshot = await _scraper.ScrapeEventOddsAsync(eventForScrape, OddsSource.Live, ct);
|
||||||
await _snapshotRepo.AddAsync(snapshot, ct);
|
await _snapshotRepo.AddAsync(snapshot, ct);
|
||||||
await _snapshotRepo.SaveChangesAsync(ct);
|
await _snapshotRepo.SaveChangesAsync(ct);
|
||||||
snapshotsCaptured++;
|
snapshotsCaptured++;
|
||||||
@@ -64,13 +125,13 @@ public sealed class PullLiveOddsUseCase
|
|||||||
{
|
{
|
||||||
_logger.LogWarning(ex,
|
_logger.LogWarning(ex,
|
||||||
"PullLiveOddsUseCase: failed to capture live snapshot for event {EventId} — skipping",
|
"PullLiveOddsUseCase: failed to capture live snapshot for event {EventId} — skipping",
|
||||||
ev.Id.Value);
|
eventForScrape.Id.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
"PullLiveOddsUseCase: cycle done — snapshots captured for {Count}/{Total} events",
|
"PullLiveOddsUseCase: cycle done — snapshots captured for {Count}/{Total} live events",
|
||||||
snapshotsCaptured, allEvents.Count);
|
snapshotsCaptured, liveEvents.Count);
|
||||||
|
|
||||||
return snapshotsCaptured;
|
return snapshotsCaptured;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,62 @@
|
|||||||
using Marathon.Application.Abstractions;
|
using Marathon.Application.Abstractions;
|
||||||
using Marathon.Application.Storage;
|
using Marathon.Application.Storage;
|
||||||
|
using Marathon.Domain.Entities;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using DomainEventId = Marathon.Domain.ValueObjects.EventId;
|
using DomainEventId = Marathon.Domain.ValueObjects.EventId;
|
||||||
|
|
||||||
namespace Marathon.Application.UseCases;
|
namespace Marathon.Application.UseCases;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Scaffolded results loader — inspects events for completion and persists
|
/// Per-event progress emitted by <see cref="PullResultsUseCase.ExecuteAsync"/>.
|
||||||
/// <see cref="Domain.Entities.EventResult"/>s when detected.
|
/// Used by the UI to render a progress bar and the running list of loaded
|
||||||
|
/// results — each tick is fired AFTER the bookmaker has been queried for
|
||||||
|
/// <see cref="EventId"/>, so the UI sees one tick per inspected event.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Processed">Total events processed so far (1-based at the first tick).</param>
|
||||||
|
/// <param name="Total">Total candidates in this run.</param>
|
||||||
|
/// <param name="EventId">The event just processed.</param>
|
||||||
|
/// <param name="Outcome">What happened — see <see cref="ResultLoadOutcome"/>.</param>
|
||||||
|
/// <param name="Result">The persisted <see cref="EventResult"/> when <paramref name="Outcome"/> is <see cref="ResultLoadOutcome.Loaded"/>; otherwise null.</param>
|
||||||
|
public sealed record PullResultsProgress(
|
||||||
|
int Processed,
|
||||||
|
int Total,
|
||||||
|
DomainEventId EventId,
|
||||||
|
ResultLoadOutcome Outcome,
|
||||||
|
EventResult? Result);
|
||||||
|
|
||||||
|
/// <summary>What happened to a single candidate event during a results load.</summary>
|
||||||
|
public enum ResultLoadOutcome
|
||||||
|
{
|
||||||
|
/// <summary>A new <see cref="EventResult"/> was scraped and persisted.</summary>
|
||||||
|
Loaded,
|
||||||
|
|
||||||
|
/// <summary>The event already had a stored result — no work was done.</summary>
|
||||||
|
AlreadyLoaded,
|
||||||
|
|
||||||
|
/// <summary>The match isn't complete yet — try again later.</summary>
|
||||||
|
NotYetComplete,
|
||||||
|
|
||||||
|
/// <summary>The scrape failed (HTTP, parse, etc.). Logged at warning.</summary>
|
||||||
|
Failed,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads completed-event results into the database.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// <b>Phase 4 scaffold:</b> This implementation is intentionally minimal.
|
/// For each candidate event, the use case:
|
||||||
/// The formal watch-list polling strategy lands in Phase 8, when
|
|
||||||
/// <c>IOddsScraper.ScrapeResultsAsync</c> will be replaced with real
|
|
||||||
/// per-event polling against <c>IResultsParser</c>.
|
|
||||||
/// </para>
|
/// </para>
|
||||||
|
/// <list type="number">
|
||||||
|
/// <item>Skips it if a result is already stored (idempotent).</item>
|
||||||
|
/// <item>Calls <see cref="IOddsScraper.ScrapeEventResultAsync"/>, which returns
|
||||||
|
/// a non-null <see cref="EventResult"/> only when the bookmaker reports
|
||||||
|
/// <c>matchIsComplete=true</c>.</item>
|
||||||
|
/// <item>Persists the result and increments the loaded count.</item>
|
||||||
|
/// </list>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// Current behaviour: calls <c>IOddsScraper.ScrapeResultsAsync</c> (which
|
/// Candidates are either an explicit <paramref name="selection"/> list or — when
|
||||||
/// returns an empty list and logs a warning per Phase 3), so
|
/// null/empty — every event scheduled in <c>range</c>.
|
||||||
/// <c>ResultsLoaded</c> will always be 0 until Phase 8.
|
|
||||||
/// All events with existing results are skipped (idempotent).
|
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public sealed class PullResultsUseCase
|
public sealed class PullResultsUseCase
|
||||||
@@ -45,79 +81,109 @@ public sealed class PullResultsUseCase
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Inspects events for completion and persists results.
|
/// Inspects events for completion and persists results.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="range">Date range to scope the event search.</param>
|
/// <param name="range">Date range used when <paramref name="selection"/> is null or empty.</param>
|
||||||
/// <param name="selection">
|
/// <param name="selection">
|
||||||
/// When non-null, only these event IDs are inspected.
|
/// When non-empty, only these event IDs are inspected.
|
||||||
/// When null, all events in <paramref name="range"/> without a result row are inspected.
|
/// When null or empty, all events in <paramref name="range"/> without a stored
|
||||||
|
/// result are inspected.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="progress">
|
||||||
|
/// Optional progress sink. Receives one update per candidate AFTER the scrape
|
||||||
|
/// has resolved. Suitable for binding to a UI progress indicator.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="ct">Cancellation token.</param>
|
/// <param name="ct">Cancellation token.</param>
|
||||||
/// <returns>
|
|
||||||
/// A tuple of <c>(Inspected, ResultsLoaded, Skipped)</c> where:
|
|
||||||
/// <list type="bullet">
|
|
||||||
/// <item><c>Inspected</c>: total candidates examined.</item>
|
|
||||||
/// <item><c>ResultsLoaded</c>: results that were persisted this cycle.</item>
|
|
||||||
/// <item><c>Skipped</c>: events already with a result (idempotency guard).</item>
|
|
||||||
/// </list>
|
|
||||||
/// </returns>
|
|
||||||
public async Task<(int Inspected, int ResultsLoaded, int Skipped)> ExecuteAsync(
|
public async Task<(int Inspected, int ResultsLoaded, int Skipped)> ExecuteAsync(
|
||||||
DateRange range,
|
DateRange range,
|
||||||
IReadOnlyList<DomainEventId>? selection,
|
IReadOnlyList<DomainEventId>? selection,
|
||||||
|
IProgress<PullResultsProgress>? progress,
|
||||||
CancellationToken ct)
|
CancellationToken ct)
|
||||||
{
|
{
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
"PullResultsUseCase: cycle started — range={From:O}..{To:O}, selection={SelectionCount}",
|
"PullResultsUseCase: cycle started — range={From:O}..{To:O}, selection={SelectionCount}",
|
||||||
range.From, range.To, selection?.Count.ToString() ?? "all");
|
range.From, range.To, selection?.Count.ToString() ?? "all");
|
||||||
|
|
||||||
// Resolve the candidate event IDs.
|
var candidates = await ResolveCandidatesAsync(range, selection, ct).ConfigureAwait(false);
|
||||||
IReadOnlyList<Domain.Entities.Event> candidates;
|
|
||||||
if (selection is { Count: > 0 })
|
|
||||||
{
|
|
||||||
var selected = new List<Domain.Entities.Event>(selection.Count);
|
|
||||||
foreach (var id in selection)
|
|
||||||
{
|
|
||||||
ct.ThrowIfCancellationRequested();
|
|
||||||
var ev = await _eventRepo.GetAsync(id, ct);
|
|
||||||
if (ev is not null)
|
|
||||||
selected.Add(ev);
|
|
||||||
}
|
|
||||||
candidates = selected;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
candidates = await _eventRepo.ListByDateRangeAsync(range, ct);
|
|
||||||
}
|
|
||||||
|
|
||||||
int inspected = 0;
|
int inspected = 0;
|
||||||
int resultsLoaded = 0;
|
int resultsLoaded = 0;
|
||||||
int skipped = 0;
|
int skipped = 0;
|
||||||
|
|
||||||
// Use the scraper's results endpoint (currently a no-op in Phase 3 — returns []).
|
|
||||||
var scraped = await _scraper.ScrapeResultsAsync(range, ct);
|
|
||||||
var scrapedByEventId = scraped.ToDictionary(r => r.EventId.Value, r => r);
|
|
||||||
|
|
||||||
foreach (var ev in candidates)
|
foreach (var ev in candidates)
|
||||||
{
|
{
|
||||||
ct.ThrowIfCancellationRequested();
|
ct.ThrowIfCancellationRequested();
|
||||||
inspected++;
|
inspected++;
|
||||||
|
|
||||||
try
|
var (outcome, persisted) = await ProcessOneAsync(ev, ct).ConfigureAwait(false);
|
||||||
|
switch (outcome)
|
||||||
{
|
{
|
||||||
// Idempotency: skip events that already have a result stored.
|
case ResultLoadOutcome.Loaded: resultsLoaded++; break;
|
||||||
var existingResult = await _resultRepo.GetAsync(ev.Id, ct);
|
case ResultLoadOutcome.AlreadyLoaded: skipped++; break;
|
||||||
if (existingResult is not null)
|
|
||||||
{
|
|
||||||
skipped++;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the scraper returned a result for this event.
|
progress?.Report(new PullResultsProgress(
|
||||||
if (scrapedByEventId.TryGetValue(ev.Id.Value, out var result))
|
Processed: inspected,
|
||||||
{
|
Total: candidates.Count,
|
||||||
await _resultRepo.AddAsync(result, ct);
|
EventId: ev.Id,
|
||||||
await _resultRepo.SaveChangesAsync(ct);
|
Outcome: outcome,
|
||||||
resultsLoaded++;
|
Result: persisted));
|
||||||
}
|
}
|
||||||
// Phase 8: else → add to watch list for next poll cycle.
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
"PullResultsUseCase: cycle done — inspected={Inspected}, loaded={Loaded}, skipped={Skipped}",
|
||||||
|
inspected, resultsLoaded, skipped);
|
||||||
|
|
||||||
|
return (inspected, resultsLoaded, skipped);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Convenience overload without progress reporting (worker callers).</summary>
|
||||||
|
public Task<(int Inspected, int ResultsLoaded, int Skipped)> ExecuteAsync(
|
||||||
|
DateRange range,
|
||||||
|
IReadOnlyList<DomainEventId>? selection,
|
||||||
|
CancellationToken ct)
|
||||||
|
=> ExecuteAsync(range, selection, progress: null, ct);
|
||||||
|
|
||||||
|
private async Task<IReadOnlyList<Event>> ResolveCandidatesAsync(
|
||||||
|
DateRange range,
|
||||||
|
IReadOnlyList<DomainEventId>? selection,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
if (selection is { Count: > 0 })
|
||||||
|
{
|
||||||
|
var resolved = new List<Event>(selection.Count);
|
||||||
|
foreach (var id in selection)
|
||||||
|
{
|
||||||
|
ct.ThrowIfCancellationRequested();
|
||||||
|
var ev = await _eventRepo.GetAsync(id, ct).ConfigureAwait(false);
|
||||||
|
if (ev is not null)
|
||||||
|
resolved.Add(ev);
|
||||||
|
}
|
||||||
|
return resolved;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await _eventRepo.ListByDateRangeAsync(range, ct).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<(ResultLoadOutcome Outcome, EventResult? Persisted)> ProcessOneAsync(
|
||||||
|
Event ev,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var existing = await _resultRepo.GetAsync(ev.Id, ct).ConfigureAwait(false);
|
||||||
|
if (existing is not null)
|
||||||
|
{
|
||||||
|
return (ResultLoadOutcome.AlreadyLoaded, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
var scraped = await _scraper.ScrapeEventResultAsync(ev, ct).ConfigureAwait(false);
|
||||||
|
if (scraped is null)
|
||||||
|
{
|
||||||
|
return (ResultLoadOutcome.NotYetComplete, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _resultRepo.AddAsync(scraped, ct).ConfigureAwait(false);
|
||||||
|
await _resultRepo.SaveChangesAsync(ct).ConfigureAwait(false);
|
||||||
|
return (ResultLoadOutcome.Loaded, scraped);
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
@@ -128,13 +194,7 @@ public sealed class PullResultsUseCase
|
|||||||
_logger.LogWarning(ex,
|
_logger.LogWarning(ex,
|
||||||
"PullResultsUseCase: error processing event {EventId} — skipping",
|
"PullResultsUseCase: error processing event {EventId} — skipping",
|
||||||
ev.Id.Value);
|
ev.Id.Value);
|
||||||
|
return (ResultLoadOutcome.Failed, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation(
|
|
||||||
"PullResultsUseCase: cycle done — inspected={Inspected}, loaded={Loaded}, skipped={Skipped}",
|
|
||||||
inspected, resultsLoaded, skipped);
|
|
||||||
|
|
||||||
return (inspected, resultsLoaded, skipped);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ public sealed class PullUpcomingEventsUseCase
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var snapshot = await _scraper.ScrapeEventOddsAsync(
|
var snapshot = await _scraper.ScrapeEventOddsAsync(
|
||||||
ev.Id,
|
ev,
|
||||||
Domain.Enums.OddsSource.PreMatch,
|
Domain.Enums.OddsSource.PreMatch,
|
||||||
ct);
|
ct);
|
||||||
|
|
||||||
|
|||||||
@@ -52,4 +52,17 @@ public sealed record Event(
|
|||||||
public string Side2Name { get; } = string.IsNullOrWhiteSpace(Side2Name)
|
public string Side2Name { get; } = string.IsNullOrWhiteSpace(Side2Name)
|
||||||
? throw new ArgumentException("Side2Name must not be empty.", nameof(Side2Name))
|
? throw new ArgumentException("Side2Name must not be empty.", nameof(Side2Name))
|
||||||
: Side2Name;
|
: Side2Name;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bookmaker URL fragment used to fetch event-detail markets, sourced from the
|
||||||
|
/// listing page's <c>data-event-path</c> attribute (e.g.
|
||||||
|
/// <c>"Football/Clubs.+International/UEFA+Champions+League/.../Arsenal+vs+Chelsea+-+28089645"</c>).
|
||||||
|
/// Combined with <c>/su/betting/</c> by the scraper.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Optional for backward compatibility with rows persisted before the column
|
||||||
|
/// was introduced. When null, the scraper falls back to the (less reliable)
|
||||||
|
/// numeric event ID.
|
||||||
|
/// </remarks>
|
||||||
|
public string? EventPath { get; init; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using Marathon.Infrastructure.Persistence;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Marathon.Infrastructure.Migrations;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
[DbContext(typeof(MarathonDbContext))]
|
||||||
|
[Migration("20260506000000_AddEventPath")]
|
||||||
|
public partial class AddEventPath : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "EventPath",
|
||||||
|
table: "Events",
|
||||||
|
type: "TEXT",
|
||||||
|
nullable: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "EventPath",
|
||||||
|
table: "Events");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -49,6 +49,7 @@ partial class MarathonDbContextModelSnapshot : ModelSnapshot
|
|||||||
b.Property<string>("EventCode").HasColumnType("TEXT");
|
b.Property<string>("EventCode").HasColumnType("TEXT");
|
||||||
b.Property<string>("Category").IsRequired().HasDefaultValue("").HasColumnType("TEXT");
|
b.Property<string>("Category").IsRequired().HasDefaultValue("").HasColumnType("TEXT");
|
||||||
b.Property<string>("CountryCode").IsRequired().HasColumnType("TEXT");
|
b.Property<string>("CountryCode").IsRequired().HasColumnType("TEXT");
|
||||||
|
b.Property<string>("EventPath").HasColumnType("TEXT");
|
||||||
b.Property<string>("LeagueId").IsRequired().HasColumnType("TEXT");
|
b.Property<string>("LeagueId").IsRequired().HasColumnType("TEXT");
|
||||||
b.Property<string>("ScheduledAt").IsRequired().HasColumnType("TEXT");
|
b.Property<string>("ScheduledAt").IsRequired().HasColumnType("TEXT");
|
||||||
b.Property<string>("Side1Name").IsRequired().HasColumnType("TEXT");
|
b.Property<string>("Side1Name").IsRequired().HasColumnType("TEXT");
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ internal sealed class EventConfiguration : IEntityTypeConfiguration<EventEntity>
|
|||||||
builder.Property(e => e.ScheduledAt).HasColumnType("TEXT").IsRequired();
|
builder.Property(e => e.ScheduledAt).HasColumnType("TEXT").IsRequired();
|
||||||
builder.Property(e => e.Side1Name).HasColumnType("TEXT").IsRequired();
|
builder.Property(e => e.Side1Name).HasColumnType("TEXT").IsRequired();
|
||||||
builder.Property(e => e.Side2Name).HasColumnType("TEXT").IsRequired();
|
builder.Property(e => e.Side2Name).HasColumnType("TEXT").IsRequired();
|
||||||
|
builder.Property(e => e.EventPath).HasColumnType("TEXT");
|
||||||
|
|
||||||
// Index for date-range queries and sport filtering
|
// Index for date-range queries and sport filtering
|
||||||
builder.HasIndex(e => new { e.SportCode, e.ScheduledAt }).HasDatabaseName("IX_Events_SportCode_ScheduledAt");
|
builder.HasIndex(e => new { e.SportCode, e.ScheduledAt }).HasDatabaseName("IX_Events_SportCode_ScheduledAt");
|
||||||
|
|||||||
@@ -30,6 +30,13 @@ public sealed class EventEntity
|
|||||||
/// <summary>Name of the second participant (away side).</summary>
|
/// <summary>Name of the second participant (away side).</summary>
|
||||||
public string Side2Name { get; set; } = default!;
|
public string Side2Name { get; set; } = default!;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Optional bookmaker URL fragment used to construct the event-detail page URL.
|
||||||
|
/// Sourced from <c>data-event-path</c> at scrape time. Nullable so older rows
|
||||||
|
/// (persisted before this column existed) round-trip without a backfill.
|
||||||
|
/// </summary>
|
||||||
|
public string? EventPath { get; set; }
|
||||||
|
|
||||||
// Navigation properties
|
// Navigation properties
|
||||||
public ICollection<SnapshotEntity> Snapshots { get; set; } = [];
|
public ICollection<SnapshotEntity> Snapshots { get; set; } = [];
|
||||||
public EventResultEntity? Result { get; set; }
|
public EventResultEntity? Result { get; set; }
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ internal static class Mapping
|
|||||||
ScheduledAt = domain.ScheduledAt.ToString("O"),
|
ScheduledAt = domain.ScheduledAt.ToString("O"),
|
||||||
Side1Name = domain.Side1Name,
|
Side1Name = domain.Side1Name,
|
||||||
Side2Name = domain.Side2Name,
|
Side2Name = domain.Side2Name,
|
||||||
|
EventPath = domain.EventPath,
|
||||||
};
|
};
|
||||||
|
|
||||||
public static Event ToDomain(EventEntity entity) =>
|
public static Event ToDomain(EventEntity entity) =>
|
||||||
@@ -35,7 +36,10 @@ internal static class Mapping
|
|||||||
Category: entity.Category,
|
Category: entity.Category,
|
||||||
ScheduledAt: DateTimeOffset.Parse(entity.ScheduledAt),
|
ScheduledAt: DateTimeOffset.Parse(entity.ScheduledAt),
|
||||||
Side1Name: entity.Side1Name,
|
Side1Name: entity.Side1Name,
|
||||||
Side2Name: entity.Side2Name);
|
Side2Name: entity.Side2Name)
|
||||||
|
{
|
||||||
|
EventPath = entity.EventPath,
|
||||||
|
};
|
||||||
|
|
||||||
// ─── OddsSnapshot ─────────────────────────────────────────────────────────
|
// ─── OddsSnapshot ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using Marathon.Application.Abstractions;
|
using Marathon.Application.Abstractions;
|
||||||
using Marathon.Application.Storage;
|
|
||||||
using Marathon.Domain.Entities;
|
using Marathon.Domain.Entities;
|
||||||
using Marathon.Domain.Enums;
|
using Marathon.Domain.Enums;
|
||||||
using Marathon.Domain.ValueObjects;
|
using Marathon.Domain.ValueObjects;
|
||||||
@@ -75,57 +74,72 @@ public sealed class MarathonbetScraper : IOddsScraper
|
|||||||
return await _upcomingParser.ParseAsync(html, ct).ConfigureAwait(false);
|
return await _upcomingParser.ParseAsync(html, ct).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public async Task<IReadOnlyList<Event>> ScrapeLiveAsync(CancellationToken ct)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Scraping live events from {Path}", LivePath);
|
||||||
|
|
||||||
|
var html = await FetchHtmlAsync(LivePath, ct).ConfigureAwait(false);
|
||||||
|
return await _liveParser.ParseAsync(html, ct).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public async Task<OddsSnapshot> ScrapeEventOddsAsync(
|
public async Task<OddsSnapshot> ScrapeEventOddsAsync(
|
||||||
Marathon.Domain.ValueObjects.EventId id,
|
Event eventInfo,
|
||||||
OddsSource source,
|
OddsSource source,
|
||||||
CancellationToken ct)
|
CancellationToken ct)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(id);
|
ArgumentNullException.ThrowIfNull(eventInfo);
|
||||||
|
|
||||||
// For event detail we need the event path (treeId URL).
|
// Prefer the parsed event-path (data-event-path attribute on the listing
|
||||||
// The caller supplies the EventId; we build the simplest valid URL.
|
// row, ending in "+{treeId}"). Fall back to the numeric event ID for
|
||||||
// In practice, the Application layer should cache the event's detail path
|
// legacy rows that pre-date the EventPath column — best-effort and
|
||||||
// from the listing parse. For now, use the eventId as a best-effort path
|
// expected to fail at the bookmaker, but better than throwing here.
|
||||||
// fragment — the site also responds to /su/betting/<eventId> in some contexts.
|
var pathFragment = string.IsNullOrWhiteSpace(eventInfo.EventPath)
|
||||||
//
|
? eventInfo.Id.Value
|
||||||
// TODO (Phase 4): pass the full detail path stored in the Event entity rather
|
: eventInfo.EventPath;
|
||||||
// than relying on eventId alone.
|
var path = $"{EventPathBase}{pathFragment}";
|
||||||
var path = $"{EventPathBase}{id.Value}";
|
|
||||||
|
if (string.IsNullOrWhiteSpace(eventInfo.EventPath))
|
||||||
|
{
|
||||||
|
_logger.LogWarning(
|
||||||
|
"ScrapeEventOddsAsync: eventId={EventId} has no EventPath; using numeric ID fallback for URL — expect a 404",
|
||||||
|
eventInfo.Id.Value);
|
||||||
|
}
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
"Scraping odds snapshot for eventId={EventId} source={Source} from {Path}",
|
"Scraping odds snapshot for eventId={EventId} source={Source} from {Path}",
|
||||||
id.Value, source, path);
|
eventInfo.Id.Value, source, path);
|
||||||
|
|
||||||
var html = await FetchHtmlAsync(path, ct).ConfigureAwait(false);
|
var html = await FetchHtmlAsync(path, ct).ConfigureAwait(false);
|
||||||
var snapshot = await _oddsParser.ParseAsync(html, source, ct).ConfigureAwait(false);
|
var snapshot = await _oddsParser.ParseAsync(html, source, ct).ConfigureAwait(false);
|
||||||
|
|
||||||
if (snapshot is null)
|
if (snapshot is null)
|
||||||
throw new InvalidOperationException(
|
throw new InvalidOperationException(
|
||||||
$"No odds found for eventId={id.Value}. " +
|
$"No odds found for eventId={eventInfo.Id.Value}. " +
|
||||||
"The event may be unavailable or the page structure has changed.");
|
"The event may be unavailable or the page structure has changed.");
|
||||||
|
|
||||||
return snapshot;
|
return snapshot;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
/// <remarks>
|
public async Task<EventResult?> ScrapeEventResultAsync(
|
||||||
/// <b>Interim no-op.</b> marathonbet.by has no public results archive endpoint
|
Event eventInfo,
|
||||||
/// (<c>/su/results</c> → 404). This method returns an empty list.
|
|
||||||
/// Results harvesting is implemented in Phase 8 via the watch-list poller
|
|
||||||
/// (<c>ResultsWatchListPoller</c>), which polls individual event-detail pages
|
|
||||||
/// until <c>matchIsComplete=true</c>.
|
|
||||||
/// </remarks>
|
|
||||||
public Task<IReadOnlyList<EventResult>> ScrapeResultsAsync(
|
|
||||||
DateRange range,
|
|
||||||
CancellationToken ct)
|
CancellationToken ct)
|
||||||
{
|
{
|
||||||
_logger.LogWarning(
|
ArgumentNullException.ThrowIfNull(eventInfo);
|
||||||
"ScrapeResultsAsync called but marathonbet.by has no public results archive. " +
|
|
||||||
"Returning empty list. Phase 8 implements results harvesting via event-detail polling.");
|
|
||||||
|
|
||||||
IReadOnlyList<EventResult> empty = Array.Empty<EventResult>();
|
var pathFragment = string.IsNullOrWhiteSpace(eventInfo.EventPath)
|
||||||
return Task.FromResult(empty);
|
? eventInfo.Id.Value
|
||||||
|
: eventInfo.EventPath;
|
||||||
|
var path = $"{EventPathBase}{pathFragment}";
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Scraping result for eventId={EventId} from {Path}",
|
||||||
|
eventInfo.Id.Value, path);
|
||||||
|
|
||||||
|
var html = await FetchHtmlAsync(path, ct).ConfigureAwait(false);
|
||||||
|
return await _resultsParser.ParseAsync(html, ct).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Private helpers ───────────────────────────────────────────────────
|
// ── Private helpers ───────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -114,7 +114,10 @@ public abstract class EventListingParserBase
|
|||||||
Category: category,
|
Category: category,
|
||||||
ScheduledAt: scheduledAt,
|
ScheduledAt: scheduledAt,
|
||||||
Side1Name: side1,
|
Side1Name: side1,
|
||||||
Side2Name: side2);
|
Side2Name: side2)
|
||||||
|
{
|
||||||
|
EventPath = eventPath,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SportCode? ExtractSportCode(IElement row)
|
private static SportCode? ExtractSportCode(IElement row)
|
||||||
|
|||||||
@@ -37,6 +37,14 @@ internal sealed class UpcomingEventsPoller : BackgroundService
|
|||||||
{
|
{
|
||||||
_logger.LogInformation("UpcomingEventsPoller: started");
|
_logger.LogInformation("UpcomingEventsPoller: started");
|
||||||
|
|
||||||
|
// Immediate kick-off cycle on startup so the events table is populated
|
||||||
|
// before we sit on the cron-wait. Without this, a freshly launched app
|
||||||
|
// would have an empty DB until the next cron tick (up to 6 h with the
|
||||||
|
// default `0 0 */6 * * *`), which makes both the PreMatch and Live
|
||||||
|
// pages — and the LiveOddsPoller, which iterates over DB events —
|
||||||
|
// appear empty until the first scheduled fire.
|
||||||
|
bool firstRun = true;
|
||||||
|
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
var options = _opts.CurrentValue;
|
var options = _opts.CurrentValue;
|
||||||
@@ -45,9 +53,12 @@ internal sealed class UpcomingEventsPoller : BackgroundService
|
|||||||
{
|
{
|
||||||
_logger.LogDebug("UpcomingEventsPoller: disabled — sleeping 60s before re-check");
|
_logger.LogDebug("UpcomingEventsPoller: disabled — sleeping 60s before re-check");
|
||||||
await Task.Delay(TimeSpan.FromSeconds(60), stoppingToken);
|
await Task.Delay(TimeSpan.FromSeconds(60), stoppingToken);
|
||||||
|
firstRun = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!firstRun)
|
||||||
|
{
|
||||||
var delay = ComputeDelayToNextFire(options.UpcomingScheduleCron);
|
var delay = ComputeDelayToNextFire(options.UpcomingScheduleCron);
|
||||||
if (delay > TimeSpan.Zero)
|
if (delay > TimeSpan.Zero)
|
||||||
{
|
{
|
||||||
@@ -63,6 +74,13 @@ internal sealed class UpcomingEventsPoller : BackgroundService
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogInformation("UpcomingEventsPoller: running initial kick-off cycle on startup");
|
||||||
|
}
|
||||||
|
|
||||||
|
firstRun = false;
|
||||||
|
|
||||||
if (stoppingToken.IsCancellationRequested)
|
if (stoppingToken.IsCancellationRequested)
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -21,22 +21,22 @@ public sealed class PullLiveOddsUseCaseTests
|
|||||||
NullLogger<PullLiveOddsUseCase>.Instance);
|
NullLogger<PullLiveOddsUseCase>.Instance);
|
||||||
|
|
||||||
[Fact]
|
[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 ev1 = TestFixtures.MakeEvent("11111111");
|
||||||
var ev2 = TestFixtures.MakeEvent("22222222");
|
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.ScrapeEventOddsAsync(
|
||||||
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
|
Arg.Is<Event>(e => e.Id == ev1.Id), OddsSource.Live, Arg.Any<CancellationToken>())
|
||||||
.Returns(Array.Empty<Event>());
|
|
||||||
|
|
||||||
_scraper.ScrapeEventOddsAsync(ev1.Id, OddsSource.Live, Arg.Any<CancellationToken>())
|
|
||||||
.Returns(TestFixtures.MakeSnapshot(ev1.Id, OddsSource.Live));
|
.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));
|
.Returns(TestFixtures.MakeSnapshot(ev2.Id, OddsSource.Live));
|
||||||
|
|
||||||
var sut = CreateSut();
|
var sut = CreateSut();
|
||||||
@@ -47,46 +47,65 @@ public sealed class PullLiveOddsUseCaseTests
|
|||||||
// Assert
|
// Assert
|
||||||
snapshotsCaptured.Should().Be(2);
|
snapshotsCaptured.Should().Be(2);
|
||||||
|
|
||||||
await _scraper.Received(1).ScrapeEventOddsAsync(ev1.Id, OddsSource.Live, Arg.Any<CancellationToken>());
|
await _eventRepo.DidNotReceive().AddAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>());
|
||||||
await _scraper.Received(1).ScrapeEventOddsAsync(ev2.Id, OddsSource.Live, Arg.Any<CancellationToken>());
|
|
||||||
await _snapshotRepo.Received(2).AddAsync(Arg.Any<OddsSnapshot>(), 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]
|
[Fact]
|
||||||
public async Task Should_ContinueAfterSnapshotFailure_And_NotPropagateException()
|
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 ev1 = TestFixtures.MakeEvent("11111111");
|
||||||
var ev2 = TestFixtures.MakeEvent("22222222");
|
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.ScrapeEventOddsAsync(
|
||||||
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
|
Arg.Is<Event>(e => e.Id == ev1.Id), OddsSource.Live, Arg.Any<CancellationToken>())
|
||||||
.Returns(Array.Empty<Event>());
|
|
||||||
|
|
||||||
_scraper.ScrapeEventOddsAsync(ev1.Id, OddsSource.Live, Arg.Any<CancellationToken>())
|
|
||||||
.ThrowsAsync(new HttpRequestException("timeout"));
|
.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));
|
.Returns(TestFixtures.MakeSnapshot(ev2.Id, OddsSource.Live));
|
||||||
|
|
||||||
var sut = CreateSut();
|
var sut = CreateSut();
|
||||||
|
|
||||||
// Act — must not throw
|
// Act — must not throw
|
||||||
var act = async () => await sut.ExecuteAsync(CancellationToken.None);
|
var act = async () => await sut.ExecuteAsync(CancellationToken.None);
|
||||||
|
|
||||||
// Assert
|
|
||||||
await act.Should().NotThrowAsync();
|
await act.Should().NotThrowAsync();
|
||||||
|
|
||||||
|
// Re-execute to assert the count (mocks are still primed)
|
||||||
var result = await sut.ExecuteAsync(CancellationToken.None);
|
var result = await sut.ExecuteAsync(CancellationToken.None);
|
||||||
result.Should().Be(1, "only ev2 succeeded; ev1 failed silently");
|
result.Should().Be(1, "only ev2 succeeded; ev1 failed silently");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Should_ReturnZero_When_NoEventsInDatabase()
|
public async Task Should_ReturnZero_When_LiveListingIsEmpty()
|
||||||
{
|
{
|
||||||
_eventRepo.ListAsync(Arg.Any<CancellationToken>())
|
_scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
||||||
.Returns(Array.Empty<Event>());
|
|
||||||
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
|
|
||||||
.Returns(Array.Empty<Event>());
|
.Returns(Array.Empty<Event>());
|
||||||
|
|
||||||
var sut = CreateSut();
|
var sut = CreateSut();
|
||||||
@@ -95,6 +114,50 @@ public sealed class PullLiveOddsUseCaseTests
|
|||||||
|
|
||||||
result.Should().Be(0);
|
result.Should().Be(0);
|
||||||
await _scraper.DidNotReceive()
|
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.Storage;
|
||||||
using Marathon.Application.UseCases;
|
using Marathon.Application.UseCases;
|
||||||
using Marathon.Domain.Entities;
|
using Marathon.Domain.Entities;
|
||||||
|
using Marathon.Domain.Enums;
|
||||||
using Marathon.Domain.ValueObjects;
|
using Marathon.Domain.ValueObjects;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
|
using NSubstitute.ExceptionExtensions;
|
||||||
|
|
||||||
namespace Marathon.Application.Tests.UseCases;
|
namespace Marathon.Application.Tests.UseCases;
|
||||||
|
|
||||||
@@ -23,13 +25,15 @@ public sealed class PullResultsUseCaseTests
|
|||||||
new(_scraper, _eventRepo, _resultRepo,
|
new(_scraper, _eventRepo, _resultRepo,
|
||||||
NullLogger<PullResultsUseCase>.Instance);
|
NullLogger<PullResultsUseCase>.Instance);
|
||||||
|
|
||||||
|
// ── Selection mode ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Should_InspectOnlySelectedEvents_When_SelectionIsProvided()
|
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 ev1 = TestFixtures.MakeEvent("11111111");
|
||||||
var ev2 = TestFixtures.MakeEvent("22222222");
|
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(ev1.Id, Arg.Any<CancellationToken>()).Returns(ev1);
|
||||||
_eventRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>()).Returns(ev2);
|
_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>())
|
_resultRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>())
|
||||||
.Returns((EventResult?)null);
|
.Returns((EventResult?)null);
|
||||||
|
_scraper.ScrapeEventResultAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>())
|
||||||
// Scraper returns no results (Phase 3 no-op)
|
.Returns((EventResult?)null);
|
||||||
_scraper.ScrapeResultsAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
|
||||||
.Returns(Array.Empty<EventResult>());
|
|
||||||
|
|
||||||
var selection = new List<EventId> { ev1.Id, ev2.Id };
|
var selection = new List<EventId> { ev1.Id, ev2.Id };
|
||||||
var sut = CreateSut();
|
var sut = CreateSut();
|
||||||
@@ -48,19 +50,20 @@ public sealed class PullResultsUseCaseTests
|
|||||||
// Act
|
// Act
|
||||||
var (inspected, loaded, skipped) = await sut.ExecuteAsync(AnyRange, selection, CancellationToken.None);
|
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);
|
inspected.Should().Be(2);
|
||||||
loaded.Should().Be(0, "scraper returns no results in Phase 3");
|
loaded.Should().Be(0);
|
||||||
skipped.Should().Be(0);
|
skipped.Should().Be(0);
|
||||||
|
|
||||||
// ev3 was never resolved
|
|
||||||
await _eventRepo.DidNotReceive().GetAsync(ev3.Id, Arg.Any<CancellationToken>());
|
await _eventRepo.DidNotReceive().GetAsync(ev3.Id, Arg.Any<CancellationToken>());
|
||||||
|
await _eventRepo.DidNotReceive().ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Bulk mode ───────────────────────────────────────────────────────────
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Should_InspectAllEventsInRange_When_SelectionIsNull()
|
public async Task Should_InspectAllEventsInRange_When_SelectionIsNull()
|
||||||
{
|
{
|
||||||
// Arrange: 3 events returned by date-range query
|
|
||||||
var ev1 = TestFixtures.MakeEvent("11111111");
|
var ev1 = TestFixtures.MakeEvent("11111111");
|
||||||
var ev2 = TestFixtures.MakeEvent("22222222");
|
var ev2 = TestFixtures.MakeEvent("22222222");
|
||||||
var ev3 = TestFixtures.MakeEvent("33333333");
|
var ev3 = TestFixtures.MakeEvent("33333333");
|
||||||
@@ -68,84 +71,188 @@ public sealed class PullResultsUseCaseTests
|
|||||||
|
|
||||||
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||||
.Returns(allEvents);
|
.Returns(allEvents);
|
||||||
|
|
||||||
_resultRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>())
|
_resultRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>())
|
||||||
.Returns((EventResult?)null);
|
.Returns((EventResult?)null);
|
||||||
|
_scraper.ScrapeEventResultAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>())
|
||||||
_scraper.ScrapeResultsAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
.Returns((EventResult?)null);
|
||||||
.Returns(Array.Empty<EventResult>());
|
|
||||||
|
|
||||||
var sut = CreateSut();
|
var sut = CreateSut();
|
||||||
|
|
||||||
// Act
|
|
||||||
var (inspected, loaded, skipped) = await sut.ExecuteAsync(AnyRange, selection: null, CancellationToken.None);
|
var (inspected, loaded, skipped) = await sut.ExecuteAsync(AnyRange, selection: null, CancellationToken.None);
|
||||||
|
|
||||||
// Assert
|
|
||||||
inspected.Should().Be(3);
|
inspected.Should().Be(3);
|
||||||
loaded.Should().Be(0);
|
loaded.Should().Be(0, "scraper says none of them are complete yet");
|
||||||
skipped.Should().Be(0);
|
skipped.Should().Be(0);
|
||||||
|
|
||||||
await _eventRepo.Received(1).ListByDateRangeAsync(AnyRange, Arg.Any<CancellationToken>());
|
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]
|
[Fact]
|
||||||
public async Task Should_SkipEventsWithExistingResult_And_BeIdempotent()
|
public async Task Should_SkipEventsWithExistingResult_And_BeIdempotent()
|
||||||
{
|
{
|
||||||
// Arrange: 2 events — ev1 already has a result stored
|
|
||||||
var ev1 = TestFixtures.MakeEvent("11111111");
|
var ev1 = TestFixtures.MakeEvent("11111111");
|
||||||
var ev2 = TestFixtures.MakeEvent("22222222");
|
var ev2 = TestFixtures.MakeEvent("22222222");
|
||||||
var allEvents = new List<Event> { ev1, ev2 }.AsReadOnly();
|
|
||||||
|
|
||||||
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||||
.Returns(allEvents);
|
.Returns(new List<Event> { ev1, ev2 }.AsReadOnly());
|
||||||
|
|
||||||
_resultRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>())
|
_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>())
|
_resultRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>())
|
||||||
.Returns((EventResult?)null);
|
.Returns((EventResult?)null);
|
||||||
|
|
||||||
_scraper.ScrapeResultsAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
_scraper.ScrapeEventResultAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>())
|
||||||
.Returns(Array.Empty<EventResult>());
|
.Returns((EventResult?)null);
|
||||||
|
|
||||||
var sut = CreateSut();
|
var sut = CreateSut();
|
||||||
|
|
||||||
// Act — run twice to verify idempotency
|
|
||||||
var (_, _, skipped1) = await sut.ExecuteAsync(AnyRange, null, CancellationToken.None);
|
var (_, _, skipped1) = await sut.ExecuteAsync(AnyRange, null, CancellationToken.None);
|
||||||
var (_, _, skipped2) = 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");
|
skipped1.Should().Be(1, "ev1 already has a result");
|
||||||
skipped2.Should().Be(1, "idempotent: ev1 still skipped on second run");
|
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 _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]
|
[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 ev1 = TestFixtures.MakeEvent("11111111");
|
||||||
var result1 = TestFixtures.MakeResult(ev1.Id);
|
var result1 = TestFixtures.MakeResult(ev1.Id);
|
||||||
var allEvents = new List<Event> { ev1 }.AsReadOnly();
|
|
||||||
|
|
||||||
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
||||||
.Returns(allEvents);
|
.Returns(new List<Event> { ev1 }.AsReadOnly());
|
||||||
_resultRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>())
|
_resultRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>())
|
||||||
.Returns((EventResult?)null);
|
.Returns((EventResult?)null);
|
||||||
_scraper.ScrapeResultsAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
|
_scraper.ScrapeEventResultAsync(
|
||||||
.Returns(new List<EventResult> { result1 }.AsReadOnly());
|
Arg.Is<Event>(e => e.Id == ev1.Id),
|
||||||
|
Arg.Any<CancellationToken>())
|
||||||
|
.Returns(result1);
|
||||||
|
|
||||||
var sut = CreateSut();
|
var sut = CreateSut();
|
||||||
|
|
||||||
// Act
|
|
||||||
var (inspected, loaded, skipped) = await sut.ExecuteAsync(AnyRange, null, CancellationToken.None);
|
var (inspected, loaded, skipped) = await sut.ExecuteAsync(AnyRange, null, CancellationToken.None);
|
||||||
|
|
||||||
// Assert
|
|
||||||
inspected.Should().Be(1);
|
inspected.Should().Be(1);
|
||||||
loaded.Should().Be(1);
|
loaded.Should().Be(1);
|
||||||
skipped.Should().Be(0);
|
skipped.Should().Be(0);
|
||||||
|
|
||||||
await _resultRepo.Received(1).AddAsync(result1, Arg.Any<CancellationToken>());
|
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);
|
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>()).Returns(events);
|
||||||
_eventRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>()).Returns((Event?)null);
|
_eventRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>()).Returns((Event?)null);
|
||||||
_scraper.ScrapeEventOddsAsync(Arg.Any<EventId>(), OddsSource.PreMatch, Arg.Any<CancellationToken>())
|
_scraper.ScrapeEventOddsAsync(Arg.Any<Event>(), OddsSource.PreMatch, Arg.Any<CancellationToken>())
|
||||||
.Returns(ci => TestFixtures.MakeSnapshot(ci.Arg<EventId>()));
|
.Returns(ci => TestFixtures.MakeSnapshot(ci.Arg<Event>().Id));
|
||||||
|
|
||||||
var sut = CreateSut();
|
var sut = CreateSut();
|
||||||
|
|
||||||
@@ -63,8 +63,8 @@ public sealed class PullUpcomingEventsUseCaseTests
|
|||||||
_eventRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>()).Returns((Event?)null);
|
_eventRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>()).Returns((Event?)null);
|
||||||
_eventRepo.GetAsync(ev3.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>())
|
_scraper.ScrapeEventOddsAsync(Arg.Any<Event>(), OddsSource.PreMatch, Arg.Any<CancellationToken>())
|
||||||
.Returns(ci => TestFixtures.MakeSnapshot(ci.Arg<EventId>()));
|
.Returns(ci => TestFixtures.MakeSnapshot(ci.Arg<Event>().Id));
|
||||||
|
|
||||||
var sut = CreateSut();
|
var sut = CreateSut();
|
||||||
|
|
||||||
@@ -91,9 +91,11 @@ public sealed class PullUpcomingEventsUseCaseTests
|
|||||||
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>()).Returns(events);
|
_scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>()).Returns(events);
|
||||||
_eventRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>()).Returns((Event?)null);
|
_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"));
|
.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));
|
.Returns(TestFixtures.MakeSnapshot(ev2.Id));
|
||||||
|
|
||||||
var sut = CreateSut();
|
var sut = CreateSut();
|
||||||
|
|||||||
@@ -121,8 +121,18 @@ public sealed class EventTests
|
|||||||
public void Event_IsImmutable_NoSettablePublicProperties()
|
public void Event_IsImmutable_NoSettablePublicProperties()
|
||||||
{
|
{
|
||||||
var eventType = typeof(Event);
|
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()
|
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();
|
.ToList();
|
||||||
|
|
||||||
settableProperties.Should().BeEmpty("Event must be immutable.");
|
settableProperties.Should().BeEmpty("Event must be immutable.");
|
||||||
|
|||||||
@@ -78,12 +78,13 @@ public sealed class LiveOddsPollerTests
|
|||||||
var eventRepo = Substitute.For<IEventRepository>();
|
var eventRepo = Substitute.For<IEventRepository>();
|
||||||
var snapshotRepo = Substitute.For<ISnapshotRepository>();
|
var snapshotRepo = Substitute.For<ISnapshotRepository>();
|
||||||
|
|
||||||
// ScrapeUpcomingAsync called by use case internally
|
// Use case discovers live events via ScrapeLiveAsync (NOT ListAsync)
|
||||||
scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
|
scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
||||||
.Returns(Array.Empty<Event>());
|
|
||||||
eventRepo.ListAsync(Arg.Any<CancellationToken>())
|
|
||||||
.Returns(new List<Event> { ev }.AsReadOnly());
|
.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));
|
.Returns(MakeSnapshot(eventId));
|
||||||
|
|
||||||
var sp = BuildServiceProvider(scraper, eventRepo, snapshotRepo);
|
var sp = BuildServiceProvider(scraper, eventRepo, snapshotRepo);
|
||||||
@@ -122,7 +123,7 @@ public sealed class LiveOddsPollerTests
|
|||||||
|
|
||||||
// Assert — no snapshot attempts while disabled
|
// Assert — no snapshot attempts while disabled
|
||||||
await snapshotRepo.DidNotReceive().AddAsync(Arg.Any<OddsSnapshot>(), Arg.Any<CancellationToken>());
|
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]
|
[Fact]
|
||||||
@@ -133,10 +134,11 @@ public sealed class LiveOddsPollerTests
|
|||||||
var eventRepo = Substitute.For<IEventRepository>();
|
var eventRepo = Substitute.For<IEventRepository>();
|
||||||
var snapshotRepo = Substitute.For<ISnapshotRepository>();
|
var snapshotRepo = Substitute.For<ISnapshotRepository>();
|
||||||
|
|
||||||
scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
|
// Use case calls ScrapeLiveAsync first; if it throws, the cycle returns 0
|
||||||
.Returns(Array.Empty<Event>());
|
// without hitting the repo. To exercise the "poller survives failures"
|
||||||
eventRepo.ListAsync(Arg.Any<CancellationToken>())
|
// path, make ScrapeLiveAsync itself throw.
|
||||||
.ThrowsAsync(new InvalidOperationException("DB unavailable"));
|
scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
||||||
|
.ThrowsAsync(new InvalidOperationException("listing unavailable"));
|
||||||
|
|
||||||
var sp = BuildServiceProvider(scraper, eventRepo, snapshotRepo);
|
var sp = BuildServiceProvider(scraper, eventRepo, snapshotRepo);
|
||||||
var opts = BuildOptions(enabled: true, intervalSeconds: 0);
|
var opts = BuildOptions(enabled: true, intervalSeconds: 0);
|
||||||
@@ -153,7 +155,9 @@ public sealed class LiveOddsPollerTests
|
|||||||
// Assert — StopAsync must not propagate the exception
|
// Assert — StopAsync must not propagate the exception
|
||||||
await stopAct.Should().NotThrowAsync();
|
await stopAct.Should().NotThrowAsync();
|
||||||
|
|
||||||
// DB was hit multiple times (poller didn't give up after first failure)
|
// Scraper was hit at least once (poller cycled at least one iteration).
|
||||||
await eventRepo.Received().ListAsync(Arg.Any<CancellationToken>());
|
// 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