Files
maraphon-app/tests/Marathon.Domain.Tests/Backtesting/SavedStrategyTests.cs
T
alexei.dolgolyov 2a0ea7b3a6 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.
2026-05-29 02:13:16 +03:00

68 lines
2.2 KiB
C#

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);
}
}