|
13 | 13 | * limitations under the License. |
14 | 14 | */ |
15 | 15 |
|
16 | | -using System.Linq; |
17 | | -using Python.Runtime; |
18 | 16 | using NUnit.Framework; |
| 17 | +using Python.Runtime; |
| 18 | +using QuantConnect.Python; |
| 19 | +using QuantConnect.Securities; |
19 | 20 | using QuantConnect.Util; |
| 21 | +using System; |
20 | 22 | using System.Collections.Generic; |
| 23 | +using System.Linq; |
21 | 24 |
|
22 | 25 | namespace QuantConnect.Tests.Common.Util |
23 | 26 | { |
@@ -88,7 +91,7 @@ public void ConvertToSymbolsTest() |
88 | 91 | Assert.AreEqual(expected.FirstOrDefault(), test1.FirstOrDefault()); |
89 | 92 |
|
90 | 93 | // Test Python List of Strings |
91 | | - var list = (new List<string> {"AIG", "BAC", "IBM", "GOOG"}).ToPyList(); |
| 94 | + var list = (new List<string> { "AIG", "BAC", "IBM", "GOOG" }).ToPyList(); |
92 | 95 | var test2 = PythonUtil.ConvertToSymbols(list); |
93 | 96 | Assert.IsTrue(typeof(List<Symbol>) == test2.GetType()); |
94 | 97 | Assert.IsTrue(test2.SequenceEqual(expected)); |
@@ -231,5 +234,152 @@ public void ParsesPythonExceptionStackTrace(string expected, string original, in |
231 | 234 | PythonUtil.ExceptionLineShift = originalShiftValue; |
232 | 235 | Assert.AreEqual(expected, result); |
233 | 236 | } |
| 237 | + |
| 238 | + [Test] |
| 239 | + public void PurePythonClassWithRequiredMethodsWorks() |
| 240 | + { |
| 241 | + using (Py.GIL()) |
| 242 | + { |
| 243 | + // Pure Python class that implements the required methods |
| 244 | + string pythonCode = @" |
| 245 | +from AlgorithmImports import * |
| 246 | +
|
| 247 | +class PurePythonBuyingPowerModel: |
| 248 | + def GetMaximumOrderQuantityForTargetBuyingPower(self, parameters): |
| 249 | + return GetMaximumOrderQuantityResult(100) |
| 250 | + |
| 251 | + def GetMaximumOrderQuantityForDeltaBuyingPower(self, parameters): |
| 252 | + return GetMaximumOrderQuantityResult(200) |
| 253 | + |
| 254 | + def HasSufficientBuyingPowerForOrder(self, parameters): |
| 255 | + return HasSufficientBuyingPowerForOrderResult(True) |
| 256 | + |
| 257 | + def GetReservedBuyingPowerForPosition(self, parameters): |
| 258 | + return ReservedBuyingPowerForPosition(0) |
| 259 | + |
| 260 | + def GetLeverage(self, security): |
| 261 | + return 1.0 |
| 262 | + |
| 263 | + def GetBuyingPower(self, parameters): |
| 264 | + return BuyingPower(1000) |
| 265 | + |
| 266 | + def SetLeverage(self, security, leverage): |
| 267 | + pass |
| 268 | + |
| 269 | + def GetMaintenanceMargin(self, parameters): |
| 270 | + return None |
| 271 | + |
| 272 | + def GetInitialMarginRequirement(self, parameters): |
| 273 | + return None |
| 274 | + |
| 275 | + def GetInitialMarginRequiredForOrder(self, parameters): |
| 276 | + return None |
| 277 | +"; |
| 278 | + var module = PyModule.FromString("TestModels", pythonCode); |
| 279 | + var pyObject = module.GetAttr("PurePythonBuyingPowerModel").Invoke(); |
| 280 | + |
| 281 | + var result = PythonUtil.CreateModelOrWrapper<IBuyingPowerModel, BuyingPowerModelPythonWrapper>(pyObject); |
| 282 | + |
| 283 | + Assert.IsNotNull(result); |
| 284 | + Assert.IsInstanceOf<BuyingPowerModelPythonWrapper>(result); |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + [Test] |
| 289 | + public void BothInheritedAndNonInheritedClassesWork() |
| 290 | + { |
| 291 | + using (Py.GIL()) |
| 292 | + { |
| 293 | + string pythonCode = @" |
| 294 | +from AlgorithmImports import * |
| 295 | +
|
| 296 | +class PurePythonBuyingPowerModel: |
| 297 | + def GetMaximumOrderQuantityForTargetBuyingPower(self, parameters): |
| 298 | + return GetMaximumOrderQuantityResult(100) |
| 299 | + |
| 300 | + def GetMaximumOrderQuantityForDeltaBuyingPower(self, parameters): |
| 301 | + return GetMaximumOrderQuantityResult(200) |
| 302 | + |
| 303 | + def HasSufficientBuyingPowerForOrder(self, parameters): |
| 304 | + return HasSufficientBuyingPowerForOrderResult(True) |
| 305 | + |
| 306 | + def GetReservedBuyingPowerForPosition(self, parameters): |
| 307 | + return ReservedBuyingPowerForPosition(0) |
| 308 | + |
| 309 | + def GetLeverage(self, security): |
| 310 | + return 1.0 |
| 311 | + |
| 312 | + def GetBuyingPower(self, parameters): |
| 313 | + return BuyingPower(1000) |
| 314 | + |
| 315 | + def SetLeverage(self, security, leverage): |
| 316 | + pass |
| 317 | + |
| 318 | + def GetMaintenanceMargin(self, parameters): |
| 319 | + return None |
| 320 | + |
| 321 | + def GetInitialMarginRequirement(self, parameters): |
| 322 | + return None |
| 323 | + |
| 324 | + def GetInitialMarginRequiredForOrder(self, parameters): |
| 325 | + return None |
| 326 | +
|
| 327 | +class InheritedBuyingPowerModel(SecurityMarginModel): |
| 328 | + def GetMaximumOrderQuantityForTargetBuyingPower(self, parameters): |
| 329 | + return GetMaximumOrderQuantityResult(200) |
| 330 | +"; |
| 331 | + var module = PyModule.FromString("TestModels", pythonCode); |
| 332 | + var purePython = module.GetAttr("PurePythonBuyingPowerModel").Invoke(); |
| 333 | + var inherited = module.GetAttr("InheritedBuyingPowerModel").Invoke(); |
| 334 | + |
| 335 | + var result1 = PythonUtil.CreateModelOrWrapper<IBuyingPowerModel, BuyingPowerModelPythonWrapper>(purePython); |
| 336 | + var result2 = PythonUtil.CreateModelOrWrapper<IBuyingPowerModel, BuyingPowerModelPythonWrapper>(inherited); |
| 337 | + |
| 338 | + Assert.IsNotNull(result1); |
| 339 | + Assert.IsNotNull(result2); |
| 340 | + Assert.IsInstanceOf<BuyingPowerModelPythonWrapper>(result1); |
| 341 | + Assert.IsInstanceOf<BuyingPowerModelPythonWrapper>(result2); |
| 342 | + } |
| 343 | + } |
| 344 | + |
| 345 | + [Test] |
| 346 | + public void MissingRequiredMethodThrowsException() |
| 347 | + { |
| 348 | + using (Py.GIL()) |
| 349 | + { |
| 350 | + string pythonCode = @" |
| 351 | +class IncompleteBuyingPowerModel: |
| 352 | + def GetMaximumOrderQuantityForTargetBuyingPower(self, parameters): |
| 353 | + return GetMaximumOrderQuantityResult(100) |
| 354 | + |
| 355 | + def HasSufficientBuyingPowerForOrder(self, parameters): |
| 356 | + return HasSufficientBuyingPowerForOrderResult(True) |
| 357 | +"; |
| 358 | + var module = PyModule.FromString("TestModels", pythonCode); |
| 359 | + var purePython = module.GetAttr("IncompleteBuyingPowerModel").Invoke(); |
| 360 | + |
| 361 | + Assert.Throws<NotImplementedException>(() => |
| 362 | + PythonUtil.CreateModelOrWrapper<IBuyingPowerModel, BuyingPowerModelPythonWrapper>(purePython)); |
| 363 | + } |
| 364 | + } |
| 365 | + |
| 366 | + [Test] |
| 367 | + public void PureCSharpClassReturnsDirectInstanceWithoutWrapper() |
| 368 | + { |
| 369 | + using (Py.GIL()) |
| 370 | + { |
| 371 | + // Create pure C# instance |
| 372 | + var csharpObject = new BuyingPowerModel(); |
| 373 | + var pyObject = csharpObject.ToPython(); |
| 374 | + |
| 375 | + // Should return the same C# instance, not a wrapper |
| 376 | + var result = PythonUtil.CreateModelOrWrapper<IBuyingPowerModel, BuyingPowerModelPythonWrapper>(pyObject); |
| 377 | + |
| 378 | + Assert.IsNotNull(result); |
| 379 | + Assert.AreSame(csharpObject, result); |
| 380 | + Assert.IsNotInstanceOf<BuyingPowerModelPythonWrapper>(result); |
| 381 | + Assert.IsInstanceOf<BuyingPowerModel>(result); |
| 382 | + } |
| 383 | + } |
234 | 384 | } |
235 | 385 | } |
0 commit comments