031b99acc8
Bugfix in DataSeries
121 lines
3.7 KiB
ObjectPascal
121 lines
3.7 KiB
ObjectPascal
unit Myc.Test.Trade.DataServer;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils,
|
|
System.Generics.Collections, // E2003: Added for TList<T>
|
|
DUnitX.TestFramework,
|
|
Myc.Signals,
|
|
Myc.Lazy,
|
|
Myc.Futures,
|
|
Myc.Trade.DataPoint,
|
|
Myc.Trade.DataSeries,
|
|
Myc.Trade.DataServer;
|
|
|
|
type
|
|
// Define ITickData as an alias for the specific IDataPoint interface.
|
|
ITickData = IDataPoint<ITick>;
|
|
|
|
[TestFixture]
|
|
TTest_TABFileServer_Equivalence = class(TObject)
|
|
private
|
|
FServer: IDataServer<ITickData>;
|
|
FExpectedData: TArray<TAskBid.TTick>;
|
|
public
|
|
[Setup]
|
|
procedure Setup;
|
|
[TearDown]
|
|
procedure TearDown;
|
|
|
|
[Test]
|
|
procedure Test_ServerReturnsSameDataAs_LoadDataSeries;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
Myc.TaskManager;
|
|
|
|
const
|
|
// The reference data file for this test.
|
|
C_TEST_FILENAME = '\\COFFEE\TickData\Pepperstone\AUDNZD_2024_05.tab_zip';
|
|
// The chunk size for fetching data from the server.
|
|
C_MAX_FETCH = 20;
|
|
|
|
{ TTest_TABFileServer_Equivalence }
|
|
|
|
procedure TTest_TABFileServer_Equivalence.Setup;
|
|
begin
|
|
// SetupTaskManagerMock;
|
|
|
|
// IsMemoryLow := function: Boolean begin exit(false); end;
|
|
|
|
// 1. Load the ground truth data using the original LoadDataSeries function.
|
|
FExpectedData := TAskBid.LoadDataSeries(C_TEST_FILENAME).WaitFor;
|
|
|
|
// 2. Set up the filename and the TABFileServer instance.
|
|
FServer := TTABFileServer.Create(C_TEST_FILENAME, C_MAX_FETCH);
|
|
end;
|
|
|
|
procedure TTest_TABFileServer_Equivalence.TearDown;
|
|
begin
|
|
FServer := nil;
|
|
SetLength(FExpectedData, 0);
|
|
IsMemoryLow := nil;
|
|
end;
|
|
|
|
procedure TTest_TABFileServer_Equivalence.Test_ServerReturnsSameDataAs_LoadDataSeries;
|
|
var
|
|
actualDataList: TList<ITickData>;
|
|
chunk: TArray<ITickData>;
|
|
newChunk: TLazy<TArray<ITickData>>;
|
|
begin
|
|
actualDataList := TList<ITickData>.Create;
|
|
try
|
|
newChunk := TLazy<TArray<ITickData>>.Construct(FServer.Data);
|
|
|
|
// Loop until the server stops providing new data chunks.
|
|
var cnt: Int64 := 0;
|
|
while not FServer.IsLiveData.Value do
|
|
begin
|
|
FServer.Fetch;
|
|
|
|
// Get the data chunk produced by the last Fetch call.
|
|
if newChunk.Pop(chunk) then
|
|
begin
|
|
for var i := 0 to High(chunk) do
|
|
begin
|
|
actualDataList.Add(chunk[i]);
|
|
Assert.AreEqual(chunk[i].Idx, cnt, 'Index mismatch');
|
|
inc(cnt);
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
// --- Assertions ---
|
|
|
|
// 1. Verify that the total number of records matches.
|
|
Assert.AreEqual(Length(FExpectedData), actualDataList.Count, 'Data record count mismatch');
|
|
|
|
// 2. Verify that the content of each record is identical.
|
|
for var i := 0 to High(FExpectedData) do
|
|
begin
|
|
var expectedTick := FExpectedData[i];
|
|
var actualTick := actualDataList[i];
|
|
|
|
// E2003: Removed assertion for 'Index', as it's not part of the IDataPoint interface.
|
|
// The index is an implementation detail of the faulty ConvertTicks and cannot be tested here.
|
|
Assert.AreEqual(expectedTick.OADateTime, actualTick.Time, 'Timestamp mismatch ');
|
|
Assert.IsTrue(Abs(expectedTick.Data.Ask - actualTick.Data.Ask) < 0.0001, 'Ask price mismatch');
|
|
Assert.IsTrue(Abs(expectedTick.Data.Bid - actualTick.Data.Bid) < 0.0001, 'Bid price mismatch');
|
|
end;
|
|
finally
|
|
actualDataList.Free;
|
|
end;
|
|
end;
|
|
|
|
initialization
|
|
TDUnitX.RegisterTestFixture(TTest_TABFileServer_Equivalence);
|
|
end.
|