Files
quantconnect--lean/Tests/Python/PythonDataTests.cs
T
Alexandre Catarino f755b91158
Regression Tests / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Fixes PythonData.EndTime (#6602)
* Fixes PythonData.EndTime

The `EndTime` property of `PythonData` didn't override the base implementation in `BaseData`. Therefore `EndTime` was always set as `Time`.

Fixes Regression Tests
- The C# version of `Bitcoin` class needs to implement `EndTime`-
- Fixes regression tests

* Minor tweaks and rebase

- Add support for python data types just setting EndTime. Adding unit
  tests for PythonData
- Fix for DynamicData EndTime property being fetched. Updating unit
  tests

Co-authored-by: Martin-Molinero <martin@quantconnect.com>
2022-09-07 14:53:05 -03:00

111 lines
3.7 KiB
C#

/*
* 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 applicable 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 System;
using NodaTime;
using Python.Runtime;
using NUnit.Framework;
using QuantConnect.Data;
using QuantConnect.Python;
namespace QuantConnect.Tests.Python
{
[TestFixture]
public class PythonDataTests
{
[Test]
public void TimeAndEndTimeCanBeSet()
{
using (Py.GIL())
{
dynamic testModule = PyModule.FromString("testModule",
@"
from AlgorithmImports import *
class CustomDataTest(PythonData):
def Reader(self, config, line, date, isLiveMode):
result = CustomDataTest()
result.Symbol = config.Symbol
result.Value = 10
result.Time = datetime.strptime(""2022-05-05"", ""%Y-%m-%d"")
result.EndTime = datetime.strptime(""2022-05-15"", ""%Y-%m-%d"")
return result");
var data = GetDataFromModule(testModule);
Assert.AreEqual(new DateTime(2022, 5, 5), data.Time);
Assert.AreEqual(new DateTime(2022, 5, 15), data.EndTime);
}
}
[Test]
public void OnlyEndTimeCanBeSet()
{
using (Py.GIL())
{
dynamic testModule = PyModule.FromString("testModule",
@"
from AlgorithmImports import *
class CustomDataTest(PythonData):
def Reader(self, config, line, date, isLiveMode):
result = CustomDataTest()
result.Symbol = config.Symbol
result.Value = 10
result.EndTime = datetime.strptime(""2022-05-05"", ""%Y-%m-%d"")
return result");
var data = GetDataFromModule(testModule);
Assert.AreEqual(new DateTime(2022, 5, 5), data.Time);
Assert.AreEqual(new DateTime(2022, 5, 5), data.EndTime);
}
}
[Test]
public void OnlyTimeCanBeSet()
{
using (Py.GIL())
{
dynamic testModule = PyModule.FromString("testModule",
@"
from AlgorithmImports import *
class CustomDataTest(PythonData):
def Reader(self, config, line, date, isLiveMode):
result = CustomDataTest()
result.Symbol = config.Symbol
result.Value = 10
result.Time = datetime.strptime(""2022-05-05"", ""%Y-%m-%d"")
return result");
var data = GetDataFromModule(testModule);
Assert.AreEqual(new DateTime(2022, 5, 5), data.Time);
Assert.AreEqual(new DateTime(2022, 5, 5), data.EndTime);
}
}
private static BaseData GetDataFromModule(dynamic testModule)
{
var type = Extensions.CreateType(testModule.GetAttr("CustomDataTest"));
var customDataTest = new PythonData(testModule.GetAttr("CustomDataTest")());
var config = new SubscriptionDataConfig(type, Symbols.SPY, Resolution.Daily, DateTimeZone.Utc,
DateTimeZone.Utc, false, false, false, isCustom: true);
return customDataTest.Reader(config, "something", DateTime.UtcNow, false);
}
}
}