/mcs/class/System.Data/Test/System.Data/DataRelationCollectionTest.cs

https://github.com/pruiz/mono · C# · 361 lines · 286 code · 46 blank · 29 comment · 0 complexity · c89bdd70952759ca08136bd214210c2f MD5 · raw file

  1. // DataRelationCollection.cs - Nunit Test Cases for for testing the DataRelationCollection
  2. // class
  3. // Author:
  4. //
  5. // Punit Kumar Todi ( punit_todi@da-iict.org )
  6. // (C) Punit Todi
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  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 NUnit.Framework;
  30. using System;
  31. using System.Data;
  32. namespace MonoTests.System.Data
  33. {
  34. [TestFixture]
  35. public class DataRelationCollectionTest
  36. {
  37. DataSet _dataset;
  38. DataTable _tblparent, _tblchild;
  39. DataRelation _relation;
  40. [SetUp]
  41. public void GetReady ()
  42. {
  43. _dataset = new DataSet ();
  44. _tblparent = new DataTable ("Customer");
  45. _tblchild = new DataTable ("Order");
  46. _dataset.Tables.Add (_tblchild);
  47. _dataset.Tables.Add (_tblparent);
  48. _dataset.Tables.Add ("Item");
  49. _dataset.Tables ["Customer"].Columns.Add ("custid");
  50. _dataset.Tables ["Customer"].Columns.Add ("custname");
  51. _dataset.Tables ["Order"].Columns.Add ("oid");
  52. _dataset.Tables ["Order"].Columns.Add ("custid");
  53. _dataset.Tables ["Order"].Columns.Add ("itemid");
  54. _dataset.Tables ["Order"].Columns.Add ("desc");
  55. _dataset.Tables ["Item"].Columns.Add ("itemid");
  56. _dataset.Tables ["Item"].Columns.Add ("desc");
  57. }
  58. [TearDown]
  59. public void Clean ()
  60. {
  61. _dataset.Relations.Clear ();
  62. }
  63. [Test]
  64. public void Add ()
  65. {
  66. DataRelationCollection drcol = _dataset.Relations;
  67. DataColumn parentCol = _dataset.Tables ["Customer"].Columns ["custid"];
  68. DataColumn childCol = _dataset.Tables ["Order"].Columns ["custid"];
  69. DataRelation dr = new DataRelation ("CustOrder", parentCol, childCol);
  70. drcol.Add (dr);
  71. Assert.That (drcol [0].RelationName, Is.EqualTo ("CustOrder"), "test#1");
  72. drcol.Clear ();
  73. drcol.Add (parentCol, childCol);
  74. Assert.That (drcol.Count, Is.EqualTo (1), "test#2");
  75. drcol.Clear ();
  76. drcol.Add ("NewRelation", parentCol, childCol);
  77. Assert.That (drcol [0].RelationName, Is.EqualTo ("NewRelation"), "test#3");
  78. drcol.Clear ();
  79. drcol.Add ("NewRelation", parentCol, childCol, false);
  80. Assert.That (drcol.Count, Is.EqualTo (1), "test#4");
  81. drcol.Clear ();
  82. drcol.Add ("NewRelation", parentCol, childCol, true);
  83. Assert.That (drcol.Count, Is.EqualTo (1), "test#5");
  84. drcol.Clear ();
  85. }
  86. [Test]
  87. [ExpectedException(typeof(ArgumentNullException))]
  88. [Ignore ("It does not pass under MS.NET")]
  89. public void AddException1 ()
  90. {
  91. DataRelationCollection drcol = _dataset.Relations;
  92. DataRelation drnull = null;
  93. drcol.Add (drnull);
  94. }
  95. [Test]
  96. [ExpectedException(typeof(ArgumentException))]
  97. public void AddException2 ()
  98. {
  99. DataRelationCollection drcol = _dataset.Relations;
  100. DataRelation dr1 = new DataRelation ("CustOrder"
  101. , _dataset.Tables ["Customer"].Columns ["custid"]
  102. , _dataset.Tables ["Order"].Columns ["custid"]);
  103. drcol.Add (dr1);
  104. drcol.Add (dr1);
  105. }
  106. [Test]
  107. [ExpectedException(typeof(DuplicateNameException))]
  108. public void AddException3 ()
  109. {
  110. DataRelationCollection drcol = _dataset.Relations;
  111. DataRelation dr1 = new DataRelation ("DuplicateName"
  112. , _dataset.Tables ["Customer"].Columns ["custid"]
  113. , _dataset.Tables ["Order"].Columns ["custid"]);
  114. DataRelation dr2 = new DataRelation ("DuplicateName"
  115. , _dataset.Tables ["Item"].Columns ["itemid"]
  116. , _dataset.Tables ["Order"].Columns ["custid"]);
  117. drcol.Add (dr1);
  118. drcol.Add (dr2);
  119. }
  120. [Test]
  121. public void AddRange ()
  122. {
  123. DataRelationCollection drcol = _dataset.Relations;
  124. DataRelation dr1 = new DataRelation ("CustOrder"
  125. , _dataset.Tables ["Customer"].Columns ["custid"]
  126. , _dataset.Tables ["Order"].Columns ["custid"]);
  127. DataRelation dr2 = new DataRelation ("ItemOrder"
  128. , _dataset.Tables ["Item"].Columns ["itemid"]
  129. , _dataset.Tables ["Order"].Columns ["custid"]);
  130. drcol.AddRange (new DataRelation[] {dr1,dr2});
  131. Assert.That (drcol [0].RelationName, Is.EqualTo ("CustOrder"), "test#1");
  132. Assert.That (drcol [1].RelationName, Is.EqualTo ("ItemOrder"), "test#2");
  133. }
  134. [Test]
  135. public void CanRemove ()
  136. {
  137. DataRelationCollection drcol = _dataset.Relations;
  138. DataColumn parentCol = _dataset.Tables ["Customer"].Columns ["custid"];
  139. DataColumn childCol = _dataset.Tables ["Order"].Columns ["custid"];
  140. DataRelation dr = new DataRelation ("CustOrder", parentCol, childCol);
  141. drcol.Add (dr);
  142. Assert.That (drcol.CanRemove (dr), Is.True, "test#1");
  143. Assert.That (drcol.CanRemove (null), Is.False, "test#2");
  144. DataRelation dr2 = new DataRelation ("ItemOrder"
  145. , _dataset.Tables ["Item"].Columns ["itemid"]
  146. , _dataset.Tables ["Order"].Columns ["custid"]);
  147. Assert.That (drcol.CanRemove (dr2), Is.False, "test#3");
  148. }
  149. [Test]
  150. public void Clear ()
  151. {
  152. DataRelationCollection drcol = _dataset.Relations;
  153. DataColumn parentCol = _dataset.Tables ["Customer"].Columns ["custid"];
  154. DataColumn childCol = _dataset.Tables ["Order"].Columns ["custid"];
  155. drcol.Add (new DataRelation ("CustOrder", parentCol, childCol));
  156. drcol.Add ("ItemOrder", _dataset.Tables ["Item"].Columns ["itemid"]
  157. , _dataset.Tables ["Order"].Columns ["itemid"]);
  158. drcol.Clear ();
  159. Assert.That (drcol.Count, Is.EqualTo (0), "test#1");
  160. }
  161. [Test]
  162. public void Contains ()
  163. {
  164. DataRelationCollection drcol = _dataset.Relations;
  165. DataColumn parentCol = _dataset.Tables ["Customer"].Columns ["custid"];
  166. DataColumn childCol = _dataset.Tables ["Order"].Columns ["custid"];
  167. DataRelation dr = new DataRelation ("CustOrder", parentCol, childCol);
  168. drcol.Add (dr);
  169. Assert.That (drcol.Contains (dr.RelationName), Is.True, "test#1");
  170. string drnull = "";
  171. Assert.That (drcol.Contains (drnull), Is.False, "test#2");
  172. dr = new DataRelation ("newRelation", childCol, parentCol);
  173. Assert.That (drcol.Contains ("NoSuchRelation"), Is.False, "test#3");
  174. }
  175. [Test]
  176. public void CopyTo ()
  177. {
  178. DataRelationCollection drcol = _dataset.Relations;
  179. drcol.Add ("CustOrder"
  180. , _dataset.Tables ["Customer"].Columns ["custid"]
  181. , _dataset.Tables ["Order"].Columns ["custid"]);
  182. drcol.Add ("ItemOrder"
  183. , _dataset.Tables ["Item"].Columns ["itemid"]
  184. , _dataset.Tables ["Order"].Columns ["custid"]);
  185. DataRelation [] array = new DataRelation[2];
  186. drcol.CopyTo (array, 0);
  187. Assert.That (array.Length, Is.EqualTo (2), "test#1");
  188. Assert.That (array [0].RelationName, Is.EqualTo ("CustOrder"), "test#2");
  189. Assert.That (array [1].RelationName, Is.EqualTo ("ItemOrder"), "test#3");
  190. DataRelation [] array1 = new DataRelation[4];
  191. drcol.CopyTo (array1, 2);
  192. Assert.That (array1 [0], Is.Null, "test#4");
  193. Assert.That (array1 [1], Is.Null, "test#5");
  194. Assert.That (array1 [2].RelationName, Is.EqualTo ("CustOrder"), "test#6");
  195. Assert.That (array1 [3].RelationName, Is.EqualTo ("ItemOrder"), "test#7");
  196. }
  197. [Test]
  198. public void Equals ()
  199. {
  200. DataRelationCollection drcol = _dataset.Relations;
  201. drcol.Add ("CustOrder"
  202. , _dataset.Tables ["Customer"].Columns ["custid"]
  203. , _dataset.Tables ["Order"].Columns ["custid"]);
  204. drcol.Add ("ItemOrder"
  205. , _dataset.Tables ["Item"].Columns ["itemid"]
  206. , _dataset.Tables ["Order"].Columns ["custid"]);
  207. DataSet newds = new DataSet ();
  208. DataRelationCollection drcol1 = newds.Relations;
  209. DataRelationCollection drcol2 = _dataset.Relations;
  210. Assert.That (drcol.Equals (drcol), Is.True, "test#1");
  211. Assert.That (drcol.Equals (drcol2), Is.True, "test#2");
  212. Assert.That (drcol1.Equals (drcol), Is.False, "test#3");
  213. Assert.That (drcol.Equals (drcol1), Is.False, "test#4");
  214. Assert.That (Object.Equals (drcol, drcol2), Is.True, "test#5");
  215. Assert.That (Object.Equals (drcol, drcol1), Is.False, "test#6");
  216. }
  217. [Test]
  218. public void IndexOf ()
  219. {
  220. DataRelationCollection drcol = _dataset.Relations;
  221. DataRelation dr1 = new DataRelation ("CustOrder"
  222. , _dataset.Tables ["Customer"].Columns ["custid"]
  223. , _dataset.Tables ["Order"].Columns ["custid"]);
  224. DataRelation dr2 = new DataRelation ("ItemOrder"
  225. , _dataset.Tables ["Item"].Columns ["itemid"]
  226. , _dataset.Tables ["Order"].Columns ["custid"]);
  227. drcol.Add (dr1);
  228. drcol.Add (dr2);
  229. Assert.That (drcol.IndexOf (dr1), Is.EqualTo (0), "test#1");
  230. Assert.That (drcol.IndexOf (dr2), Is.EqualTo (1), "test#2");
  231. Assert.That (drcol.IndexOf ("CustOrder"), Is.EqualTo (0), "test#3");
  232. Assert.That (drcol.IndexOf ("ItemOrder"), Is.EqualTo (1), "test#4");
  233. Assert.That (drcol.IndexOf (drcol [0]), Is.EqualTo (0), "test#5");
  234. Assert.That (drcol.IndexOf (drcol [1]), Is.EqualTo (1), "test#6");
  235. Assert.That (drcol.IndexOf ("_noRelation_"), Is.EqualTo (-1), "test#7");
  236. DataRelation newdr = new DataRelation ("newdr"
  237. , _dataset.Tables ["Customer"].Columns ["custid"]
  238. , _dataset.Tables ["Order"].Columns ["custid"]);
  239. Assert.That (drcol.IndexOf (newdr), Is.EqualTo (-1), "test#8");
  240. }
  241. [Test]
  242. public void Remove ()
  243. {
  244. DataRelationCollection drcol = _dataset.Relations;
  245. DataRelation dr1 = new DataRelation ("CustOrder"
  246. , _dataset.Tables ["Customer"].Columns ["custid"]
  247. , _dataset.Tables ["Order"].Columns ["custid"]);
  248. DataRelation dr2 = new DataRelation ("ItemOrder"
  249. , _dataset.Tables ["Item"].Columns ["itemid"]
  250. , _dataset.Tables ["Order"].Columns ["custid"]);
  251. drcol.Add (dr1);
  252. drcol.Add (dr2);
  253. drcol.Remove (dr1);
  254. Assert.That (drcol.Contains (dr1.RelationName), Is.False, "test#1");
  255. drcol.Add (dr1);
  256. drcol.Remove ("CustOrder");
  257. Assert.That (drcol.Contains ("CustOrder"), Is.False, "test#2");
  258. drcol.Add (dr1);
  259. DataRelation drnull = null;
  260. drcol.Remove (drnull);
  261. DataRelation newdr = new DataRelation ("newdr"
  262. , _dataset.Tables ["Customer"].Columns ["custid"]
  263. , _dataset.Tables ["Order"].Columns ["custid"]);
  264. try {
  265. drcol.Remove (newdr);
  266. Assert.Fail ("Err: removed relation which not part of collection");
  267. } catch (Exception e) {
  268. Assert.That (e, Is.TypeOf (typeof(ArgumentException)), "test#4");
  269. }
  270. try {
  271. drcol.Remove ("newdr");
  272. Assert.Fail ("Err: removed relation which not part of collection");
  273. } catch (ArgumentException e) {
  274. }
  275. }
  276. [Test]
  277. public void RemoveAt ()
  278. {
  279. DataRelationCollection drcol = _dataset.Relations;
  280. DataRelation dr1 = new DataRelation ("CustOrder"
  281. , _dataset.Tables ["Customer"].Columns ["custid"]
  282. , _dataset.Tables ["Order"].Columns ["custid"]);
  283. DataRelation dr2 = new DataRelation ("ItemOrder"
  284. , _dataset.Tables ["Item"].Columns ["itemid"]
  285. , _dataset.Tables ["Order"].Columns ["custid"]);
  286. drcol.Add (dr1);
  287. drcol.Add (dr2);
  288. try {
  289. drcol.RemoveAt (-1);
  290. Assert.Fail ("the index was out of bound: must have failed");
  291. } catch (IndexOutOfRangeException e) {
  292. }
  293. try {
  294. drcol.RemoveAt (101);
  295. Assert.Fail ("the index was out of bound: must have failed");
  296. } catch (IndexOutOfRangeException e) {
  297. }
  298. drcol.RemoveAt (1);
  299. Assert.That (drcol.Contains (dr2.RelationName), Is.False, "test#5");
  300. drcol.RemoveAt (0);
  301. Assert.That (drcol.Contains (dr1.RelationName), Is.False, "test#6");
  302. }
  303. //[Test]
  304. public void ToStringTest ()
  305. {
  306. DataRelationCollection drcol = _dataset.Relations;
  307. DataRelation dr1 = new DataRelation ("CustOrder"
  308. , _dataset.Tables ["Customer"].Columns ["custid"]
  309. , _dataset.Tables ["Order"].Columns ["custid"]);
  310. drcol.Add (dr1);
  311. Assert.That (drcol.ToString (), Is.EqualTo ("System.Data.DataRelationCollection"), "test#1");
  312. Console.WriteLine (drcol.ToString ());
  313. }
  314. }
  315. }