DataRowComparerTest.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // DataRowComparerTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc. http://www.novell.com
  8. //
  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. using System;
  30. using System.Collections.Generic;
  31. using System.Data;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Data
  34. {
  35. [TestFixture]
  36. public class DataRowComparerTest
  37. {
  38. [Test]
  39. public void Default ()
  40. {
  41. DataRowComparer<DataRow> c1 = DataRowComparer.Default;
  42. DataRowComparer<DataRow> c2 = DataRowComparer.Default;
  43. Assert.AreSame (c1, c2);
  44. }
  45. [Test]
  46. public void Equals ()
  47. {
  48. DataRowComparer<DataRow> c = DataRowComparer.Default;
  49. DataTable dtA = new DataTable ("tableA");
  50. dtA.Columns.Add ("col1", typeof (int));
  51. dtA.Columns.Add ("col2", typeof (string));
  52. dtA.Columns.Add ("col3", typeof (DateTime));
  53. DataRow r1 = dtA.Rows.Add (3, "bar", new DateTime (2008, 5, 7));
  54. DataRow r2 = dtA.Rows.Add (3, "bar", new DateTime (2008, 5, 7));
  55. Assert.IsTrue (c.Equals (r1, r2), "#A1");
  56. r1 ["col1"] = 4;
  57. Assert.IsFalse (c.Equals (r1, r2), "#A2");
  58. r1 ["col1"] = 3;
  59. Assert.IsTrue (c.Equals (r1, r2), "#A3");
  60. r1 ["col2"] = null;
  61. Assert.IsFalse (c.Equals (r1, r2), "#B1");
  62. r2 ["col2"] = null;
  63. Assert.IsTrue (c.Equals (r1, r2), "#B2");
  64. r1 ["col2"] = "bar";
  65. Assert.IsFalse (c.Equals (r1, r2), "#B3");
  66. r2 ["col2"] = "bar";
  67. Assert.IsTrue (c.Equals (r1, r2), "#B4");
  68. r1 ["col3"] = DBNull.Value;
  69. Assert.IsFalse (c.Equals (r1, r2), "#C1");
  70. r2 ["col3"] = DBNull.Value;
  71. Assert.IsTrue (c.Equals (r1, r2), "#C2");
  72. r1 ["col3"] = new DateTime (2008, 5, 7);
  73. Assert.IsFalse (c.Equals (r1, r2), "#C3");
  74. r2 ["col3"] = new DateTime (2008, 5, 7);
  75. Assert.IsTrue (c.Equals (r1, r2), "#C4");
  76. Assert.IsFalse (c.Equals (r1, null), "#D1");
  77. Assert.IsFalse (c.Equals (null, r1), "#D2");
  78. Assert.IsTrue (c.Equals (null, null), "#D3");
  79. // rows do not have to share the same parent
  80. DataTable dtB = new DataTable ("tableB");
  81. dtB.Columns.Add ("colB1", typeof (int));
  82. dtB.Columns.Add ("colB2", typeof (string));
  83. dtB.Columns.Add ("colB3", typeof (DateTime));
  84. DataRow r3 = dtB.Rows.Add (3, "bar", new DateTime (2008, 5, 7));
  85. Assert.IsTrue (c.Equals (r1, r3), "#E1");
  86. r1 ["col1"] = 4;
  87. Assert.IsFalse (c.Equals (r1, r3), "#E2");
  88. r1 ["col1"] = 3;
  89. Assert.IsTrue (c.Equals (r1, r3), "#E3");
  90. // difference in rowstate is ignored
  91. r1.AcceptChanges ();
  92. Assert.IsTrue (c.Equals (r1, r2), "#G1");
  93. r1 ["col1"] = 4;
  94. Assert.IsFalse (c.Equals (r1, r2), "#G2");
  95. r1 ["col1"] = 3;
  96. Assert.IsTrue (c.Equals (r1, r2), "#G3");
  97. // rows have different number of columns
  98. DataTable dtC = new DataTable ("tableC");
  99. dtC.Columns.Add ("colC1", typeof (int));
  100. dtC.Columns.Add ("colC2", typeof (string));
  101. DataRow r4 = dtC.Rows.Add (3, "bar");
  102. Assert.IsFalse (c.Equals (r1, r4), "#H1");
  103. r1 ["col3"] = DBNull.Value;
  104. Assert.IsFalse (c.Equals (r1, r4), "#H2");
  105. }
  106. [Test]
  107. public void Equals_Rows_Detached ()
  108. {
  109. DataRowComparer<DataRow> c = DataRowComparer.Default;
  110. DataTable dt = new DataTable ("tableA");
  111. dt.Columns.Add ("col1", typeof (int));
  112. dt.Columns.Add ("col2", typeof (string));
  113. dt.Columns.Add ("col3", typeof (DateTime));
  114. DataRow r1 = dt.Rows.Add (3, "bar", new DateTime (2008, 5, 7));
  115. DataRow r2 = dt.NewRow ();
  116. r2.ItemArray = new object [] { 3, "bar", new DateTime (2008, 5, 7) };
  117. DataRow r3 = dt.NewRow ();
  118. r3.ItemArray = new object [] { 3, "bar", new DateTime (2008, 5, 7) };
  119. // left row detached
  120. Assert.IsTrue (c.Equals (r2, r1), "#A1");
  121. r1 ["col1"] = 4;
  122. Assert.IsFalse (c.Equals (r2, r1), "#A2");
  123. r1 ["col1"] = 3;
  124. Assert.IsTrue (c.Equals (r2, r1), "#A3");
  125. // right row detached
  126. Assert.IsTrue (c.Equals (r1, r2), "#B1");
  127. r1 ["col2"] = "baz";
  128. Assert.IsFalse (c.Equals (r1, r2), "#B2");
  129. r1 ["col2"] = "bar";
  130. Assert.IsTrue (c.Equals (r1, r2), "#B3");
  131. // both rows detached
  132. Assert.IsTrue (c.Equals (r2, r3), "#C1");
  133. r2 ["col3"] = new DateTime (2008, 6, 7);
  134. Assert.IsFalse (c.Equals (r2, r3), "#C2");
  135. r2 ["col3"] = new DateTime (2008, 5, 7);
  136. Assert.IsTrue (c.Equals (r2, r3), "#C3");
  137. }
  138. [Test]
  139. public void Equals_Rows_Deleted ()
  140. {
  141. DataRowComparer<DataRow> c = DataRowComparer.Default;
  142. DataTable dtA = new DataTable ("tableA");
  143. dtA.Columns.Add ("col1", typeof (int));
  144. dtA.Columns.Add ("col2", typeof (string));
  145. dtA.Columns.Add ("col3", typeof (DateTime));
  146. DataRow r1 = dtA.Rows.Add (3, "bar", new DateTime (2008, 5, 7));
  147. DataRow r2 = dtA.Rows.Add (3, "bar", new DateTime (2008, 5, 7));
  148. r1.Delete ();
  149. // left row deleted
  150. try {
  151. c.Equals (r1, r2);
  152. Assert.Fail ("#A1");
  153. } catch (RowNotInTableException ex) {
  154. Assert.AreEqual (typeof (RowNotInTableException), ex.GetType (), "#A2");
  155. Assert.IsNull (ex.InnerException, "#A3");
  156. Assert.IsNotNull (ex.Message, "#A4");
  157. }
  158. // right row deleted
  159. try {
  160. c.Equals (r2, r1);
  161. Assert.Fail ("#B1");
  162. } catch (RowNotInTableException ex) {
  163. Assert.AreEqual (typeof (RowNotInTableException), ex.GetType (), "#B2");
  164. Assert.IsNull (ex.InnerException, "#B3");
  165. Assert.IsNotNull (ex.Message, "#B4");
  166. }
  167. r2.Delete ();
  168. // both rows deleted
  169. try {
  170. c.Equals (r2, r1);
  171. Assert.Fail ("#C1");
  172. } catch (RowNotInTableException ex) {
  173. Assert.AreEqual (typeof (RowNotInTableException), ex.GetType (), "#C2");
  174. Assert.IsNull (ex.InnerException, "#C3");
  175. Assert.IsNotNull (ex.Message, "#C4");
  176. }
  177. }
  178. [Test]
  179. public void GetHashCodeWithVersions ()
  180. {
  181. DataSet ds = new DataSet ();
  182. DataTable dt = new DataTable ("MyTable");
  183. ds.Tables.Add (dt);
  184. dt.Columns.Add ("col1");
  185. dt.Columns.Add ("col2");
  186. DataRow r1 = dt.Rows.Add (new object [] {"foo", "bar"});
  187. DataRow r2 = dt.Rows.Add (new object [] {"foo", "bar"});
  188. ds.AcceptChanges ();
  189. DataRowComparer<DataRow> c = DataRowComparer.Default;
  190. Assert.IsTrue (c.GetHashCode (r1) == c.GetHashCode (r2), "#1");
  191. /*
  192. // LAMESPEC: .NET fails here
  193. r2 ["col2"] = "baz";
  194. r2.AcceptChanges ();
  195. Assert.IsFalse (c.GetHashCode (r1) == c.GetHashCode (r2), "#2");
  196. ds.AcceptChanges (); // now r2 original value is "baz"
  197. r2 ["col2"] = "bar";
  198. Assert.IsFalse (c.GetHashCode (r1) == c.GetHashCode (r2), "#3");
  199. // LAMESPEC: .NET fails here
  200. DataRow r3 = dt.Rows.Add (new object [] {"foo", "baz"});
  201. Assert.IsFalse (c.GetHashCode (r1) == c.GetHashCode (r3), "#4");
  202. */
  203. }
  204. [Test]
  205. public void GetHashCode_Row_Null ()
  206. {
  207. DataRowComparer<DataRow> c = DataRowComparer.Default;
  208. try {
  209. c.GetHashCode (null);
  210. Assert.Fail ("#1");
  211. } catch (ArgumentNullException ex) {
  212. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  213. Assert.IsNull (ex.InnerException, "#3");
  214. Assert.IsNotNull (ex.Message, "#4");
  215. Assert.AreEqual ("row", ex.ParamName, "#5");
  216. }
  217. }
  218. }
  219. }