Files
maraphon-app/tests/Marathon.Application.Tests/UseCases/ExportToExcelUseCaseTests.cs
T
alexei.dolgolyov 2acbaa5b77 feat(phase-4): application layer + background workers — 202/202 tests green
Use cases (Marathon.Application/UseCases/):
- PullUpcomingEventsUseCase: scrape + persist new events + capture pre-match snapshots
- PullLiveOddsUseCase: refresh live snapshots for all stored events
- PullResultsUseCase: Phase 4 scaffold; delegates to ScrapeResultsAsync (Phase 3 no-op);
  Phase 8 will replace with watch-list polling
- ExportToExcelUseCase: resolves export dir from StorageOptions, delegates to IExcelExporter

ApplicationModule.AddMarathonApplication(IServiceCollection) — no IConfiguration needed.

Background workers (Marathon.Infrastructure/Workers/):
- UpcomingEventsPoller: Cronos 6-field cron schedule (default every 6 h)
- LiveOddsPoller: fixed interval (WorkerOptions.LivePollIntervalSeconds, default 30 s)
- ResultsWatchListPoller: scaffold, disabled by default (WorkerOptions.ResultsPollerEnabled=false)
All three: exception-swallowing, cancellation-aware, scoped DI via CreateAsyncScope().

InfrastructureModule.AddMarathonInfrastructure(IServiceCollection, IConfiguration):
- Composes AddMarathonPersistence + AddMarathonScraping + WorkerOptions + 3 hosted services

App.xaml.cs: replace reflection-based TryAddApplicationAndInfrastructure with direct
AddMarathonApplication() + AddMarathonInfrastructure(config) calls.

Resolved Phase 3 TODO: bind Sports:Basketball:QuarterMode from config in ScrapingModule.

appsettings.json: add Workers.LivePollIntervalSeconds, ResultsPollIntervalSeconds,
ResultsPollerEnabled; add Sports.Basketball.QuarterMode.

Settings.razor + WorkerOptions (UI) + SharedResource.*.resx: surface new Workers fields.

Tests: +14 Application use-case tests, +3 Infrastructure worker tests (185 → 202 total).
2026-05-05 12:28:15 +03:00

94 lines
3.2 KiB
C#

using FluentAssertions;
using Marathon.Application.Abstractions;
using Marathon.Application.Storage;
using Marathon.Application.UseCases;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
namespace Marathon.Application.Tests.UseCases;
public sealed class ExportToExcelUseCaseTests
{
private readonly IExcelExporter _exporter = Substitute.For<IExcelExporter>();
private ExportToExcelUseCase CreateSut(string exportDir = "./exports")
{
var opts = Options.Create(new StorageOptions
{
ExportDirectory = exportDir,
DatabasePath = "./data/marathon.db",
});
return new ExportToExcelUseCase(_exporter, opts, NullLogger<ExportToExcelUseCase>.Instance);
}
[Fact]
public async Task Should_InvokeExporterWithCorrectArguments_When_Executed()
{
// Arrange
var range = new DateRange(
new DateTimeOffset(2026, 5, 1, 0, 0, 0, TimeSpan.Zero),
new DateTimeOffset(2026, 5, 7, 23, 59, 59, TimeSpan.Zero));
const ExportKind kind = ExportKind.Combined;
const string expectedOutputPath = "./exports/Marathon_2026-05-01_to_2026-05-07.xlsx";
_exporter
.ExportAsync(range, kind, Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(expectedOutputPath);
var sut = CreateSut();
// Act
var outputPath = await sut.ExecuteAsync(range, kind, CancellationToken.None);
// Assert
outputPath.Should().Be(expectedOutputPath);
await _exporter.Received(1).ExportAsync(
range,
kind,
Arg.Is<string>(dir => dir.Contains("exports")),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Should_ReturnAbsoluteOutputPath_When_ExporterSucceeds()
{
// Arrange
var range = new DateRange(DateTimeOffset.UtcNow.AddDays(-7), DateTimeOffset.UtcNow);
const string absolutePath = @"C:\exports\Marathon_2026-05-01_to_2026-05-07.xlsx";
_exporter
.ExportAsync(Arg.Any<DateRange>(), Arg.Any<ExportKind>(), Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(absolutePath);
var sut = CreateSut();
// Act
var result = await sut.ExecuteAsync(range, ExportKind.PreMatch, CancellationToken.None);
// Assert
result.Should().Be(absolutePath);
}
[Fact]
public async Task Should_PropagateExporterException_When_ExportFails()
{
// Arrange
var range = new DateRange(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow);
_exporter
.ExportAsync(Arg.Any<DateRange>(), Arg.Any<ExportKind>(), Arg.Any<string>(), Arg.Any<CancellationToken>())
.ThrowsAsync(new InvalidOperationException("disk full"));
var sut = CreateSut();
// Act
var act = async () => await sut.ExecuteAsync(range, ExportKind.Live, CancellationToken.None);
// Assert — ExportToExcelUseCase does not swallow exporter exceptions; callers decide how to handle
await act.Should().ThrowAsync<InvalidOperationException>().WithMessage("disk full");
}
}