testbasics.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. unit TestBasics;
  2. {$IFDEF FPC}
  3. {$mode objfpc}{$H+}
  4. {$ENDIF}
  5. interface
  6. uses
  7. fpcunit, testutils, testregistry, testdecorator,
  8. Classes, SysUtils;
  9. type
  10. { TTestBasics }
  11. TTestBasics = class(TTestCase)
  12. private
  13. protected
  14. published
  15. procedure TestParseSQL;
  16. procedure TestInitFielddefsFromFields;
  17. procedure TestDoubleFieldDef;
  18. end;
  19. implementation
  20. uses db, toolsunit;
  21. Type HackedDataset = class(TDataset);
  22. { TTestBasics }
  23. procedure TTestBasics.TestParseSQL;
  24. var Params : TParams;
  25. ReplStr : string;
  26. pb : TParamBinding;
  27. begin
  28. Params := TParams.Create;
  29. AssertEquals( 'select * from table where id = $1',
  30. params.ParseSQL('select * from table where id = :id',true,True,True,psPostgreSQL));
  31. AssertEquals( 'select * from table where id = $1',
  32. params.ParseSQL('select * from table where id = :id',false,True,True,psPostgreSQL));
  33. 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)',
  34. 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));
  35. 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'')',
  36. 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));
  37. 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'')',
  38. 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));
  39. AssertEquals( 'select * from table where id = $1',
  40. params.ParseSQL('select * from table where id = :id',true,true,false,psSimulated,pb,ReplStr));
  41. AssertEquals('$',ReplStr);
  42. 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)',
  43. 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));
  44. AssertEquals('$',ReplStr);
  45. 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'')',
  46. 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));
  47. AssertEquals('$$',ReplStr);
  48. 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'')',
  49. 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));
  50. AssertEquals('$$',ReplStr);
  51. 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'')',
  52. 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));
  53. AssertEquals('$$$',ReplStr);
  54. AssertEquals( 'select * from table where id = ?',
  55. params.ParseSQL('select * from table where id = :id',true,true,true,psInterbase));
  56. // Test bug 10345
  57. AssertEquals( 'select email from table where upper(email) like ''%''||?||''%''',
  58. params.ParseSQL('select email from table where upper(email) like ''%''||:email||''%''',true,true,true,psInterbase));
  59. // Test escape-sequences:
  60. AssertEquals( 'select * from table where ''id '''' = :id''',
  61. params.ParseSQL('select * from table where ''id '''' = :id''',true,False,True,psPostgreSQL));
  62. AssertEquals( 'select * from table where "id "" = :id"',
  63. params.ParseSQL('select * from table where "id "" = :id"',true,False,True,psPostgreSQL));
  64. AssertEquals( 'select * from table where "id \" = :id"',
  65. params.ParseSQL('select * from table where "id \" = :id"',true,True,False,psPostgreSQL));
  66. AssertEquals( 'select * from table where "id \" = $1',
  67. params.ParseSQL('select * from table where "id \" = :id',true,False,False,psPostgreSQL));
  68. AssertEquals( 'select * from table where "id = :id\',
  69. params.ParseSQL('select * from table where "id = :id\',true,True,True,psInterbase));
  70. // Test strange-field names
  71. AssertEquals( 'select * from table where "field-name" = ?',
  72. params.ParseSQL('select * from table where "field-name" = :"field-name"',true,True,True,psInterbase));
  73. AssertEquals('field-name',Params.Items[0].Name);
  74. AssertEquals( 'select * from table where "field-name" = ?',
  75. params.ParseSQL('select * from table where "field-name" = :"field-name',true,True,True,psInterbase));
  76. Params.Free;
  77. end;
  78. procedure TTestBasics.TestInitFielddefsFromFields;
  79. var ds : TDataset;
  80. F1,F2,F3 : Tfield;
  81. Procedure CompareFieldAndFieldDef(Fld: TField; FldDef : TFieldDef);
  82. begin
  83. AssertEquals(Fld.FieldName,FldDef.Name);
  84. AssertEquals(Fld.Size,FldDef.Size);
  85. AssertEquals(Fld.Required,FldDef.Required);
  86. AssertTrue(Fld.DataType=FldDef.DataType);
  87. end;
  88. begin
  89. ds := TDataset.Create(nil);
  90. F1:=TStringField.Create(ds);
  91. F1.Size := 10;
  92. F1.Name := 'StringFld';
  93. F1.FieldName := 'FStringFld';
  94. F1.Required := false;
  95. F1.Dataset:=ds;
  96. F2:=TIntegerField.Create(ds);
  97. F2.Name := 'IntegerFld';
  98. F2.FieldName := 'FIntegerFld';
  99. F2.Required := True;
  100. F2.Dataset:=ds;
  101. F3:=TBCDField.Create(ds);
  102. F3.Name := 'BCDFld';
  103. F3.FieldName := 'FBCDFld';
  104. F3.Required := false;
  105. F3.Dataset:=ds;
  106. (f3 as TBCDField).Precision := 2;
  107. HackedDataset(ds).InitFieldDefsFromfields;
  108. AssertEquals(3,ds.FieldDefs.Count);
  109. CompareFieldAndFieldDef(F1,ds.FieldDefs[0]);
  110. CompareFieldAndFieldDef(F2,ds.FieldDefs[1]);
  111. CompareFieldAndFieldDef(F3,ds.FieldDefs[2]);
  112. end;
  113. procedure TTestBasics.TestDoubleFieldDef;
  114. var ds : TDataset;
  115. PassException : boolean;
  116. begin
  117. // If a second field with the same name is added to a TFieldDefs, an exception
  118. // should occur
  119. ds := TDataset.create(nil);
  120. ds.FieldDefs.Add('Field1',ftInteger);
  121. PassException:=False;
  122. try
  123. ds.FieldDefs.Add('Field1',ftString,10,false)
  124. except
  125. on E: EDatabaseError do PassException := True;
  126. end;
  127. AssertTrue(PassException);
  128. end;
  129. initialization
  130. RegisterTest(TTestBasics);
  131. end.