-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored tests with base test class. Added Python wrapper tests
Base test class and python wrapper tests
- Loading branch information
Showing
5 changed files
with
186 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
Tests/Algorithm/Framework/Portfolio/PortfolioOptimizerPythonWrapperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by aaplicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using NUnit.Framework; | ||
using Python.Runtime; | ||
using QuantConnect.Algorithm.Framework.Portfolio; | ||
using System; | ||
|
||
namespace QuantConnect.Tests.Algorithm.Framework.Portfolio; | ||
|
||
[TestFixture] | ||
public class PortfolioOptimizerPythonWrapperTests | ||
{ | ||
[Test] | ||
public void OptimizeIsCalled() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
var module = PyModule.FromString(Guid.NewGuid().ToString(), | ||
@$" | ||
from AlgorithmImports import * | ||
class CustomPortfolioOptimizer: | ||
def __init__(self): | ||
self.OptimizeWasCalled = False | ||
def Optimize(self, historicalReturns, expectedReturns = None, covariance = None): | ||
self.OptimizeWasCalled= True"); | ||
|
||
var pyCustomOptimizer = module.GetAttr("CustomPortfolioOptimizer").Invoke(); | ||
var wrapper = new PortfolioOptimizerPythonWrapper(pyCustomOptimizer); | ||
var historicalReturns = new double[,] { { -0.50, -0.13 }, { 0.81, 0.31 }, { -0.02, 0.01 } }; | ||
|
||
wrapper.Optimize(historicalReturns); | ||
pyCustomOptimizer | ||
.GetAttr("OptimizeWasCalled") | ||
.TryConvert(out bool optimizerWasCalled); | ||
|
||
Assert.IsTrue(optimizerWasCalled); | ||
} | ||
} | ||
|
||
[Test] | ||
public void WrapperThrowsIfOptimizerDoesNotImplementInterface() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
var module = PyModule.FromString(Guid.NewGuid().ToString(), | ||
@$" | ||
from AlgorithmImports import * | ||
class CustomPortfolioOptimizer: | ||
def __init__(self): | ||
self.OptimizeWasCalled = False | ||
def Calculate(self, historicalReturns, expectedReturns = None, covariance = None): | ||
pass"); | ||
|
||
var pyCustomOptimizer = module.GetAttr("CustomPortfolioOptimizer").Invoke(); | ||
|
||
Assert.Throws<NotImplementedException>(() => new PortfolioOptimizerPythonWrapper(pyCustomOptimizer)); | ||
} | ||
} | ||
} |
Oops, something went wrong.