Skip to content

Commit b8231e7

Browse files
committed
Improve unit tests
1 parent e7fb0ef commit b8231e7

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Engine/Setup/BrokerageSetupHandler.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ private bool LoadCashBalance(IBrokerage brokerage, IAlgorithm algorithm)
389389
return true;
390390
}
391391

392-
private bool LoadExistingHoldingsAndOrders(IBrokerage brokerage, IAlgorithm algorithm, SetupHandlerParameters parameters)
392+
protected bool LoadExistingHoldingsAndOrders(IBrokerage brokerage, IAlgorithm algorithm, SetupHandlerParameters parameters)
393393
{
394394
Log.Trace("BrokerageSetupHandler.Setup(): Fetching open orders from brokerage...");
395395
try
@@ -417,9 +417,10 @@ private bool LoadExistingHoldingsAndOrders(IBrokerage brokerage, IAlgorithm algo
417417
Log.Trace("BrokerageSetupHandler.Setup(): Has existing holding: " + holding);
418418

419419
// verify existing holding security type
420-
if (!algorithm.Securities.TryGetValue(holding.Symbol, out var security))
420+
Security security;
421+
if (!algorithm.Securities.TryGetValue(holding.Symbol, out security))
421422
{
422-
if (!AddUnrequestedSecurity(algorithm, holding.Symbol, holding.Type))
423+
if (!AddUnrequestedSecurity(algorithm, holding.Symbol, holding.Type, out security))
423424
{
424425
continue;
425426
}
@@ -461,15 +462,16 @@ private bool LoadExistingHoldingsAndOrders(IBrokerage brokerage, IAlgorithm algo
461462
return true;
462463
}
463464

464-
private bool AddUnrequestedSecurity(IAlgorithm algorithm, Symbol symbol, SecurityType securityType)
465+
private bool AddUnrequestedSecurity(IAlgorithm algorithm, Symbol symbol, SecurityType securityType, out Security security)
465466
{
466-
if (!algorithm.Securities.TryGetValue(symbol, out Security security))
467+
if (!algorithm.Securities.TryGetValue(symbol, out security))
467468
{
468469
if (!_supportedSecurityTypes.Contains((SecurityType)securityType))
469470
{
470471
Log.Error("BrokerageSetupHandler.Setup(): Unsupported security type: " + securityType + "-" + symbol.Value);
471472
AddInitializationError("Found unsupported security type in existing brokerage holdings: " + securityType + ". " +
472473
"QuantConnect currently supports the following security types: " + string.Join(",", _supportedSecurityTypes));
474+
security = null;
473475
return false;
474476
}
475477

@@ -525,9 +527,10 @@ protected void GetOpenOrders(IAlgorithm algorithm, IResultHandler resultHandler,
525527
foreach (var order in openOrders.OrderByDescending(x => x.SecurityType))
526528
{
527529
// verify existing holding security type
528-
if (!algorithm.Securities.TryGetValue(order.Symbol, out var security))
530+
Security security;
531+
if (!algorithm.Securities.TryGetValue(order.Symbol, out security))
529532
{
530-
if (!AddUnrequestedSecurity(algorithm, order.Symbol, order.SecurityType))
533+
if (!AddUnrequestedSecurity(algorithm, order.Symbol, order.SecurityType, out security))
531534
{
532535
// keep aggregating these errors
533536
continue;

Tests/Engine/Setup/BrokerageSetupHandlerTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,12 @@ public void HasErrorWithZeroTotalPortfolioValue(bool hasCashBalance, bool hasHol
631631
new object[] {
632632
new Func<List<Holding>>(() => new List<Holding> { new Holding { Symbol = new Symbol(
633633
SecurityIdentifier.GenerateBase(typeof(CustomDataBitcoinAlgorithm.Bitcoin), "BTC", Market.USA, false), "BTC"), Quantity = 1 }}),
634-
new Func<List<Order>>(() => new List<Order> { new LimitOrder(new Symbol(
635-
SecurityIdentifier.GenerateBase(typeof(CustomDataBitcoinAlgorithm.Bitcoin), "BTC", Market.USA, false), "BTC"), 1, 1, DateTime.UtcNow)}),
636-
true }
634+
new Func<List<Order>>(() => new List<Order>()),
635+
true },
636+
new object[] {
637+
new Func<List<Holding>>(() => new List<Holding> { new Holding { Symbol = Symbols.SPY, Quantity = 1 }}),
638+
new Func<List<Order>>(() => new List<Order>()),
639+
true }
637640
};
638641

639642
private static TestCaseData[] GetExistingHoldingsAndOrdersTestCaseData()

0 commit comments

Comments
 (0)