testsqldb.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. unit TestSQLDB;
  2. {
  3. Unit tests which are specific to the sqlDB components like TSQLQuery, TSQLConnection.
  4. }
  5. {$mode objfpc}{$H+}
  6. interface
  7. uses
  8. Classes, SysUtils, fpcunit, testregistry,
  9. db;
  10. type
  11. { TSQLDBTestCase }
  12. TSQLDBTestCase = class(TTestCase)
  13. protected
  14. procedure SetUp; override;
  15. procedure TearDown; override;
  16. end;
  17. { TTestTSQLQuery }
  18. TTestTSQLQuery = class(TSQLDBTestCase)
  19. private
  20. published
  21. procedure TestUpdateServerIndexDefs;
  22. end;
  23. { TTestTSQLConnection }
  24. TTestTSQLConnection = class(TSQLDBTestCase)
  25. private
  26. published
  27. procedure ReplaceMe;
  28. end;
  29. { TTestTSQLScript }
  30. TTestTSQLScript = class(TSQLDBTestCase)
  31. published
  32. procedure TestExecuteScript;
  33. end;
  34. implementation
  35. uses sqldbtoolsunit, toolsunit, sqldb;
  36. { TTestTSQLQuery }
  37. procedure TTestTSQLQuery.TestUpdateServerIndexDefs;
  38. var Q: TSQLQuery;
  39. name1, name2, name3: string;
  40. begin
  41. // Test retrieval of information about indexes on unquoted and quoted table names
  42. // (tests also case-sensitivity for DB's that support case-sensitivity of quoted identifiers)
  43. // For ODBC Firebird/Interbase we must define primary key as named constraint and
  44. // in ODBC driver must be set: "quoted identifiers" and "sensitive identifier"
  45. // See also: TTestFieldTypes.TestUpdateIndexDefs
  46. with TSQLDBConnector(DBConnector) do
  47. begin
  48. // SQLite ignores case-sensitivity of quoted table names
  49. // MS SQL Server case-sensitivity of identifiers depends on the case-sensitivity of default collation of the database
  50. // MySQL case-sensitivity depends on case-sensitivity of server's file system
  51. if SQLServerType in [ssMSSQL,ssSQLite{$IFDEF WINDOWS},ssMySQL{$ENDIF}] then
  52. name1 := Connection.FieldNameQuoteChars[0]+'fpdev 2'+Connection.FieldNameQuoteChars[1]
  53. else
  54. name1 := 'FPDEV2';
  55. ExecuteDirect('create table '+name1+' (id integer not null, constraint PK_FPDEV21 primary key(id))');
  56. // same but quoted table name
  57. name2 := Connection.FieldNameQuoteChars[0]+'FPdev2'+Connection.FieldNameQuoteChars[1];
  58. ExecuteDirect('create table '+name2+' (ID2 integer not null, constraint PK_FPDEV22 primary key(ID2))');
  59. // embedded quote in table name
  60. if SQLServerType in [ssMySQL] then
  61. name3 := '`FPdev``2`'
  62. else
  63. name3 := Connection.FieldNameQuoteChars[0]+'FPdev""2'+Connection.FieldNameQuoteChars[1];
  64. ExecuteDirect('create table '+name3+' (Id3 integer not null, constraint PK_FPDEV23 primary key(Id3))');
  65. CommitDDL;
  66. end;
  67. try
  68. Q := TSQLDBConnector(DBConnector).Query;
  69. Q.SQL.Text:='select * from '+name1;
  70. Q.Prepare;
  71. Q.ServerIndexDefs.Update;
  72. CheckEquals(1, Q.ServerIndexDefs.Count);
  73. Q.SQL.Text:='select * from '+name2;
  74. Q.Prepare;
  75. Q.ServerIndexDefs.Update;
  76. CheckEquals(1, Q.ServerIndexDefs.Count, '2.1');
  77. CheckTrue(CompareText('ID2', Q.ServerIndexDefs[0].Fields)=0, '2.2'+Q.ServerIndexDefs[0].Fields);
  78. CheckTrue(Q.ServerIndexDefs[0].Options=[ixPrimary,ixUnique], '2.3');
  79. Q.SQL.Text:='select * from '+name3;
  80. Q.Prepare;
  81. Q.ServerIndexDefs.Update;
  82. CheckEquals(1, Q.ServerIndexDefs.Count, '3.1');
  83. CheckTrue(CompareText('ID3', Q.ServerIndexDefs[0].Fields)=0, '3.2');
  84. CheckTrue(Q.ServerIndexDefs[0].Options=[ixPrimary,ixUnique], '3.3');
  85. finally
  86. Q.UnPrepare;
  87. with TSQLDBConnector(DBConnector) do
  88. begin
  89. ExecuteDirect('DROP TABLE '+name1);
  90. ExecuteDirect('DROP TABLE '+name2);
  91. ExecuteDirect('DROP TABLE '+name3);
  92. CommitDDL;
  93. end;
  94. end;
  95. end;
  96. { TTestTSQLConnection }
  97. procedure TTestTSQLConnection.ReplaceMe;
  98. begin
  99. // replace this procedure with any test for TSQLConnection
  100. end;
  101. { TTestTSQLScript }
  102. procedure TTestTSQLScript.TestExecuteScript;
  103. var Ascript : TSQLScript;
  104. begin
  105. Ascript := TSQLScript.Create(nil);
  106. try
  107. with Ascript do
  108. begin
  109. DataBase := TSQLDBConnector(DBConnector).Connection;
  110. Transaction := TSQLDBConnector(DBConnector).Transaction;
  111. Script.Clear;
  112. Script.Append('create table a (id int);');
  113. Script.Append('create table b (id int);');
  114. ExecuteScript;
  115. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  116. TSQLDBConnector(DBConnector).CommitDDL;
  117. end;
  118. finally
  119. AScript.Free;
  120. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('drop table a');
  121. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('drop table b');
  122. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  123. TSQLDBConnector(DBConnector).CommitDDL;
  124. end;
  125. end;
  126. { TSQLDBTestCase }
  127. procedure TSQLDBTestCase.SetUp;
  128. begin
  129. inherited SetUp;
  130. InitialiseDBConnector;
  131. DBConnector.StartTest;
  132. end;
  133. procedure TSQLDBTestCase.TearDown;
  134. begin
  135. DBConnector.StopTest;
  136. if assigned(DBConnector) then
  137. with TSQLDBConnector(DBConnector) do
  138. Transaction.Rollback;
  139. FreeDBConnector;
  140. inherited TearDown;
  141. end;
  142. initialization
  143. if uppercase(dbconnectorname)='SQL' then
  144. begin
  145. RegisterTest(TTestTSQLQuery);
  146. RegisterTest(TTestTSQLConnection);
  147. RegisterTest(TTestTSQLScript);
  148. end;
  149. end.