Reworking DataRecords and DataFlow

This commit is contained in:
Michael Schimmel
2025-07-22 15:49:29 +02:00
parent 5b8b475da7
commit c530f2fc0b
4 changed files with 142 additions and 16 deletions
+92 -4
View File
@@ -18,13 +18,23 @@ type
MyDouble: Double;
MyString: string;
MyTimestamp: TDateTime;
MyRecord: TDataRecord;
end;
// A simple record to be nested inside another
TNestedTestRecord = record
NestedValue: Integer;
NestedString: string;
end;
// A record that contains another record field of type TDataRecord
TOuterTestRecord = record
OuterValue: Integer;
MyNestedRecord: TDataRecord;
end;
[TestFixture]
TTestDataRecord = class(TObject)
private
FCtx: TRttiContext;
public
[Test]
[IgnoreMemoryLeaks]
@@ -37,6 +47,14 @@ type
[Test]
[IgnoreMemoryLeaks]
procedure TestSetValueAndGetValue;
[Test]
// [IgnoreMemoryLeaks]
procedure TestNestedDataRecord;
[Test]
// [IgnoreMemoryLeaks]
procedure TestNestedSetValueAndGetValue;
end;
implementation
@@ -52,7 +70,7 @@ begin
layout := TDataRecord.TLayout.FromRecord<TMyTestRecord>;
// Assert: Check field count
Assert.IsTrue(Length(layout.Fields) = 6, 'Layout should have 6 fields');
Assert.IsTrue(Length(layout.Fields) = 7, 'Layout should have 7 fields');
// Assert: Check specific fields for correct type mapping
idx := layout.IndexOf('MyTimestamp');
@@ -70,6 +88,10 @@ begin
idx := layout.IndexOf('MySingle');
Assert.IsTrue(idx > -1, 'Field MySingle not found');
Assert.AreEqual(TDataRecord.TFieldType.dfFloat, layout.Fields[idx].FieldType, 'MySingle should be mapped to dfFloat');
idx := layout.IndexOf('MyRecord');
Assert.IsTrue(idx > -1, 'Field MyRecord not found');
Assert.AreEqual(TDataRecord.TFieldType.dfRecord, layout.Fields[idx].FieldType, 'MyRecord should be mapped to dfRecord');
end;
procedure TTestDataRecord.TestCreateFromInstanceAndGetValue;
@@ -84,6 +106,7 @@ begin
srcRecord.MyDouble := 789.012345;
srcRecord.MyString := 'Hello Delphi!';
srcRecord.MyTimestamp := EncodeDateTime(2024, 7, 19, 10, 30, 0, 0);
srcRecord.MyRecord := default(TDataRecord); // Not testing nested here
// Test: Create a TDataRecord from the source record instance
dataRecord := TDataRecord.FromRecord<TMyTestRecord>(srcRecord);
@@ -141,6 +164,71 @@ begin
Assert.AreEqual(testDouble, dataRecord.GetValue<Double>('MyDouble'), 1e-9, 'SetValue failed for Double');
end;
procedure TTestDataRecord.TestNestedDataRecord;
var
nestedSrc: TNestedTestRecord;
nestedDataRecord: TDataRecord;
outerSrc: TOuterTestRecord;
mainDataRecord: TDataRecord;
retrievedNestedRecord: TDataRecord;
begin
// Setup: Create the nested record and convert it to a TDataRecord
nestedSrc.NestedValue := 999;
nestedSrc.NestedString := 'Nested Record Test';
nestedDataRecord := TDataRecord.FromRecord<TNestedTestRecord>(nestedSrc);
// Setup: Create the outer record containing the nested TDataRecord
outerSrc.OuterValue := 111;
outerSrc.MyNestedRecord := nestedDataRecord;
// Test: Create the main TDataRecord from the outer record instance
mainDataRecord := TDataRecord.FromRecord<TOuterTestRecord>(outerSrc);
// Assert: Retrieve the nested record and check its contents
retrievedNestedRecord := mainDataRecord.GetValue<TDataRecord>('MyNestedRecord');
Assert.IsNotNull(retrievedNestedRecord.Layout.Fields, 'Nested record layout should not be nil');
Assert.IsTrue(Length(retrievedNestedRecord.Layout.Fields) = 2, 'Nested record should have 2 fields');
// Assert values within the nested record
Assert.AreEqual(999, retrievedNestedRecord.GetValue<Integer>('NestedValue'), 'Nested integer value mismatch');
Assert.AreEqual('Nested Record Test', retrievedNestedRecord.GetValue<string>('NestedString'), 'Nested string value mismatch');
// Assert value in the outer record
Assert.AreEqual(111, mainDataRecord.GetValue<Integer>('OuterValue'), 'Outer integer value mismatch');
end;
procedure TTestDataRecord.TestNestedSetValueAndGetValue;
var
outerRecord: TDataRecord;
nestedRecord: TDataRecord;
retrievedNestedRecord: TDataRecord;
begin
// Setup: Create an empty TDataRecord for the outer structure
outerRecord := TDataRecord.FromRecord<TOuterTestRecord>;
// Setup: Create a TDataRecord for the nested structure and populate it
nestedRecord := TDataRecord.FromRecord<TNestedTestRecord>;
nestedRecord.SetValue<Integer>('NestedValue', 888);
nestedRecord.SetValue<string>('NestedString', 'Manual Nested Set');
// Test: Set the nested record and another value in the outer record
outerRecord.SetValue<Integer>('OuterValue', 222);
outerRecord.SetValue<TDataRecord>('MyNestedRecord', nestedRecord);
// Assert: Retrieve the nested record and check its contents
retrievedNestedRecord := outerRecord.GetValue<TDataRecord>('MyNestedRecord');
Assert.IsNotNull(retrievedNestedRecord.Layout.Fields, 'Retrieved nested record layout should not be nil');
Assert.IsTrue(Length(retrievedNestedRecord.Layout.Fields) = 2, 'Retrieved nested record should have 2 fields');
Assert.AreEqual(888, retrievedNestedRecord.GetValue<Integer>('NestedValue'), 'Nested integer check failed after SetValue');
Assert
.AreEqual('Manual Nested Set', retrievedNestedRecord.GetValue<string>('NestedString'), 'Nested string check failed after SetValue');
// Assert: Check the outer value as well
Assert.AreEqual(222, outerRecord.GetValue<Integer>('OuterValue'), 'Outer value check failed after SetValue');
end;
initialization
TDUnitX.RegisterTestFixture(TTestDataRecord);