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
+5
-121
@@ -1,16 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PolyTrader.Core.Persistence;
|
||||
using PolyTrader.Core.Persistence.Ef;
|
||||
using PolyTrader.Modules.CopyTrading.ConfigImport;
|
||||
using PolyTrader.Modules.CopyTrading.Persistence;
|
||||
using PolyTrader.Modules.CopyTrading.Persistence.Ef;
|
||||
using PolyTraderSharp.Models;
|
||||
|
||||
namespace PolyTraderSharp.Services
|
||||
{
|
||||
@@ -49,45 +46,11 @@ namespace PolyTraderSharp.Services
|
||||
int accCount = 0, setCount = 0;
|
||||
if (File.Exists(accPath))
|
||||
{
|
||||
using var doc = JsonDocument.Parse(File.ReadAllText(accPath));
|
||||
foreach (var el in doc.RootElement.EnumerateArray())
|
||||
foreach (var imp in MongoExportParser.ParseAccounts(File.ReadAllText(accPath)))
|
||||
{
|
||||
var acc = new AccountState
|
||||
{
|
||||
AccountId = el.GetProperty("_id").GetInt32(),
|
||||
Name = Str(el, "Name"),
|
||||
WalletAddress = Str(el, "WalletAddress"),
|
||||
ApiKey = Str(el, "ApiKey"),
|
||||
ApiSecret = Str(el, "ApiSecret"),
|
||||
ApiPassphrase = Str(el, "ApiPassphrase"),
|
||||
PrivateKey = Str(el, "PrivateKey"),
|
||||
IsDemo = Bool(el, "IsDemo"),
|
||||
IsActive = Bool(el, "IsActive", true),
|
||||
CloseOnlyMode = Bool(el, "CloseOnlyMode"),
|
||||
PayoutAddress = Str(el, "PayoutAddress"),
|
||||
PayoutLimitUsd = Dec(el, "PayoutLimitUsd", 0m),
|
||||
TotalBalance = Dec(el, "TotalBalance", 0m),
|
||||
AvailableBalance = Dec(el, "AvailableBalance", 0m),
|
||||
HasOpenLimitOrders = Bool(el, "HasOpenLimitOrders")
|
||||
};
|
||||
dstAccounts.Upsert(acc);
|
||||
dstAccounts.Upsert(imp.Account);
|
||||
dstSettings.Upsert(imp.Settings);
|
||||
accCount++;
|
||||
|
||||
var s = new CopyTradingAccountSettings
|
||||
{
|
||||
AccountId = acc.AccountId,
|
||||
PerMarketLimit = Dec(el, "PerMarketLimit", 5.0m),
|
||||
MaxPriceDifference = Dec(el, "MaxPriceDifference", 2.0m),
|
||||
MaxBuyPrice = Dec(el, "MaxBuyPrice", 0.98m),
|
||||
ProfitTarget = Dec(el, "ProfitTarget", 50.0m),
|
||||
PreRedeemLimit = Dec(el, "PreRedeemLimit", 0.0m),
|
||||
PerMasterLimit = Dec(el, "PerMasterLimit", 10.0m),
|
||||
perMaxTime6h = Dec(el, "perMaxTime6h", 20.0m),
|
||||
perMaxTime24h = Dec(el, "perMaxTime24h", 20.0m),
|
||||
perMaxTime72h = Dec(el, "perMaxTime72h", 20.0m),
|
||||
perMaxTimeNone = Dec(el, "perMaxTimeNone", 40.0m)
|
||||
};
|
||||
dstSettings.Upsert(s);
|
||||
setCount++;
|
||||
}
|
||||
}
|
||||
@@ -102,25 +65,8 @@ namespace PolyTraderSharp.Services
|
||||
int trCount = 0;
|
||||
if (File.Exists(trPath))
|
||||
{
|
||||
using var doc = JsonDocument.Parse(File.ReadAllText(trPath));
|
||||
foreach (var el in doc.RootElement.EnumerateArray())
|
||||
foreach (var t in MongoExportParser.ParseTraders(File.ReadAllText(trPath)))
|
||||
{
|
||||
var t = new TrackedTrader
|
||||
{
|
||||
Id = el.GetProperty("_id").GetInt32(),
|
||||
WalletAddress = Str(el, "WalletAddress"),
|
||||
DisplayName = Str(el, "DisplayName"),
|
||||
Category = Str(el, "Category", "NEW_BIG_BET"),
|
||||
Description = Str(el, "Description"),
|
||||
Reasoning = Str(el, "Reasoning"),
|
||||
IsActive = Bool(el, "IsActive", true),
|
||||
IsHidden = Bool(el, "IsHidden"),
|
||||
TotalTrades = Int(el, "TotalTrades"),
|
||||
WinningTrades = Int(el, "WinningTrades"),
|
||||
Winrate30t = Dbl(el, "Winrate30t"),
|
||||
TotalPnl = Dbl(el, "TotalPnl"),
|
||||
AssignedAccountIds = IntSet(el, "AssignedAccountIds")
|
||||
};
|
||||
dstTraders.Upsert(t);
|
||||
trCount++;
|
||||
}
|
||||
@@ -180,68 +126,6 @@ namespace PolyTraderSharp.Services
|
||||
private static string Short(string s) =>
|
||||
string.IsNullOrEmpty(s) || s.Length <= 12 ? s : $"{s[..6]}…{s[^4..]}";
|
||||
|
||||
// --- JSON-Helfer (mongoexport: Dezimalwerte als Strings, null moeglich) ---
|
||||
private static string Str(JsonElement el, string name, string fallback = "")
|
||||
{
|
||||
if (el.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.String)
|
||||
return v.GetString() ?? fallback;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private static bool Bool(JsonElement el, string name, bool fallback = false)
|
||||
{
|
||||
if (el.TryGetProperty(name, out var v))
|
||||
{
|
||||
if (v.ValueKind == JsonValueKind.True) return true;
|
||||
if (v.ValueKind == JsonValueKind.False) return false;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private static int Int(JsonElement el, string name, int fallback = 0)
|
||||
{
|
||||
if (el.TryGetProperty(name, out var v))
|
||||
{
|
||||
if (v.ValueKind == JsonValueKind.Number && v.TryGetInt32(out var n)) return n;
|
||||
if (v.ValueKind == JsonValueKind.String && int.TryParse(v.GetString(), NumberStyles.Any, CultureInfo.InvariantCulture, out var s)) return s;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private static double Dbl(JsonElement el, string name, double fallback = 0)
|
||||
{
|
||||
if (el.TryGetProperty(name, out var v))
|
||||
{
|
||||
if (v.ValueKind == JsonValueKind.Number && v.TryGetDouble(out var n)) return n;
|
||||
if (v.ValueKind == JsonValueKind.String && double.TryParse(v.GetString(), NumberStyles.Any, CultureInfo.InvariantCulture, out var s)) return s;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private static decimal Dec(JsonElement el, string name, decimal fallback)
|
||||
{
|
||||
if (el.TryGetProperty(name, out var v))
|
||||
{
|
||||
if (v.ValueKind == JsonValueKind.Number && v.TryGetDecimal(out var n)) return n;
|
||||
if (v.ValueKind == JsonValueKind.String && decimal.TryParse(v.GetString(), NumberStyles.Any, CultureInfo.InvariantCulture, out var s)) return s;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private static HashSet<int> IntSet(JsonElement el, string name)
|
||||
{
|
||||
var set = new HashSet<int>();
|
||||
if (el.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.Array)
|
||||
{
|
||||
foreach (var item in v.EnumerateArray())
|
||||
{
|
||||
if (item.ValueKind == JsonValueKind.Number && item.TryGetInt32(out var n)) set.Add(n);
|
||||
else if (item.ValueKind == JsonValueKind.String && int.TryParse(item.GetString(), NumberStyles.Any, CultureInfo.InvariantCulture, out var s)) set.Add(s);
|
||||
}
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
private static string Redact(string conn) =>
|
||||
System.Text.RegularExpressions.Regex.Replace(conn, "(?i)(password=)[^;]*", "$1***");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user