toolsunit.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. unit ToolsUnit;
  2. {$IFDEF FPC}
  3. {$mode objfpc}{$H+}
  4. {$ENDIF}
  5. interface
  6. uses
  7. Classes, SysUtils, DB, testdecorator;
  8. // Number of "N" test datasets (as opposed to FieldDatasets) that will be created
  9. // The connectors should have these records prepared in their Create*Dataset procedures.
  10. Const MaxDataSet = 35;
  11. type
  12. { TDBConnector }
  13. TDBConnectorClass = class of TDBConnector;
  14. TDBConnector = class(TPersistent)
  15. private
  16. FChangedDatasets : array[0..MaxDataSet] of boolean;
  17. FFormatSettings: TFormatSettings;
  18. FUsedDatasets : TFPList;
  19. FChangedFieldDataset : boolean;
  20. protected
  21. procedure SetTestUniDirectional(const AValue: boolean); virtual;
  22. function GetTestUniDirectional: boolean; virtual;
  23. // These methods should be implemented by all descendents
  24. // They are called each time a test needs a TDataset descendent
  25. // n: the dataset index to return (also number of records in set)
  26. // Presupposes that Create*Dataset(s) has been called already.
  27. Function InternalGetNDataset(n : integer) : TDataset; virtual; abstract;
  28. Function InternalGetFieldDataset : TDataSet; virtual; abstract;
  29. // These methods should be implemented by all descendents
  30. // They are called e.g. in the constructor. They can be used
  31. // to create the tables on disk, or on a DB server
  32. procedure CreateNDatasets; virtual; abstract;
  33. procedure CreateFieldDataset; virtual; abstract;
  34. // These methods are called after each test in which a dataset is used
  35. // by calling GetXXXDataset with Achange=true
  36. // They should reset all data to their right/initial values.
  37. procedure ResetNDatasets; virtual;
  38. procedure ResetFieldDataset; virtual;
  39. // These methods are called e.g. in the destructor.
  40. // They should clean up all mess, like tables on disk or on a DB server
  41. procedure DropNDatasets; virtual; abstract;
  42. procedure DropFieldDataset; virtual; abstract;
  43. public
  44. constructor create; virtual;
  45. destructor destroy; override;
  46. procedure DataEvent(dataset :TDataset);
  47. Function GetNDataset(n : integer) : TDataset; overload;
  48. Function GetNDataset(AChange : Boolean; n : integer) : TDataset; overload;
  49. Function GetFieldDataset : TDataSet; overload;
  50. Function GetFieldDataset(AChange : Boolean) : TDataSet; overload;
  51. Function GetTraceDataset(AChange : Boolean) : TDataset; virtual;
  52. procedure StartTest;
  53. procedure StopTest;
  54. property TestUniDirectional: boolean read GetTestUniDirectional write SetTestUniDirectional;
  55. property FormatSettings: TFormatSettings read FFormatSettings;
  56. end;
  57. { TDBBasicsTestSetup }
  58. TDBBasicsTestSetup = class(TTestSetup)
  59. protected
  60. procedure OneTimeSetup; override;
  61. procedure OneTimeTearDown; override;
  62. end;
  63. { TTestDataLink }
  64. TTestDataLink = class(TDataLink)
  65. protected
  66. procedure DataSetScrolled(Distance: Integer); override;
  67. procedure DataSetChanged; override;
  68. {$IFDEF fpc}
  69. procedure DataEvent(Event: TDataEvent; Info: Ptrint); override;
  70. {$ELSE}
  71. procedure DataEvent(Event: TDataEvent; Info: longint); override;
  72. {$ENDIF}
  73. end;
  74. const
  75. DataEventnames : Array [TDataEvent] of String[21] =
  76. ('deFieldChange', 'deRecordChange', 'deDataSetChange', 'deDataSetScroll',
  77. 'deLayoutChange', 'deUpdateRecord', 'deUpdateState', 'deCheckBrowseMode',
  78. 'dePropertyChange', 'deFieldListChange', 'deFocusControl' ,'deParentScroll',
  79. 'deConnectChange', 'deReconcileError', 'deDisabledStateChange');
  80. const
  81. testValuesCount = 25;
  82. testFloatValues : Array[0..testValuesCount-1] of double = (-maxSmallint-1,-maxSmallint,-256,-255,-128,-127,-1,0,1,127,128,255,256,maxSmallint,maxSmallint+1,0.123456,-0.123456,4.35,12.434E7,9.876e-5,123.45678,2.4,3.2,0.4,23);
  83. testCurrencyValues : Array[0..testValuesCount-1] of currency = (-100,-65.5,-54.34,-43.34,-2.50,-0.2,45.40,0.3,45.4,127,128,255,256,45,0.3,45.4,127,128,255,256,45,1234.56,43.23,43.43,99.88);
  84. testFmtBCDValues : Array[0..testValuesCount-1] of string = ('-100','-65.5','-54.3333','-43.3334','-2.5','-0.234567','45.4','0.3','45.414585','127','128','255','256','45','0.3','45.4','127','128','255','256','45','1234.56789','43.23','43.500001','99.88');
  85. testIntValues : Array[0..testValuesCount-1] of integer = (-maxInt,-maxInt+1,-maxSmallint-1,-maxSmallint,-256,-255,-128,-127,-1,0,1,127,128,255,256,maxSmallint,maxSmallint+1,MaxInt-1,MaxInt,100,130,150,-150,-132,234);
  86. testSmallIntValues : Array[0..testValuesCount-1] of smallint = (-maxSmallint,-maxSmallint+1,-256,-255,-128,-127,-1,0,1,127,128,255,256,maxSmallint,maxSmallint-1,100,110,120,130,150,-150,-132,234,231,42);
  87. testLargeIntValues : Array[0..testValuesCount-1] of LargeInt = ( -$7fffffffffffffff,-$7ffffffffffffffe,-maxInt-1,-maxInt+1,-maxSmallint,-maxSmallint+1,-256,-255,-128,-127,-1,0,1,127,128,255,256,maxSmallint,maxSmallint-1,maxSmallint+1,MaxInt-1,MaxInt,$7fffffffffffffff-1,$7fffffffffffffff,235253244);
  88. testBooleanValues : Array[0..testValuesCount-1] of boolean = (true,false,false,true,true,false,false,true,false,true,true,true,false,false,false,false,true,true,true,true,false,true,true,false,false);
  89. testStringValues : Array[0..testValuesCount-1] of string = (
  90. '',
  91. 'a',
  92. 'ab',
  93. 'abc',
  94. 'abcd',
  95. 'abcde',
  96. 'abcdef',
  97. 'abcdefg',
  98. 'abcdefgh',
  99. 'abcdefghi',
  100. 'abcdefghij',
  101. 'lMnOpQrStU',
  102. '1234567890',
  103. '_!@#$%^&*(',
  104. '_!@#$%^&*(',
  105. ' ''quotes'' ',
  106. ')-;:/?.<>',
  107. '~`|{}- =', // note that there's no \ (backslash) since some db's uses that as escape-character
  108. ' WRaP ',
  109. 'wRaP ',
  110. ' wRAP',
  111. 'this',
  112. // 'is',
  113. 'fun',
  114. 'VB7^',
  115. 'vdfbst'
  116. );
  117. testDateValues : Array[0..testValuesCount-1] of string = (
  118. '2000-01-01',
  119. '1999-12-31',
  120. '2004-02-29',
  121. '2004-03-01',
  122. '1991-02-28',
  123. '1991-03-01',
  124. '1997-11-29',
  125. '2040-10-16',
  126. '1977-09-29',
  127. '1977-12-31',
  128. '1917-12-29',
  129. '1900-01-01',
  130. '1899-12-31',
  131. '1899-12-30',
  132. '1899-12-29',
  133. '1800-03-30',
  134. '1754-06-04',
  135. '1753-01-01',
  136. '1650-05-10',
  137. '0904-04-12',
  138. '0199-07-09',
  139. '0079-11-29',
  140. '0031-11-02',
  141. '0001-12-31',
  142. '0001-01-01'
  143. );
  144. testTimeValues : Array[0..testValuesCount-1] of string = (
  145. '10:45:12.000',
  146. '00:00:00.000',
  147. '24:00:00.000',
  148. '33:25:15.000',
  149. '04:59:16.000',
  150. '05:45:59.000',
  151. '16:35:42.000',
  152. '14:45:52.000',
  153. '12:45:12.000',
  154. '18:45:22.000',
  155. '19:45:12.000',
  156. '14:45:14.000',
  157. '16:45:12.000',
  158. '11:45:12.000',
  159. '15:35:12.000',
  160. '16:45:12.010',
  161. '13:55:12.200',
  162. '13:46:12.543',
  163. '15:35:12.000',
  164. '17:25:12.530',
  165. '19:45:12.003',
  166. '10:54:12.999',
  167. '12:25:12.000',
  168. '20:15:12.758',
  169. '12:25:12.000'
  170. );
  171. var dbtype,
  172. dbconnectorname,
  173. dbconnectorparams,
  174. dbname,
  175. dbuser,
  176. dbhostname,
  177. dbpassword,
  178. dbQuoteChars : string;
  179. DataEvents : string;
  180. DBConnector : TDBConnector;
  181. testValues : Array [TFieldType,0..testvaluescount -1] of string;
  182. procedure InitialiseDBConnector;
  183. procedure FreeDBConnector;
  184. function DateTimeToTimeString(d: tdatetime) : string;
  185. function TimeStringToDateTime(d: String): TDateTime;
  186. function StringToByteArray(s: ansistring): Variant;
  187. implementation
  188. uses
  189. inifiles, FmtBCD, Variants;
  190. var DBConnectorRefCount: integer;
  191. constructor TDBConnector.create;
  192. begin
  193. FFormatSettings.DecimalSeparator:='.';
  194. FFormatSettings.ThousandSeparator:=#0;
  195. FFormatSettings.DateSeparator:='-';
  196. FFormatSettings.TimeSeparator:=':';
  197. FFormatSettings.ShortDateFormat:='yyyy/mm/dd';
  198. FFormatSettings.LongTimeFormat:='hh:nn:ss.zzz';
  199. FUsedDatasets := TFPList.Create;
  200. CreateFieldDataset;
  201. CreateNDatasets;
  202. end;
  203. destructor TDBConnector.destroy;
  204. begin
  205. if assigned(FUsedDatasets) then FUsedDatasets.Destroy;
  206. DropNDatasets;
  207. DropFieldDataset;
  208. end;
  209. function TDBConnector.GetTestUniDirectional: boolean;
  210. begin
  211. result := false;
  212. end;
  213. procedure TDBConnector.SetTestUniDirectional(const AValue: boolean);
  214. begin
  215. raise exception.create('Connector does not support tests for unidirectional datasets');
  216. end;
  217. procedure TDBConnector.ResetNDatasets;
  218. begin
  219. DropNDatasets;
  220. CreateNDatasets;
  221. end;
  222. procedure TDBConnector.ResetFieldDataset;
  223. begin
  224. DropFieldDataset;
  225. CreateFieldDataset;
  226. end;
  227. procedure TDBConnector.DataEvent(dataset : tdataset);
  228. begin
  229. DataEvents := DataEvents + 'DataEvent' + ';';
  230. end;
  231. function TDBConnector.GetNDataset(n: integer): TDataset;
  232. begin
  233. Result := GetNDataset(False,n);
  234. end;
  235. procedure ReadIniFile;
  236. var IniFile : TIniFile;
  237. begin
  238. IniFile := TIniFile.Create(getcurrentdir + PathDelim + 'database.ini');
  239. dbtype:='';
  240. if Paramcount>0 then
  241. dbtype := ParamStr(1);
  242. if (dbtype='') or not inifile.SectionExists(dbtype) then
  243. dbtype := IniFile.ReadString('Database','Type','');
  244. dbconnectorname := IniFile.ReadString(dbtype,'Connector','');
  245. dbname := IniFile.ReadString(dbtype,'Name','');
  246. dbuser := IniFile.ReadString(dbtype,'User','');
  247. dbhostname := IniFile.ReadString(dbtype,'Hostname','');
  248. dbpassword := IniFile.ReadString(dbtype,'Password','');
  249. dbconnectorparams := IniFile.ReadString(dbtype,'ConnectorParams','');
  250. dbquotechars := IniFile.ReadString(dbtype,'QuoteChars','"');
  251. IniFile.Free;
  252. end;
  253. procedure InitialiseDBConnector;
  254. const B: array[boolean] of char=('0','1'); // should be exported from some main db unit, as SQL true/false?
  255. var DBConnectorClass : TPersistentClass;
  256. i : integer;
  257. FormatSettings : TFormatSettings;
  258. begin
  259. if DBConnectorRefCount>0 then exit;
  260. FormatSettings.DecimalSeparator:='.';
  261. FormatSettings.ThousandSeparator:=#0;
  262. testValues[ftString] := testStringValues;
  263. testValues[ftFixedChar] := testStringValues;
  264. testValues[ftTime] := testTimeValues;
  265. testValues[ftDate] := testDateValues;
  266. testValues[ftBlob] := testStringValues;
  267. testValues[ftMemo] := testStringValues;
  268. testValues[ftFMTBcd] := testFmtBCDValues;
  269. for i := 0 to testValuesCount-1 do
  270. begin
  271. testValues[ftBoolean,i] := B[testBooleanValues[i]];
  272. testValues[ftFloat,i] := FloatToStr(testFloatValues[i],FormatSettings);
  273. testValues[ftSmallint,i] := IntToStr(testSmallIntValues[i]);
  274. testValues[ftInteger,i] := IntToStr(testIntValues[i]);
  275. testValues[ftLargeint,i] := IntToStr(testLargeIntValues[i]);
  276. testValues[ftCurrency,i] := CurrToStr(testCurrencyValues[i],FormatSettings);
  277. testValues[ftBCD,i] := CurrToStr(testCurrencyValues[i],FormatSettings);
  278. // For date '0001-01-01' other time-part like '00:00:00' causes "Invalid variant type cast", because of < MinDateTime constant
  279. if (testDateValues[i]>'0001-01-01') and (testTimeValues[i]>='00:00:01') and (testTimeValues[i]<'24:00:00') then
  280. testValues[ftDateTime,i] := testDateValues[i] + ' ' + testTimeValues[i]
  281. else
  282. testValues[ftDateTime,i] := testDateValues[i];
  283. end;
  284. if dbconnectorname = '' then raise Exception.Create('There is no db-connector specified');
  285. DBConnectorClass := GetClass('T'+dbconnectorname+'DBConnector');
  286. if assigned(DBConnectorClass) then
  287. DBConnector := TDBConnectorClass(DBConnectorClass).create
  288. else Raise Exception.Create('Unknown db-connector specified: ' + 'T'+dbconnectorname+'DBConnector');
  289. inc(DBConnectorRefCount);
  290. end;
  291. procedure FreeDBConnector;
  292. begin
  293. dec(DBConnectorRefCount);
  294. if DBConnectorRefCount=0 then
  295. FreeAndNil(DBConnector);
  296. end;
  297. function DateTimeToTimeString(d: tdatetime): string;
  298. var
  299. millisecond: word;
  300. second : word;
  301. minute : word;
  302. hour : word;
  303. begin
  304. // Format the datetime in the format hh:nn:ss.zzz, where the hours can be bigger then 23.
  305. DecodeTime(d,hour,minute,second,millisecond);
  306. hour := hour + (trunc(d) * 24);
  307. result := Format('%.2d:%.2d:%.2d.%.3d',[hour,minute,second,millisecond]);
  308. end;
  309. function TimeStringToDateTime(d: String): TDateTime;
  310. var
  311. millisecond: word;
  312. second : word;
  313. minute : word;
  314. hour : word;
  315. days : word;
  316. begin
  317. // Convert the string in the format hh:nn:ss.zzz to a datetime.
  318. hour := strtoint(copy(d,1,2));
  319. minute := strtoint(copy(d,4,2));
  320. second := strtoint(copy(d,7,2));
  321. millisecond := strtoint(copy(d,10,3));
  322. days := hour div 24;
  323. hour := hour mod 24;
  324. result := ComposeDateTime(days,EncodeTime(hour,minute,second,millisecond));
  325. end;
  326. function StringToByteArray(s: ansistring): Variant;
  327. var P: Pointer;
  328. Len: integer;
  329. begin
  330. Len := Length(s) * SizeOf(AnsiChar);
  331. Result := VarArrayCreate([0, Len-1], varByte);
  332. P := VarArrayLock(Result);
  333. try
  334. Move(s[1], P^, Len);
  335. finally
  336. VarArrayUnlock(Result);
  337. end;
  338. end;
  339. { TTestDataLink }
  340. {$IFDEF FPC}
  341. procedure TTestDataLink.DataSetScrolled(Distance: Integer);
  342. begin
  343. DataEvents := DataEvents + 'DataSetScrolled' + ':' + inttostr(Distance) + ';';
  344. inherited DataSetScrolled(Distance);
  345. end;
  346. procedure TTestDataLink.DataSetChanged;
  347. begin
  348. DataEvents := DataEvents + 'DataSetChanged;';
  349. inherited DataSetChanged;
  350. end;
  351. procedure TTestDataLink.DataEvent(Event: TDataEvent; Info: Ptrint);
  352. {$ELSE}
  353. procedure TTestDataLink.DataEvent(Event: TDataEvent; Info: Longint);
  354. {$ENDIF}
  355. begin
  356. if Event <> deFieldChange then
  357. DataEvents := DataEvents + DataEventnames[Event] + ':' + inttostr(info) + ';'
  358. else
  359. DataEvents := DataEvents + DataEventnames[Event] + ':' + TField(info).FieldName + ';';
  360. inherited DataEvent(Event, Info);
  361. end;
  362. { TDBConnector }
  363. function TDBConnector.GetNDataset(AChange : Boolean; n: integer): TDataset;
  364. begin
  365. if AChange then FChangedDatasets[n] := True;
  366. Result := InternalGetNDataset(n);
  367. FUsedDatasets.Add(Result);
  368. end;
  369. function TDBConnector.GetFieldDataset: TDataSet;
  370. begin
  371. Result := GetFieldDataset(False);
  372. end;
  373. function TDBConnector.GetFieldDataset(AChange: Boolean): TDataSet;
  374. begin
  375. if AChange then FChangedFieldDataset := True;
  376. Result := InternalGetFieldDataset;
  377. FUsedDatasets.Add(Result);
  378. end;
  379. function TDBConnector.GetTraceDataset(AChange: Boolean): TDataset;
  380. begin
  381. result := GetNDataset(AChange,15);
  382. end;
  383. procedure TDBConnector.StartTest;
  384. begin
  385. // Do nothing?
  386. end;
  387. procedure TDBConnector.StopTest;
  388. var i : integer;
  389. ds : TDataset;
  390. begin
  391. for i := 0 to FUsedDatasets.Count -1 do
  392. begin
  393. ds := tdataset(FUsedDatasets[i]);
  394. if ds.active then ds.Close;
  395. ds.Free;
  396. end;
  397. FUsedDatasets.Clear;
  398. if FChangedFieldDataset then ResetFieldDataset;
  399. for i := 0 to MaxDataSet do if FChangedDatasets[i] then
  400. begin
  401. ResetNDatasets;
  402. fillchar(FChangedDatasets,sizeof(FChangedDatasets),ord(False));
  403. break;
  404. end;
  405. end;
  406. { TDBBasicsTestSetup }
  407. procedure TDBBasicsTestSetup.OneTimeSetup;
  408. begin
  409. InitialiseDBConnector;
  410. end;
  411. procedure TDBBasicsTestSetup.OneTimeTearDown;
  412. begin
  413. FreeDBConnector;
  414. end;
  415. initialization
  416. ReadIniFile;
  417. DBConnectorRefCount:=0;
  418. end.