toolsunit.pas 15 KB

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