testspecifictbufdataset.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. unit TestSpecificTBufDataset;
  2. {
  3. Unit tests which are specific to stand-alone TBufDataset-datasets. (So not
  4. for derrived datasets like TQuery )
  5. }
  6. {$IFDEF FPC}
  7. {$mode Delphi}{$H+}
  8. {$ENDIF}
  9. interface
  10. uses
  11. {$IFDEF FPC}
  12. fpcunit, testutils, testregistry, testdecorator, BufDataset,
  13. {$ELSE FPC}
  14. TestFramework,
  15. {$ENDIF FPC}
  16. Classes, SysUtils, db, ToolsUnit;
  17. type
  18. { TTestSpecificTBufDataset }
  19. TTestSpecificTBufDataset = class(TTestCase)
  20. private
  21. procedure TestDataset(ABufDataset: TBufDataset);
  22. protected
  23. procedure SetUp; override;
  24. procedure TearDown; override;
  25. published
  26. procedure CreateDatasetFromFielddefs;
  27. procedure CreateDatasetFromFields;
  28. procedure TestCreationDatasetWithCalcFields;
  29. end;
  30. implementation
  31. uses
  32. {$ifdef fpc}
  33. //
  34. {$endif fpc}
  35. variants,
  36. strutils,
  37. FmtBCD;
  38. { TTestSpecificTBufDataset }
  39. procedure TTestSpecificTBufDataset.TestDataset(ABufDataset: TBufDataset);
  40. var
  41. i : integer;
  42. begin
  43. for i := 1 to 10 do
  44. begin
  45. ABufDataset.Append;
  46. ABufDataset.FieldByName('ID').AsInteger := i;
  47. ABufDataset.FieldByName('NAME').AsString := 'TestName' + inttostr(i);
  48. ABufDataset.Post;
  49. end;
  50. ABufDataset.first;
  51. for i := 1 to 10 do
  52. begin
  53. CheckEquals(i,ABufDataset.fieldbyname('ID').asinteger);
  54. CheckEquals('TestName' + inttostr(i),ABufDataset.fieldbyname('NAME').AsString);
  55. ABufDataset.next;
  56. end;
  57. CheckTrue(ABufDataset.EOF);
  58. end;
  59. procedure TTestSpecificTBufDataset.SetUp;
  60. begin
  61. DBConnector.StartTest;
  62. end;
  63. procedure TTestSpecificTBufDataset.TearDown;
  64. begin
  65. DBConnector.StopTest;
  66. end;
  67. procedure TTestSpecificTBufDataset.CreateDatasetFromFielddefs;
  68. var ds : TBufDataset;
  69. begin
  70. ds := TBufDataset.Create(nil);
  71. DS.FieldDefs.Add('ID',ftInteger);
  72. DS.FieldDefs.Add('NAME',ftString,50);
  73. DS.CreateDataset;
  74. TestDataset(ds);
  75. DS.Close;
  76. DS.CreateDataset;
  77. TestDataset(ds);
  78. end;
  79. procedure TTestSpecificTBufDataset.CreateDatasetFromFields;
  80. var ds : TBufDataset;
  81. f: TField;
  82. begin
  83. ds := TBufDataset.Create(nil);
  84. F := TIntegerField.Create(ds);
  85. F.FieldName:='ID';
  86. F.DataSet:=ds;
  87. F := TStringField.Create(ds);
  88. F.FieldName:='NAME';
  89. F.DataSet:=ds;
  90. F.Size:=50;
  91. DS.CreateDataset;
  92. TestDataset(ds);
  93. DS.Close;
  94. DS.CreateDataset;
  95. TestDataset(ds);
  96. end;
  97. procedure TTestSpecificTBufDataset.TestCreationDatasetWithCalcFields;
  98. var ds : TBufDataset;
  99. f: TField;
  100. i: integer;
  101. begin
  102. ds := TBufDataset.Create(nil);
  103. try
  104. F := TIntegerField.Create(ds);
  105. F.FieldName:='ID';
  106. F.DataSet:=ds;
  107. F := TStringField.Create(ds);
  108. F.FieldName:='NAME';
  109. F.DataSet:=ds;
  110. F.Size:=50;
  111. F := TStringField.Create(ds);
  112. F.FieldKind:=fkCalculated;
  113. F.FieldName:='NAME_CALC';
  114. F.DataSet:=ds;
  115. F.Size:=50;
  116. F := TStringField.Create(ds);
  117. F.FieldKind:=fkLookup;
  118. F.FieldName:='NAME_LKP';
  119. F.LookupDataSet:=DBConnector.GetNDataset(5);
  120. F.KeyFields:='ID';
  121. F.LookupKeyFields:='ID';
  122. F.LookupResultField:='NAME';
  123. F.DataSet:=ds;
  124. F.Size:=50;
  125. DS.CreateDataset;
  126. TestDataset(ds);
  127. for i := 0 to ds.FieldDefs.Count-1 do
  128. begin
  129. CheckNotEquals(ds.FieldDefs[i].Name,'NAME_CALC');
  130. CheckNotEquals(ds.FieldDefs[i].Name,'NAME_LKP');
  131. end;
  132. DS.Close;
  133. finally
  134. ds.Free;
  135. end;
  136. end;
  137. initialization
  138. {$ifdef fpc}
  139. if uppercase(dbconnectorname)='BUFDATASET' then
  140. begin
  141. RegisterTestDecorator(TDBBasicsTestSetup, TTestSpecificTBufDataset);
  142. end;
  143. {$endif fpc}
  144. end.