OdbcCommandBuilderTest.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // OdbcCommandBuilderTest.cs - NUnit Test Cases for testing the
  2. // OdbcCommandBuilder Test.
  3. //
  4. // Authors:
  5. // Sureshkumar T ([email protected])
  6. //
  7. // Copyright (c) 2004 Novell Inc., and the individuals listed on the
  8. // ChangeLog entries.
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person
  12. // obtaining a copy of this software and associated documentation
  13. // files (the "Software"), to deal in the Software without
  14. // restriction, including without limitation the rights to use, copy,
  15. // modify, merge, publish, distribute, sublicense, and/or sell copies
  16. // of the Software, and to permit persons to whom the Software is
  17. // furnished to do so, subject to the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  26. // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. // SOFTWARE.
  30. using System;
  31. using System.Data;
  32. using System.Data.Common;
  33. using System.Data.Odbc;
  34. using Mono.Data;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Data
  37. {
  38. [TestFixture]
  39. [Category ("odbc")]
  40. public class OdbcCommandBuilderTest
  41. {
  42. [Test]
  43. public void GetInsertCommandTest ()
  44. {
  45. IDbConnection conn = ConnectionManager.Singleton.Connection;
  46. try {
  47. string selectQuery = "select id, lname from employee where id = 1";
  48. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, (OdbcConnection) conn);
  49. DataSet ds = new DataSet ();
  50. da.Fill (ds, "IntTest");
  51. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  52. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  53. OdbcCommand cmd = cb.GetInsertCommand ();
  54. Assert.AreEqual ("INSERT INTO employee (id, lname) VALUES (?, ?)",
  55. cmd.CommandText, "#2");
  56. } finally {
  57. ConnectionManager.Singleton.CloseConnection ();
  58. }
  59. }
  60. [Test]
  61. public void GetInsertCommandTestWithExpression ()
  62. {
  63. IDbConnection conn = ConnectionManager.Singleton.Connection;
  64. try {
  65. string selectQuery = "select id, lname, id+1 as next_id from employee where id = 1";
  66. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, (OdbcConnection) conn);
  67. DataSet ds = new DataSet ();
  68. da.Fill (ds, "IntTest");
  69. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  70. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  71. OdbcCommand cmd = cb.GetInsertCommand ();
  72. Assert.AreEqual ("INSERT INTO employee (id, lname) VALUES (?, ?)",
  73. cmd.CommandText, "#2");
  74. } finally {
  75. ConnectionManager.Singleton.CloseConnection ();
  76. }
  77. }
  78. [Test]
  79. public void GetUpdateCommandTest ()
  80. {
  81. IDbConnection conn = ConnectionManager.Singleton.Connection;
  82. using (conn) {
  83. string selectQuery = "select id, lname, id+1 as next_id from employee where id = 1";
  84. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, (OdbcConnection) conn);
  85. DataSet ds = new DataSet ();
  86. da.Fill (ds, "IntTest");
  87. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  88. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  89. OdbcCommand cmd = cb.GetUpdateCommand ();
  90. Assert.AreEqual ("UPDATE employee SET id = ?, lname = ? WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
  91. cmd.CommandText, "#2");
  92. Assert.AreEqual (5, cmd.Parameters.Count, "#3");
  93. }
  94. }
  95. [Test]
  96. [ExpectedException (typeof (DBConcurrencyException))]
  97. public void GetUpdateCommandDBConcurrencyExceptionTest ()
  98. {
  99. IDbConnection conn = ConnectionManager.Singleton.Connection;
  100. try {
  101. string selectQuery = "select id, lname from employee where id = 1";
  102. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, (OdbcConnection) conn);
  103. DataSet ds = new DataSet ();
  104. da.Fill (ds, "IntTest");
  105. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  106. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  107. DataRow [] rows = ds.Tables [0].Select ("id=1");
  108. rows [0] [0] = 6660; // non existent
  109. ds.Tables [0].AcceptChanges (); // moves 6660 to original value
  110. rows [0] [0] = 1; // moves 6660 as search key into db table
  111. da.Update (rows);
  112. } finally {
  113. ConnectionManager.Singleton.CloseConnection ();
  114. }
  115. }
  116. [Test]
  117. public void GetDeleteCommandTest ()
  118. {
  119. IDbConnection conn = ConnectionManager.Singleton.Connection;
  120. try {
  121. string selectQuery = "select id, lname, id+1 as next_id from employee where id = 1";
  122. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, (OdbcConnection) conn);
  123. DataSet ds = new DataSet ();
  124. da.Fill (ds, "IntTest");
  125. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  126. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  127. OdbcCommand cmd = cb.GetDeleteCommand ();
  128. Assert.AreEqual ("DELETE FROM employee WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
  129. cmd.CommandText, "#2");
  130. } finally {
  131. ConnectionManager.Singleton.CloseConnection ();
  132. }
  133. }
  134. [Test]
  135. public void DefaultPropertiesTest ()
  136. {
  137. OdbcCommandBuilder cb = new OdbcCommandBuilder ();
  138. #if NET_1_1 || NET_1_0 || ONLY_1_1
  139. Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictDetection);
  140. #endif // NET_1_1 || NET_1_0 || ONLY_1_1
  141. Assert.AreEqual ("", cb.QuotePrefix, "#5");
  142. Assert.AreEqual ("", cb.QuoteSuffix, "#6");
  143. #if NET_2_0
  144. Assert.AreEqual (".", cb.CatalogSeparator, "#2");
  145. Assert.AreEqual ("", cb.DecimalSeparator, "#3");
  146. Assert.AreEqual (".", cb.SchemaSeparator, "#4");
  147. Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
  148. IDbConnection conn = ConnectionManager.Singleton.Connection;
  149. try {
  150. conn.Open ();
  151. cb = new OdbcCommandBuilder ();
  152. Assert.AreEqual ("\"monotest\"", cb.QuoteIdentifier ("monotest", (OdbcConnection) conn), "#7");
  153. Assert.AreEqual ("monotest", cb.UnquoteIdentifier ("\"monotest\"", (OdbcConnection) conn), "#8");
  154. conn.Close ();
  155. } finally {
  156. ConnectionManager.Singleton.CloseConnection ();
  157. }
  158. // FIXME: test SetAllValues
  159. #endif // NET_2_0
  160. }
  161. // FIXME: Add tests for examining RowError
  162. // FIXME: Add test for ContinueUpdateOnError property
  163. }
  164. }