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>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using FluentAssertions;
|
||||
using Marathon.Domain.Backtesting;
|
||||
|
||||
namespace Marathon.Domain.Tests.Backtesting;
|
||||
|
||||
public sealed class SavedStrategyTests
|
||||
{
|
||||
private static BacktestStrategy AnyStrategy() => BacktestStrategy.Default;
|
||||
|
||||
[Fact]
|
||||
public void Create_AssignsIdentity_AndRecentTimestamp()
|
||||
{
|
||||
var s = SavedStrategy.Create("My preset", AnyStrategy());
|
||||
|
||||
s.Id.Should().NotBe(Guid.Empty);
|
||||
s.Name.Should().Be("My preset");
|
||||
s.Strategy.Should().Be(AnyStrategy());
|
||||
// Offset-agnostic instant comparison — robust to however MoscowTime is sourced.
|
||||
s.CreatedAt.Should().BeCloseTo(DateTimeOffset.UtcNow, TimeSpan.FromMinutes(1));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(" Spaced ", "Spaced")]
|
||||
[InlineData("\tTabbed\n", "Tabbed")]
|
||||
public void Constructor_TrimsName(string input, string expected)
|
||||
{
|
||||
SavedStrategy.Create(input, AnyStrategy()).Name.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Constructor_Throws_When_NameBlank(string blank)
|
||||
{
|
||||
var act = () => SavedStrategy.Create(blank, AnyStrategy());
|
||||
act.Should().Throw<ArgumentException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_Throws_When_NameExceedsMax()
|
||||
{
|
||||
var tooLong = new string('x', SavedStrategy.MaxNameLength + 1);
|
||||
var act = () => SavedStrategy.Create(tooLong, AnyStrategy());
|
||||
act.Should().Throw<ArgumentException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_Accepts_NameAtMaxLength()
|
||||
{
|
||||
var maxName = new string('x', SavedStrategy.MaxNameLength);
|
||||
SavedStrategy.Create(maxName, AnyStrategy()).Name.Should().HaveLength(SavedStrategy.MaxNameLength);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void With_SwapsStrategy_KeepingIdentityAndName()
|
||||
{
|
||||
var s = SavedStrategy.Create("Preset", AnyStrategy());
|
||||
var tweaked = new BacktestStrategy(1000m, 0.6m, StakeRule.Flat, 50m, 0.02m, 0.25m);
|
||||
|
||||
var updated = s with { Strategy = tweaked };
|
||||
|
||||
updated.Id.Should().Be(s.Id);
|
||||
updated.Name.Should().Be("Preset");
|
||||
updated.CreatedAt.Should().Be(s.CreatedAt);
|
||||
updated.Strategy.MinScore.Should().Be(0.6m);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
using FluentAssertions;
|
||||
using Marathon.Domain.Backtesting;
|
||||
using Marathon.Infrastructure.Persistence.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Marathon.Infrastructure.Tests.Persistence;
|
||||
|
||||
/// <summary>
|
||||
/// Round-trip + query tests for <see cref="SavedStrategyRepository"/>. Uses the
|
||||
/// in-memory SQLite fixture so the table + unique name index declared in the
|
||||
/// configuration are exercised on every test.
|
||||
/// </summary>
|
||||
public sealed class SavedStrategyRoundTripTests : IDisposable
|
||||
{
|
||||
private readonly InMemoryDbFixture _fixture;
|
||||
private readonly SavedStrategyRepository _repo;
|
||||
|
||||
public SavedStrategyRoundTripTests()
|
||||
{
|
||||
_fixture = new InMemoryDbFixture();
|
||||
_repo = new SavedStrategyRepository(_fixture.DbContext);
|
||||
}
|
||||
|
||||
public void Dispose() => _fixture.Dispose();
|
||||
|
||||
private static BacktestStrategy Strategy(
|
||||
decimal minScore = 0.45m, StakeRule rule = StakeRule.Kelly) =>
|
||||
new(
|
||||
StartingBankroll: 2000m,
|
||||
MinScore: minScore,
|
||||
StakeRule: rule,
|
||||
FlatStake: 75m,
|
||||
PercentOfBankroll: 0.03m,
|
||||
KellyFraction: 0.5m);
|
||||
|
||||
[Fact]
|
||||
public async Task RoundTrip_PreservesAllFields()
|
||||
{
|
||||
var saved = SavedStrategy.Create("Quarter Kelly", Strategy());
|
||||
|
||||
await _repo.AddAsync(saved);
|
||||
await _repo.SaveChangesAsync();
|
||||
_fixture.DbContext.ChangeTracker.Clear();
|
||||
|
||||
var got = await _repo.GetAsync(saved.Id);
|
||||
|
||||
got.Should().NotBeNull();
|
||||
got!.Id.Should().Be(saved.Id);
|
||||
got.Name.Should().Be("Quarter Kelly");
|
||||
got.Strategy.StartingBankroll.Should().Be(2000m);
|
||||
got.Strategy.MinScore.Should().Be(0.45m);
|
||||
got.Strategy.StakeRule.Should().Be(StakeRule.Kelly);
|
||||
got.Strategy.FlatStake.Should().Be(75m);
|
||||
got.Strategy.PercentOfBankroll.Should().Be(0.03m);
|
||||
got.Strategy.KellyFraction.Should().Be(0.5m);
|
||||
got.CreatedAt.Should().Be(saved.CreatedAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByNameAsync_MatchesTrimmed_AndReturnsNullWhenMissing()
|
||||
{
|
||||
var saved = SavedStrategy.Create("Aggro", Strategy());
|
||||
await _repo.AddAsync(saved);
|
||||
await _repo.SaveChangesAsync();
|
||||
_fixture.DbContext.ChangeTracker.Clear();
|
||||
|
||||
(await _repo.GetByNameAsync(" Aggro "))!.Id.Should().Be(saved.Id);
|
||||
(await _repo.GetByNameAsync("nope")).Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Name_IsCaseInsensitive_ForLookupAndUniqueness()
|
||||
{
|
||||
await _repo.AddAsync(SavedStrategy.Create("Kelly", Strategy()));
|
||||
await _repo.SaveChangesAsync();
|
||||
_fixture.DbContext.ChangeTracker.Clear();
|
||||
|
||||
// Lookup folds case (NOCASE column collation).
|
||||
(await _repo.GetByNameAsync("kelly")).Should().NotBeNull();
|
||||
|
||||
// And a case-variant is rejected as a duplicate by the unique index.
|
||||
await _repo.AddAsync(SavedStrategy.Create("KELLY", Strategy()));
|
||||
var act = async () => await _repo.SaveChangesAsync();
|
||||
await act.Should().ThrowAsync<DbUpdateException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ListAsync_OrdersByName()
|
||||
{
|
||||
await _repo.AddAsync(SavedStrategy.Create("Zeta", Strategy()));
|
||||
await _repo.AddAsync(SavedStrategy.Create("Alpha", Strategy()));
|
||||
await _repo.AddAsync(SavedStrategy.Create("Mu", Strategy()));
|
||||
await _repo.SaveChangesAsync();
|
||||
_fixture.DbContext.ChangeTracker.Clear();
|
||||
|
||||
var list = await _repo.ListAsync();
|
||||
|
||||
list.Select(s => s.Name).Should().ContainInOrder("Alpha", "Mu", "Zeta");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_PersistsStrategyChange()
|
||||
{
|
||||
var saved = SavedStrategy.Create("Tweak me", Strategy(minScore: 0.45m, rule: StakeRule.Kelly));
|
||||
await _repo.AddAsync(saved);
|
||||
await _repo.SaveChangesAsync();
|
||||
_fixture.DbContext.ChangeTracker.Clear();
|
||||
|
||||
var updated = saved with { Strategy = Strategy(minScore: 0.7m, rule: StakeRule.Flat) };
|
||||
await _repo.UpdateAsync(updated);
|
||||
await _repo.SaveChangesAsync();
|
||||
_fixture.DbContext.ChangeTracker.Clear();
|
||||
|
||||
var got = await _repo.GetAsync(saved.Id);
|
||||
got!.Strategy.MinScore.Should().Be(0.7m);
|
||||
got.Strategy.StakeRule.Should().Be(StakeRule.Flat);
|
||||
got.Name.Should().Be("Tweak me");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteAsync_Removes()
|
||||
{
|
||||
var saved = SavedStrategy.Create("Trash", Strategy());
|
||||
await _repo.AddAsync(saved);
|
||||
await _repo.SaveChangesAsync();
|
||||
_fixture.DbContext.ChangeTracker.Clear();
|
||||
|
||||
await _repo.DeleteAsync(saved.Id);
|
||||
await _repo.SaveChangesAsync();
|
||||
|
||||
(await _repo.GetAsync(saved.Id)).Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UniqueNameIndex_RejectsDuplicateName()
|
||||
{
|
||||
await _repo.AddAsync(SavedStrategy.Create("dupe", Strategy()));
|
||||
await _repo.SaveChangesAsync();
|
||||
_fixture.DbContext.ChangeTracker.Clear();
|
||||
|
||||
await _repo.AddAsync(SavedStrategy.Create("dupe", Strategy()));
|
||||
var act = async () => await _repo.SaveChangesAsync();
|
||||
|
||||
await act.Should().ThrowAsync<DbUpdateException>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using FluentAssertions;
|
||||
using Marathon.Application.Abstractions;
|
||||
using Marathon.Application.UseCases;
|
||||
using Marathon.Domain.Backtesting;
|
||||
using Marathon.UI.Services;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NSubstitute;
|
||||
|
||||
namespace Marathon.UI.Tests.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Covers the saved-strategy surface of <see cref="BacktestService"/> — chiefly the
|
||||
/// fraction↔percent conversion (domain stores fractions, the form/VM speak percent)
|
||||
/// and the form-validation guard. Run-simulation behaviour is covered elsewhere.
|
||||
/// </summary>
|
||||
public sealed class BacktestServiceStrategyTests
|
||||
{
|
||||
private readonly ISavedStrategyRepository _strategies = Substitute.For<ISavedStrategyRepository>();
|
||||
private readonly IAnomalyRepository _anomalies = Substitute.For<IAnomalyRepository>();
|
||||
private readonly IEventRepository _events = Substitute.For<IEventRepository>();
|
||||
private readonly IResultRepository _results = Substitute.For<IResultRepository>();
|
||||
|
||||
private BacktestService CreateSut()
|
||||
{
|
||||
var run = new RunBacktestUseCase(_anomalies, _events, _results, NullLogger<RunBacktestUseCase>.Instance);
|
||||
var save = new SaveStrategyUseCase(_strategies, NullLogger<SaveStrategyUseCase>.Instance);
|
||||
var delete = new DeleteStrategyUseCase(_strategies, NullLogger<DeleteStrategyUseCase>.Instance);
|
||||
return new BacktestService(run, save, delete, _strategies);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ListStrategiesAsync_ConvertsStoredFractionsToPercent()
|
||||
{
|
||||
var preset = new SavedStrategy(
|
||||
Guid.NewGuid(),
|
||||
"Quarter Kelly",
|
||||
new BacktestStrategy(1000m, 0.45m, StakeRule.Kelly, 50m, PercentOfBankroll: 0.03m, KellyFraction: 0.25m),
|
||||
DateTimeOffset.UtcNow);
|
||||
_strategies.ListAsync(Arg.Any<CancellationToken>()).Returns(new[] { preset });
|
||||
|
||||
var vms = await CreateSut().ListStrategiesAsync(CancellationToken.None);
|
||||
|
||||
vms.Should().ContainSingle();
|
||||
vms[0].Name.Should().Be("Quarter Kelly");
|
||||
vms[0].StakeRule.Should().Be(StakeRule.Kelly);
|
||||
vms[0].PercentOfBankrollPercent.Should().Be(3m); // 0.03 fraction → 3%
|
||||
vms[0].KellyFractionPercent.Should().Be(25m); // 0.25 fraction → 25%
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveStrategyAsync_PersistsFormPercents_AsFractions()
|
||||
{
|
||||
_strategies.GetByNameAsync(Arg.Any<string>(), Arg.Any<CancellationToken>()).Returns((SavedStrategy?)null);
|
||||
var form = new BacktestForm
|
||||
{
|
||||
StartingBankroll = 1000m,
|
||||
MinScore = 0.5m,
|
||||
StakeRule = StakeRule.PercentOfBankroll,
|
||||
FlatStake = 50m,
|
||||
PercentOfBankrollPercent = 4m,
|
||||
KellyFractionPercent = 25m,
|
||||
};
|
||||
|
||||
var vm = await CreateSut().SaveStrategyAsync("PoB", form, CancellationToken.None);
|
||||
|
||||
vm.PercentOfBankrollPercent.Should().Be(4m);
|
||||
await _strategies.Received(1).AddAsync(
|
||||
Arg.Is<SavedStrategy>(s => s.Name == "PoB" && s.Strategy.PercentOfBankroll == 0.04m),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveStrategyAsync_Throws_And_DoesNotPersist_When_FormInvalid()
|
||||
{
|
||||
var badForm = new BacktestForm { StartingBankroll = 0m }; // fails IsValid
|
||||
|
||||
var act = async () => await CreateSut().SaveStrategyAsync("X", badForm, CancellationToken.None);
|
||||
|
||||
await act.Should().ThrowAsync<ArgumentException>();
|
||||
await _strategies.DidNotReceive().AddAsync(Arg.Any<SavedStrategy>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteStrategyAsync_DelegatesToRepository()
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
|
||||
await CreateSut().DeleteStrategyAsync(id, CancellationToken.None);
|
||||
|
||||
await _strategies.Received(1).DeleteAsync(id, Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user