Skip to content

Commit acc9a8a

Browse files
committed
Add unit tests
1 parent 4553a52 commit acc9a8a

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

Tests/Algorithm/Framework/Alphas/ConstantAlphaModelTests.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
33
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
44
*
@@ -19,6 +19,7 @@
1919
using System;
2020
using System.Collections.Generic;
2121
using System.Linq;
22+
using System.Reflection;
2223
using static System.FormattableString;
2324

2425
namespace QuantConnect.Tests.Algorithm.Framework.Alphas
@@ -44,6 +45,47 @@ protected override IAlphaModel CreatePythonAlphaModel()
4445
}
4546
}
4647

48+
[TestCase(Language.CSharp)]
49+
[TestCase(Language.Python)]
50+
public void ConstructorWithWeightOnlySetsWeightCorrectly(Language language)
51+
{
52+
IAlphaModel alpha;
53+
if (language == Language.CSharp)
54+
{
55+
alpha = new ConstantAlphaModel(InsightType.Price, InsightDirection.Up, TimeSpan.FromDays(1), weight: 0.1);
56+
}
57+
else
58+
{
59+
using (Py.GIL())
60+
{
61+
var testModule = PyModule.FromString("test_module",
62+
@"
63+
from AlgorithmImports import *
64+
65+
def test_constructor():
66+
model = ConstantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(1), weight=0.1)
67+
return model
68+
");
69+
70+
alpha = testModule.GetAttr("test_constructor").Invoke().As<ConstantAlphaModel>();
71+
}
72+
}
73+
74+
var magnitude = GetPrivateField(alpha, "_magnitude");
75+
var confidence = GetPrivateField(alpha, "_confidence");
76+
var weight = GetPrivateField(alpha, "_weight");
77+
78+
Assert.IsNull(magnitude);
79+
Assert.IsNull(confidence);
80+
Assert.AreEqual(0.1, weight);
81+
}
82+
83+
private static object GetPrivateField(object obj, string fieldName)
84+
{
85+
var field = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
86+
return field?.GetValue(obj);
87+
}
88+
4789
protected override IEnumerable<Insight> ExpectedInsights()
4890
{
4991
return Enumerable.Range(0, 360).Select(x => new Insight(Symbols.SPY, _period, _type, _direction, _magnitude, _confidence));

0 commit comments

Comments
 (0)