Phase 7: Tests erweitert + Mongo-Parser extrahiert (testbar)
Tests (jetzt 48 grün): - DbContextMappingTests: sichert core_/mod_-Tabellennamen, Position-Composite-Key, TrackedTrader-Key, ClosedTradeRow nicht gemappt, AccountState.OpenPositions ignoriert. - PositionRepositoryTests: Live/Demo-Trennung, Upsert setzt Account+Demo-Flag, Update ohne Duplikat, DeleteLive, DropDemo. - MarketRepositoryTests: GetActive filtert Closed, Upsert-Update, FindByTokenId. - TradeLogRepositoryTests: GetRecent (Sortierung+Limit), Find-Predikat, Guid-Id unique. - MongoExportParserTests: String-Dezimale, null->"", fehlende Felder->Default, AssignedAccountIds-Array, Category-Default, Nicht-Array->leer. Refactor: - Mongo-Export-Parsing aus ConfigMigrator in testbare Klasse PolyTrader.Modules.CopyTrading.ConfigImport.MongoExportParser ausgelagert (ParseAccounts -> Account+Settings, ParseTraders). ConfigMigrator nutzt sie; --migrate-json end-to-end verifiziert (3/3/32). - PolyTrader.App.csproj: tests/** vom Default-Glob ausgeschlossen (App zog sonst die Testdateien mit rein -> doppelte AssemblyInfo/Xunit-Fehler). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
ca16cd8a91
commit
1b51872f00
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PolyTrader.Core.Persistence.Ef;
|
||||
using PolyTrader.Modules.CopyTrading.Persistence.Ef;
|
||||
using PolyTrader.Tests.TestSupport;
|
||||
using PolyTraderSharp.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace PolyTrader.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Sichert die projektweiten Mapping-Invarianten ab: Core-Tabellen mit Präfix core_,
|
||||
/// Modul-Tabellen mit mod_<modul>_, korrekte Schlüssel, UI-Klasse nicht gemappt.
|
||||
/// </summary>
|
||||
public class DbContextMappingTests
|
||||
{
|
||||
private static CoreDbContext CoreCtx() =>
|
||||
new InMemoryContextFactory<CoreDbContext>(o => new CoreDbContext(o)).CreateDbContext();
|
||||
|
||||
private static CopyTradingDbContext CtCtx() =>
|
||||
new InMemoryContextFactory<CopyTradingDbContext>(o => new CopyTradingDbContext(o)).CreateDbContext();
|
||||
|
||||
[Theory]
|
||||
[InlineData(typeof(AccountState), "core_accounts")]
|
||||
[InlineData(typeof(Position), "core_positions")]
|
||||
[InlineData(typeof(MarketData), "core_markets")]
|
||||
[InlineData(typeof(TradeRecord), "core_trade_log")]
|
||||
public void Core_entities_map_to_core_prefixed_tables(Type clrType, string expectedTable)
|
||||
{
|
||||
using var ctx = CoreCtx();
|
||||
var entity = ctx.Model.FindEntityType(clrType);
|
||||
Assert.NotNull(entity);
|
||||
Assert.Equal(expectedTable, entity!.GetTableName());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(typeof(ClosedTrade), "mod_copytrading_closed_trades")]
|
||||
[InlineData(typeof(TrackedTrader), "mod_copytrading_traders")]
|
||||
[InlineData(typeof(CopyTradingAccountSettings), "mod_copytrading_account_settings")]
|
||||
[InlineData(typeof(MasterTraderHistoryRecord), "mod_copytrading_mt_history")]
|
||||
public void Module_entities_map_to_mod_copytrading_prefixed_tables(Type clrType, string expectedTable)
|
||||
{
|
||||
using var ctx = CtCtx();
|
||||
var entity = ctx.Model.FindEntityType(clrType);
|
||||
Assert.NotNull(entity);
|
||||
Assert.Equal(expectedTable, entity!.GetTableName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Position_has_composite_key_account_isdemo_token()
|
||||
{
|
||||
using var ctx = CoreCtx();
|
||||
var key = ctx.Model.FindEntityType(typeof(Position))!.FindPrimaryKey();
|
||||
Assert.NotNull(key);
|
||||
Assert.Equal(
|
||||
new[] { nameof(Position.AccountId), nameof(Position.IsDemo), nameof(Position.TokenId) },
|
||||
key!.Properties.Select(p => p.Name).ToArray());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TrackedTrader_key_is_id()
|
||||
{
|
||||
using var ctx = CtCtx();
|
||||
var key = ctx.Model.FindEntityType(typeof(TrackedTrader))!.FindPrimaryKey();
|
||||
Assert.Equal(nameof(TrackedTrader.Id), Assert.Single(key!.Properties).Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClosedTradeRow_ui_class_is_not_mapped()
|
||||
{
|
||||
using var ctx = CtCtx();
|
||||
Assert.Null(ctx.Model.FindEntityType(typeof(ClosedTradeRow)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AccountState_open_positions_is_ignored()
|
||||
{
|
||||
using var ctx = CoreCtx();
|
||||
var entity = ctx.Model.FindEntityType(typeof(AccountState))!;
|
||||
Assert.Null(entity.FindProperty(nameof(AccountState.OpenPositions)));
|
||||
Assert.Null(entity.FindNavigation(nameof(AccountState.OpenPositions)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Linq;
|
||||
using PolyTrader.Core.Persistence.Ef;
|
||||
using PolyTrader.Tests.TestSupport;
|
||||
using PolyTraderSharp.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace PolyTrader.Tests
|
||||
{
|
||||
public class MarketRepositoryTests
|
||||
{
|
||||
private static EfMarketRepository NewRepo() =>
|
||||
new(new InMemoryContextFactory<CoreDbContext>(o => new CoreDbContext(o)));
|
||||
|
||||
[Fact]
|
||||
public void GetActive_returns_only_non_closed_markets()
|
||||
{
|
||||
var repo = NewRepo();
|
||||
repo.Upsert(new MarketData { Id = "m1", Closed = false });
|
||||
repo.Upsert(new MarketData { Id = "m2", Closed = true });
|
||||
|
||||
var active = repo.GetActive();
|
||||
Assert.Single(active);
|
||||
Assert.Equal("m1", active[0].Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Upsert_updates_existing_market()
|
||||
{
|
||||
var repo = NewRepo();
|
||||
repo.Upsert(new MarketData { Id = "m1", Question = "old" });
|
||||
repo.Upsert(new MarketData { Id = "m1", Question = "new" });
|
||||
|
||||
Assert.Equal("new", repo.GetById("m1")!.Question);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FindByTokenId_matches_within_clob_token_ids()
|
||||
{
|
||||
var repo = NewRepo();
|
||||
repo.Upsert(new MarketData { Id = "m1", ClobTokenIds = "[\"abc\",\"def\"]" });
|
||||
|
||||
Assert.Equal("m1", repo.FindByTokenId("def")!.Id);
|
||||
Assert.Null(repo.FindByTokenId("zzz"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using PolyTrader.Modules.CopyTrading.ConfigImport;
|
||||
using Xunit;
|
||||
|
||||
namespace PolyTrader.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Sichert das robuste Parsen der mongoexport-JSON ab: Dezimalwerte als Strings,
|
||||
/// null-Felder, fehlende Felder → Default, Account-Arrays.
|
||||
/// </summary>
|
||||
public class MongoExportParserTests
|
||||
{
|
||||
[Fact]
|
||||
public void ParseAccounts_reads_core_fields_and_string_decimals()
|
||||
{
|
||||
const string json = """
|
||||
[{
|
||||
"_id": 1,
|
||||
"Name": "Richard Test",
|
||||
"WalletAddress": "0x628",
|
||||
"IsDemo": false,
|
||||
"IsActive": true,
|
||||
"TotalBalance": "64.763626",
|
||||
"AvailableBalance": "1.155226"
|
||||
}]
|
||||
""";
|
||||
|
||||
var imp = Assert.Single(MongoExportParser.ParseAccounts(json));
|
||||
Assert.Equal(1, imp.Account.AccountId);
|
||||
Assert.Equal("Richard Test", imp.Account.Name);
|
||||
Assert.Equal("0x628", imp.Account.WalletAddress);
|
||||
Assert.False(imp.Account.IsDemo);
|
||||
Assert.True(imp.Account.IsActive);
|
||||
Assert.Equal(64.763626m, imp.Account.TotalBalance);
|
||||
Assert.Equal(1.155226m, imp.Account.AvailableBalance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAccounts_maps_old_limit_fields_into_settings()
|
||||
{
|
||||
const string json = """
|
||||
[{ "_id": 3, "PerMarketLimit": "10", "PerMasterLimit": "20", "MaxBuyPrice": "0.85", "PreRedeemLimit": "99.5" }]
|
||||
""";
|
||||
|
||||
var imp = Assert.Single(MongoExportParser.ParseAccounts(json));
|
||||
Assert.Equal(3, imp.Settings.AccountId);
|
||||
Assert.Equal(10m, imp.Settings.PerMarketLimit);
|
||||
Assert.Equal(20m, imp.Settings.PerMasterLimit);
|
||||
Assert.Equal(0.85m, imp.Settings.MaxBuyPrice);
|
||||
Assert.Equal(99.5m, imp.Settings.PreRedeemLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAccounts_null_string_falls_back_to_empty()
|
||||
{
|
||||
const string json = """[{ "_id": 3, "PayoutAddress": null }]""";
|
||||
|
||||
var imp = Assert.Single(MongoExportParser.ParseAccounts(json));
|
||||
Assert.Equal(string.Empty, imp.Account.PayoutAddress);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAccounts_missing_settings_fields_use_defaults()
|
||||
{
|
||||
const string json = """[{ "_id": 1 }]""";
|
||||
|
||||
var imp = Assert.Single(MongoExportParser.ParseAccounts(json));
|
||||
Assert.Equal(5.0m, imp.Settings.PerMarketLimit);
|
||||
Assert.Equal(2.0m, imp.Settings.MaxPriceDifference);
|
||||
Assert.Equal(0.98m, imp.Settings.MaxBuyPrice);
|
||||
Assert.Equal(40.0m, imp.Settings.perMaxTimeNone);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseTraders_reads_fields_and_assigned_accounts()
|
||||
{
|
||||
const string json = """
|
||||
[{
|
||||
"_id": 2,
|
||||
"WalletAddress": "0xa2711",
|
||||
"DisplayName": "0xa2711",
|
||||
"Category": "TRADING_BOT",
|
||||
"IsActive": false,
|
||||
"TotalTrades": 32,
|
||||
"TotalPnl": 7386.655743958501,
|
||||
"AssignedAccountIds": [1, 3],
|
||||
"Description": null
|
||||
}]
|
||||
""";
|
||||
|
||||
var t = Assert.Single(MongoExportParser.ParseTraders(json));
|
||||
Assert.Equal(2, t.Id);
|
||||
Assert.Equal("TRADING_BOT", t.Category);
|
||||
Assert.False(t.IsActive);
|
||||
Assert.Equal(32, t.TotalTrades);
|
||||
Assert.Equal(7386.655743958501, t.TotalPnl, 6);
|
||||
Assert.Equal(new HashSet<int> { 1, 3 }, t.AssignedAccountIds);
|
||||
Assert.Equal(string.Empty, t.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseTraders_missing_category_uses_default()
|
||||
{
|
||||
const string json = """[{ "_id": 5, "DisplayName": "x" }]""";
|
||||
|
||||
var t = Assert.Single(MongoExportParser.ParseTraders(json));
|
||||
Assert.Equal("NEW_BIG_BET", t.Category);
|
||||
Assert.Empty(t.AssignedAccountIds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_returns_empty_for_non_array_or_empty()
|
||||
{
|
||||
Assert.Empty(MongoExportParser.ParseAccounts("{}"));
|
||||
Assert.Empty(MongoExportParser.ParseAccounts("[]"));
|
||||
Assert.Empty(MongoExportParser.ParseTraders("[]"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using System.Linq;
|
||||
using PolyTrader.Core.Persistence.Ef;
|
||||
using PolyTrader.Tests.TestSupport;
|
||||
using PolyTraderSharp.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace PolyTrader.Tests
|
||||
{
|
||||
public class PositionRepositoryTests
|
||||
{
|
||||
private static EfPositionRepository NewRepo() =>
|
||||
new(new InMemoryContextFactory<CoreDbContext>(o => new CoreDbContext(o)));
|
||||
|
||||
[Fact]
|
||||
public void Live_and_demo_positions_are_kept_separate_per_account()
|
||||
{
|
||||
var repo = NewRepo();
|
||||
repo.UpsertLive(1, new Position { TokenId = "t1" });
|
||||
repo.UpsertDemo(1, new Position { TokenId = "t1" });
|
||||
repo.UpsertLive(2, new Position { TokenId = "t1" });
|
||||
|
||||
Assert.Single(repo.GetLive(1));
|
||||
Assert.Single(repo.GetDemo(1));
|
||||
Assert.Single(repo.GetLive(2));
|
||||
Assert.Empty(repo.GetDemo(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Upsert_sets_account_and_demo_flag_on_position()
|
||||
{
|
||||
var repo = NewRepo();
|
||||
repo.UpsertDemo(5, new Position { TokenId = "t1" });
|
||||
|
||||
var pos = repo.FindDemo(5, "t1");
|
||||
Assert.NotNull(pos);
|
||||
Assert.Equal(5, pos!.AccountId);
|
||||
Assert.True(pos.IsDemo);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Upsert_updates_existing_position_without_duplicating()
|
||||
{
|
||||
var repo = NewRepo();
|
||||
repo.UpsertLive(1, new Position { TokenId = "t1", Size = 10m });
|
||||
repo.UpsertLive(1, new Position { TokenId = "t1", Size = 20m });
|
||||
|
||||
var live = repo.GetLive(1);
|
||||
Assert.Single(live);
|
||||
Assert.Equal(20m, live[0].Size);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteLive_removes_only_live()
|
||||
{
|
||||
var repo = NewRepo();
|
||||
repo.UpsertLive(1, new Position { TokenId = "t1" });
|
||||
repo.UpsertDemo(1, new Position { TokenId = "t1" });
|
||||
|
||||
repo.DeleteLive(1, "t1");
|
||||
|
||||
Assert.Empty(repo.GetLive(1));
|
||||
Assert.Single(repo.GetDemo(1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DropDemo_clears_only_demo_positions_of_account()
|
||||
{
|
||||
var repo = NewRepo();
|
||||
repo.UpsertDemo(1, new Position { TokenId = "t1" });
|
||||
repo.UpsertDemo(1, new Position { TokenId = "t2" });
|
||||
repo.UpsertLive(1, new Position { TokenId = "t3" });
|
||||
|
||||
repo.DropDemo(1);
|
||||
|
||||
Assert.Empty(repo.GetDemo(1));
|
||||
Assert.Single(repo.GetLive(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using PolyTrader.Core.Persistence.Ef;
|
||||
using PolyTrader.Tests.TestSupport;
|
||||
using PolyTraderSharp.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace PolyTrader.Tests
|
||||
{
|
||||
public class TradeLogRepositoryTests
|
||||
{
|
||||
private static EfTradeLogRepository NewRepo() =>
|
||||
new(new InMemoryContextFactory<CoreDbContext>(o => new CoreDbContext(o)));
|
||||
|
||||
[Fact]
|
||||
public void GetRecent_orders_by_closed_at_descending_and_limits()
|
||||
{
|
||||
var repo = NewRepo();
|
||||
var t = new DateTime(2026, 7, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
repo.Insert(new TradeRecord { Id = "a", ClosedAt = t.AddHours(1) });
|
||||
repo.Insert(new TradeRecord { Id = "b", ClosedAt = t.AddHours(3) });
|
||||
repo.Insert(new TradeRecord { Id = "c", ClosedAt = t.AddHours(2) });
|
||||
|
||||
var recent = repo.GetRecent(2);
|
||||
|
||||
Assert.Equal(2, recent.Count);
|
||||
Assert.Equal("b", recent[0].Id);
|
||||
Assert.Equal("c", recent[1].Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Find_filters_by_module_name()
|
||||
{
|
||||
var repo = NewRepo();
|
||||
repo.Insert(new TradeRecord { Id = "a", ModuleName = "CopyTrading" });
|
||||
repo.Insert(new TradeRecord { Id = "b", ModuleName = "Other" });
|
||||
|
||||
var ct = repo.Find(x => x.ModuleName == "CopyTrading");
|
||||
Assert.Single(ct);
|
||||
Assert.Equal("a", ct[0].Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Generated_id_is_unique_per_record()
|
||||
{
|
||||
var a = new TradeRecord();
|
||||
var b = new TradeRecord();
|
||||
Assert.False(string.IsNullOrEmpty(a.Id));
|
||||
Assert.NotEqual(a.Id, b.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user