DataTableLoadRowTest.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. //
  2. // DataTableLoadRowTest.cs - NUnit Test Cases for testing the
  3. // DataTable's LoadRow method
  4. // Author:
  5. // Sureshkumar T ([email protected])
  6. //
  7. // Copyright (c) 2004 Novell Inc., and the individuals listed
  8. // on the ChangeLog entries.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Data;
  32. using System.Data.SqlClient;
  33. using System.Text;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Data.SqlClient
  36. {
  37. [TestFixture]
  38. public class DataTableLoadRowTest
  39. {
  40. bool rowChanging;
  41. bool rowChanged;
  42. bool rowDeleting;
  43. bool rowDeleted;
  44. DataRow rowInAction_Changing;
  45. DataRowAction rowAction_Changing;
  46. DataRow rowInAction_Changed;
  47. DataRowAction rowAction_Changed;
  48. DataRow rowInAction_Deleting;
  49. DataRowAction rowAction_Deleting;
  50. DataRow rowInAction_Deleted;
  51. DataRowAction rowAction_Deleted;
  52. [Test]
  53. public void LoadRowTest ()
  54. {
  55. DataTable dt = new DataTable ();
  56. dt.Columns.Add ("id", typeof (int));
  57. dt.Columns.Add ("name", typeof (string));
  58. dt.Rows.Add (new object [] { 1, "mono 1" });
  59. dt.Rows.Add (new object [] { 2, "mono 2" });
  60. dt.Rows.Add (new object [] { 3, "mono 3" });
  61. dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
  62. dt.AcceptChanges ();
  63. dt.LoadDataRow (new object [] { 4, "mono 4" }, LoadOption.Upsert);
  64. Assert.AreEqual (4, dt.Rows.Count, "#1 has not added a new row");
  65. }
  66. [Test]
  67. public void LoadRowTestUpsert ()
  68. {
  69. DataTable dt = new DataTable ();
  70. dt.Columns.Add ("id", typeof (int));
  71. dt.Columns.Add ("name", typeof (string));
  72. dt.Rows.Add (new object [] { 1, "mono 1" });
  73. dt.Rows.Add (new object [] { 2, "mono 2" });
  74. dt.Rows.Add (new object [] { 3, "mono 3" });
  75. dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
  76. dt.AcceptChanges ();
  77. try {
  78. SubscribeEvents (dt);
  79. ResetEventFlags ();
  80. dt.LoadDataRow (new object [] { 2, "mono test" }, LoadOption.Upsert);
  81. Assert.AreEqual (3, dt.Rows.Count, "#1 should not add a row");
  82. Assert.AreEqual ("mono test", dt.Rows [1] [1], "#2 should change the current");
  83. Assert.AreEqual ("mono 2", dt.Rows [1] [1, DataRowVersion.Original], "#3 should not change original");
  84. Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#4 should change state");
  85. Assert.IsTrue (rowChanging, "#ev1 row changing not called");
  86. Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev2 this row is not intended to change");
  87. Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev3 row action is not Change");
  88. Assert.IsTrue (rowChanged, "#ev4 row changed not called");
  89. Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev5 this row is not intended to change");
  90. Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev6 row action is not Change");
  91. // Row State tests
  92. // current - modified ; result - modified
  93. ResetEventFlags ();
  94. dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.Upsert);
  95. Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c1 should change the current");
  96. Assert.AreEqual ("mono 2", dt.Rows [1] [1, DataRowVersion.Original], "#c2 should not change original");
  97. Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#c3 should not change state");
  98. Assert.IsTrue (rowChanging, "#ev11 row changing not called");
  99. Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev12 this row is not intended to change");
  100. Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev13 row action is not Change");
  101. Assert.IsTrue (rowChanged, "#ev14 row changed not called");
  102. Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev15 this row is not intended to change");
  103. Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev16 row action is not Change");
  104. // current - Unchanged; result - Unchanged if no new value
  105. dt.AcceptChanges ();
  106. ResetEventFlags ();
  107. dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.Upsert);
  108. Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c4 should not change the current");
  109. Assert.AreEqual ("mono test 2", dt.Rows [1] [1, DataRowVersion.Original], "#c5 should not change original");
  110. Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#c6 should not change state");
  111. Assert.IsTrue (rowChanging, "#ev21 row changing not called");
  112. Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev22 this row is not intended to change");
  113. Assert.AreEqual (DataRowAction.Nothing, rowAction_Changing, "#ev13 row action is not Nothing");
  114. Assert.IsTrue (rowChanged, "#ev24 row changed not called");
  115. Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev25 this row is not intended to change");
  116. Assert.AreEqual (DataRowAction.Nothing, rowAction_Changed, "#ev26 row action is not Nothing");
  117. // not the same value again
  118. dt.RejectChanges ();
  119. ResetEventFlags ();
  120. dt.LoadDataRow (new object [] { 2, "mono test 3" }, LoadOption.Upsert);
  121. Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#c7 should not change state");
  122. Assert.IsTrue (rowChanging, "#ev31 row changing not called");
  123. Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev32 this row is not intended to change");
  124. Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev33 row action is not Change");
  125. Assert.IsTrue (rowChanged, "#ev34 row changed not called");
  126. Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev35 this row is not intended to change");
  127. Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev36 row action is not Change");
  128. // current - added; result - added
  129. dt.Rows.Add (new object [] { 4, "mono 4" });
  130. ResetEventFlags ();
  131. dt.LoadDataRow (new object [] { 4, "mono 4" }, LoadOption.Upsert);
  132. Assert.AreEqual ("mono 4", dt.Rows [3] [1], "#c8 should change the current");
  133. try {
  134. object o = dt.Rows [3] [1, DataRowVersion.Original];
  135. Assert.Fail ("#c9 should have thrown version not found exception");
  136. } catch (VersionNotFoundException) { }
  137. Assert.AreEqual (DataRowState.Added, dt.Rows [3].RowState, "#c10 should not change state");
  138. Assert.IsTrue (rowChanging, "#ev41 row changing not called");
  139. Assert.AreEqual (dt.Rows [3], rowInAction_Changing, "#ev42 this row is not intended to change");
  140. Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev43 row action is not Change");
  141. Assert.IsTrue (rowChanged, "#ev44 row changed not called");
  142. Assert.AreEqual (dt.Rows [3], rowInAction_Changed, "#ev45 this row is not intended to change");
  143. Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev46 row action is not Change");
  144. // current - none; result - added
  145. ResetEventFlags ();
  146. dt.LoadDataRow (new object [] { 5, "mono 5" }, LoadOption.Upsert);
  147. Assert.AreEqual ("mono 5", dt.Rows [4] [1], "#c11 should change the current");
  148. try {
  149. object o = dt.Rows [4] [1, DataRowVersion.Original];
  150. Assert.Fail ("#c12 should have thrown version not found exception");
  151. } catch (VersionNotFoundException) { }
  152. Assert.AreEqual (DataRowState.Added, dt.Rows [4].RowState, "#c13 should change state");
  153. Assert.IsTrue (rowChanging, "#ev51 row changing not called");
  154. Assert.AreEqual (dt.Rows [4], rowInAction_Changing, "#ev52 this row is not intended to change");
  155. Assert.AreEqual (DataRowAction.Add, rowAction_Changing, "#ev53 row action is not Change");
  156. Assert.IsTrue (rowChanged, "#ev54 row changed not called");
  157. Assert.AreEqual (dt.Rows [4], rowInAction_Changed, "#ev55 this row is not intended to change");
  158. Assert.AreEqual (DataRowAction.Add, rowAction_Changed, "#ev56 row action is not Change");
  159. // current - deleted; result - added a new row
  160. ResetEventFlags ();
  161. dt.AcceptChanges ();
  162. dt.Rows [4].Delete ();
  163. Assert.IsTrue (rowDeleting, "#ev57 row deleting");
  164. Assert.IsTrue (rowDeleted, "#ev58 row deleted");
  165. Assert.AreEqual (rowInAction_Deleting, dt.Rows[4], "#ev59 rowInAction_Deleting");
  166. Assert.AreEqual (rowInAction_Deleted, dt.Rows[4], "#ev59 rowInAction_Deleted");
  167. Assert.AreEqual (rowAction_Deleting, DataRowAction.Delete, "#ev60 rowInAction_Deleting");
  168. Assert.AreEqual (rowAction_Deleted, DataRowAction.Delete, "#ev61 rowInAction_Deleted");
  169. dt.LoadDataRow (new object [] { 5, "mono 5" }, LoadOption.Upsert);
  170. Assert.AreEqual (6, dt.Rows.Count, "#c14 should not add a row");
  171. Assert.AreEqual ("mono 5", dt.Rows [5] [1], "#c15 should change the current");
  172. try {
  173. object o = dt.Rows [5] [1, DataRowVersion.Original];
  174. Assert.Fail ("#c16 expected version not found exception ");
  175. } catch (VersionNotFoundException) { }
  176. Assert.AreEqual (DataRowState.Added, dt.Rows [5].RowState, "#c17 should change state");
  177. Assert.IsTrue (rowChanging, "#ev61 row changing not called");
  178. Assert.AreEqual (dt.Rows [5], rowInAction_Changing, "#ev62 this row is not intended to change");
  179. Assert.AreEqual (DataRowAction.Add, rowAction_Changing, "#ev63 row action is not Change");
  180. Assert.IsTrue (rowChanged, "#ev64 row changed not called");
  181. Assert.AreEqual (dt.Rows [5], rowInAction_Changed, "#ev65 this row is not intended to change");
  182. Assert.AreEqual (DataRowAction.Add, rowAction_Changed, "#ev66 row action is not Change");
  183. } finally {
  184. UnsubscribeEvents (dt);
  185. }
  186. }
  187. [Test]
  188. public void LoadRowTestOverwriteChanges ()
  189. {
  190. DataTable dt = new DataTable ();
  191. dt.Columns.Add ("id", typeof (int));
  192. dt.Columns.Add ("name", typeof (string));
  193. dt.Rows.Add (new object [] { 1, "mono 1" });
  194. dt.Rows.Add (new object [] { 2, "mono 2" });
  195. dt.Rows.Add (new object [] { 3, "mono 3" });
  196. dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
  197. dt.AcceptChanges ();
  198. dt.Rows [1] [1] = "overwrite";
  199. Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#1 has not changed the row state");
  200. try {
  201. SubscribeEvents (dt);
  202. ResetEventFlags ();
  203. dt.LoadDataRow (new object [] { 2, "mono test" }, LoadOption.OverwriteChanges);
  204. Assert.AreEqual (3, dt.Rows.Count, "#2 has not added a new row");
  205. Assert.AreEqual ("mono test", dt.Rows [1] [1], "#3 should change the current");
  206. Assert.AreEqual ("mono test", dt.Rows [1] [1, DataRowVersion.Original], "#4 should change the original");
  207. Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#5 has not changed the row state");
  208. Assert.IsTrue (rowChanging, "#ltoc11 row changing not called");
  209. Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltoc12 this row is not intended to change");
  210. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc13 row action is not Change");
  211. Assert.IsTrue (rowChanged, "#ltoc14 row changed not called");
  212. Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltoc15 this row is not intended to change");
  213. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc16 row action is not Change");
  214. DataRow r = dt.Rows [1];
  215. r [1] = "test";
  216. Assert.AreEqual ("test", dt.Rows [1] [1], "#6 should change the current");
  217. Assert.AreEqual ("mono test", dt.Rows [1] [1, DataRowVersion.Original], "#7 should change the original");
  218. //Assert.AreEqual ("ramesh", dt.Rows [1] [1, DataRowVersion.Proposed], "#8 should change the original");
  219. // Row State tests
  220. // current - modified ; result - modified
  221. ResetEventFlags ();
  222. dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.OverwriteChanges);
  223. Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c1 should change the current");
  224. Assert.AreEqual ("mono test 2", dt.Rows [1] [1, DataRowVersion.Original], "#c2 should change original");
  225. Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#c3 should not change state");
  226. Assert.IsTrue (rowChanging, "#ltoc21 row changing not called");
  227. Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltoc22 this row is not intended to change");
  228. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc23 row action is not Change");
  229. Assert.IsTrue (rowChanged, "#ltoc24 row changed not called");
  230. Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltoc25 this row is not intended to change");
  231. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc26 row action is not Change");
  232. // current - Unchanged; result - Unchanged
  233. dt.AcceptChanges ();
  234. ResetEventFlags ();
  235. dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.OverwriteChanges);
  236. Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c4 should change the current");
  237. Assert.AreEqual ("mono test 2", dt.Rows [1] [1, DataRowVersion.Original], "#c5 should change original");
  238. Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#c6 should not change state");
  239. Assert.IsTrue (rowChanging, "#ltoc31 row changing not called");
  240. Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltoc32 this row is not intended to change");
  241. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc33 row action is not Change");
  242. Assert.IsTrue (rowChanged, "#ltoc34 row changed not called");
  243. Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltoc35 this row is not intended to change");
  244. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc36 row action is not Change");
  245. // current - added; result - added
  246. dt.Rows.Add (new object [] { 4, "mono 4" });
  247. ResetEventFlags ();
  248. dt.LoadDataRow (new object [] { 4, "mono 4" }, LoadOption.OverwriteChanges);
  249. Assert.AreEqual ("mono 4", dt.Rows [3] [1], "#c8 should change the current");
  250. Assert.AreEqual ("mono 4", dt.Rows [3] [1, DataRowVersion.Original], "#c9 should change the original");
  251. Assert.AreEqual (DataRowState.Unchanged, dt.Rows [3].RowState, "#c10 should not change state");
  252. Assert.IsTrue (rowChanging, "#ltoc41 row changing not called");
  253. Assert.AreEqual (dt.Rows [3], rowInAction_Changing, "#ltoc42 this row is not intended to change");
  254. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc43 row action is not Change");
  255. Assert.IsTrue (rowChanged, "#ltoc44 row changed not called");
  256. Assert.AreEqual (dt.Rows [3], rowInAction_Changed, "#ltoc45 this row is not intended to change");
  257. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc46 row action is not Change");
  258. // current - new; result - added
  259. ResetEventFlags ();
  260. dt.LoadDataRow (new object [] { 5, "mono 5" }, LoadOption.OverwriteChanges);
  261. Assert.AreEqual ("mono 5", dt.Rows [4] [1], "#c11 should change the current");
  262. Assert.AreEqual ("mono 5", dt.Rows [4] [1, DataRowVersion.Original], "#c12 should change original");
  263. Assert.AreEqual (DataRowState.Unchanged, dt.Rows [4].RowState, "#c13 should change state");
  264. Assert.IsTrue (rowChanging, "#ltoc51 row changing not called");
  265. Assert.AreEqual (dt.Rows [4], rowInAction_Changing, "#ltoc52 this row is not intended to change");
  266. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc53 row action is not Change");
  267. Assert.IsTrue (rowChanged, "#ltoc54 row changed not called");
  268. Assert.AreEqual (dt.Rows [4], rowInAction_Changed, "#ltoc55 this row is not intended to change");
  269. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc56 row action is not Change");
  270. // current - deleted; result - added a new row
  271. ResetEventFlags ();
  272. dt.AcceptChanges ();
  273. dt.Rows [4].Delete ();
  274. Assert.IsTrue (rowDeleting, "#ltoc57 row deleting");
  275. Assert.IsTrue (rowDeleted, "#ltoc58 row deleted");
  276. Assert.AreEqual (rowInAction_Deleting, dt.Rows[4], "#ltoc59 rowInAction_Deleting");
  277. Assert.AreEqual (rowInAction_Deleted, dt.Rows[4], "#ltoc60 rowInAction_Deleted");
  278. Assert.AreEqual (rowAction_Deleting, DataRowAction.Delete, "#ltoc60 rowInAction_Deleting");
  279. Assert.AreEqual (rowAction_Deleted, DataRowAction.Delete, "#ltoc61 rowInAction_Deleted");
  280. dt.LoadDataRow (new object [] { 5, "mono 51" }, LoadOption.OverwriteChanges);
  281. Assert.AreEqual (5, dt.Rows.Count, "#c14 should not add a row");
  282. Assert.AreEqual ("mono 51", dt.Rows [4] [1], "#c15 should change the current");
  283. Assert.AreEqual ("mono 51", dt.Rows [4] [1, DataRowVersion.Original], "#c16 should change the current");
  284. Assert.AreEqual (DataRowState.Unchanged, dt.Rows [4].RowState, "#c17 should change state");
  285. Assert.IsTrue (rowChanging, "#ltoc61 row changing not called");
  286. Assert.AreEqual (dt.Rows [4], rowInAction_Changing, "#ltoc62 this row is not intended to change");
  287. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc63 row action is not Change");
  288. Assert.IsTrue (rowChanged, "#ltoc64 row changed not called");
  289. Assert.AreEqual (dt.Rows [4], rowInAction_Changed, "#ltoc65 this row is not intended to change");
  290. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc66 row action is not Change");
  291. } finally {
  292. UnsubscribeEvents (dt);
  293. }
  294. }
  295. [Test]
  296. public void LoadRowTestPreserveChanges ()
  297. {
  298. DataTable dt = new DataTable ();
  299. dt.Columns.Add ("id", typeof (int));
  300. dt.Columns.Add ("name", typeof (string));
  301. dt.Rows.Add (new object [] { 1, "mono 1" });
  302. dt.Rows.Add (new object [] { 2, "mono 2" });
  303. dt.Rows.Add (new object [] { 3, "mono 3" });
  304. dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
  305. dt.AcceptChanges ();
  306. try {
  307. SubscribeEvents (dt);
  308. // current - modified; new - modified
  309. ResetEventFlags ();
  310. dt.LoadDataRow (new object [] { 2, "mono test" }, LoadOption.PreserveChanges);
  311. Assert.AreEqual (3, dt.Rows.Count, "#1 should not add a new row");
  312. Assert.AreEqual ("mono test", dt.Rows [1] [1], "#2 should change the current");
  313. Assert.AreEqual ("mono test", dt.Rows [1] [1, DataRowVersion.Original], "#3 should change the original");
  314. Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#4 has not changed the row state");
  315. Assert.IsTrue (rowChanging, "#ltpc11 row changing not called");
  316. Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltpc12 this row is not intended to change");
  317. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltpc13 row action is not Change");
  318. Assert.IsTrue (rowChanged, "#ltpc14 row changed not called");
  319. Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltpc15 this row is not intended to change");
  320. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltpc16 row action is not Change");
  321. // current - none; new - unchanged
  322. ResetEventFlags ();
  323. dt.LoadDataRow (new object [] { 4, "mono 4" }, LoadOption.PreserveChanges);
  324. Assert.AreEqual (4, dt.Rows.Count,"#5 should add a new row");
  325. Assert.AreEqual ("mono 4", dt.Rows [3] [1], "#6 should change the current");
  326. Assert.AreEqual ("mono 4", dt.Rows [3] [1, DataRowVersion.Original], "#7 should change the original");
  327. Assert.AreEqual (DataRowState.Unchanged, dt.Rows [3].RowState, "#8 has not changed the row state");
  328. Assert.IsTrue (rowChanging, "#ltpc21 row changing not called");
  329. Assert.AreEqual (dt.Rows [3], rowInAction_Changing, "#ltpc22 this row is not intended to change");
  330. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltpc23 row action is not Change");
  331. Assert.IsTrue (rowChanged, "#ltpc24 row changed not called");
  332. Assert.AreEqual (dt.Rows [3], rowInAction_Changed, "#ltpc25 this row is not intended to change");
  333. Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltpc16 row action is not Change");
  334. dt.RejectChanges ();
  335. // current - added; new - modified
  336. dt.Rows.Add (new object [] { 5, "mono 5" });
  337. ResetEventFlags ();
  338. dt.LoadDataRow (new object [] { 5, "mono test" }, LoadOption.PreserveChanges);
  339. Assert.AreEqual (5, dt.Rows.Count, "#9 should not add a new row");
  340. Assert.AreEqual ("mono 5", dt.Rows [4] [1], "#10 should not change the current");
  341. Assert.AreEqual ("mono test", dt.Rows [4] [1, DataRowVersion.Original], "#11 should change the original");
  342. Assert.AreEqual (DataRowState.Modified, dt.Rows [4].RowState, "#12 has not changed the row state");
  343. Assert.IsTrue (rowChanging, "#ltpc31 row changing not called");
  344. Assert.AreEqual (dt.Rows [4], rowInAction_Changing, "#ltpc32 this row is not intended to change");
  345. Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changing, "#ltpc33 row action is not Change");
  346. Assert.IsTrue (rowChanged, "#ltpc34 row changed not called");
  347. Assert.AreEqual (dt.Rows [4], rowInAction_Changed, "#ltpc35 this row is not intended to change");
  348. Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changed, "#ltpc36 row action is not Change");
  349. dt.RejectChanges ();
  350. // current - deleted ; new - deleted ChangeOriginal
  351. ResetEventFlags ();
  352. dt.Rows [1].Delete ();
  353. Assert.IsTrue (rowDeleting, "#ltpc37 row deleting");
  354. Assert.IsTrue (rowDeleted, "#ltpc38 row deleted");
  355. Assert.AreEqual (rowInAction_Deleting, dt.Rows[1], "#ltpc39 rowInAction_Deleting");
  356. Assert.AreEqual (rowInAction_Deleted, dt.Rows[1], "#ltpc40 rowInAction_Deleted");
  357. Assert.AreEqual (rowAction_Deleting, DataRowAction.Delete, "#ltpc60 rowInAction_Deleting");
  358. Assert.AreEqual (rowAction_Deleted, DataRowAction.Delete, "#ltpc61 rowInAction_Deleted");
  359. dt.LoadDataRow (new object [] { 2, "mono deleted" }, LoadOption.PreserveChanges);
  360. Assert.AreEqual (5, dt.Rows.Count, "#13 should not add a new row");
  361. Assert.AreEqual ("mono deleted", dt.Rows [1] [1, DataRowVersion.Original], "#14 should change the original");
  362. Assert.AreEqual (DataRowState.Deleted, dt.Rows [1].RowState, "#15 has not changed the row state");
  363. Assert.IsTrue (rowChanging, "#ltpc41 row changing not called");
  364. Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltpc42 this row is not intended to change");
  365. Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changing, "#ltoc43 row action is not Change");
  366. Assert.IsTrue (rowChanged, "#ltpc44 row changed not called");
  367. Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltpc45 this row is not intended to change");
  368. Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changed, "#ltpc46 row action is not Change");
  369. } finally {
  370. UnsubscribeEvents (dt);
  371. }
  372. }
  373. [Test]
  374. public void LoadRowDefaultValueTest ()
  375. {
  376. DataTable dt = new DataTable ();
  377. dt.Columns.Add ("id", typeof (int));
  378. dt.Columns.Add ("age", typeof (int));
  379. dt.Columns.Add ("name", typeof (string));
  380. dt.Columns [1].DefaultValue = 20;
  381. dt.Rows.Add (new object [] { 1, 15, "mono 1" });
  382. dt.Rows.Add (new object [] { 2, 25, "mono 2" });
  383. dt.Rows.Add (new object [] { 3, 35, "mono 3" });
  384. dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
  385. dt.AcceptChanges ();
  386. dt.LoadDataRow (new object [] { 2, null, "mono test" }, LoadOption.OverwriteChanges);
  387. Assert.AreEqual (3, dt.Rows.Count, "#1 should not have added a row");
  388. Assert.AreEqual (20, dt.Rows [1] [1], "#2 should be default value");
  389. Assert.AreEqual (20, dt.Rows [1] [1, DataRowVersion.Original], "#3 should be default value");
  390. }
  391. [Test]
  392. public void LoadRowAutoIncrementTest ()
  393. {
  394. DataTable dt = new DataTable ();
  395. dt.Columns.Add ("id", typeof (int));
  396. dt.Columns.Add ("age", typeof (int));
  397. dt.Columns.Add ("name", typeof (string));
  398. dt.Columns [0].AutoIncrementSeed = 10;
  399. dt.Columns [0].AutoIncrementStep = 5;
  400. dt.Columns [0].AutoIncrement = true;
  401. dt.Rows.Add (new object [] { null, 15, "mono 1" });
  402. dt.Rows.Add (new object [] { null, 25, "mono 2" });
  403. dt.Rows.Add (new object [] { null, 35, "mono 3" });
  404. dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
  405. dt.AcceptChanges ();
  406. dt.LoadDataRow (new object [] { null, 20, "mono test" }, LoadOption.OverwriteChanges);
  407. Assert.AreEqual (4, dt.Rows.Count, "#1 has not added a new row");
  408. Assert.AreEqual (25, dt.Rows [3] [0], "#2 current should be ai");
  409. Assert.AreEqual (25, dt.Rows [3] [0, DataRowVersion.Original], "#3 original should be ai");
  410. dt.LoadDataRow (new object [] { 25, 20, "mono test" }, LoadOption.Upsert);
  411. dt.LoadDataRow (new object [] { 25, 20, "mono test 2" }, LoadOption.Upsert);
  412. dt.LoadDataRow (new object [] { null, 20, "mono test aaa" }, LoadOption.Upsert);
  413. Assert.AreEqual (5, dt.Rows.Count, "#4 has not added a new row");
  414. Assert.AreEqual (25, dt.Rows [3] [0], "#5 current should be ai");
  415. Assert.AreEqual (25, dt.Rows [3] [0, DataRowVersion.Original], "#6 original should be ai");
  416. Assert.AreEqual (30, dt.Rows [4] [0], "#7 current should be ai");
  417. }
  418. public void SubscribeEvents (DataTable dt)
  419. {
  420. dt.RowChanging += new DataRowChangeEventHandler (dt_RowChanging);
  421. dt.RowChanged += new DataRowChangeEventHandler (dt_RowChanged);
  422. dt.RowDeleted += new DataRowChangeEventHandler (dt_RowDeleted);
  423. dt.RowDeleting += new DataRowChangeEventHandler (dt_RowDeleting);
  424. //dt.TableNewRow += new DataTableNewRowEventHandler (dt_TableNewRow);
  425. }
  426. public void UnsubscribeEvents (DataTable dt)
  427. {
  428. dt.RowChanging -= new DataRowChangeEventHandler (dt_RowChanging);
  429. dt.RowChanged -= new DataRowChangeEventHandler (dt_RowChanged);
  430. dt.RowDeleted -= new DataRowChangeEventHandler (dt_RowDeleted);
  431. dt.RowDeleting -= new DataRowChangeEventHandler (dt_RowDeleting);
  432. //dt.TableNewRow -= new DataTableNewRowEventHandler (dt_TableNewRow);
  433. }
  434. public void ResetEventFlags ()
  435. {
  436. rowChanging = false;
  437. rowChanged = false;
  438. rowDeleting = false;
  439. rowDeleted = false;
  440. rowInAction_Changing = null;
  441. rowAction_Changing = DataRowAction.Nothing;
  442. rowInAction_Changed = null;
  443. rowAction_Changed = DataRowAction.Nothing;
  444. rowInAction_Deleting = null;
  445. rowAction_Deleting = DataRowAction.Nothing;
  446. rowInAction_Deleted = null;
  447. rowAction_Deleted = DataRowAction.Nothing;
  448. }
  449. void dt_RowDeleting (object sender, DataRowChangeEventArgs e)
  450. {
  451. rowDeleting = true;
  452. rowInAction_Deleting = e.Row;
  453. rowAction_Deleting = e.Action;
  454. }
  455. void dt_RowDeleted (object sender, DataRowChangeEventArgs e)
  456. {
  457. rowDeleted = true;
  458. rowInAction_Deleted = e.Row;
  459. rowAction_Deleted = e.Action;
  460. }
  461. void dt_RowChanged (object sender, DataRowChangeEventArgs e)
  462. {
  463. rowChanged = true;
  464. rowInAction_Changed = e.Row;
  465. rowAction_Changed = e.Action;
  466. }
  467. void dt_RowChanging (object sender, DataRowChangeEventArgs e)
  468. {
  469. rowChanging = true;
  470. rowInAction_Changing = e.Row;
  471. rowAction_Changing = e.Action;
  472. }
  473. }
  474. }
  475. #endif // NET_2_0