DbDataAdapterTest.cs 9.6 KB

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