testbasics.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. Params.Free;
  114. end;
  115. procedure TTestBasics.TestInitFielddefsFromFields;
  116. var ds : TDataset;
  117. F1,F2,F3 : Tfield;
  118. Procedure CompareFieldAndFieldDef(Fld: TField; FldDef : TFieldDef);
  119. begin
  120. AssertEquals(Fld.FieldName,FldDef.Name);
  121. AssertEquals(Fld.Size,FldDef.Size);
  122. AssertEquals(Fld.Required,FldDef.Required);
  123. AssertTrue(Fld.DataType=FldDef.DataType);
  124. end;
  125. begin
  126. ds := TDataset.Create(nil);
  127. try
  128. F1:=TStringField.Create(ds);
  129. F1.Size := 10;
  130. F1.Name := 'StringFld';
  131. F1.FieldName := 'FStringFld';
  132. F1.Required := false;
  133. F1.Dataset:=ds;
  134. F2:=TIntegerField.Create(ds);
  135. F2.Name := 'IntegerFld';
  136. F2.FieldName := 'FIntegerFld';
  137. F2.Required := True;
  138. F2.Dataset:=ds;
  139. F3:=TBCDField.Create(ds);
  140. F3.Name := 'BCDFld';
  141. F3.FieldName := 'FBCDFld';
  142. F3.Required := false;
  143. F3.Dataset:=ds;
  144. (f3 as TBCDField).Precision := 2;
  145. HackedDataset(ds).InitFieldDefsFromfields;
  146. AssertEquals(3,ds.FieldDefs.Count);
  147. CompareFieldAndFieldDef(F1,ds.FieldDefs[0]);
  148. CompareFieldAndFieldDef(F2,ds.FieldDefs[1]);
  149. CompareFieldAndFieldDef(F3,ds.FieldDefs[2]);
  150. finally
  151. ds.Free;
  152. end;
  153. end;
  154. procedure TTestBasics.TestDoubleFieldDef;
  155. var ds : TDataset;
  156. PassException : boolean;
  157. begin
  158. // If a second field with the same name is added to a TFieldDefs, an exception
  159. // should occur
  160. ds := TDataset.create(nil);
  161. try
  162. ds.FieldDefs.Add('Field1',ftInteger);
  163. PassException:=False;
  164. try
  165. ds.FieldDefs.Add('Field1',ftString,10,false)
  166. except
  167. on E: EDatabaseError do PassException := True;
  168. end;
  169. AssertTrue(PassException);
  170. finally
  171. ds.Free;
  172. end;
  173. end;
  174. procedure TTestBasics.TestFieldDefWithoutDS;
  175. var FieldDefs : TFieldDefs;
  176. begin
  177. FieldDefs := TFieldDefs.Create(nil);
  178. try
  179. FieldDefs.Add('test',ftString);
  180. finally
  181. FieldDefs.Free;
  182. end;
  183. end;
  184. procedure TTestBasics.TestGetFieldList;
  185. var
  186. ds: TDataSet;
  187. List: TList;
  188. ExceptionRaised: Boolean;
  189. begin
  190. ds := CreateDatasetWith3Fields;
  191. try
  192. List := TList.Create;
  193. try
  194. //should not
  195. List.Clear;
  196. ds.GetFieldList(List, '');
  197. AssertEquals(0, List.Count);
  198. List.Clear;
  199. ExceptionRaised := False;
  200. try
  201. ds.GetFieldList(List, ' ');
  202. except
  203. on E: EDatabaseError do ExceptionRaised := True;
  204. end;
  205. AssertTrue(ExceptionRaised);
  206. List.Clear;
  207. ds.GetFieldList(List, 'Field1');
  208. AssertEquals(1, List.Count);
  209. List.Clear;
  210. ds.GetFieldList(List, ' Field1 ');
  211. AssertEquals(1, List.Count);
  212. List.Clear;
  213. ds.GetFieldList(List, 'Field1;Field2');
  214. AssertEquals(2, 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;Field3');
  220. AssertEquals(3, List.Count);
  221. finally
  222. List.Destroy;
  223. end;
  224. finally
  225. ds.Destroy;
  226. end;
  227. end;
  228. procedure TTestBasics.TestGetParamList;
  229. var
  230. Params: TParams;
  231. P: TParam;
  232. List: TList;
  233. ExceptionRaised: Boolean;
  234. begin
  235. Params := TParams.Create(nil);
  236. try
  237. P := TParam.Create(Params, ptInput);
  238. P.Name := 'Param1';
  239. P := TParam.Create(Params, ptInput);
  240. P.Name := 'Param2';
  241. P := TParam.Create(Params, ptInput);
  242. P.Name := 'Param3';
  243. List := TList.Create;
  244. try
  245. List.Clear;
  246. Params.GetParamList(List, '');
  247. AssertEquals(0, List.Count);
  248. List.Clear;
  249. ExceptionRaised := False;
  250. try
  251. Params.GetParamList(List, ' ');
  252. except
  253. on E: EDatabaseError do ExceptionRaised := True;
  254. end;
  255. AssertTrue(ExceptionRaised);
  256. List.Clear;
  257. Params.GetParamList(List, 'Param1');
  258. AssertEquals(1, List.Count);
  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;Param2');
  267. AssertEquals(2, List.Count);
  268. List.Clear;
  269. Params.GetParamList(List, 'Param1;Param2;Param3');
  270. AssertEquals(3, List.Count);
  271. finally
  272. List.Destroy;
  273. end;
  274. finally
  275. Params.Destroy;
  276. end;
  277. end;
  278. procedure TTestBasics.TestExtractFieldName;
  279. var
  280. i: Integer;
  281. Fields: String;
  282. FieldName: String;
  283. begin
  284. Fields := '';
  285. i := 1;
  286. FieldName := ExtractFieldName(Fields, i);
  287. AssertEquals(1, i);
  288. AssertEquals('', FieldName);
  289. Fields := 'test';
  290. i := 1;
  291. FieldName := ExtractFieldName(Fields, i);
  292. AssertEquals(5, i);
  293. AssertEquals('test', FieldName);
  294. Fields := 'test;';
  295. i := 1;
  296. FieldName := ExtractFieldName(Fields, i);
  297. AssertEquals(6, i);
  298. AssertEquals('test', FieldName);
  299. Fields := ' test ';
  300. i := 1;
  301. FieldName := ExtractFieldName(Fields, i);
  302. AssertEquals(7, i);
  303. AssertEquals('test', FieldName);
  304. Fields := 'test;xxx';
  305. i := 1;
  306. FieldName := ExtractFieldName(Fields, i);
  307. AssertEquals(6, i);
  308. AssertEquals('test', FieldName);
  309. FieldName := ExtractFieldName(Fields, i);
  310. AssertEquals(9, i);
  311. AssertEquals('xxx', FieldName);
  312. end;
  313. procedure TTestBasics.TestCheckFieldNames;
  314. var
  315. ds: TDataSet;
  316. ExceptionRaised: Boolean;
  317. begin
  318. ds := CreateDatasetWith3Fields;
  319. try
  320. ExceptionRaised := False;
  321. try
  322. ds.Fields.CheckFieldNames('');
  323. except
  324. ExceptionRaised := True;
  325. end;
  326. AssertFalse(ExceptionRaised);
  327. ExceptionRaised := False;
  328. try
  329. ds.Fields.CheckFieldNames('Field1;Field2');
  330. except
  331. ExceptionRaised := True;
  332. end;
  333. AssertFalse(ExceptionRaised);
  334. ExceptionRaised := False;
  335. try
  336. ds.Fields.CheckFieldNames('Field1;NonExistentField');
  337. except
  338. ExceptionRaised := True;
  339. end;
  340. AssertTrue(ExceptionRaised);
  341. finally
  342. ds.Destroy;
  343. end;
  344. end;
  345. procedure TTestBasics.TestFindField;
  346. var
  347. ds: TDataSet;
  348. F: TField;
  349. begin
  350. ds := CreateDatasetWith3Fields;
  351. try
  352. F := ds.FindField('');
  353. AssertTrue(F = nil);
  354. F := ds.FindField('field3');
  355. AssertTrue(F <> nil);
  356. F := ds.FindField('NonExistentField');
  357. AssertTrue(F = nil);
  358. finally
  359. ds.Destroy;
  360. end;
  361. end;
  362. initialization
  363. RegisterTest(TTestBasics);
  364. end.