feat(backtest): saved strategy presets (strategy editor v1)
Persist named backtest-strategy presets so a staking config (bankroll, min-score, stake rule, flat/percent/Kelly params) can be saved, listed, loaded back into the form, and deleted. The per-run date range is not part of a preset. - Domain: SavedStrategy record (name trimmed + bounded to 80 chars, Create() factory) wrapping the pure BacktestStrategy. - Persistence: SavedStrategyEntity + config (TEXT decimals, unique case-insensitive NOCASE index on Name), repository, mapping, and a hand-trimmed AddSavedStrategies migration (additive — only the new table). Case-insensitive names mean save-by-name overwrites instead of creating near-duplicates. - Application: SaveStrategyUseCase (upsert by name, keeps Id+CreatedAt) + DeleteStrategyUseCase. - UI: presets panel on the Backtest page (load/save/delete) + service methods; fraction<->percent round-trip; en/ru resx. - Fix: pin Sports.Code as ValueGeneratedNever — it is the bookmaker's natural sport id, not an autoincrement surrogate. Corrects long-standing model-snapshot drift; the snapshot is regenerated to match the DB. - 25 tests across all four layers: domain validation, real-SQLite round-trip incl. case-insensitive lookup/uniqueness, the upsert use case, and the service percent mapping.
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using Marathon.Application.Abstractions;
|
||||
using Marathon.Application.UseCases;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NSubstitute;
|
||||
|
||||
namespace Marathon.Application.Tests.UseCases;
|
||||
|
||||
public sealed class DeleteStrategyUseCaseTests
|
||||
{
|
||||
private readonly ISavedStrategyRepository _repo = Substitute.For<ISavedStrategyRepository>();
|
||||
|
||||
private DeleteStrategyUseCase CreateSut() =>
|
||||
new(_repo, NullLogger<DeleteStrategyUseCase>.Instance);
|
||||
|
||||
[Fact]
|
||||
public async Task Delegates_Delete_Then_SaveChanges()
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
|
||||
await CreateSut().ExecuteAsync(id, CancellationToken.None);
|
||||
|
||||
await _repo.Received(1).DeleteAsync(id, Arg.Any<CancellationToken>());
|
||||
await _repo.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using FluentAssertions;
|
||||
using Marathon.Application.Abstractions;
|
||||
using Marathon.Application.UseCases;
|
||||
using Marathon.Domain.Backtesting;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NSubstitute;
|
||||
|
||||
namespace Marathon.Application.Tests.UseCases;
|
||||
|
||||
public sealed class SaveStrategyUseCaseTests
|
||||
{
|
||||
private readonly ISavedStrategyRepository _repo = Substitute.For<ISavedStrategyRepository>();
|
||||
|
||||
private SaveStrategyUseCase CreateSut() =>
|
||||
new(_repo, NullLogger<SaveStrategyUseCase>.Instance);
|
||||
|
||||
[Fact]
|
||||
public async Task Creates_New_When_NameUnused()
|
||||
{
|
||||
_repo.GetByNameAsync("Fresh", Arg.Any<CancellationToken>()).Returns((SavedStrategy?)null);
|
||||
|
||||
var result = await CreateSut().ExecuteAsync("Fresh", BacktestStrategy.Default, CancellationToken.None);
|
||||
|
||||
result.Name.Should().Be("Fresh");
|
||||
await _repo.Received(1).AddAsync(
|
||||
Arg.Is<SavedStrategy>(s => s.Name == "Fresh"), Arg.Any<CancellationToken>());
|
||||
await _repo.DidNotReceive().UpdateAsync(Arg.Any<SavedStrategy>(), Arg.Any<CancellationToken>());
|
||||
await _repo.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Overwrites_Existing_KeepingIdentityAndCreatedAt()
|
||||
{
|
||||
var existing = SavedStrategy.Create("Reused", BacktestStrategy.Default);
|
||||
_repo.GetByNameAsync("Reused", Arg.Any<CancellationToken>()).Returns(existing);
|
||||
|
||||
var newStrategy = new BacktestStrategy(1000m, 0.7m, StakeRule.Flat, 50m, 0.02m, 0.25m);
|
||||
var result = await CreateSut().ExecuteAsync("Reused", newStrategy, CancellationToken.None);
|
||||
|
||||
result.Id.Should().Be(existing.Id);
|
||||
result.CreatedAt.Should().Be(existing.CreatedAt);
|
||||
result.Strategy.MinScore.Should().Be(0.7m);
|
||||
await _repo.Received(1).UpdateAsync(
|
||||
Arg.Is<SavedStrategy>(s => s.Id == existing.Id && s.Strategy.MinScore == 0.7m),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _repo.DidNotReceive().AddAsync(Arg.Any<SavedStrategy>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpsertLookup_UsesTrimmedName()
|
||||
{
|
||||
_repo.GetByNameAsync("Padded", Arg.Any<CancellationToken>()).Returns((SavedStrategy?)null);
|
||||
|
||||
await CreateSut().ExecuteAsync(" Padded ", BacktestStrategy.Default, CancellationToken.None);
|
||||
|
||||
await _repo.Received(1).GetByNameAsync("Padded", Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public async Task Throws_When_NameBlank(string blank)
|
||||
{
|
||||
var act = async () => await CreateSut().ExecuteAsync(blank, BacktestStrategy.Default, CancellationToken.None);
|
||||
|
||||
await act.Should().ThrowAsync<ArgumentException>();
|
||||
await _repo.DidNotReceive().AddAsync(Arg.Any<SavedStrategy>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user