DbDataAdapterTest.cs 8.8 KB

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