testbasics.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. end;
  17. implementation
  18. uses db, toolsunit;
  19. { TTestBasics }
  20. procedure TTestBasics.TestParseSQL;
  21. var Params : TParams;
  22. ReplStr : string;
  23. pb : TParamBinding;
  24. begin
  25. Params := TParams.Create;
  26. AssertEquals( 'select * from table where id = $1',
  27. params.ParseSQL('select * from table where id = :id',true,True,True,psPostgreSQL));
  28. AssertEquals( 'select * from table where id = $1',
  29. params.ParseSQL('select * from table where id = :id',false,True,True,psPostgreSQL));
  30. 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)',
  31. 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));
  32. 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'')',
  33. 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));
  34. 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'')',
  35. 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));
  36. AssertEquals( 'select * from table where id = $1',
  37. params.ParseSQL('select * from table where id = :id',true,true,false,psSimulated,pb,ReplStr));
  38. AssertEquals('$',ReplStr);
  39. 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)',
  40. 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));
  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 = $$3) and (test=''$test'')',
  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 = :par3) and (test=''$test'')',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 10=$$10 11=$$11 12=$$5 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 10=:par10 11=:11 12=:par5 where (id = :par3) and (test=''$test'')',true,True,True,psSimulated));
  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,False,psSimulated,pb,ReplStr));
  50. AssertEquals('$$$',ReplStr);
  51. AssertEquals( 'select * from table where id = ?',
  52. params.ParseSQL('select * from table where id = :id',true,true,true,psInterbase));
  53. // Test escape-sequences:
  54. AssertEquals( 'select * from table where ''id '''' = :id''',
  55. params.ParseSQL('select * from table where ''id '''' = :id''',true,False,True,psPostgreSQL));
  56. AssertEquals( 'select * from table where "id "" = :id"',
  57. params.ParseSQL('select * from table where "id "" = :id"',true,False,True,psPostgreSQL));
  58. AssertEquals( 'select * from table where "id \" = :id"',
  59. params.ParseSQL('select * from table where "id \" = :id"',true,True,False,psPostgreSQL));
  60. AssertEquals( 'select * from table where "id \" = $1',
  61. params.ParseSQL('select * from table where "id \" = :id',true,False,False,psPostgreSQL));
  62. AssertEquals( 'select * from table where "id = :id\',
  63. params.ParseSQL('select * from table where "id = :id\',true,True,True,psInterbase));
  64. Params.Free;
  65. end;
  66. initialization
  67. RegisterTest(TTestBasics);
  68. end.