Merge pull request #1685 from esdrubal/touint64
[mono.git] / mcs / class / System.Data / Test / System.Data / ForeignKeyConstraintTest.cs
1 // ForeignKeyConstraintTest.cs - NUnit Test Cases for [explain here]
2 //
3 // Authors:
4 //   Franklin Wise (gracenote@earthlink.net)
5 //   Martin Willemoes Hansen (mwh@sysrq.dk)
6 //
7 // (C) Franklin Wise
8 // (C) 2003 Martin Willemoes Hansen
9 // 
10
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
34 using NUnit.Framework;
35 using System;
36 using System.Data;
37
38 namespace MonoTests.System.Data
39 {
40         [TestFixture]
41         public class ForeignKeyConstraintTest : Assertion
42         {
43                 private DataSet _ds;
44
45                 //NOTE: fk constraints only work when the table is part of a DataSet
46
47                 [SetUp]
48                 public void GetReady() 
49                 {
50                         _ds = new DataSet();
51
52                         //Setup DataTable
53                         DataTable table;
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));
58
59                         _ds.Tables.Add(table);
60
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));
65
66                         _ds.Tables.Add(table);
67
68                 }
69
70                 // Tests ctor (string, DataColumn, DataColumn)
71                 [Test]
72                 public void Ctor1 ()
73                 {
74                         DataTable Table =  _ds.Tables [0];
75                         
76                         AssertEquals ("test#01", 0, Table.Constraints.Count);
77                         Table =  _ds.Tables [1];
78                         AssertEquals ("test#02", 0, Table.Constraints.Count);
79                         
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);
84                         
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 ());
88
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 ());
93                 }
94                 
95                 // Tests ctor (DataColumn, DataColumn)
96                 [Test]
97                 public void Ctor2 ()
98                 {
99                         DataTable Table =  _ds.Tables [0];
100                         
101                         AssertEquals ("test#01", 0, Table.Constraints.Count);
102                         Table =  _ds.Tables [1];
103                         AssertEquals ("test#02", 0, Table.Constraints.Count);
104                         
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);
109                         
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 ());
113
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 ());
118                 }
119                 
120                 // Test ctor (DataColumn [], DataColumn [])
121                 [Test]
122                 public void Ctor3 ()
123                 {
124                         DataTable Table =  _ds.Tables [0];
125                         
126                         AssertEquals ("test#01", 0, Table.Constraints.Count);
127                         Table =  _ds.Tables [1];
128                         AssertEquals ("test#02", 0, Table.Constraints.Count);
129                                                 
130                         DataColumn [] Cols1 = new DataColumn [2];
131                         Cols1 [0] = _ds.Tables [0].Columns [1];
132                         Cols1 [1] = _ds.Tables [0].Columns [2];
133                         
134                         DataColumn [] Cols2 = new DataColumn [2];
135                         Cols2 [0] = _ds.Tables [1].Columns [0];
136                         Cols2 [1] = _ds.Tables [1].Columns [1];
137                         
138                         ForeignKeyConstraint Constraint = new ForeignKeyConstraint (Cols1, Cols2);
139                         Table = _ds.Tables [1];
140                         Table.Constraints.Add (Constraint);
141                         
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 ());
145
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 ());
150
151                 }
152         
153                 // Tests ctor (string, DataColumn [], DataColumn [])    
154                 [Test]
155                 public void Ctor4 ()
156                 {
157                         DataTable Table =  _ds.Tables [0];
158                         
159                         AssertEquals ("test#01", 0, Table.Constraints.Count);
160                         Table =  _ds.Tables [1];
161                         AssertEquals ("test#02", 0, Table.Constraints.Count);
162                                                 
163                         DataColumn [] Cols1 = new DataColumn [2];
164                         Cols1 [0] = _ds.Tables [0].Columns [1];
165                         Cols1 [1] = _ds.Tables [0].Columns [2];
166                         
167                         DataColumn [] Cols2 = new DataColumn [2];
168                         Cols2 [0] = _ds.Tables [1].Columns [0];
169                         Cols2 [1] = _ds.Tables [1].Columns [1];
170                         
171                         ForeignKeyConstraint Constraint = new ForeignKeyConstraint ("Test", Cols1, Cols2);
172                         Table = _ds.Tables [1];
173                         Table.Constraints.Add (Constraint);
174                         
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 ());
178
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 ());                  
183                 }
184                 
185                 [Test]
186                 public void TestCtor5()
187                 {
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";
208                 
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
212                         try {
213                                 DataTable tmp = fkc.Table;
214                                 Fail ("When table is null, get_Table causes an InvalidOperationException.");
215                         } catch (NullReferenceException) { // actually .NET throws this (bug)
216                         } catch (InvalidOperationException) {
217                         }
218                                                                                                     
219                         Constraint []constraints = new Constraint[3];
220                         constraints [0] = new UniqueConstraint (column1);
221                         constraints [1] = new UniqueConstraint (column2);
222                         constraints [2] = fkc;
223                                                                                                     
224                         // Try to add the constraint to ConstraintCollection of the DataTable through Add()
225                         try{
226                                 table2.Constraints.Add (fkc);
227                                 throw new ApplicationException ("An Exception was expected");
228                         }
229                         // LAMESPEC : spec says InvalidConstraintException but throws this
230                         catch (NullReferenceException) {
231                         }
232
233 #if false // FIXME: Here this test crashes under MS.NET.
234                         // OK - So AddRange() is the only way!
235                         table2.Constraints.AddRange (constraints);
236                            // After AddRange(), Check the properties of ForeignKeyConstraint object
237                         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"));
238                         Assertion.Assert("#A08", fkc.Columns [1].ColumnName.Equals ("col5"));
239                         Assertion.Assert("#A09", fkc.Columns [2].ColumnName.Equals ("col6"));
240 #endif
241                         // Try to add columns with names which do not exist in the table
242                         parentColumnNames [2] = "noColumn";
243                         ForeignKeyConstraint foreignKeyConstraint = new ForeignKeyConstraint ("hello world", parentTableName, parentColumnNames, childColumnNames, AcceptRejectRule.Cascade, Rule.Cascade, Rule.Cascade);
244                         constraints [0] = new UniqueConstraint (column1);
245                         constraints [1] = new UniqueConstraint (column2);
246                         constraints [2] = foreignKeyConstraint;
247                         try{
248                                 table2.Constraints.AddRange (constraints);
249                                 throw new ApplicationException ("An Exception was expected");
250                         }
251                         catch (ArgumentException e) {
252                         }
253                         catch (InvalidConstraintException e){ // Could not test on ms.net, as ms.net does not reach here so far.        
254                         }
255                         
256 #if false // FIXME: Here this test crashes under MS.NET.
257                         // Check whether the child table really contains the foreign key constraint named "hello world"
258                         Assertion.Assert("#A11 ", table2.Constraints.Contains ("hello world"));
259 #endif
260                 }
261
262
263
264                 //  If Childs and parents are in same table
265                 [Test]
266                 public void KeyBetweenColumns ()
267                 {
268                         DataTable Table =  _ds.Tables [0];
269                         
270                         AssertEquals ("test#01", 0, Table.Constraints.Count);
271                         Table =  _ds.Tables [1];
272                         AssertEquals ("test#02", 0, Table.Constraints.Count);
273                                                 
274                         
275                         ForeignKeyConstraint Constraint = new ForeignKeyConstraint ("Test", _ds.Tables [0].Columns [0], _ds.Tables [0].Columns [2]);
276                         Table = _ds.Tables [0];
277                         Table.Constraints.Add (Constraint);
278                         
279                         AssertEquals ("test#03", 2, Table.Constraints.Count);
280                         AssertEquals ("test#04", "Constraint1", Table.Constraints [0].ConstraintName);
281                         AssertEquals ("test#05", typeof (UniqueConstraint), Table.Constraints [0].GetType ());
282                         AssertEquals ("test#04", "Test", Table.Constraints [1].ConstraintName);
283                         AssertEquals ("test#05", typeof (ForeignKeyConstraint), Table.Constraints [1].GetType ());
284
285                 }
286
287                 [Test]
288                 public void CtorExceptions ()
289                 {
290                         ForeignKeyConstraint fkc;
291
292                         DataTable localTable = new DataTable();
293                         localTable.Columns.Add("Col1",typeof(int));
294                         localTable.Columns.Add("Col2",typeof(bool));
295
296                         //Null
297                         try
298                         {
299                                 fkc = new ForeignKeyConstraint((DataColumn)null,(DataColumn)null);
300                                 Fail("Failed to throw ArgumentNullException.");
301                         }
302                         catch (NullReferenceException) {}
303                         catch (AssertionException exc) {throw exc;}
304                         catch (Exception exc)
305                         {
306                                 Fail("A1: Wrong Exception type. " + exc.ToString());
307                         }
308
309                         //zero length collection
310                         try
311                         {
312                                 fkc = new ForeignKeyConstraint(new DataColumn[]{},new DataColumn[]{});
313                                 Fail("B1: Failed to throw ArgumentException.");
314                         }
315                         catch (ArgumentException) {}
316                         catch (AssertionException exc) {throw exc;}
317                         catch (Exception exc)
318                         {
319                                 Fail("A2: Wrong Exception type. " + exc.ToString());
320                         }
321
322                         //different datasets
323                         try
324                         {
325                                 fkc = new ForeignKeyConstraint(_ds.Tables[0].Columns[0], localTable.Columns[0]);
326                                 Fail("Failed to throw InvalidOperationException.");
327                         }
328                         catch (InvalidOperationException) {}
329                         catch (AssertionException exc) {throw exc;}
330                         catch (Exception exc)
331                         {
332                                 Fail("A3: Wrong Exception type. " + exc.ToString());
333                         }
334
335                         try
336                         {
337                                 fkc = new ForeignKeyConstraint(_ds.Tables[0].Columns[0], localTable.Columns[1]);
338                                 Fail("Failed to throw InvalidConstraintException.");
339                         }
340                         // tables in different datasets
341                         catch (InvalidOperationException) {}
342
343                         // Cannot create a Key from Columns that belong to
344                         // different tables.
345                         try                                           
346                         {                                             
347                                 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]});    
348                                 Fail("Failed to throw InvalidOperationException.");                                         
349                         }                                             
350                         catch (InvalidConstraintException) {}         
351                         catch (AssertionException exc) {throw exc;} 
352                 }
353
354                 [Test]
355                 public void CtorExceptions2 () 
356                 {
357                         DataColumn col = new DataColumn("MyCol1",typeof(int));
358
359                         ForeignKeyConstraint fkc;
360                         
361                         //Columns must belong to a Table
362                         try 
363                         {
364                                 fkc = new ForeignKeyConstraint(col, _ds.Tables[0].Columns[0]);
365                                 Fail("FTT1: Failed to throw ArgumentException.");
366                         }
367                         catch (ArgumentException) {}
368                         catch (AssertionException exc) {throw exc;}
369 //                      catch (Exception exc)
370 //                      {
371 //                              Fail("WET1: Wrong Exception type. " + exc.ToString());
372 //                      }
373
374                         //Columns must belong to the same table
375                         //InvalidConstraintException
376                         
377                         DataColumn [] difTable = new DataColumn [] {_ds.Tables[0].Columns[2],
378                                                                            _ds.Tables[1].Columns[0]};
379                         try 
380                         {
381                                 fkc = new ForeignKeyConstraint(difTable,new DataColumn[] {
382                                                                  _ds.Tables[0].Columns[1],
383                                                                 _ds.Tables[0].Columns[0]});
384                                         
385                                 Fail("FTT2: Failed to throw InvalidConstraintException.");
386                         }
387                         catch (InvalidConstraintException) {}
388                         catch (AssertionException exc) {throw exc;}
389                         catch (Exception exc)
390                         {
391                                 Fail("WET2: Wrong Exception type. " + exc.ToString());
392                         }
393
394
395                         //parent columns and child columns should be the same length
396                         //ArgumentException
397                         DataColumn [] twoCol = 
398                                 new DataColumn [] {_ds.Tables[0].Columns[0],_ds.Tables[0].Columns[1]};
399                                                           
400
401                         try 
402                         {
403                                 fkc = new ForeignKeyConstraint(twoCol, 
404                                         new DataColumn[] { _ds.Tables[0].Columns[0]});
405                                         
406                                 Fail("FTT3: Failed to throw ArgumentException.");
407                         }
408                         catch (ArgumentException) {}
409                         catch (AssertionException exc) {throw exc;}
410                         catch (Exception exc)
411                         {
412                                 Fail("WET3: Wrong Exception type. " + exc.ToString());
413                         }
414
415                         //InvalidOperation: Parent and child are the same column.
416                         try 
417                         {
418                                 fkc = new ForeignKeyConstraint( _ds.Tables[0].Columns[0],
419                                         _ds.Tables[0].Columns[0] );
420                                         
421                                 Fail("FTT4: Failed to throw InvalidOperationException.");
422                         }
423                         catch (InvalidOperationException) {}
424                         catch (AssertionException exc) {throw exc;}
425                         catch (Exception exc)
426                         {
427                                 Fail("WET4: Wrong Exception type. " + exc.ToString());
428                         }
429
430                 }
431
432                 [Test]
433                 public void EqualsAndHashCode()
434                 {
435                         DataTable tbl = _ds.Tables[0];
436                         DataTable tbl2 = _ds.Tables[1];
437
438                         ForeignKeyConstraint fkc = new ForeignKeyConstraint( 
439                                 new DataColumn[] {tbl.Columns[0], tbl.Columns[1]} ,
440                                 new DataColumn[] {tbl2.Columns[0], tbl2.Columns[1]} );
441
442                         ForeignKeyConstraint fkc2 = new ForeignKeyConstraint( 
443                                 new DataColumn[] {tbl.Columns[0], tbl.Columns[1]} ,
444                                 new DataColumn[] {tbl2.Columns[0], tbl2.Columns[1]} );
445
446                         ForeignKeyConstraint fkcDiff = 
447                                 new ForeignKeyConstraint( tbl.Columns[1], tbl.Columns[2]);
448                 
449                         Assert( "Equals failed. 1" , fkc.Equals(fkc2));
450                         Assert( "Equals failed. 2" , fkc2.Equals(fkc));
451                         Assert( "Equals failed. 3" , fkc.Equals(fkc));
452
453                         Assert( "Equals failed diff. 1" , fkc.Equals(fkcDiff) == false);
454
455                         //Assert( "Hash Code Failed. 1", fkc.GetHashCode() == fkc2.GetHashCode() );
456                         Assert( "Hash Code Failed. 2", fkc.GetHashCode() != fkcDiff.GetHashCode() );
457         
458                 }
459
460                 [Test]
461                 [ExpectedException (typeof (ArgumentException))]
462                 public void ViolationTest ()
463                 {
464                         DataTable parent = _ds.Tables [0];
465                         DataTable child  = _ds.Tables [1];
466                         
467                         parent.Rows.Add (new object [] {1, 1, 1});
468                         child.Rows.Add (new object [] {2, 2, 2});
469
470                         try {
471                                 child.Constraints.Add (new ForeignKeyConstraint ( parent.Columns [0],
472                                                                                   child.Columns [0])
473                                                        );
474                         } finally {
475                                 // clear the rows for further testing
476                                 _ds.Clear ();
477                         }
478                 }
479
480                 [Test]
481                 public void NoViolationTest ()
482                 {
483                         DataTable parent = _ds.Tables [0];
484                         DataTable child  = _ds.Tables [1];
485                         
486                         parent.Rows.Add (new object [] {1, 1, 1});
487                         child.Rows.Add (new object [] {2, 2, 2});
488
489                         try {
490                                 _ds.EnforceConstraints = false;
491                                 child.Constraints.Add (new ForeignKeyConstraint ( parent.Columns [0],
492                                                                                   child.Columns [0])
493                                                        );
494                         } finally {
495                                 // clear the rows for further testing
496                                 _ds.Clear ();
497                                 _ds.EnforceConstraints = true;
498                         }
499                 }
500
501                 [Test]
502                 public void ModifyParentKeyBeforeAcceptChanges ()
503                 {
504                         DataSet ds1 = new DataSet();
505                         DataTable t1= ds1.Tables.Add ("t1");
506                         DataTable t2= ds1.Tables.Add ("t2");
507                         t1.Columns.Add ("col1", typeof (int));
508                         t2.Columns.Add ("col2", typeof (int));
509                         ds1.Relations.Add ("fk", t1.Columns [0], t2.Columns [0]);
510
511                         t1.Rows.Add (new object[] {10});
512                         t2.Rows.Add (new object [] {10});
513
514                         t1.Rows [0][0]=20;
515                         Assert("#1", (int)t2.Rows [0][0] == 20);
516                 }
517
518                 [Test]
519                 // https://bugzilla.novell.com/show_bug.cgi?id=650402
520                 public void ForeignKey_650402 ()
521                 {
522                         DataSet data = new DataSet ();
523                         DataTable parent = new DataTable ("parent");
524                         DataColumn pk = parent.Columns.Add ("PK");
525                         DataTable child = new DataTable ("child");
526                         DataColumn fk = child.Columns.Add ("FK");
527                         
528                         data.Tables.Add (parent);
529                         data.Tables.Add (child);
530                         data.Relations.Add (pk, fk);
531                         
532                         parent.Rows.Add ("value");
533                         child.Rows.Add ("value");
534                         data.AcceptChanges ();
535                         child.Rows[0].Delete ();
536                         parent.Rows[0][0] = "value2";
537                         
538                         data.EnforceConstraints = false;
539                         data.EnforceConstraints = true;
540                 }
541         }
542 }