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
{
///
/// Sichert die projektweiten Mapping-Invarianten ab: Core-Tabellen mit Präfix core_,
/// Modul-Tabellen mit mod_<modul>_, korrekte Schlüssel, UI-Klasse nicht gemappt.
///
public class DbContextMappingTests
{
private static CoreDbContext CoreCtx() =>
new InMemoryContextFactory(o => new CoreDbContext(o)).CreateDbContext();
private static CopyTradingDbContext CtCtx() =>
new InMemoryContextFactory(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)));
}
}
}