Files
RichardandClaude Opus 4.8 ca16cd8a91 Phase 7: Testprojekt (xUnit) + Repository-Tests
- Neues Projekt tests/PolyTrader.Tests (xUnit, EF Core InMemory-Provider),
  zur Solution hinzugefügt.
- InMemoryContextFactory<TContext>: Test-IDbContextFactory (isolierte In-Memory-DB
  je Test, geteilt über alle Kontexte einer Factory – wie im echten Betrieb).
- 18 Tests über die EF-Repos:
  - TrackedTrader: Upsert insert/update ohne Duplikat, Delete,
    AssignedAccountIds-JSON-Round-Trip (Value-Converter) + Set-Ersetzung.
  - CopyTradingAccountSettings: Upsert/Get (Dezimalwerte), Get-null, Delete.
  - CopyTradeLog: Insert/Find, Exists-Dedup (Account+Token), Predikat-Filter.
  - MasterTraderHistory: Exists-Zeitfenster, GetByTraderSince-Cutoff.
  - Core Accounts: Upsert insert/update, Delete.
- Alle 18 grün.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 11:24:33 +02:00

30 lines
1.1 KiB
C#

using System;
using Microsoft.EntityFrameworkCore;
namespace PolyTrader.Tests.TestSupport
{
/// <summary>
/// Test-Implementierung von <see cref="IDbContextFactory{TContext}"/> auf Basis des
/// EF-Core-InMemory-Providers. Jede Factory-Instanz nutzt eine eigene (per GUID benannte)
/// In-Memory-DB, sodass Tests voneinander isoliert sind; alle vom selben Factory erzeugten
/// Kontexte teilen sich die gleiche Datenbank (wie im echten Betrieb der DbContextFactory).
/// </summary>
public sealed class InMemoryContextFactory<TContext> : IDbContextFactory<TContext>
where TContext : DbContext
{
private readonly Func<DbContextOptions<TContext>, TContext> _ctor;
private readonly DbContextOptions<TContext> _options;
public InMemoryContextFactory(Func<DbContextOptions<TContext>, TContext> ctor)
{
_ctor = ctor;
_options = new DbContextOptionsBuilder<TContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.EnableSensitiveDataLogging()
.Options;
}
public TContext CreateDbContext() => _ctor(_options);
}
}