-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathMyCustomDataTypeTests.cs
More file actions
327 lines (279 loc) · 11.3 KB
/
MyCustomDataTypeTests.cs
File metadata and controls
327 lines (279 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* 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 ProtoBuf;
using System.IO;
using System.Linq;
using ProtoBuf.Meta;
using Newtonsoft.Json;
using NUnit.Framework;
using QuantConnect.Data;
using QuantConnect.DataSource;
namespace QuantConnect.DataLibrary.Tests
{
[TestFixture]
public class MyCustomDataTypeTests
{
/// <summary>
/// Path to the sample data file in the repo output/ directory.
/// Adjust if your sample data uses a different filename.
/// </summary>
private static readonly string _repoRoot = Path.GetFullPath(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", ".."));
private readonly string _sampleDataPath = Path.Combine(
_repoRoot,
"output",
"alternative",
"myvendorname",
"mydatasetname",
"spy.csv"
);
private readonly string _universeDataPath = Path.Combine(
_repoRoot,
"output",
"alternative",
"myvendorname",
"mydatasetname",
"universe",
"20220214.csv"
);
private SubscriptionDataConfig _config;
[SetUp]
public void SetUp()
{
_config = new SubscriptionDataConfig(
typeof(MyCustomDataType),
Symbol.Create("SPY", SecurityType.Base, Market.USA),
Resolution.Daily,
TimeZones.Utc,
TimeZones.Utc,
false,
false,
false
);
}
[Test]
public void JsonRoundTrip()
{
var expected = CreateNewInstance();
var type = expected.GetType();
var serialized = JsonConvert.SerializeObject(expected);
var result = JsonConvert.DeserializeObject(serialized, type);
AssertAreEqual(expected, result);
}
[Test]
public void ProtobufRoundTrip()
{
var expected = CreateNewInstance();
var type = expected.GetType();
RuntimeTypeModel.Default[typeof(BaseData)].AddSubType(2000, type);
using (var stream = new MemoryStream())
{
Serializer.Serialize(stream, expected);
stream.Position = 0;
var result = Serializer.Deserialize(type, stream);
AssertAreEqual(expected, result, filterByCustomAttributes: true);
}
}
[Test]
public void Clone()
{
var expected = CreateNewInstance();
var result = expected.Clone();
AssertAreEqual(expected, result);
}
/// <summary>
/// Verifies that the sample data file exists in the output directory.
/// </summary>
[Test]
public void SampleDataFileExists()
{
Assert.IsTrue(
File.Exists(_sampleDataPath),
$"Sample data file not found at {_sampleDataPath}. " +
"Ensure minimal sample data is present in the output/ directory."
);
}
/// <summary>
/// Reads every line of the sample data file through the Reader and
/// asserts that each line produces a valid, non-null instance with
/// correct Symbol, non-default Time/EndTime, and non-zero Value.
/// </summary>
[Test]
public void ReaderParsesAllSampleDataLines()
{
Assert.IsTrue(File.Exists(_sampleDataPath), $"Missing: {_sampleDataPath}");
var lines = File.ReadAllLines(_sampleDataPath)
.Where(l => !string.IsNullOrWhiteSpace(l))
.ToList();
Assert.IsNotEmpty(lines, "Sample data file is empty");
var instance = CreateNewInstance();
foreach (var line in lines)
{
var result = instance.Reader(_config, line, DateTime.UtcNow, false) as MyCustomDataType;
Assert.IsNotNull(result, $"Reader returned null for line: {line}");
Assert.AreEqual(_config.Symbol, result.Symbol, $"Wrong Symbol for line: {line}");
Assert.AreNotEqual(default(DateTime), result.Time, $"Time not set for line: {line}");
Assert.AreNotEqual(default(DateTime), result.EndTime, $"EndTime not set for line: {line}");
// TODO: add property-specific assertions if needed
// Assert.AreNotEqual(0m, result.SomeCustomProperty, $"SomeCustomProperty is zero for line: {line}");
}
Assert.IsTrue(lines.Count > 0, "Expected at least one data row");
}
/// <summary>
/// Reads the first sample data line through the Reader and verifies
/// that Clone produces an exact copy of every property.
/// </summary>
[Test]
public void CloneCopiesAllPropertiesFromSampleData()
{
Assert.IsTrue(File.Exists(_sampleDataPath), $"Missing: {_sampleDataPath}");
var firstLine = File.ReadLines(_sampleDataPath)
.FirstOrDefault(l => !string.IsNullOrWhiteSpace(l));
Assert.IsNotNull(firstLine, "Sample data file has no data lines");
var instance = CreateNewInstance();
var original = instance.Reader(_config, firstLine, DateTime.UtcNow, false) as MyCustomDataType;
Assert.IsNotNull(original);
var clone = original.Clone() as MyCustomDataType;
Assert.IsNotNull(clone);
Assert.AreEqual(original.Symbol, clone.Symbol);
Assert.AreEqual(original.Time, clone.Time);
Assert.AreEqual(original.EndTime, clone.EndTime);
Assert.AreEqual(original.Value, clone.Value);
Assert.AreEqual(original.SomeCustomProperty, clone.SomeCustomProperty);
}
/// <summary>
/// Tests that ToString returns a non-empty string for parsed sample data.
/// </summary>
[Test]
public void ToStringReturnsNonEmpty()
{
Assert.IsTrue(File.Exists(_sampleDataPath), $"Missing: {_sampleDataPath}");
var firstLine = File.ReadLines(_sampleDataPath)
.FirstOrDefault(l => !string.IsNullOrWhiteSpace(l));
Assert.IsNotNull(firstLine);
var instance = CreateNewInstance();
var result = instance.Reader(_config, firstLine, DateTime.UtcNow, false) as MyCustomDataType;
Assert.IsNotNull(result);
Assert.IsNotNull(result.ToString());
Assert.IsNotEmpty(result.ToString());
}
/// <summary>
/// Tests GetSource returns a valid SubscriptionDataSource with LocalFile transport.
/// </summary>
[Test]
public void GetSourceReturnsLocalFile()
{
var instance = CreateNewInstance();
var source = instance.GetSource(_config, DateTime.UtcNow, false);
Assert.IsNotNull(source);
Assert.AreEqual(SubscriptionTransportMedium.LocalFile, source.TransportMedium);
}
/// <summary>
/// Tests that the default resolution is correctly set.
/// </summary>
[Test]
public void DefaultResolutionIsCorrect()
{
var instance = CreateNewInstance();
Assert.AreEqual(Resolution.Daily, instance.DefaultResolution());
}
/// <summary>
/// Tests that RequiresMapping returns the expected value.
/// </summary>
[Test]
public void RequiresMappingIsCorrect()
{
var instance = CreateNewInstance();
Assert.AreEqual(true, instance.RequiresMapping());
}
/// <summary>
/// Tests that IsSparseData returns the expected value.
/// </summary>
[Test]
public void IsSparseDataIsCorrect()
{
var instance = CreateNewInstance();
Assert.AreEqual(true, instance.IsSparseData());
}
/// <summary>
/// Verifies that the universe sample data file exists in the output directory.
/// </summary>
[Test]
public void SampleUniverseDataFileExists()
{
Assert.IsTrue(
File.Exists(_universeDataPath),
$"Universe sample data file not found at {_universeDataPath}. " +
"Ensure minimal sample data is present in the output/ directory."
);
}
/// <summary>
/// Reads the universe sample data file and verifies the universe Reader.
/// </summary>
[Test]
public void UniverseReaderParsesAllSampleDataLines()
{
Assert.IsTrue(File.Exists(_universeDataPath), $"Missing: {_universeDataPath}");
var lines = File.ReadAllLines(_universeDataPath)
.Where(l => !string.IsNullOrWhiteSpace(l))
.ToList();
Assert.IsNotEmpty(lines, "Universe sample data file is empty");
var universeConfig = new SubscriptionDataConfig(
typeof(MyCustomDataUniverse),
Symbol.Create("SPY", SecurityType.Base, Market.USA),
Resolution.Daily,
TimeZones.Utc,
TimeZones.Utc,
false,
false,
false
);
var instance = new MyCustomDataUniverse();
foreach (var line in lines)
{
var result = instance.Reader(universeConfig, line, DateTime.UtcNow, false)
as MyCustomDataUniverse;
Assert.IsNotNull(result, $"Universe reader returned null for line: {line}");
}
}
private void AssertAreEqual(object expected, object result, bool filterByCustomAttributes = false)
{
foreach (var propertyInfo in expected.GetType().GetProperties())
{
// we skip Symbol which isn't protobuffed
if (filterByCustomAttributes && propertyInfo.CustomAttributes.Count() != 0)
{
Assert.AreEqual(propertyInfo.GetValue(expected), propertyInfo.GetValue(result));
}
}
foreach (var fieldInfo in expected.GetType().GetFields())
{
Assert.AreEqual(fieldInfo.GetValue(expected), fieldInfo.GetValue(result));
}
}
private BaseData CreateNewInstance()
{
return new MyCustomDataType
{
Symbol = Symbol.Empty,
Time = DateTime.Today,
DataType = MarketDataType.Base,
SomeCustomProperty = "This is some market related information"
};
}
}
}