testbasics.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. unit TestBasics;
  2. {$IFDEF FPC}
  3. {$mode objfpc}{$H+}
  4. {$ENDIF}
  5. interface
  6. uses
  7. fpcunit, testutils, testregistry, testdecorator,
  8. Classes, SysUtils, db;
  9. type
  10. { TTestBasics }
  11. TTestBasics = class(TTestCase)
  12. private
  13. function CreateDatasetWith3Fields: TDataset;
  14. protected
  15. published
  16. procedure TestParseSQL;
  17. procedure TestInitFielddefsFromFields;
  18. procedure TestDoubleFieldDef;
  19. procedure TestFieldDefWithoutDS;
  20. procedure TestGetParamList;
  21. procedure TestGetFieldList;
  22. procedure TestExtractFieldName; //move record then copy. Is copy identical? Has record position changed?
  23. procedure TestCheckFieldNames;
  24. procedure TestFindField;
  25. end;
  26. implementation
  27. uses toolsunit;
  28. Type HackedDataset = class(TDataset);
  29. { TTestBasics }
  30. function TTestBasics.CreateDatasetWith3Fields: TDataset;
  31. var
  32. F: TField;
  33. begin
  34. Result := TDataSet.Create(nil);
  35. F := TIntegerField.Create(Result);
  36. F.FieldName := 'Field1';
  37. F.DataSet := Result;
  38. F := TIntegerField.Create(Result);
  39. F.FieldName := 'Field2';
  40. F.DataSet := Result;
  41. F := TIntegerField.Create(Result);
  42. F.FieldName := 'Field3';
  43. F.DataSet := Result;
  44. end;
  45. procedure TTestBasics.TestParseSQL;
  46. var Params : TParams;
  47. ReplStr : string;
  48. pb : TParamBinding;
  49. i : integer;
  50. SQLStr : string;
  51. begin
  52. Params := TParams.Create;
  53. AssertEquals( 'select * from table where id = $1',
  54. params.ParseSQL('select * from table where id = :id',true,True,True,psPostgreSQL));
  55. AssertEquals( 'select * from table where id = $1',
  56. params.ParseSQL('select * from table where id = :id',false,True,True,psPostgreSQL));
  57. AssertEquals( 'update test set 1=$1 2=$2 3=$3 4=$4 5=$5 6=$6 7=$7 8=$8 9=$9 where (id = $2)',
  58. params.ParseSQL('update test set 1=:1 2=:2 3=:par3 4=:par4 5=:par5 6=:par6 7=:par7 8=:par8 9=:par9 where (id = :2)',true,True,True,psPostgreSQL));
  59. AssertEquals( 'update test set 1=$1 2=$2 3=$3 4=$4 5=$5 6=$6 7=$7 8=$8 9=$9 where (id = $3) and (test=''$test'')',
  60. params.ParseSQL('update test set 1=:1 2=:2 3=:par3 4=:par4 5=:par5 6=:par6 7=:par7 8=:par8 9=:par9 where (id = :par3) and (test=''$test'')',true,true,true,psPostgreSQL));
  61. AssertEquals( 'update test set 1=$1 2=$2 3=$3 4=$4 5=$5 6=$6 7=$7 8=$8 9=$9 10=$10 11=$11 12=$5 where (id = $3) and (test=''$test'')',
  62. params.ParseSQL('update test set 1=:1 2=:2 3=:par3 4=:par4 5=:par5 6=:par6 7=:par7 8=:par8 9=:par9 10=:par10 11=:11 12=:par5 where (id = :par3) and (test=''$test'')',true,true,true,psPostgreSQL));
  63. AssertEquals( 'select * from table where id = $1',
  64. params.ParseSQL('select * from table where id = :id',true,true,false,psSimulated,pb,ReplStr));
  65. AssertEquals('$',ReplStr);
  66. AssertEquals( 'update test set 1=$1 2=$2 3=$3 4=$4 5=$5 6=$6 7=$7 8=$8 9=$9 where (id = $2)',
  67. params.ParseSQL('update test set 1=:1 2=:2 3=:par3 4=:par4 5=:par5 6=:par6 7=:par7 8=:par8 9=:par9 where (id = :2)',true,true,false,psSimulated,pb,ReplStr));
  68. AssertEquals('$',ReplStr);
  69. AssertEquals( 'update test set 1=$$1 2=$$2 3=$$3 4=$$4 5=$$5 6=$$6 7=$$7 8=$$8 9=$$9 where (id = $$3) and (test=''$test'')',
  70. params.ParseSQL('update test set 1=:1 2=:2 3=:par3 4=:par4 5=:par5 6=:par6 7=:par7 8=:par8 9=:par9 where (id = :par3) and (test=''$test'')',true,true,false,psSimulated,pb,ReplStr));
  71. AssertEquals('$$',ReplStr);
  72. AssertEquals( 'update test set 1=$$1 2=$$2 3=$$3 4=$$4 5=$$5 6=$$6 7=$$7 8=$$8 9=$$9 10=$$10 11=$$11 12=$$5 where (id = $$3) and (test=''$test'')',
  73. params.ParseSQL('update test set 1=:1 2=:2 3=:par3 4=:par4 5=:par5 6=:par6 7=:par7 8=:par8 9=:par9 10=:par10 11=:11 12=:par5 where (id = :par3) and (test=''$test'')',true,True,True,psSimulated));
  74. AssertEquals('$$',ReplStr);
  75. AssertEquals( 'update test set 1=$$$1 2=$$$2 3=$$$3 4=$$$4 5=$$$5 6=$$$6 7=$$$7 8=$$$8 9=$$$9 10=$$$10 11=$$$11 12=$$$5 where (id$$ = $$$3) and (test$=''$test'')',
  76. params.ParseSQL('update test set 1=:1 2=:2 3=:par3 4=:par4 5=:par5 6=:par6 7=:par7 8=:par8 9=:par9 10=:par10 11=:11 12=:par5 where (id$$ = :par3) and (test$=''$test'')',true,true,False,psSimulated,pb,ReplStr));
  77. AssertEquals('$$$',ReplStr);
  78. AssertEquals( 'select * from table where id = ?',
  79. params.ParseSQL('select * from table where id = :id',true,true,true,psInterbase));
  80. // Test bug 10345
  81. AssertEquals( 'select email from table where upper(email) like ''%''||?||''%''',
  82. params.ParseSQL('select email from table where upper(email) like ''%''||:email||''%''',true,true,true,psInterbase));
  83. // Test escape-sequences:
  84. AssertEquals( 'select * from table where ''id '''' = :id''',
  85. params.ParseSQL('select * from table where ''id '''' = :id''',true,False,True,psPostgreSQL));
  86. AssertEquals( 'select * from table where "id "" = :id"',
  87. params.ParseSQL('select * from table where "id "" = :id"',true,False,True,psPostgreSQL));
  88. AssertEquals( 'select * from table where "id \" = :id"',
  89. params.ParseSQL('select * from table where "id \" = :id"',true,True,False,psPostgreSQL));
  90. AssertEquals( 'select * from table where "id \" = $1',
  91. params.ParseSQL('select * from table where "id \" = :id',true,False,False,psPostgreSQL));
  92. AssertEquals( 'select * from table where "id = :id\',
  93. params.ParseSQL('select * from table where "id = :id\',true,True,True,psInterbase));
  94. // Test strange-field names
  95. AssertEquals( 'select * from table where "field-name" = ?',
  96. params.ParseSQL('select * from table where "field-name" = :"field-name"',true,True,True,psInterbase));
  97. AssertEquals('field-name',Params.Items[0].Name);
  98. AssertEquals( 'select * from table where "field-name" = ?',
  99. params.ParseSQL('select * from table where "field-name" = :"field-name',true,True,True,psInterbase));
  100. // Test more than 99 params - bug 19645
  101. SQLStr := 'update test set';
  102. for i := 1 to 101 do
  103. SQLStr := format('%s field%d=:par%d', [SQLStr,i,i]);
  104. AssertEquals( StringReplace(SQLStr, ':par', '$', [rfReplaceAll]),
  105. Params.ParseSQL(SQLStr, True, True, True, psPostgreSQL) );
  106. // Test comments:
  107. // Simple comment
  108. AssertEquals( 'select * from table where id= --comment :c'#10'$1-$2 or id= --:c'#13'-$3',
  109. Params.ParseSQL('select * from table where id= --comment :c'#10':a-:b or id= --:c'#13'-:d', True, True, True, psPostgreSQL));
  110. // Bracketed comment
  111. AssertEquals( 'select * from table where id=/*comment :c*/$1-$2',
  112. Params.ParseSQL('select * from table where id=/*comment :c*/:a-:b', True, True, True, psPostgreSQL));
  113. // Consecutive comments, with quote in second comment
  114. AssertEquals( '--c1'#10'--c'''#10'select '':a'' from table where id=$1',
  115. Params.ParseSQL('--c1'#10'--c'''#10'select '':a'' from table where id=:id', True, True, True, psPostgreSQL));
  116. Params.Free;
  117. end;
  118. procedure TTestBasics.TestInitFielddefsFromFields;
  119. var ds : TDataset;
  120. F1,F2,F3 : Tfield;
  121. Procedure CompareFieldAndFieldDef(Fld: TField; FldDef : TFieldDef);
  122. begin
  123. AssertEquals(Fld.FieldName,FldDef.Name);
  124. AssertEquals(Fld.Size,FldDef.Size);
  125. AssertEquals(Fld.Required,FldDef.Required);
  126. AssertTrue(Fld.DataType=FldDef.DataType);
  127. end;
  128. begin
  129. ds := TDataset.Create(nil);
  130. try
  131. F1:=TStringField.Create(ds);
  132. F1.Size := 10;
  133. F1.Name := 'StringFld';
  134. F1.FieldName := 'FStringFld';
  135. F1.Required := false;
  136. F1.Dataset:=ds;
  137. F2:=TIntegerField.Create(ds);
  138. F2.Name := 'IntegerFld';
  139. F2.FieldName := 'FIntegerFld';
  140. F2.Required := True;
  141. F2.Dataset:=ds;
  142. F3:=TBCDField.Create(ds);
  143. F3.Name := 'BCDFld';
  144. F3.FieldName := 'FBCDFld';
  145. F3.Required := false;
  146. F3.Dataset:=ds;
  147. (f3 as TBCDField).Precision := 2;
  148. HackedDataset(ds).InitFieldDefsFromfields;
  149. AssertEquals(3,ds.FieldDefs.Count);
  150. CompareFieldAndFieldDef(F1,ds.FieldDefs[0]);
  151. CompareFieldAndFieldDef(F2,ds.FieldDefs[1]);
  152. CompareFieldAndFieldDef(F3,ds.FieldDefs[2]);
  153. finally
  154. ds.Free;
  155. end;
  156. end;
  157. procedure TTestBasics.TestDoubleFieldDef;
  158. var ds : TDataset;
  159. PassException : boolean;
  160. begin
  161. // If a second field with the same name is added to a TFieldDefs, an exception
  162. // should occur
  163. ds := TDataset.create(nil);
  164. try
  165. ds.FieldDefs.Add('Field1',ftInteger);
  166. PassException:=False;
  167. try
  168. ds.FieldDefs.Add('Field1',ftString,10,false)
  169. except
  170. on E: EDatabaseError do PassException := True;
  171. end;
  172. AssertTrue(PassException);
  173. finally
  174. ds.Free;
  175. end;
  176. end;
  177. procedure TTestBasics.TestFieldDefWithoutDS;
  178. var FieldDefs : TFieldDefs;
  179. begin
  180. FieldDefs := TFieldDefs.Create(nil);
  181. try
  182. FieldDefs.Add('test',ftString);
  183. finally
  184. FieldDefs.Free;
  185. end;
  186. end;
  187. procedure TTestBasics.TestGetFieldList;
  188. var
  189. ds: TDataSet;
  190. List: TList;
  191. ExceptionRaised: Boolean;
  192. begin
  193. ds := CreateDatasetWith3Fields;
  194. try
  195. List := TList.Create;
  196. try
  197. //should not
  198. List.Clear;
  199. ds.GetFieldList(List, '');
  200. AssertEquals(0, List.Count);
  201. List.Clear;
  202. ExceptionRaised := False;
  203. try
  204. ds.GetFieldList(List, ' ');
  205. except
  206. on E: EDatabaseError do ExceptionRaised := True;
  207. end;
  208. AssertTrue(ExceptionRaised);
  209. List.Clear;
  210. ds.GetFieldList(List, 'Field1');
  211. AssertEquals(1, List.Count);
  212. List.Clear;
  213. ds.GetFieldList(List, ' Field1 ');
  214. AssertEquals(1, List.Count);
  215. List.Clear;
  216. ds.GetFieldList(List, 'Field1;Field2');
  217. AssertEquals(2, List.Count);
  218. List.Clear;
  219. ds.GetFieldList(List, 'Field1;Field2;');
  220. AssertEquals(2, List.Count);
  221. List.Clear;
  222. ds.GetFieldList(List, 'Field1;Field2;Field3');
  223. AssertEquals(3, List.Count);
  224. finally
  225. List.Destroy;
  226. end;
  227. finally
  228. ds.Destroy;
  229. end;
  230. end;
  231. procedure TTestBasics.TestGetParamList;
  232. var
  233. Params: TParams;
  234. P: TParam;
  235. List: TList;
  236. ExceptionRaised: Boolean;
  237. begin
  238. Params := TParams.Create(nil);
  239. try
  240. P := TParam.Create(Params, ptInput);
  241. P.Name := 'Param1';
  242. P := TParam.Create(Params, ptInput);
  243. P.Name := 'Param2';
  244. P := TParam.Create(Params, ptInput);
  245. P.Name := 'Param3';
  246. List := TList.Create;
  247. try
  248. List.Clear;
  249. Params.GetParamList(List, '');
  250. AssertEquals(0, List.Count);
  251. List.Clear;
  252. ExceptionRaised := False;
  253. try
  254. Params.GetParamList(List, ' ');
  255. except
  256. on E: EDatabaseError do ExceptionRaised := True;
  257. end;
  258. AssertTrue(ExceptionRaised);
  259. List.Clear;
  260. Params.GetParamList(List, 'Param1');
  261. AssertEquals(1, List.Count);
  262. List.Clear;
  263. Params.GetParamList(List, ' Param1 ');
  264. AssertEquals(1, List.Count);
  265. List.Clear;
  266. Params.GetParamList(List, 'Param1;');
  267. AssertEquals(1, List.Count);
  268. List.Clear;
  269. Params.GetParamList(List, 'Param1;Param2');
  270. AssertEquals(2, List.Count);
  271. List.Clear;
  272. Params.GetParamList(List, 'Param1;Param2;Param3');
  273. AssertEquals(3, List.Count);
  274. finally
  275. List.Destroy;
  276. end;
  277. finally
  278. Params.Destroy;
  279. end;
  280. end;
  281. procedure TTestBasics.TestExtractFieldName;
  282. var
  283. i: Integer;
  284. Fields: String;
  285. FieldName: String;
  286. begin
  287. Fields := '';
  288. i := 1;
  289. FieldName := ExtractFieldName(Fields, i);
  290. AssertEquals(1, i);
  291. AssertEquals('', FieldName);
  292. Fields := 'test';
  293. i := 1;
  294. FieldName := ExtractFieldName(Fields, i);
  295. AssertEquals(5, i);
  296. AssertEquals('test', FieldName);
  297. Fields := 'test;';
  298. i := 1;
  299. FieldName := ExtractFieldName(Fields, i);
  300. AssertEquals(6, i);
  301. AssertEquals('test', FieldName);
  302. Fields := ' test ';
  303. i := 1;
  304. FieldName := ExtractFieldName(Fields, i);
  305. AssertEquals(7, i);
  306. AssertEquals('test', FieldName);
  307. Fields := 'test;xxx';
  308. i := 1;
  309. FieldName := ExtractFieldName(Fields, i);
  310. AssertEquals(6, i);
  311. AssertEquals('test', FieldName);
  312. FieldName := ExtractFieldName(Fields, i);
  313. AssertEquals(9, i);
  314. AssertEquals('xxx', FieldName);
  315. end;
  316. procedure TTestBasics.TestCheckFieldNames;
  317. var
  318. ds: TDataSet;
  319. ExceptionRaised: Boolean;
  320. begin
  321. ds := CreateDatasetWith3Fields;
  322. try
  323. ExceptionRaised := False;
  324. try
  325. ds.Fields.CheckFieldNames('');
  326. except
  327. ExceptionRaised := True;
  328. end;
  329. AssertFalse(ExceptionRaised);
  330. ExceptionRaised := False;
  331. try
  332. ds.Fields.CheckFieldNames('Field1;Field2');
  333. except
  334. ExceptionRaised := True;
  335. end;
  336. AssertFalse(ExceptionRaised);
  337. ExceptionRaised := False;
  338. try
  339. ds.Fields.CheckFieldNames('Field1;NonExistentField');
  340. except
  341. ExceptionRaised := True;
  342. end;
  343. AssertTrue(ExceptionRaised);
  344. finally
  345. ds.Destroy;
  346. end;
  347. end;
  348. procedure TTestBasics.TestFindField;
  349. var
  350. ds: TDataSet;
  351. F: TField;
  352. begin
  353. ds := CreateDatasetWith3Fields;
  354. try
  355. F := ds.FindField('');
  356. AssertTrue(F = nil);
  357. F := ds.FindField('field3');
  358. AssertTrue(F <> nil);
  359. F := ds.FindField('NonExistentField');
  360. AssertTrue(F = nil);
  361. finally
  362. ds.Destroy;
  363. end;
  364. end;
  365. initialization
  366. RegisterTest(TTestBasics);
  367. end.