ConstraintCollectionTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // ConstraintCollection.cs - NUnit Test Cases for testing the ConstraintCollection
  2. // class.
  3. //
  4. // Authors:
  5. // Franklin Wise ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. // Roopa Wilson ([email protected])
  8. //
  9. // (C) Franklin Wise
  10. // (C) 2003 Martin Willemoes Hansen
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using NUnit.Framework;
  34. using System;
  35. using System.Data;
  36. namespace MonoTests.System.Data
  37. {
  38. [TestFixture]
  39. public class ConstraintCollectionTest : Assertion {
  40. private DataTable _table;
  41. private DataTable _table2;
  42. private Constraint _constraint1;
  43. private Constraint _constraint2;
  44. [SetUp]
  45. public void GetReady()
  46. {
  47. //Setup DataTable
  48. _table = new DataTable("TestTable");
  49. _table.Columns.Add("Col1",typeof(int));
  50. _table.Columns.Add("Col2",typeof(int));
  51. _table.Columns.Add("Col3",typeof(int));
  52. _table2 = new DataTable("TestTable");
  53. _table2.Columns.Add("Col1",typeof(int));
  54. _table2.Columns.Add("Col2",typeof(int));
  55. //Use UniqueConstraint to test Constraint Base Class
  56. _constraint1 = new UniqueConstraint(_table.Columns[0],false);
  57. _constraint2 = new UniqueConstraint(_table.Columns[1],false);
  58. // not sure why this is needed since a new _table was just created
  59. // for us, but this Clear() keeps the tests from throwing
  60. // an exception when the Add() is called.
  61. _table.Constraints.Clear();
  62. }
  63. [Test]
  64. public void Add()
  65. {
  66. ConstraintCollection col = _table.Constraints;
  67. col.Add(_constraint1);
  68. col.Add(_constraint2);
  69. AssertEquals("Count doesn't equal added.",2, col.Count);
  70. }
  71. [Test]
  72. public void AddExceptions()
  73. {
  74. ConstraintCollection col = _table.Constraints;
  75. //null
  76. try
  77. {
  78. col.Add(null);
  79. Fail("B1: Failed to throw ArgumentNullException.");
  80. }
  81. catch (ArgumentNullException) {}
  82. catch (AssertionException exc) {throw exc;}
  83. catch
  84. {
  85. Fail("A1: Wrong exception type");
  86. }
  87. //duplicate name
  88. try
  89. {
  90. _constraint1.ConstraintName = "Dog";
  91. _constraint2.ConstraintName = "dog"; //case insensitive
  92. col.Add(_constraint1);
  93. col.Add(_constraint2);
  94. #if NET_1_1
  95. #else
  96. Fail("Failed to throw Duplicate name exception.");
  97. #endif
  98. col.Remove (_constraint2); // only for !1.0
  99. col.Remove (_constraint1);
  100. }
  101. #if ! NET_1_1
  102. catch (DuplicateNameException) {
  103. }
  104. #endif
  105. catch (AssertionException exc) {throw exc;}
  106. /* Don't use such catch. They cover our eyes from the exact exception location.
  107. catch (Exception exc)
  108. {
  109. Fail("A2: Wrong exception type. " + exc.ToString());
  110. }
  111. */
  112. //Constraint Already exists
  113. try
  114. {
  115. col.Add(_constraint1);
  116. #if NET_1_1
  117. #else
  118. Fail("B2: Failed to throw ArgumentException.");
  119. #endif
  120. col.Remove (_constraint1);
  121. }
  122. catch (ArgumentException) {
  123. #if NET_1_1
  124. #else
  125. throw;
  126. #endif
  127. }
  128. catch (AssertionException exc) {throw exc;}
  129. catch
  130. {
  131. Fail("A3: Wrong exception type");
  132. }
  133. }
  134. [Test]
  135. public void Indexer()
  136. {
  137. Constraint c1 = new UniqueConstraint(_table.Columns[0]);
  138. Constraint c2 = new UniqueConstraint(_table.Columns[1]);
  139. c1.ConstraintName = "first";
  140. c2.ConstraintName = "second";
  141. _table.Constraints.Add(c1);
  142. _table.Constraints.Add(c2);
  143. AssertSame("A1", c1, _table.Constraints[0]);
  144. AssertSame("A2", c2, _table.Constraints[1]);
  145. AssertSame("A3", c1, _table.Constraints["first"]);
  146. AssertSame("A4", c2, _table.Constraints["sEcond"]); //case insensitive
  147. }
  148. [Test]
  149. public void IndexOf()
  150. {
  151. Constraint c1 = new UniqueConstraint(_table.Columns[0]);
  152. Constraint c2 = new UniqueConstraint(_table.Columns[1]);
  153. c1.ConstraintName = "first";
  154. c2.ConstraintName = "second";
  155. _table.Constraints.Add(c1);
  156. _table.Constraints.Add(c2);
  157. AssertEquals("A1", 0, _table.Constraints.IndexOf(c1));
  158. AssertEquals("A2", 1, _table.Constraints.IndexOf(c2));
  159. AssertEquals("A3", 0, _table.Constraints.IndexOf("first"));
  160. AssertEquals("A4", 1, _table.Constraints.IndexOf("second"));
  161. }
  162. [Test]
  163. public void Contains()
  164. {
  165. Constraint c1 = new UniqueConstraint(_table.Columns[0]);
  166. Constraint c2 = new UniqueConstraint(_table.Columns[1]);
  167. c1.ConstraintName = "first";
  168. c2.ConstraintName = "second";
  169. _table.Constraints.Add(c1);
  170. Assert("A1", _table.Constraints.Contains(c1.ConstraintName)); //true
  171. Assert("A2", _table.Constraints.Contains(c2.ConstraintName) == false); //doesn't contain
  172. }
  173. [Test]
  174. public void IndexerFailures()
  175. {
  176. _table.Constraints.Add(new UniqueConstraint(_table.Columns[0]));
  177. //This doesn't throw
  178. AssertNull(_table.Constraints["notInCollection"]);
  179. //Index too high
  180. try
  181. {
  182. Constraint c = _table.Constraints[_table.Constraints.Count];
  183. Fail("B1: Failed to throw IndexOutOfRangeException.");
  184. }
  185. catch (IndexOutOfRangeException) {}
  186. catch (AssertionException exc) {throw exc;}
  187. catch
  188. {
  189. Fail("A1: Wrong exception type");
  190. }
  191. //Index too low
  192. try
  193. {
  194. Constraint c = _table.Constraints[-1];
  195. Fail("B2: Failed to throw IndexOutOfRangeException.");
  196. }
  197. catch (IndexOutOfRangeException) {}
  198. catch (AssertionException exc) {throw exc;}
  199. catch
  200. {
  201. Fail("A2: Wrong exception type");
  202. }
  203. }
  204. [Test]
  205. public void AddFkException1()
  206. {
  207. DataSet ds = new DataSet();
  208. ds.Tables.Add(_table);
  209. _table2.TableName = "TestTable2";
  210. ds.Tables.Add(_table2);
  211. _table.Rows.Add(new object [] {1});
  212. _table.Rows.Add(new object [] {1});
  213. //FKC: can't create unique constraint because duplicate values already exist
  214. try
  215. {
  216. ForeignKeyConstraint fkc = new ForeignKeyConstraint( _table.Columns[0],
  217. _table2.Columns[0]);
  218. _table2.Constraints.Add(fkc); //should throw
  219. Fail("B1: Failed to throw ArgumentException.");
  220. }
  221. catch (ArgumentException) {}
  222. catch (AssertionException exc) {throw exc;}
  223. catch (Exception exc)
  224. {
  225. Fail("A1: Wrong Exception type. " + exc.ToString());
  226. }
  227. }
  228. [Test]
  229. public void AddFkException2()
  230. {
  231. //Foreign key rules only work when the tables
  232. //are apart of the dataset
  233. DataSet ds = new DataSet();
  234. ds.Tables.Add(_table);
  235. _table2.TableName = "TestTable2";
  236. ds.Tables.Add(_table2);
  237. _table.Rows.Add(new object [] {1});
  238. // will need a matching parent value in
  239. // _table
  240. _table2.Rows.Add(new object [] {3});
  241. //FKC: no matching parent value
  242. try
  243. {
  244. ForeignKeyConstraint fkc = new ForeignKeyConstraint( _table.Columns[0],
  245. _table2.Columns[0]);
  246. _table2.Constraints.Add(fkc); //should throw
  247. Fail("B1: Failed to throw ArgumentException.");
  248. }
  249. catch (ArgumentException) {}
  250. catch (AssertionException exc) {throw exc;}
  251. catch (Exception exc)
  252. {
  253. Fail("A1: Wrong Exception type. " + exc.ToString());
  254. }
  255. }
  256. [Test]
  257. public void AddUniqueExceptions()
  258. {
  259. //UC: can't create unique constraint because duplicate values already exist
  260. try
  261. {
  262. _table.Rows.Add(new object [] {1});
  263. _table.Rows.Add(new object [] {1});
  264. UniqueConstraint uc = new UniqueConstraint( _table.Columns[0]);
  265. _table.Constraints.Add(uc); //should throw
  266. Fail("B1: Failed to throw ArgumentException.");
  267. }
  268. catch (ArgumentException) {}
  269. catch (AssertionException exc) {throw exc;}
  270. catch (Exception exc)
  271. {
  272. Fail("A1: Wrong Exception type. " + exc.ToString());
  273. }
  274. }
  275. [Test]
  276. //Tests AddRange (), CanRemove (), RemoveAt (), Remove (), Exceptions of Remove(), and Clear ()
  277. public void AddRemoveTest ()
  278. {
  279. AddRange ();
  280. // CanRemove (); This test is ignored
  281. Remove ();
  282. // RemoveAt (); This test is ignored
  283. // This test is expected to be failed, so don't reuse it.
  284. // RemoveExceptions ();
  285. _table.Constraints.Remove (_table.Constraints [0]);
  286. Clear ();
  287. }
  288. [Test]
  289. public void AddRange()
  290. {
  291. _constraint1.ConstraintName = "UK1";
  292. _constraint2.ConstraintName = "UK12";
  293. ForeignKeyConstraint _constraint3 = new ForeignKeyConstraint ("FK2", _table.Columns [0],
  294. _table2.Columns [0]);
  295. UniqueConstraint _constraint4=new UniqueConstraint("UK2", _table2.Columns [1]);
  296. // Add the constraints.
  297. Constraint [] constraints = {_constraint1, _constraint2};
  298. _table.Constraints.AddRange (constraints);
  299. Constraint [] constraints1 = {_constraint3, _constraint4};
  300. _table2.Constraints.AddRange (constraints1);
  301. AssertEquals ("A1", "UK1", _table.Constraints [0].ConstraintName);
  302. AssertEquals ("A2", "UK12", _table.Constraints [1].ConstraintName);
  303. AssertEquals ("A3", "FK2", _table2.Constraints [0].ConstraintName);
  304. AssertEquals ("A4", "UK2", _table2.Constraints [1].ConstraintName);
  305. }
  306. [Test]
  307. [Category ("NotDotNet")]
  308. // Even after EndInit(), MS.NET does not fill Table property
  309. // on UniqueConstraint.
  310. public void TestAddRange2()
  311. {
  312. DataTable table = new DataTable ("Table");
  313. DataColumn column1 = new DataColumn ("col1");
  314. DataColumn column2 = new DataColumn ("col2");
  315. DataColumn column3 = new DataColumn ("col3");
  316. table.Columns.Add (column1);
  317. table.Columns.Add (column2);
  318. table.Columns.Add (column3);
  319. string []columnNames = {"col1", "col2", "col3"};
  320. Constraint []constraints = new Constraint[3];
  321. constraints [0] = new UniqueConstraint ("Unique1",column1);
  322. constraints [1] = new UniqueConstraint ("Unique2",column2);
  323. constraints [2] = new UniqueConstraint ("Unique3", columnNames, true);
  324. table.BeginInit();
  325. //Console.WriteLine(table.InitStatus == DataTable.initStatus.BeginInit);
  326. table.Constraints.AddRange (constraints);
  327. //Check the table property of UniqueConstraint Object
  328. try{
  329. Assertion.AssertNull ("#01", constraints [2].Table);
  330. }
  331. catch (Exception e) {
  332. Assertion.Assert ("#A02", "System.NullReferenceException".Equals (e.GetType().ToString()));
  333. }
  334. table.EndInit();
  335. // After EndInit is called the constraints associated with most recent call to AddRange() must be
  336. // added to the ConstraintCollection
  337. Assertion.Assert ("#A03", constraints [2].Table.ToString().Equals ("Table"));
  338. Assertion.Assert ("#A04", table.Constraints.Contains ("Unique1"));
  339. Assertion.Assert ("#A05", table.Constraints.Contains ("Unique2"));
  340. Assertion.Assert ("#A06", table.Constraints.Contains ("Unique3"));
  341. }
  342. [Test]
  343. public void Clear()
  344. {
  345. try {
  346. _table.Constraints.Clear (); //Clear all constraints
  347. AssertEquals ("A1", 0, _table.Constraints.Count); //No constraints should remain
  348. _table2.Constraints.Clear ();
  349. AssertEquals ("A2", 0, _table2.Constraints.Count);
  350. }
  351. catch (Exception e) {
  352. Console.WriteLine (e);
  353. }
  354. }
  355. [Test]
  356. [Ignore ("This never works on MS.NET (and it should not)")]
  357. public void CanRemove()
  358. {
  359. AssertEquals ("A1", false, _table.Constraints.CanRemove (_table.Constraints [0]));
  360. }
  361. [Test]
  362. public void CollectionChanged()
  363. {
  364. }
  365. #if false
  366. //
  367. // If this fails on MS.NET and its supposed to fail, why do we have this as Ignore?
  368. //
  369. [Test]
  370. [Ignore ("MS.NET fails this test (and it should fail)")]
  371. public void RemoveAt()
  372. {
  373. _table2.Constraints.RemoveAt (1); //Remove constraint and again add it
  374. AssertEquals ("A1", 1, _table2.Constraints.Count); UniqueConstraint _constraint4 = new UniqueConstraint ("UK2", _table2.Columns [1]);
  375. // Add the constraints.
  376. Constraint [] constraints = {_constraint4};
  377. _table.Constraints.AddRange (constraints);
  378. }
  379. #endif
  380. //[Test]
  381. [Ignore ("MS.NET fails this test (and it should fail)")]
  382. public void Remove()
  383. {
  384. _table2.Constraints.Remove (_table2.Constraints [1]); //Remove constraint and again add it
  385. AssertEquals ("A1", 1, _table2.Constraints.Count);
  386. UniqueConstraint _constraint4 = new UniqueConstraint ("UK2", _table2.Columns [1]);
  387. // Add the constraints.
  388. Constraint [] constraints = {_constraint4};
  389. _table2.Constraints.AddRange (constraints);
  390. }
  391. [Test]
  392. public void RemoveExceptions()
  393. {
  394. try {
  395. //Remove constraint that cannot be removed
  396. _table.Constraints.Remove (_table.Constraints [0]);
  397. Fail ("A1");
  398. } catch (Exception e) {
  399. AssertEquals ("A2", typeof (IndexOutOfRangeException), e.GetType ());
  400. }
  401. }
  402. }
  403. }