ForeignKeyConstraintTest.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // ForeignKeyConstraintTest.cs - NUnit Test Cases for [explain here]
  2. //
  3. // Authors:
  4. // Franklin Wise ([email protected])
  5. // Martin Willemoes Hansen ([email protected])
  6. //
  7. // (C) Franklin Wise
  8. // (C) 2003 Martin Willemoes Hansen
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using NUnit.Framework;
  33. using System;
  34. using System.Data;
  35. namespace MonoTests.System.Data
  36. {
  37. [TestFixture]
  38. public class ForeignKeyConstraintTest : Assertion
  39. {
  40. private DataSet _ds;
  41. //NOTE: fk constraints only work when the table is part of a DataSet
  42. [SetUp]
  43. public void GetReady()
  44. {
  45. _ds = new DataSet();
  46. //Setup DataTable
  47. DataTable table;
  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. _ds.Tables.Add(table);
  53. table = new DataTable("TestTable2");
  54. table.Columns.Add("Col1",typeof(int));
  55. table.Columns.Add("Col2",typeof(int));
  56. table.Columns.Add("Col3",typeof(int));
  57. _ds.Tables.Add(table);
  58. }
  59. // Tests ctor (string, DataColumn, DataColumn)
  60. [Test]
  61. public void Ctor1 ()
  62. {
  63. DataTable Table = _ds.Tables [0];
  64. AssertEquals ("test#01", 0, Table.Constraints.Count);
  65. Table = _ds.Tables [1];
  66. AssertEquals ("test#02", 0, Table.Constraints.Count);
  67. // ctor (string, DataColumn, DataColumn
  68. ForeignKeyConstraint Constraint = new ForeignKeyConstraint ("test", _ds.Tables [0].Columns [2], _ds.Tables [1].Columns [0]);
  69. Table = _ds.Tables [1];
  70. Table.Constraints.Add (Constraint);
  71. AssertEquals ("test#03", 1, Table.Constraints.Count);
  72. AssertEquals ("test#04", "test", Table.Constraints [0].ConstraintName);
  73. AssertEquals ("test#05", typeof (ForeignKeyConstraint), Table.Constraints [0].GetType ());
  74. Table = _ds.Tables [0];
  75. AssertEquals ("test#06", 1, Table.Constraints.Count);
  76. AssertEquals ("test#07", "Constraint1", Table.Constraints [0].ConstraintName);
  77. AssertEquals ("test#08", typeof (UniqueConstraint), Table.Constraints [0].GetType ());
  78. }
  79. // Tests ctor (DataColumn, DataColumn)
  80. [Test]
  81. public void Ctor2 ()
  82. {
  83. DataTable Table = _ds.Tables [0];
  84. AssertEquals ("test#01", 0, Table.Constraints.Count);
  85. Table = _ds.Tables [1];
  86. AssertEquals ("test#02", 0, Table.Constraints.Count);
  87. // ctor (string, DataColumn, DataColumn
  88. ForeignKeyConstraint Constraint = new ForeignKeyConstraint (_ds.Tables [0].Columns [2], _ds.Tables [1].Columns [0]);
  89. Table = _ds.Tables [1];
  90. Table.Constraints.Add (Constraint);
  91. AssertEquals ("test#03", 1, Table.Constraints.Count);
  92. AssertEquals ("test#04", "Constraint1", Table.Constraints [0].ConstraintName);
  93. AssertEquals ("test#05", typeof (ForeignKeyConstraint), Table.Constraints [0].GetType ());
  94. Table = _ds.Tables [0];
  95. AssertEquals ("test#06", 1, Table.Constraints.Count);
  96. AssertEquals ("test#07", "Constraint1", Table.Constraints [0].ConstraintName);
  97. AssertEquals ("test#08", typeof (UniqueConstraint), Table.Constraints [0].GetType ());
  98. }
  99. // Test ctor (DataColumn [], DataColumn [])
  100. [Test]
  101. public void Ctor3 ()
  102. {
  103. DataTable Table = _ds.Tables [0];
  104. AssertEquals ("test#01", 0, Table.Constraints.Count);
  105. Table = _ds.Tables [1];
  106. AssertEquals ("test#02", 0, Table.Constraints.Count);
  107. DataColumn [] Cols1 = new DataColumn [2];
  108. Cols1 [0] = _ds.Tables [0].Columns [1];
  109. Cols1 [1] = _ds.Tables [0].Columns [2];
  110. DataColumn [] Cols2 = new DataColumn [2];
  111. Cols2 [0] = _ds.Tables [1].Columns [0];
  112. Cols2 [1] = _ds.Tables [1].Columns [1];
  113. ForeignKeyConstraint Constraint = new ForeignKeyConstraint (Cols1, Cols2);
  114. Table = _ds.Tables [1];
  115. Table.Constraints.Add (Constraint);
  116. AssertEquals ("test#03", 1, Table.Constraints.Count);
  117. AssertEquals ("test#04", "Constraint1", Table.Constraints [0].ConstraintName);
  118. AssertEquals ("test#05", typeof (ForeignKeyConstraint), Table.Constraints [0].GetType ());
  119. Table = _ds.Tables [0];
  120. AssertEquals ("test#06", 1, Table.Constraints.Count);
  121. AssertEquals ("test#07", "Constraint1", Table.Constraints [0].ConstraintName);
  122. AssertEquals ("test#08", typeof (UniqueConstraint), Table.Constraints [0].GetType ());
  123. }
  124. // Tests ctor (string, DataColumn [], DataColumn [])
  125. [Test]
  126. public void Ctor4 ()
  127. {
  128. DataTable Table = _ds.Tables [0];
  129. AssertEquals ("test#01", 0, Table.Constraints.Count);
  130. Table = _ds.Tables [1];
  131. AssertEquals ("test#02", 0, Table.Constraints.Count);
  132. DataColumn [] Cols1 = new DataColumn [2];
  133. Cols1 [0] = _ds.Tables [0].Columns [1];
  134. Cols1 [1] = _ds.Tables [0].Columns [2];
  135. DataColumn [] Cols2 = new DataColumn [2];
  136. Cols2 [0] = _ds.Tables [1].Columns [0];
  137. Cols2 [1] = _ds.Tables [1].Columns [1];
  138. ForeignKeyConstraint Constraint = new ForeignKeyConstraint ("Test", Cols1, Cols2);
  139. Table = _ds.Tables [1];
  140. Table.Constraints.Add (Constraint);
  141. AssertEquals ("test#03", 1, Table.Constraints.Count);
  142. AssertEquals ("test#04", "Test", Table.Constraints [0].ConstraintName);
  143. AssertEquals ("test#05", typeof (ForeignKeyConstraint), Table.Constraints [0].GetType ());
  144. Table = _ds.Tables [0];
  145. AssertEquals ("test#06", 1, Table.Constraints.Count);
  146. AssertEquals ("test#07", "Constraint1", Table.Constraints [0].ConstraintName);
  147. AssertEquals ("test#08", typeof (UniqueConstraint), Table.Constraints [0].GetType ());
  148. }
  149. [Test]
  150. public void TestCtor5()
  151. {
  152. DataTable table1 = new DataTable ("Table1");
  153. DataTable table2 = new DataTable ("Table2");
  154. DataSet dataSet = new DataSet();
  155. dataSet.Tables.Add (table1);
  156. dataSet.Tables.Add (table2);
  157. DataColumn column1 = new DataColumn ("col1");
  158. DataColumn column2 = new DataColumn ("col2");
  159. DataColumn column3 = new DataColumn ("col3");
  160. table1.Columns.Add (column1);
  161. table1.Columns.Add (column2);
  162. table1.Columns.Add (column3);
  163. DataColumn column4 = new DataColumn ("col4");
  164. DataColumn column5 = new DataColumn ("col5");
  165. DataColumn column6 = new DataColumn ("col6");
  166. table2.Columns.Add (column4);
  167. table2.Columns.Add (column5);
  168. table2.Columns.Add (column6);
  169. string []parentColumnNames = {"col1", "col2", "col3"};
  170. string []childColumnNames = {"col4", "col5", "col6"};
  171. string parentTableName = "table1";
  172. // Create a ForeingKeyConstraint Object using the constructor
  173. // ForeignKeyConstraint (string, string, string[], string[], AcceptRejectRule, Rule, Rule);
  174. ForeignKeyConstraint fkc = new ForeignKeyConstraint ("hello world", parentTableName, parentColumnNames, childColumnNames, AcceptRejectRule.Cascade, Rule.Cascade, Rule.Cascade); // Assert that the Constraint object does not belong to any table yet
  175. #if NET_1_1
  176. try {
  177. DataTable tmp = fkc.Table;
  178. Fail ("When table is null, get_Table causes an InvalidOperationException.");
  179. } catch (InvalidOperationException) {
  180. }
  181. #else
  182. Assertion.AssertEquals ("#A01 Table should not be set", fkc.Table, null);
  183. #endif
  184. Constraint []constraints = new Constraint[3];
  185. constraints [0] = new UniqueConstraint (column1);
  186. constraints [1] = new UniqueConstraint (column2);
  187. constraints [2] = fkc;
  188. // Try to add the constraint to ConstraintCollection of the DataTable through Add()
  189. try{
  190. table2.Constraints.Add (fkc);
  191. throw new ApplicationException ("An Exception was expected");
  192. }
  193. // LAMESPEC : spec says InvalidConstraintException but throws this
  194. catch (ArgumentException) {
  195. }
  196. #if false // FIXME: Here this test crashes under MS.NET.
  197. // OK - So AddRange() is the only way!
  198. table2.Constraints.AddRange (constraints);
  199. // After AddRange(), Check the properties of ForeignKeyConstraint object
  200. Assertion.Assert("#A04", fkc.RelatedColumns [0].ColumnName.Equals ("col1")); Assertion.Assert("#A05", fkc.RelatedColumns [1].ColumnName.Equals ("col2")); Assertion.Assert("#A06", fkc.RelatedColumns [2].ColumnName.Equals ("col3")); Assertion.Assert("#A07", fkc.Columns [0].ColumnName.Equals ("col4"));
  201. Assertion.Assert("#A08", fkc.Columns [1].ColumnName.Equals ("col5"));
  202. Assertion.Assert("#A09", fkc.Columns [2].ColumnName.Equals ("col6"));
  203. #endif
  204. // Try to add columns with names which do not exist in the table
  205. parentColumnNames [2] = "noColumn";
  206. ForeignKeyConstraint foreignKeyConstraint = new ForeignKeyConstraint ("hello world", parentTableName, parentColumnNames, childColumnNames, AcceptRejectRule.Cascade, Rule.Cascade, Rule.Cascade);
  207. constraints [0] = new UniqueConstraint (column1);
  208. constraints [1] = new UniqueConstraint (column2);
  209. constraints [2] = foreignKeyConstraint;
  210. try{
  211. table2.Constraints.AddRange (constraints);
  212. throw new ApplicationException ("An Exception was expected");
  213. }
  214. catch (ArgumentException e) {
  215. }
  216. catch (InvalidConstraintException e){ // Could not test on ms.net, as ms.net does not reach here so far.
  217. }
  218. #if false // FIXME: Here this test crashes under MS.NET.
  219. // Check whether the child table really contains the foreign key constraint named "hello world"
  220. Assertion.Assert("#A11 ", table2.Constraints.Contains ("hello world"));
  221. #endif
  222. }
  223. // If Childs and parents are in same table
  224. [Test]
  225. public void KeyBetweenColumns ()
  226. {
  227. DataTable Table = _ds.Tables [0];
  228. AssertEquals ("test#01", 0, Table.Constraints.Count);
  229. Table = _ds.Tables [1];
  230. AssertEquals ("test#02", 0, Table.Constraints.Count);
  231. ForeignKeyConstraint Constraint = new ForeignKeyConstraint ("Test", _ds.Tables [0].Columns [0], _ds.Tables [0].Columns [2]);
  232. Table = _ds.Tables [0];
  233. Table.Constraints.Add (Constraint);
  234. AssertEquals ("test#03", 2, Table.Constraints.Count);
  235. AssertEquals ("test#04", "Constraint1", Table.Constraints [0].ConstraintName);
  236. AssertEquals ("test#05", typeof (UniqueConstraint), Table.Constraints [0].GetType ());
  237. AssertEquals ("test#04", "Test", Table.Constraints [1].ConstraintName);
  238. AssertEquals ("test#05", typeof (ForeignKeyConstraint), Table.Constraints [1].GetType ());
  239. }
  240. [Test]
  241. public void CtorExceptions ()
  242. {
  243. ForeignKeyConstraint fkc;
  244. DataTable localTable = new DataTable();
  245. localTable.Columns.Add("Col1",typeof(int));
  246. localTable.Columns.Add("Col2",typeof(bool));
  247. //Null
  248. try
  249. {
  250. fkc = new ForeignKeyConstraint((DataColumn)null,(DataColumn)null);
  251. Fail("Failed to throw ArgumentNullException.");
  252. }
  253. #if NET_1_1
  254. catch (NullReferenceException) {}
  255. #else
  256. catch (ArgumentNullException) {}
  257. #endif
  258. catch (AssertionException exc) {throw exc;}
  259. catch (Exception exc)
  260. {
  261. Fail("A1: Wrong Exception type. " + exc.ToString());
  262. }
  263. //zero length collection
  264. try
  265. {
  266. fkc = new ForeignKeyConstraint(new DataColumn[]{},new DataColumn[]{});
  267. Fail("B1: Failed to throw ArgumentException.");
  268. }
  269. catch (ArgumentException) {}
  270. catch (AssertionException exc) {throw exc;}
  271. catch (Exception exc)
  272. {
  273. Fail("A2: Wrong Exception type. " + exc.ToString());
  274. }
  275. //different datasets
  276. try
  277. {
  278. fkc = new ForeignKeyConstraint(_ds.Tables[0].Columns[0], localTable.Columns[0]);
  279. Fail("Failed to throw InvalidOperationException.");
  280. }
  281. catch (InvalidOperationException) {}
  282. catch (AssertionException exc) {throw exc;}
  283. catch (Exception exc)
  284. {
  285. Fail("A3: Wrong Exception type. " + exc.ToString());
  286. }
  287. try
  288. {
  289. fkc = new ForeignKeyConstraint(_ds.Tables[0].Columns[0], localTable.Columns[1]);
  290. Fail("Failed to throw InvalidConstraintException.");
  291. }
  292. #if NET_1_1
  293. // tables in different datasets
  294. catch (InvalidOperationException) {}
  295. #else
  296. //different dataTypes
  297. catch (InvalidConstraintException) {}
  298. #endif
  299. // Cannot create a Key from Columns that belong to
  300. // different tables.
  301. try
  302. {
  303. fkc = new ForeignKeyConstraint(new DataColumn [] {_ds.Tables[0].Columns[0], _ds.Tables[0].Columns[1]}, new DataColumn [] {localTable.Columns[1], _ds.Tables[1].Columns [0]});
  304. Fail("Failed to throw InvalidOperationException.");
  305. }
  306. catch (InvalidConstraintException) {}
  307. catch (AssertionException exc) {throw exc;}
  308. }
  309. [Test]
  310. public void CtorExceptions2 ()
  311. {
  312. DataColumn col = new DataColumn("MyCol1",typeof(int));
  313. ForeignKeyConstraint fkc;
  314. //Columns must belong to a Table
  315. try
  316. {
  317. fkc = new ForeignKeyConstraint(col, _ds.Tables[0].Columns[0]);
  318. Fail("FTT1: Failed to throw ArgumentException.");
  319. }
  320. catch (ArgumentException) {}
  321. catch (AssertionException exc) {throw exc;}
  322. // catch (Exception exc)
  323. // {
  324. // Fail("WET1: Wrong Exception type. " + exc.ToString());
  325. // }
  326. //Columns must belong to the same table
  327. //InvalidConstraintException
  328. DataColumn [] difTable = new DataColumn [] {_ds.Tables[0].Columns[2],
  329. _ds.Tables[1].Columns[0]};
  330. try
  331. {
  332. fkc = new ForeignKeyConstraint(difTable,new DataColumn[] {
  333. _ds.Tables[0].Columns[1],
  334. _ds.Tables[0].Columns[0]});
  335. Fail("FTT2: Failed to throw InvalidConstraintException.");
  336. }
  337. catch (InvalidConstraintException) {}
  338. catch (AssertionException exc) {throw exc;}
  339. catch (Exception exc)
  340. {
  341. Fail("WET2: Wrong Exception type. " + exc.ToString());
  342. }
  343. //parent columns and child columns should be the same length
  344. //ArgumentException
  345. DataColumn [] twoCol =
  346. new DataColumn [] {_ds.Tables[0].Columns[0],_ds.Tables[0].Columns[1]};
  347. try
  348. {
  349. fkc = new ForeignKeyConstraint(twoCol,
  350. new DataColumn[] { _ds.Tables[0].Columns[0]});
  351. Fail("FTT3: Failed to throw ArgumentException.");
  352. }
  353. catch (ArgumentException) {}
  354. catch (AssertionException exc) {throw exc;}
  355. catch (Exception exc)
  356. {
  357. Fail("WET3: Wrong Exception type. " + exc.ToString());
  358. }
  359. //InvalidOperation: Parent and child are the same column.
  360. try
  361. {
  362. fkc = new ForeignKeyConstraint( _ds.Tables[0].Columns[0],
  363. _ds.Tables[0].Columns[0] );
  364. Fail("FTT4: Failed to throw InvalidOperationException.");
  365. }
  366. catch (InvalidOperationException) {}
  367. catch (AssertionException exc) {throw exc;}
  368. catch (Exception exc)
  369. {
  370. Fail("WET4: Wrong Exception type. " + exc.ToString());
  371. }
  372. }
  373. [Test]
  374. public void EqualsAndHashCode()
  375. {
  376. DataTable tbl = _ds.Tables[0];
  377. DataTable tbl2 = _ds.Tables[1];
  378. ForeignKeyConstraint fkc = new ForeignKeyConstraint(
  379. new DataColumn[] {tbl.Columns[0], tbl.Columns[1]} ,
  380. new DataColumn[] {tbl2.Columns[0], tbl2.Columns[1]} );
  381. ForeignKeyConstraint fkc2 = new ForeignKeyConstraint(
  382. new DataColumn[] {tbl.Columns[0], tbl.Columns[1]} ,
  383. new DataColumn[] {tbl2.Columns[0], tbl2.Columns[1]} );
  384. ForeignKeyConstraint fkcDiff =
  385. new ForeignKeyConstraint( tbl.Columns[1], tbl.Columns[2]);
  386. Assert( "Equals failed. 1" , fkc.Equals(fkc2));
  387. Assert( "Equals failed. 2" , fkc2.Equals(fkc));
  388. Assert( "Equals failed. 3" , fkc.Equals(fkc));
  389. Assert( "Equals failed diff. 1" , fkc.Equals(fkcDiff) == false);
  390. Assert( "Hash Code Failed. 1", fkc.GetHashCode() == fkc2.GetHashCode() );
  391. Assert( "Hash Code Failed. 2", fkc.GetHashCode() != fkcDiff.GetHashCode() );
  392. }
  393. [Test]
  394. [ExpectedException (typeof (ArgumentException))]
  395. public void ViolationTest ()
  396. {
  397. DataTable parent = _ds.Tables [0];
  398. DataTable child = _ds.Tables [1];
  399. parent.Rows.Add (new object [] {1, 1, 1});
  400. child.Rows.Add (new object [] {2, 2, 2});
  401. try {
  402. child.Constraints.Add (new ForeignKeyConstraint ( parent.Columns [0],
  403. child.Columns [0])
  404. );
  405. } finally {
  406. // clear the rows for further testing
  407. _ds.Clear ();
  408. }
  409. }
  410. [Test]
  411. public void NoViolationTest ()
  412. {
  413. DataTable parent = _ds.Tables [0];
  414. DataTable child = _ds.Tables [1];
  415. parent.Rows.Add (new object [] {1, 1, 1});
  416. child.Rows.Add (new object [] {2, 2, 2});
  417. try {
  418. _ds.EnforceConstraints = false;
  419. child.Constraints.Add (new ForeignKeyConstraint ( parent.Columns [0],
  420. child.Columns [0])
  421. );
  422. } finally {
  423. // clear the rows for further testing
  424. _ds.Clear ();
  425. _ds.EnforceConstraints = true;
  426. }
  427. }
  428. [Test]
  429. public void ModifyParentKeyBeforeAcceptChanges ()
  430. {
  431. DataSet ds1 = new DataSet();
  432. DataTable t1= ds1.Tables.Add ("t1");
  433. DataTable t2= ds1.Tables.Add ("t2");
  434. t1.Columns.Add ("col1", typeof (int));
  435. t2.Columns.Add ("col2", typeof (int));
  436. ds1.Relations.Add ("fk", t1.Columns [0], t2.Columns [0]);
  437. t1.Rows.Add (new object[] {10});
  438. t2.Rows.Add (new object [] {10});
  439. t1.Rows [0][0]=20;
  440. Assert("#1", (int)t2.Rows [0][0] == 20);
  441. }
  442. }
  443. }