1 // ForeignKeyConstraintTest.cs - NUnit Test Cases for [explain here]
4 // Franklin Wise (gracenote@earthlink.net)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
8 // (C) 2003 Martin Willemoes Hansen
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
34 using NUnit.Framework;
38 namespace MonoTests.System.Data
41 public class ForeignKeyConstraintTest : Assertion
45 //NOTE: fk constraints only work when the table is part of a DataSet
48 public void GetReady()
54 table = new DataTable("TestTable");
55 table.Columns.Add("Col1",typeof(int));
56 table.Columns.Add("Col2",typeof(int));
57 table.Columns.Add("Col3",typeof(int));
59 _ds.Tables.Add(table);
61 table = new DataTable("TestTable2");
62 table.Columns.Add("Col1",typeof(int));
63 table.Columns.Add("Col2",typeof(int));
64 table.Columns.Add("Col3",typeof(int));
66 _ds.Tables.Add(table);
70 // Tests ctor (string, DataColumn, DataColumn)
74 DataTable Table = _ds.Tables [0];
76 AssertEquals ("test#01", 0, Table.Constraints.Count);
77 Table = _ds.Tables [1];
78 AssertEquals ("test#02", 0, Table.Constraints.Count);
80 // ctor (string, DataColumn, DataColumn
81 ForeignKeyConstraint Constraint = new ForeignKeyConstraint ("test", _ds.Tables [0].Columns [2], _ds.Tables [1].Columns [0]);
82 Table = _ds.Tables [1];
83 Table.Constraints.Add (Constraint);
85 AssertEquals ("test#03", 1, Table.Constraints.Count);
86 AssertEquals ("test#04", "test", Table.Constraints [0].ConstraintName);
87 AssertEquals ("test#05", typeof (ForeignKeyConstraint), Table.Constraints [0].GetType ());
89 Table = _ds.Tables [0];
90 AssertEquals ("test#06", 1, Table.Constraints.Count);
91 AssertEquals ("test#07", "Constraint1", Table.Constraints [0].ConstraintName);
92 AssertEquals ("test#08", typeof (UniqueConstraint), Table.Constraints [0].GetType ());
95 // Tests ctor (DataColumn, DataColumn)
99 DataTable Table = _ds.Tables [0];
101 AssertEquals ("test#01", 0, Table.Constraints.Count);
102 Table = _ds.Tables [1];
103 AssertEquals ("test#02", 0, Table.Constraints.Count);
105 // ctor (string, DataColumn, DataColumn
106 ForeignKeyConstraint Constraint = new ForeignKeyConstraint (_ds.Tables [0].Columns [2], _ds.Tables [1].Columns [0]);
107 Table = _ds.Tables [1];
108 Table.Constraints.Add (Constraint);
110 AssertEquals ("test#03", 1, Table.Constraints.Count);
111 AssertEquals ("test#04", "Constraint1", Table.Constraints [0].ConstraintName);
112 AssertEquals ("test#05", typeof (ForeignKeyConstraint), Table.Constraints [0].GetType ());
114 Table = _ds.Tables [0];
115 AssertEquals ("test#06", 1, Table.Constraints.Count);
116 AssertEquals ("test#07", "Constraint1", Table.Constraints [0].ConstraintName);
117 AssertEquals ("test#08", typeof (UniqueConstraint), Table.Constraints [0].GetType ());
120 // Test ctor (DataColumn [], DataColumn [])
124 DataTable Table = _ds.Tables [0];
126 AssertEquals ("test#01", 0, Table.Constraints.Count);
127 Table = _ds.Tables [1];
128 AssertEquals ("test#02", 0, Table.Constraints.Count);
130 DataColumn [] Cols1 = new DataColumn [2];
131 Cols1 [0] = _ds.Tables [0].Columns [1];
132 Cols1 [1] = _ds.Tables [0].Columns [2];
134 DataColumn [] Cols2 = new DataColumn [2];
135 Cols2 [0] = _ds.Tables [1].Columns [0];
136 Cols2 [1] = _ds.Tables [1].Columns [1];
138 ForeignKeyConstraint Constraint = new ForeignKeyConstraint (Cols1, Cols2);
139 Table = _ds.Tables [1];
140 Table.Constraints.Add (Constraint);
142 AssertEquals ("test#03", 1, Table.Constraints.Count);
143 AssertEquals ("test#04", "Constraint1", Table.Constraints [0].ConstraintName);
144 AssertEquals ("test#05", typeof (ForeignKeyConstraint), Table.Constraints [0].GetType ());
146 Table = _ds.Tables [0];
147 AssertEquals ("test#06", 1, Table.Constraints.Count);
148 AssertEquals ("test#07", "Constraint1", Table.Constraints [0].ConstraintName);
149 AssertEquals ("test#08", typeof (UniqueConstraint), Table.Constraints [0].GetType ());
153 // Tests ctor (string, DataColumn [], DataColumn [])
157 DataTable Table = _ds.Tables [0];
159 AssertEquals ("test#01", 0, Table.Constraints.Count);
160 Table = _ds.Tables [1];
161 AssertEquals ("test#02", 0, Table.Constraints.Count);
163 DataColumn [] Cols1 = new DataColumn [2];
164 Cols1 [0] = _ds.Tables [0].Columns [1];
165 Cols1 [1] = _ds.Tables [0].Columns [2];
167 DataColumn [] Cols2 = new DataColumn [2];
168 Cols2 [0] = _ds.Tables [1].Columns [0];
169 Cols2 [1] = _ds.Tables [1].Columns [1];
171 ForeignKeyConstraint Constraint = new ForeignKeyConstraint ("Test", Cols1, Cols2);
172 Table = _ds.Tables [1];
173 Table.Constraints.Add (Constraint);
175 AssertEquals ("test#03", 1, Table.Constraints.Count);
176 AssertEquals ("test#04", "Test", Table.Constraints [0].ConstraintName);
177 AssertEquals ("test#05", typeof (ForeignKeyConstraint), Table.Constraints [0].GetType ());
179 Table = _ds.Tables [0];
180 AssertEquals ("test#06", 1, Table.Constraints.Count);
181 AssertEquals ("test#07", "Constraint1", Table.Constraints [0].ConstraintName);
182 AssertEquals ("test#08", typeof (UniqueConstraint), Table.Constraints [0].GetType ());
186 public void TestCtor5()
188 DataTable table1 = new DataTable ("Table1");
189 DataTable table2 = new DataTable ("Table2");
190 DataSet dataSet = new DataSet();
191 dataSet.Tables.Add (table1);
192 dataSet.Tables.Add (table2);
193 DataColumn column1 = new DataColumn ("col1");
194 DataColumn column2 = new DataColumn ("col2");
195 DataColumn column3 = new DataColumn ("col3");
196 table1.Columns.Add (column1);
197 table1.Columns.Add (column2);
198 table1.Columns.Add (column3);
199 DataColumn column4 = new DataColumn ("col4");
200 DataColumn column5 = new DataColumn ("col5");
201 DataColumn column6 = new DataColumn ("col6");
202 table2.Columns.Add (column4);
203 table2.Columns.Add (column5);
204 table2.Columns.Add (column6);
205 string []parentColumnNames = {"col1", "col2", "col3"};
206 string []childColumnNames = {"col4", "col5", "col6"};
207 string parentTableName = "table1";
209 // Create a ForeingKeyConstraint Object using the constructor
210 // ForeignKeyConstraint (string, string, string[], string[], AcceptRejectRule, Rule, Rule);
211 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
214 DataTable tmp = fkc.Table;
215 Fail ("When table is null, get_Table causes an InvalidOperationException.");
216 } catch (InvalidOperationException) {
219 Assertion.AssertEquals ("#A01 Table should not be set", fkc.Table, null);
222 Constraint []constraints = new Constraint[3];
223 constraints [0] = new UniqueConstraint (column1);
224 constraints [1] = new UniqueConstraint (column2);
225 constraints [2] = fkc;
227 // Try to add the constraint to ConstraintCollection of the DataTable through Add()
229 table2.Constraints.Add (fkc);
230 throw new ApplicationException ("An Exception was expected");
232 // LAMESPEC : spec says InvalidConstraintException but throws this
233 catch (ArgumentException) {
236 #if false // FIXME: Here this test crashes under MS.NET.
237 // OK - So AddRange() is the only way!
238 table2.Constraints.AddRange (constraints);
239 // After AddRange(), Check the properties of ForeignKeyConstraint object
240 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"));
241 Assertion.Assert("#A08", fkc.Columns [1].ColumnName.Equals ("col5"));
242 Assertion.Assert("#A09", fkc.Columns [2].ColumnName.Equals ("col6"));
244 // Try to add columns with names which do not exist in the table
245 parentColumnNames [2] = "noColumn";
246 ForeignKeyConstraint foreignKeyConstraint = new ForeignKeyConstraint ("hello world", parentTableName, parentColumnNames, childColumnNames, AcceptRejectRule.Cascade, Rule.Cascade, Rule.Cascade);
247 constraints [0] = new UniqueConstraint (column1);
248 constraints [1] = new UniqueConstraint (column2);
249 constraints [2] = foreignKeyConstraint;
251 table2.Constraints.AddRange (constraints);
252 throw new ApplicationException ("An Exception was expected");
254 catch (ArgumentException e) {
256 catch (InvalidConstraintException e){ // Could not test on ms.net, as ms.net does not reach here so far.
259 #if false // FIXME: Here this test crashes under MS.NET.
260 // Check whether the child table really contains the foreign key constraint named "hello world"
261 Assertion.Assert("#A11 ", table2.Constraints.Contains ("hello world"));
267 // If Childs and parents are in same table
269 public void KeyBetweenColumns ()
271 DataTable Table = _ds.Tables [0];
273 AssertEquals ("test#01", 0, Table.Constraints.Count);
274 Table = _ds.Tables [1];
275 AssertEquals ("test#02", 0, Table.Constraints.Count);
278 ForeignKeyConstraint Constraint = new ForeignKeyConstraint ("Test", _ds.Tables [0].Columns [0], _ds.Tables [0].Columns [2]);
279 Table = _ds.Tables [0];
280 Table.Constraints.Add (Constraint);
282 AssertEquals ("test#03", 2, Table.Constraints.Count);
283 AssertEquals ("test#04", "Constraint1", Table.Constraints [0].ConstraintName);
284 AssertEquals ("test#05", typeof (UniqueConstraint), Table.Constraints [0].GetType ());
285 AssertEquals ("test#04", "Test", Table.Constraints [1].ConstraintName);
286 AssertEquals ("test#05", typeof (ForeignKeyConstraint), Table.Constraints [1].GetType ());
291 public void CtorExceptions ()
293 ForeignKeyConstraint fkc;
295 DataTable localTable = new DataTable();
296 localTable.Columns.Add("Col1",typeof(int));
297 localTable.Columns.Add("Col2",typeof(bool));
302 fkc = new ForeignKeyConstraint((DataColumn)null,(DataColumn)null);
303 Fail("Failed to throw ArgumentNullException.");
306 catch (NullReferenceException) {}
308 catch (ArgumentNullException) {}
310 catch (AssertionException exc) {throw exc;}
311 catch (Exception exc)
313 Fail("A1: Wrong Exception type. " + exc.ToString());
316 //zero length collection
319 fkc = new ForeignKeyConstraint(new DataColumn[]{},new DataColumn[]{});
320 Fail("B1: Failed to throw ArgumentException.");
322 catch (ArgumentException) {}
323 catch (AssertionException exc) {throw exc;}
324 catch (Exception exc)
326 Fail("A2: Wrong Exception type. " + exc.ToString());
332 fkc = new ForeignKeyConstraint(_ds.Tables[0].Columns[0], localTable.Columns[0]);
333 Fail("Failed to throw InvalidOperationException.");
335 catch (InvalidOperationException) {}
336 catch (AssertionException exc) {throw exc;}
337 catch (Exception exc)
339 Fail("A3: Wrong Exception type. " + exc.ToString());
344 fkc = new ForeignKeyConstraint(_ds.Tables[0].Columns[0], localTable.Columns[1]);
345 Fail("Failed to throw InvalidConstraintException.");
348 // tables in different datasets
349 catch (InvalidOperationException) {}
351 //different dataTypes
352 catch (InvalidConstraintException) {}
355 // Cannot create a Key from Columns that belong to
359 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]});
360 Fail("Failed to throw InvalidOperationException.");
362 catch (InvalidConstraintException) {}
363 catch (AssertionException exc) {throw exc;}
367 public void CtorExceptions2 ()
369 DataColumn col = new DataColumn("MyCol1",typeof(int));
371 ForeignKeyConstraint fkc;
373 //Columns must belong to a Table
376 fkc = new ForeignKeyConstraint(col, _ds.Tables[0].Columns[0]);
377 Fail("FTT1: Failed to throw ArgumentException.");
379 catch (ArgumentException) {}
380 catch (AssertionException exc) {throw exc;}
381 // catch (Exception exc)
383 // Fail("WET1: Wrong Exception type. " + exc.ToString());
386 //Columns must belong to the same table
387 //InvalidConstraintException
389 DataColumn [] difTable = new DataColumn [] {_ds.Tables[0].Columns[2],
390 _ds.Tables[1].Columns[0]};
393 fkc = new ForeignKeyConstraint(difTable,new DataColumn[] {
394 _ds.Tables[0].Columns[1],
395 _ds.Tables[0].Columns[0]});
397 Fail("FTT2: Failed to throw InvalidConstraintException.");
399 catch (InvalidConstraintException) {}
400 catch (AssertionException exc) {throw exc;}
401 catch (Exception exc)
403 Fail("WET2: Wrong Exception type. " + exc.ToString());
407 //parent columns and child columns should be the same length
409 DataColumn [] twoCol =
410 new DataColumn [] {_ds.Tables[0].Columns[0],_ds.Tables[0].Columns[1]};
415 fkc = new ForeignKeyConstraint(twoCol,
416 new DataColumn[] { _ds.Tables[0].Columns[0]});
418 Fail("FTT3: Failed to throw ArgumentException.");
420 catch (ArgumentException) {}
421 catch (AssertionException exc) {throw exc;}
422 catch (Exception exc)
424 Fail("WET3: Wrong Exception type. " + exc.ToString());
427 //InvalidOperation: Parent and child are the same column.
430 fkc = new ForeignKeyConstraint( _ds.Tables[0].Columns[0],
431 _ds.Tables[0].Columns[0] );
433 Fail("FTT4: Failed to throw InvalidOperationException.");
435 catch (InvalidOperationException) {}
436 catch (AssertionException exc) {throw exc;}
437 catch (Exception exc)
439 Fail("WET4: Wrong Exception type. " + exc.ToString());
445 public void EqualsAndHashCode()
447 DataTable tbl = _ds.Tables[0];
448 DataTable tbl2 = _ds.Tables[1];
450 ForeignKeyConstraint fkc = new ForeignKeyConstraint(
451 new DataColumn[] {tbl.Columns[0], tbl.Columns[1]} ,
452 new DataColumn[] {tbl2.Columns[0], tbl2.Columns[1]} );
454 ForeignKeyConstraint fkc2 = new ForeignKeyConstraint(
455 new DataColumn[] {tbl.Columns[0], tbl.Columns[1]} ,
456 new DataColumn[] {tbl2.Columns[0], tbl2.Columns[1]} );
458 ForeignKeyConstraint fkcDiff =
459 new ForeignKeyConstraint( tbl.Columns[1], tbl.Columns[2]);
461 Assert( "Equals failed. 1" , fkc.Equals(fkc2));
462 Assert( "Equals failed. 2" , fkc2.Equals(fkc));
463 Assert( "Equals failed. 3" , fkc.Equals(fkc));
465 Assert( "Equals failed diff. 1" , fkc.Equals(fkcDiff) == false);
467 Assert( "Hash Code Failed. 1", fkc.GetHashCode() == fkc2.GetHashCode() );
468 Assert( "Hash Code Failed. 2", fkc.GetHashCode() != fkcDiff.GetHashCode() );
473 [ExpectedException (typeof (ArgumentException))]
474 public void ViolationTest ()
476 DataTable parent = _ds.Tables [0];
477 DataTable child = _ds.Tables [1];
479 parent.Rows.Add (new object [] {1, 1, 1});
480 child.Rows.Add (new object [] {2, 2, 2});
483 child.Constraints.Add (new ForeignKeyConstraint ( parent.Columns [0],
487 // clear the rows for further testing
493 public void NoViolationTest ()
495 DataTable parent = _ds.Tables [0];
496 DataTable child = _ds.Tables [1];
498 parent.Rows.Add (new object [] {1, 1, 1});
499 child.Rows.Add (new object [] {2, 2, 2});
502 _ds.EnforceConstraints = false;
503 child.Constraints.Add (new ForeignKeyConstraint ( parent.Columns [0],
507 // clear the rows for further testing
509 _ds.EnforceConstraints = true;
514 public void ModifyParentKeyBeforeAcceptChanges ()
516 DataSet ds1 = new DataSet();
517 DataTable t1= ds1.Tables.Add ("t1");
518 DataTable t2= ds1.Tables.Add ("t2");
519 t1.Columns.Add ("col1", typeof (int));
520 t2.Columns.Add ("col2", typeof (int));
521 ds1.Relations.Add ("fk", t1.Columns [0], t2.Columns [0]);
523 t1.Rows.Add (new object[] {10});
524 t2.Rows.Add (new object [] {10});
527 Assert("#1", (int)t2.Rows [0][0] == 20);