MySqlTestBed.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // MySqlTestBed.cs : This is base class which manages the connections to
  3. // mysql database. This serves as a base class for all
  4. // mysql database dependant tests.
  5. //
  6. // To run :
  7. // * create a test database in mysql server.
  8. // * create an DNS entry.
  9. // * update the MySqlTestBed.config with the DNS names and
  10. // username, password for connection in the configuration key
  11. // MySql-DSN
  12. // * compile using following command
  13. // mcs /r:System.Data.dll,NUnit.Framework.dll /t:library /debug
  14. // /out:MySqlTestBed.dll MySqlTestBed.cs System.Data.Odbc/*.cs
  15. // * To run the tests
  16. // mono /usr/local/bin/nunit-console.exe MySqlTestBed.dll
  17. //
  18. // Author:
  19. // Sureshkumar T ([email protected])
  20. //
  21. // Copyright (c) 2004 Novell Inc., and the individuals listed
  22. // on the ChangeLog entries.
  23. //
  24. // Permission is hereby granted, free of charge, to any person obtaining
  25. // a copy of this software and associated documentation files (the
  26. // "Software"), to deal in the Software without restriction, including
  27. // without limitation the rights to use, copy, modify, merge, publish,
  28. // distribute, sublicense, and/or sell copies of the Software, and to
  29. // permit persons to whom the Software is furnished to do so, subject to
  30. // the following conditions:
  31. //
  32. // The above copyright notice and this permission notice shall be
  33. // included in all copies or substantial portions of the Software.
  34. //
  35. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  36. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  37. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  38. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  39. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  40. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  41. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  42. //
  43. using System;
  44. using System.Data;
  45. using System.Configuration;
  46. using System.Data.Odbc;
  47. using System.Collections.Specialized;
  48. namespace MonoTests.System.Data
  49. {
  50. public class MySqlOdbcBaseClient
  51. {
  52. #region protected members
  53. protected string connectionString = null;
  54. protected OdbcConnection conn = null;
  55. protected bool isConnAlive = false;
  56. #endregion
  57. public MySqlOdbcBaseClient ()
  58. {
  59. //Connection String with DSN.
  60. NameValueCollection appSettings = ConfigurationSettings.AppSettings ;
  61. connectionString = appSettings ["MySql-DSN"];
  62. conn = new OdbcConnection (connectionString);
  63. }
  64. protected void OpenConnection ()
  65. {
  66. conn.ConnectionString = connectionString;
  67. conn.Open ();
  68. // run tests only if the connection is open,
  69. // otherwise make it fail, to setup with correct
  70. // database settings
  71. if (conn != null && conn.State != ConnectionState.Closed)
  72. isConnAlive = true;
  73. }
  74. protected void CloseConnection ()
  75. {
  76. if (conn != null && conn.State != ConnectionState.Closed) {
  77. conn.Close ();
  78. isConnAlive = false;
  79. }
  80. }
  81. protected void CreateTestSetup ()
  82. {
  83. if (!isConnAlive)
  84. return ;
  85. // Create test database & tables
  86. // mysql odbc does not supports batch sql statements
  87. string createQuery = "DROP TABLE IF EXISTS test;" ;
  88. ExecuteQuery (createQuery);
  89. createQuery = "CREATE TABLE test (" +
  90. "pk_tint TINYINT NOT NULL PRIMARY KEY," +
  91. "col_char CHAR(20)," +
  92. "col_int INT," +
  93. "col_blob TINYBLOB," +
  94. "col_datetime DATETIME," +
  95. "col_date DATE," +
  96. "col_time TIME" +
  97. ");";
  98. ExecuteQuery (createQuery);
  99. createQuery = "INSERT INTO test VALUES (1, 'mono test" +
  100. "#1', 255, 127123645917568585638457243856234985, '2004-08-22', '2004-08-22', '12:00:00' );" ;
  101. ExecuteQuery (createQuery);
  102. createQuery = "INSERT INTO test VALUES (2, 'mono test" +
  103. "#2', 256, NULL, NULL, NULL, NULL );";
  104. ExecuteQuery (createQuery);
  105. createQuery = "INSERT INTO test VALUES (3, 'mono test" +
  106. "#3', 257 , 127123645917568585638457243856234985, '2004-08-22', '2004-08-22', '12:00:00');" ;
  107. ExecuteQuery (createQuery);
  108. createQuery = "INSERT INTO test VALUES (4, 'mono test" +
  109. "#4', 258 , 127123645917568585638457243856234985, '2004-08-22', '2004-08-22', '12:00:00');" ;
  110. ExecuteQuery (createQuery);
  111. createQuery = "INSERT INTO test VALUES (5, 'mono test" +
  112. "#5', 259, 127123645917568585638457243856234985, '2004-08-22', '2004-08-22', '12:00:00' );" ;
  113. ExecuteQuery (createQuery);
  114. }
  115. private void ExecuteQuery (string query)
  116. {
  117. OdbcCommand cmd = new OdbcCommand ();
  118. cmd.Connection = conn;
  119. cmd.CommandText = query;
  120. try {
  121. int recordsAff = cmd.ExecuteNonQuery ();
  122. } catch (Exception e) {
  123. }
  124. }
  125. protected void CleanTestSetup ()
  126. {
  127. if (!isConnAlive)
  128. return;
  129. // delete test database
  130. string dropQuery = "DROP table IF EXISTS test";
  131. //ExecuteQuery(dropQuery);
  132. }
  133. }
  134. }