OdbcDataAdapterTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // OdbcDataAdapterTest.cs - NUnit Test Cases for testing the
  2. // OdbcDataAdapter class
  3. // Author:
  4. // Sureshkumar T ([email protected])
  5. //
  6. // Copyright (c) 2004 Novell Inc., and the individuals listed
  7. // on the ChangeLog entries.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if !NO_ODBC
  29. using System;
  30. using System.Data;
  31. using System.Data.Odbc;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Data.Connected.Odbc
  34. {
  35. [TestFixture]
  36. [Category ("odbc")]
  37. public class OdbcDataAdapterTest
  38. {
  39. [Test]
  40. public void FillTest ()
  41. {
  42. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  43. try
  44. {
  45. // For this Test, you must create sample table
  46. // called person-age, with a non-zero number of rows
  47. // and non-zero number of columns
  48. // run the test initialization script mono_test_mysql.sql
  49. string tableName = "employee";
  50. string sql= "select * from " + tableName;
  51. OdbcDataAdapter da = new OdbcDataAdapter (sql, (OdbcConnection) conn);
  52. DataSet ds = new DataSet (tableName);
  53. da.Fill (ds, tableName);
  54. Assert.AreEqual (true,
  55. ds.Tables.Count > 0,
  56. "#1 Table count must not be zero");
  57. Assert.AreEqual (true,
  58. ds.Tables [0].Rows.Count > 0,
  59. "#2 Row count must not be zero");
  60. foreach (DataColumn dc in ds.Tables [0].Columns)
  61. Assert.AreEqual (true,
  62. dc.ColumnName.Length > 0,
  63. "#3 DataSet column names must noot be of size 0");
  64. foreach (DataRow dr in ds.Tables [0].Rows) {
  65. foreach (DataColumn dc in ds.Tables [0].Columns)
  66. Assert.AreEqual (true,
  67. dc.ColumnName.Length > 0,
  68. "#4 column values must not be of size 0");
  69. }
  70. } finally {
  71. ConnectionManager.Instance.Odbc.CloseConnection ();
  72. }
  73. }
  74. [Test]
  75. [Ignore]
  76. public void InsertUtf8Test ()
  77. {
  78. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  79. try
  80. {
  81. DoExecuteNonQuery ((OdbcConnection) conn,
  82. "CREATE TABLE odbc_ins_utf8_test(ival int not null, sval varchar(20))");
  83. Assert.AreEqual (DoExecuteNonQuery ((OdbcConnection) conn,
  84. "INSERT INTO odbc_ins_utf8_test(ival, sval) VALUES (1, 'English')"),
  85. 1);
  86. Assert.AreEqual (DoExecuteNonQuery ((OdbcConnection) conn,
  87. "INSERT INTO odbc_ins_utf8_test(ival, sval) VALUES (2, 'Français')"),
  88. 1);
  89. Assert.AreEqual (DoExecuteNonQuery ((OdbcConnection) conn,
  90. "INSERT INTO odbc_ins_utf8_test(ival, sval) VALUES (3, 'Español')"),
  91. 1);
  92. Assert.AreEqual (DoExecuteScalar ((OdbcConnection) conn,
  93. "SELECT COUNT(*) FROM odbc_ins_utf8_test WHERE sval " +
  94. "IN('English', 'Français', 'Español')"),
  95. 3);
  96. } finally {
  97. DoExecuteNonQuery ((OdbcConnection) conn, "DROP TABLE odbc_ins_utf8_test");
  98. ConnectionManager.Instance.Odbc.CloseConnection ();
  99. }
  100. }
  101. private int DoExecuteNonQuery (OdbcConnection conn, string sql)
  102. {
  103. OdbcCommand cmd = new OdbcCommand (sql, conn);
  104. return cmd.ExecuteNonQuery ();
  105. }
  106. private int DoExecuteScalar (OdbcConnection conn, string sql)
  107. {
  108. OdbcCommand cmd = new OdbcCommand (sql, conn);
  109. object value = cmd.ExecuteScalar ();
  110. return (int) Convert.ChangeType (value, typeof (int));
  111. }
  112. }
  113. }
  114. #endif