DbDataAdapterTest.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // DbDataAdapterTest.cs - NUnit Test Cases for testing the DbDataAdapter class
  3. //
  4. // Author:
  5. // Gert Driesen ([email protected])
  6. //
  7. // Copyright (c) 2007 Gert Driesen
  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. using System;
  29. using System.Data;
  30. using System.Data.Common;
  31. using System.Data.SqlClient;
  32. /*--For Bug 853 Test Begin--*/
  33. using Mono.Data.Sqlite;
  34. /*--For Bug 853 Test End--*/
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Data.Common
  37. {
  38. [TestFixture]
  39. public class DbDataAdapterTest
  40. {
  41. #if NET_2_0
  42. [Test]
  43. public void UpdateBatchSize ()
  44. {
  45. MyAdapter da = new MyAdapter ();
  46. try {
  47. da.UpdateBatchSize = 0;
  48. Assert.Fail ("#A1");
  49. } catch (NotSupportedException ex) {
  50. // Specified method is not supported
  51. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
  52. Assert.IsNull (ex.InnerException, "#A3");
  53. Assert.IsNotNull (ex.Message, "#A4");
  54. }
  55. Assert.AreEqual (1, da.UpdateBatchSize, "#A5");
  56. try {
  57. da.UpdateBatchSize = int.MaxValue;
  58. Assert.Fail ("#B1");
  59. } catch (NotSupportedException ex) {
  60. // Specified method is not supported
  61. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
  62. Assert.IsNull (ex.InnerException, "#B3");
  63. Assert.IsNotNull (ex.Message, "#B4");
  64. }
  65. Assert.AreEqual (1, da.UpdateBatchSize, "#B5");
  66. da.UpdateBatchSize = 1;
  67. Assert.AreEqual (1, da.UpdateBatchSize, "#C");
  68. }
  69. [Test]
  70. public void UpdateBatchSize_Negative ()
  71. {
  72. MyAdapter da = new MyAdapter ();
  73. try {
  74. da.UpdateBatchSize = -1;
  75. Assert.Fail ("#1");
  76. } catch (NotSupportedException ex) {
  77. // Specified method is not supported
  78. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  79. Assert.IsNull (ex.InnerException, "#3");
  80. Assert.IsNotNull (ex.Message, "#4");
  81. }
  82. }
  83. [Test]
  84. public void AddToBatch ()
  85. {
  86. MyAdapter da = new MyAdapter ();
  87. try {
  88. da.AddToBatch (new SqlCommand ());
  89. Assert.Fail ("#1");
  90. } catch (NotSupportedException ex) {
  91. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  92. Assert.IsNull (ex.InnerException, "#3");
  93. Assert.IsNotNull (ex.Message, "#4");
  94. }
  95. }
  96. [Test]
  97. public void ClearBatch ()
  98. {
  99. MyAdapter da = new MyAdapter ();
  100. try {
  101. da.ClearBatch ();
  102. Assert.Fail ("#1");
  103. } catch (NotSupportedException ex) {
  104. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  105. Assert.IsNull (ex.InnerException, "#3");
  106. Assert.IsNotNull (ex.Message, "#4");
  107. }
  108. }
  109. [Test]
  110. public void ExecuteBatch ()
  111. {
  112. MyAdapter da = new MyAdapter ();
  113. try {
  114. da.ExecuteBatch ();
  115. Assert.Fail ("#1");
  116. } catch (NotSupportedException ex) {
  117. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  118. Assert.IsNull (ex.InnerException, "#3");
  119. Assert.IsNotNull (ex.Message, "#4");
  120. }
  121. }
  122. [Test]
  123. public void GetBatchedParameter ()
  124. {
  125. MyAdapter da = new MyAdapter ();
  126. try {
  127. da.GetBatchedParameter (1, 1);
  128. Assert.Fail ("#1");
  129. } catch (NotSupportedException ex) {
  130. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  131. Assert.IsNull (ex.InnerException, "#3");
  132. Assert.IsNotNull (ex.Message, "#4");
  133. }
  134. }
  135. [Test]
  136. public void GetBatchedRecordsAffected ()
  137. {
  138. MyAdapter da = new MyAdapter ();
  139. int recordsAffected = 0;
  140. Exception error = null;
  141. Assert.IsTrue (da. GetBatchedRecordsAffected (int.MinValue,
  142. out recordsAffected, out error), "#1");
  143. Assert.AreEqual (1, recordsAffected, "#2");
  144. Assert.IsNull (error, "#3");
  145. }
  146. [Test]
  147. public void InitializeBatching ()
  148. {
  149. MyAdapter da = new MyAdapter ();
  150. try {
  151. da.InitializeBatching ();
  152. Assert.Fail ("#1");
  153. } catch (NotSupportedException ex) {
  154. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  155. Assert.IsNull (ex.InnerException, "#3");
  156. Assert.IsNotNull (ex.Message, "#4");
  157. }
  158. }
  159. [Test]
  160. public void TerminateBatching ()
  161. {
  162. MyAdapter da = new MyAdapter ();
  163. try {
  164. da.TerminateBatching ();
  165. Assert.Fail ("#1");
  166. } catch (NotSupportedException ex) {
  167. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  168. Assert.IsNull (ex.InnerException, "#3");
  169. Assert.IsNotNull (ex.Message, "#4");
  170. }
  171. }
  172. [Test]
  173. [Category ("NotWorking")] // Requires newer sqlite than is on wrench
  174. public void XimarinBugzillaBug853Test()
  175. {
  176. const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
  177. SqliteConnection dbConnection = new SqliteConnection(connectionString);
  178. dbConnection.Open();
  179. SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
  180. ClearTableEntry.ExecuteNonQuery();
  181. SqliteDataAdapter sqliteDataAdapter = new SqliteDataAdapter("SELECT * FROM primus", dbConnection);
  182. SqliteCommandBuilder builder = new SqliteCommandBuilder(sqliteDataAdapter);
  183. sqliteDataAdapter.InsertCommand = builder.GetInsertCommand();
  184. sqliteDataAdapter.DeleteCommand = builder.GetDeleteCommand();
  185. DataSet dataSet = new DataSet();
  186. sqliteDataAdapter.Fill(dataSet, "Primus");//reset
  187. DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
  188. rowToBeAdded["id"] = 123;
  189. rowToBeAdded["name"] = "Name";//not null primary key
  190. rowToBeAdded["value"] = 777;
  191. dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
  192. sqliteDataAdapter.Update (dataSet, "Primus");
  193. //This would fail with NULL constraint violation in bug
  194. //report. Because before the patch, it would create
  195. //a new record with all fields being null-- if the
  196. //exception rises, test fails
  197. sqliteDataAdapter.Update (dataSet, "Primus");
  198. dbConnection.Close();
  199. dbConnection = null;
  200. }
  201. #endif
  202. class MyAdapter : DbDataAdapter
  203. {
  204. #if ONLY_1_1
  205. protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
  206. StatementType statementType,
  207. DataTableMapping tableMapping)
  208. {
  209. throw new NotImplementedException ();
  210. }
  211. protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command,
  212. StatementType statementType,
  213. DataTableMapping tableMapping)
  214. {
  215. throw new NotImplementedException ();
  216. }
  217. protected override void OnRowUpdated (RowUpdatedEventArgs value)
  218. {
  219. throw new NotImplementedException ();
  220. }
  221. protected override void OnRowUpdating (RowUpdatingEventArgs value)
  222. {
  223. throw new NotImplementedException ();
  224. }
  225. #endif
  226. #if NET_2_0
  227. public new int AddToBatch (IDbCommand command)
  228. {
  229. return base.AddToBatch (command);
  230. }
  231. public new void ClearBatch ()
  232. {
  233. base.ClearBatch ();
  234. }
  235. public new void ExecuteBatch ()
  236. {
  237. base.ClearBatch ();
  238. }
  239. public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
  240. {
  241. return base.GetBatchedParameter (commandIdentifier, parameterIndex);
  242. }
  243. public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
  244. {
  245. return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
  246. }
  247. public new void InitializeBatching ()
  248. {
  249. base.InitializeBatching ();
  250. }
  251. public new void TerminateBatching ()
  252. {
  253. base.TerminateBatching ();
  254. }
  255. #endif
  256. }
  257. }
  258. }