testsqldb.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 TestMasterDetail;
  22. procedure TestUpdateServerIndexDefs;
  23. end;
  24. { TTestTSQLConnection }
  25. TTestTSQLConnection = class(TSQLDBTestCase)
  26. private
  27. published
  28. procedure ReplaceMe;
  29. end;
  30. { TTestTSQLScript }
  31. TTestTSQLScript = class(TSQLDBTestCase)
  32. published
  33. procedure TestExecuteScript;
  34. end;
  35. implementation
  36. uses sqldbtoolsunit, toolsunit, sqldb;
  37. { TTestTSQLQuery }
  38. procedure TTestTSQLQuery.TestMasterDetail;
  39. var MasterQuery, DetailQuery: TSQLQuery;
  40. MasterSource: TDataSource;
  41. begin
  42. with TSQLDBConnector(DBConnector) do
  43. try
  44. MasterQuery := GetNDataset(10) as TSQLQuery;
  45. MasterSource := TDatasource.Create(nil);
  46. MasterSource.DataSet := MasterQuery;
  47. DetailQuery := Query;
  48. DetailQuery.SQL.Text := 'select NAME from FPDEV where ID=:ID';
  49. DetailQuery.DataSource := MasterSource;
  50. MasterQuery.Open;
  51. DetailQuery.Open;
  52. CheckEquals('TestName1', DetailQuery.Fields[0].AsString);
  53. MasterQuery.MoveBy(3);
  54. CheckEquals('TestName4', DetailQuery.Fields[0].AsString);
  55. finally
  56. MasterSource.Free;
  57. end;
  58. end;
  59. procedure TTestTSQLQuery.TestUpdateServerIndexDefs;
  60. var Q: TSQLQuery;
  61. name1, name2, name3: string;
  62. begin
  63. // Test retrieval of information about indexes on unquoted and quoted table names
  64. // (tests also case-sensitivity for DB's that support case-sensitivity of quoted identifiers)
  65. // For ODBC Firebird/Interbase we must define primary key as named constraint and
  66. // in ODBC driver must be set: "quoted identifiers" and "sensitive identifier"
  67. // See also: TTestFieldTypes.TestUpdateIndexDefs
  68. with TSQLDBConnector(DBConnector) do
  69. begin
  70. // SQLite ignores case-sensitivity of quoted table names
  71. // MS SQL Server case-sensitivity of identifiers depends on the case-sensitivity of default collation of the database
  72. // MySQL case-sensitivity depends on case-sensitivity of server's file system
  73. if SQLServerType in [ssMSSQL,ssSQLite{$IFDEF WINDOWS},ssMySQL{$ENDIF}] then
  74. name1 := Connection.FieldNameQuoteChars[0]+'fpdev 2'+Connection.FieldNameQuoteChars[1]
  75. else
  76. name1 := 'FPDEV2';
  77. ExecuteDirect('create table '+name1+' (id integer not null, constraint PK_FPDEV21 primary key(id))');
  78. // same but quoted table name
  79. name2 := Connection.FieldNameQuoteChars[0]+'FPdev2'+Connection.FieldNameQuoteChars[1];
  80. ExecuteDirect('create table '+name2+' (ID2 integer not null, constraint PK_FPDEV22 primary key(ID2))');
  81. // embedded quote in table name
  82. if SQLServerType in [ssMySQL] then
  83. name3 := '`FPdev``2`'
  84. else
  85. name3 := Connection.FieldNameQuoteChars[0]+'FPdev""2'+Connection.FieldNameQuoteChars[1];
  86. ExecuteDirect('create table '+name3+' (Id3 integer not null, constraint PK_FPDEV23 primary key(Id3))');
  87. CommitDDL;
  88. end;
  89. try
  90. Q := TSQLDBConnector(DBConnector).Query;
  91. Q.SQL.Text:='select * from '+name1;
  92. Q.Prepare;
  93. Q.ServerIndexDefs.Update;
  94. CheckEquals(1, Q.ServerIndexDefs.Count);
  95. Q.SQL.Text:='select * from '+name2;
  96. Q.Prepare;
  97. Q.ServerIndexDefs.Update;
  98. CheckEquals(1, Q.ServerIndexDefs.Count, '2.1');
  99. CheckTrue(CompareText('ID2', Q.ServerIndexDefs[0].Fields)=0, '2.2'+Q.ServerIndexDefs[0].Fields);
  100. CheckTrue(Q.ServerIndexDefs[0].Options=[ixPrimary,ixUnique], '2.3');
  101. Q.SQL.Text:='select * from '+name3;
  102. Q.Prepare;
  103. Q.ServerIndexDefs.Update;
  104. CheckEquals(1, Q.ServerIndexDefs.Count, '3.1');
  105. CheckTrue(CompareText('ID3', Q.ServerIndexDefs[0].Fields)=0, '3.2');
  106. CheckTrue(Q.ServerIndexDefs[0].Options=[ixPrimary,ixUnique], '3.3');
  107. finally
  108. Q.UnPrepare;
  109. with TSQLDBConnector(DBConnector) do
  110. begin
  111. ExecuteDirect('DROP TABLE '+name1);
  112. ExecuteDirect('DROP TABLE '+name2);
  113. ExecuteDirect('DROP TABLE '+name3);
  114. CommitDDL;
  115. end;
  116. end;
  117. end;
  118. { TTestTSQLConnection }
  119. procedure TTestTSQLConnection.ReplaceMe;
  120. begin
  121. // replace this procedure with any test for TSQLConnection
  122. end;
  123. { TTestTSQLScript }
  124. procedure TTestTSQLScript.TestExecuteScript;
  125. var Ascript : TSQLScript;
  126. begin
  127. Ascript := TSQLScript.Create(nil);
  128. try
  129. with Ascript do
  130. begin
  131. DataBase := TSQLDBConnector(DBConnector).Connection;
  132. Transaction := TSQLDBConnector(DBConnector).Transaction;
  133. Script.Clear;
  134. Script.Append('create table a (id int);');
  135. Script.Append('create table b (id int);');
  136. ExecuteScript;
  137. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  138. TSQLDBConnector(DBConnector).CommitDDL;
  139. end;
  140. finally
  141. AScript.Free;
  142. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('drop table a');
  143. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('drop table b');
  144. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  145. TSQLDBConnector(DBConnector).CommitDDL;
  146. end;
  147. end;
  148. { TSQLDBTestCase }
  149. procedure TSQLDBTestCase.SetUp;
  150. begin
  151. inherited SetUp;
  152. InitialiseDBConnector;
  153. DBConnector.StartTest;
  154. end;
  155. procedure TSQLDBTestCase.TearDown;
  156. begin
  157. DBConnector.StopTest;
  158. if assigned(DBConnector) then
  159. with TSQLDBConnector(DBConnector) do
  160. Transaction.Rollback;
  161. FreeDBConnector;
  162. inherited TearDown;
  163. end;
  164. initialization
  165. if uppercase(dbconnectorname)='SQL' then
  166. begin
  167. RegisterTest(TTestTSQLQuery);
  168. RegisterTest(TTestTSQLConnection);
  169. RegisterTest(TTestTSQLScript);
  170. end;
  171. end.