2004-05-06 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Data / Test / System.Data / ConstraintCollectionTest.cs
1 // ConstraintCollection.cs - NUnit Test Cases for testing the ConstraintCollection 
2 //      class.
3 //      
4 // Authors:
5 //   Franklin Wise (gracenote@earthlink.net)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //   Roopa Wilson (rowilson@novell.com) 
8 //
9 // (C) Franklin Wise
10 // (C) 2003 Martin Willemoes Hansen
11 // (C) Novell,Inc       
12 // 
13
14
15 using NUnit.Framework;
16 using System;
17 using System.Data;
18
19 namespace MonoTests.System.Data
20 {
21         [TestFixture]
22         public class ConstraintCollectionTest : Assertion {
23                 private DataTable _table;
24                 private DataTable _table2;
25                 private Constraint _constraint1;
26                 private Constraint _constraint2;
27
28                 [SetUp]
29                 public void GetReady() 
30                 {
31                         //Setup DataTable
32                         _table = new DataTable("TestTable");
33                         _table.Columns.Add("Col1",typeof(int));
34                         _table.Columns.Add("Col2",typeof(int));
35                         _table.Columns.Add("Col3",typeof(int));
36
37                         _table2 = new DataTable("TestTable");
38                         _table2.Columns.Add("Col1",typeof(int));
39                         _table2.Columns.Add("Col2",typeof(int));
40
41                         //Use UniqueConstraint to test Constraint Base Class
42                         _constraint1 = new UniqueConstraint(_table.Columns[0],false); 
43                         _constraint2 = new UniqueConstraint(_table.Columns[1],false); 
44
45                         // not sure why this is needed since a new _table was just created
46                         // for us, but this Clear() keeps the tests from throwing
47                         // an exception when the Add() is called.
48                         _table.Constraints.Clear();
49                 }
50
51                 [Test]
52                 public void Add()
53                 {
54                         ConstraintCollection col = _table.Constraints;
55                         col.Add(_constraint1);
56                         col.Add(_constraint2);
57
58                         AssertEquals("Count doesn't equal added.",2, col.Count);
59                 }
60
61                 [Test]
62                 public void AddExceptions()
63                 {
64                         ConstraintCollection col = _table.Constraints;
65                         
66                         //null
67                         try 
68                         {
69                                 col.Add(null);
70                                 Fail("B1: Failed to throw ArgumentNullException.");
71                         }
72                         catch (ArgumentNullException) {}
73                         catch (AssertionException exc) {throw exc;}
74                         catch 
75                         {
76                                 Fail("A1: Wrong exception type");
77                         }
78
79                         //duplicate name
80                         try 
81                         {
82                                 _constraint1.ConstraintName = "Dog";
83                                 _constraint2.ConstraintName = "dog"; //case insensitive
84                                 col.Add(_constraint1);
85                                 col.Add(_constraint2);
86 #if NET_1_1
87 #else
88                                 Fail("Failed to throw Duplicate name exception.");
89 #endif
90                                 col.Remove (_constraint2); // only for !1.0
91                                 col.Remove (_constraint1);
92                         }
93 #if ! NET_1_1
94                         catch (DuplicateNameException) {
95                         }
96 #endif
97                         catch (AssertionException exc) {throw exc;}
98 /* Don't use such catch. They cover our eyes from the exact exception location.
99                         catch (Exception exc)
100                         {
101                                 Fail("A2: Wrong exception type. " + exc.ToString());
102                         }
103 */
104                         //Constraint Already exists
105                         try 
106                         {
107                                 col.Add(_constraint1);
108 #if NET_1_1
109 #else
110                                 Fail("B2: Failed to throw ArgumentException.");
111 #endif
112                                 col.Remove (_constraint1);
113                         }
114                         catch (ArgumentException) {
115 #if NET_1_1
116 #else
117                                 throw;
118 #endif
119                         }
120                         catch (AssertionException exc) {throw exc;}
121                         catch 
122                         {
123                                 Fail("A3: Wrong exception type");
124                         }
125                 }
126
127                 [Test]
128                 public void Indexer()
129                 {
130                         Constraint c1 = new UniqueConstraint(_table.Columns[0]);
131                         Constraint c2 = new UniqueConstraint(_table.Columns[1]);
132
133                         c1.ConstraintName = "first";
134                         c2.ConstraintName = "second";
135
136
137                         _table.Constraints.Add(c1);
138                         _table.Constraints.Add(c2);
139
140                         AssertSame("A1", c1, _table.Constraints[0]); 
141                         AssertSame("A2", c2, _table.Constraints[1]);
142
143                         AssertSame("A3", c1, _table.Constraints["first"]);
144                         AssertSame("A4", c2, _table.Constraints["sEcond"]); //case insensitive
145
146                 }
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
154                         c1.ConstraintName = "first";
155                         c2.ConstraintName = "second";
156
157                         _table.Constraints.Add(c1);
158                         _table.Constraints.Add(c2);
159
160                         AssertEquals("A1", 0, _table.Constraints.IndexOf(c1));
161                         AssertEquals("A2", 1, _table.Constraints.IndexOf(c2));
162                         AssertEquals("A3", 0, _table.Constraints.IndexOf("first"));
163                         AssertEquals("A4", 1, _table.Constraints.IndexOf("second"));
164                 }
165
166                 [Test]
167                 public void Contains()
168                 {
169                         Constraint c1 = new UniqueConstraint(_table.Columns[0]);
170                         Constraint c2 = new UniqueConstraint(_table.Columns[1]);
171
172                         c1.ConstraintName = "first";
173                         c2.ConstraintName = "second";
174
175                         _table.Constraints.Add(c1);
176
177                         Assert("A1", _table.Constraints.Contains(c1.ConstraintName)); //true
178                         Assert("A2", _table.Constraints.Contains(c2.ConstraintName) == false); //doesn't contain
179                 }
180
181                 [Test]
182                 public void IndexerFailures()
183                 {
184                         _table.Constraints.Add(new UniqueConstraint(_table.Columns[0]));
185
186                         //This doesn't throw
187                         AssertNull(_table.Constraints["notInCollection"]);
188                         
189                         //Index too high
190                         try 
191                         {
192                                 Constraint c = _table.Constraints[_table.Constraints.Count];
193                                 Fail("B1: Failed to throw IndexOutOfRangeException.");
194                         }
195                         catch (IndexOutOfRangeException) {}
196                         catch (AssertionException exc) {throw exc;}
197                         catch 
198                         {
199                                 Fail("A1: Wrong exception type");
200                         }
201
202                         //Index too low
203                         try 
204                         {
205                                 Constraint c = _table.Constraints[-1];
206                                 Fail("B2: Failed to throw IndexOutOfRangeException.");
207                         }
208                         catch (IndexOutOfRangeException) {}
209                         catch (AssertionException exc) {throw exc;}
210                         catch 
211                         {
212                                 Fail("A2: Wrong exception type");
213                         }       
214
215                 }
216
217                 [Test]
218                 public void AddFkException1()
219                 {
220                         DataSet ds = new DataSet();
221                         ds.Tables.Add(_table);
222                         _table2.TableName = "TestTable2";
223                         ds.Tables.Add(_table2);
224
225                         _table.Rows.Add(new object [] {1});
226                         _table.Rows.Add(new object [] {1});
227
228                         //FKC: can't create unique constraint because duplicate values already exist
229                         try
230                         {
231                                 ForeignKeyConstraint fkc = new ForeignKeyConstraint( _table.Columns[0],
232                                                                                         _table2.Columns[0]);
233                                 
234                                 _table2.Constraints.Add(fkc);   //should throw                  
235                                 Fail("B1: Failed to throw ArgumentException.");
236                         }
237                         catch (ArgumentException) {}
238                         catch (AssertionException exc) {throw exc;}
239                         catch (Exception exc)
240                         {
241                                 Fail("A1: Wrong Exception type. " + exc.ToString());
242                         }
243
244
245                 }
246
247
248                 [Test]
249                 public void AddFkException2()
250                 {
251                         //Foreign key rules only work when the tables
252                         //are apart of the dataset
253                         DataSet ds = new DataSet();
254                         ds.Tables.Add(_table);
255                         _table2.TableName = "TestTable2";
256                         ds.Tables.Add(_table2);
257
258                         _table.Rows.Add(new object [] {1});
259                         
260                         // will need a matching parent value in 
261                         // _table
262                         _table2.Rows.Add(new object [] {3}); 
263                                                                 
264
265                         //FKC: no matching parent value
266                         try
267                         {
268                                 ForeignKeyConstraint fkc = new ForeignKeyConstraint( _table.Columns[0],
269                                         _table2.Columns[0]);
270                                 
271                                 _table2.Constraints.Add(fkc);   //should throw                  
272                                 Fail("B1: Failed to throw ArgumentException.");
273                         }
274                         catch (ArgumentException) {}
275                         catch (AssertionException exc) {throw exc;}
276                         catch (Exception exc)
277                         {
278                                 Fail("A1: Wrong Exception type. " + exc.ToString());
279                         }
280
281
282                 }
283
284
285                 [Test]
286                 public void AddUniqueExceptions()
287                 {
288                         
289
290                         //UC: can't create unique constraint because duplicate values already exist
291                         try
292                         {
293                                 _table.Rows.Add(new object [] {1});
294                                 _table.Rows.Add(new object [] {1});
295                                 UniqueConstraint uc = new UniqueConstraint( _table.Columns[0]);
296                                 
297                                 _table.Constraints.Add(uc);     //should throw                  
298                                 Fail("B1: Failed to throw ArgumentException.");
299                         }
300                         catch (ArgumentException) {}
301                         catch (AssertionException exc) {throw exc;}
302                         catch (Exception exc)
303                         {
304                                 Fail("A1: Wrong Exception type. " + exc.ToString());
305                         }
306                 }
307
308                 [Test]
309                 //Tests AddRange (), CanRemove (), RemoveAt (), Remove (), Exceptions of  Remove(), and Clear ()
310                 public void AddRemoveTest ()
311                 {
312                         AddRange ();
313 //                      CanRemove (); This test is ignored
314                         Remove ();
315 //                      RemoveAt (); This test is ignored
316
317                         // This test is expected to be failed, so don't reuse it.
318 //                        RemoveExceptions ();
319                         _table.Constraints.Remove (_table.Constraints [0]);
320
321                         Clear ();
322                 }
323
324                 [Test]
325                 public void AddRange()
326                 {
327                         _constraint1.ConstraintName = "UK1";
328                         _constraint2.ConstraintName = "UK12";
329                                                                                                     
330                         ForeignKeyConstraint _constraint3 = new ForeignKeyConstraint ("FK2", _table.Columns [0],
331                                         _table2.Columns [0]);
332                         UniqueConstraint _constraint4=new UniqueConstraint("UK2", _table2.Columns [1]);
333                                                                                                     
334                         // Add the constraints.
335                         Constraint [] constraints = {_constraint1, _constraint2};
336                         _table.Constraints.AddRange (constraints);
337                                                                                                                                                                                                          
338                         Constraint [] constraints1 = {_constraint3, _constraint4};
339                         _table2.Constraints.AddRange (constraints1);
340                                                                                                     
341                         AssertEquals ("A1", "UK1", _table.Constraints [0].ConstraintName);
342                         AssertEquals ("A2", "UK12", _table.Constraints [1].ConstraintName);
343                                                                                                     
344                         AssertEquals ("A3", "FK2", _table2.Constraints [0].ConstraintName);
345                         AssertEquals ("A4", "UK2", _table2.Constraints [1].ConstraintName);
346
347                 }
348                 
349                 [Test]
350                 public void TestAddRange2()
351                 {
352                         DataTable table = new DataTable ("Table");
353                         DataColumn column1 = new DataColumn ("col1");
354                         DataColumn column2 = new DataColumn ("col2");
355                         DataColumn column3 = new DataColumn ("col3");
356                         table.Columns.Add (column1);
357                         table.Columns.Add (column2);
358                         table.Columns.Add (column3);
359                         string []columnNames = {"col1", "col2", "col3"};
360                                                                                                     
361                         Constraint []constraints = new Constraint[3];
362                         constraints [0] = new UniqueConstraint ("Unique1",column1);
363                         constraints [1] = new UniqueConstraint ("Unique2",column2);
364                         constraints [2] = new UniqueConstraint ("Unique3", columnNames, true);
365                                                                                                     
366                         table.BeginInit();
367                         //Console.WriteLine(table.InitStatus == DataTable.initStatus.BeginInit);
368                         table.Constraints.AddRange (constraints);
369                                                                                                     
370                         //Check the table property of UniqueConstraint Object
371                         try{
372                                 Assertion.AssertNull ("#01", constraints [2].Table);
373                         }
374                         catch (Exception e) {
375                                 Assertion.Assert ("#A02", "System.NullReferenceException".Equals (e.GetType().ToString()));
376                         }
377
378                         table.EndInit();
379
380                         // After EndInit is called the constraints associated with most recent call to AddRange() must be
381                         // added to the ConstraintCollection
382                         Assertion.AssertNull ("#A03", constraints [2].Table);
383 //                        Assertion.Assert ("#A03", constraints [2].Table.ToString().Equals ("Table"));
384                         Assertion.Assert ("#A04", table.Constraints.Contains ("Unique1"));
385                         Assertion.Assert ("#A05", table.Constraints.Contains ("Unique2"));
386 #if NET_1_1 // really?
387                         Assertion.Assert ("#A06", !table.Constraints.Contains ("Unique3"));
388 #else
389                         Assertion.Assert ("#A06", table.Constraints.Contains ("Unique3"));
390 #endif
391                 }
392         
393
394
395                 [Test]
396                 public void Clear()
397                 {
398                         try {
399                                _table.Constraints.Clear (); //Clear all constraints
400                                 AssertEquals ("A1", 0, _table.Constraints.Count); //No constraints should remain
401                                 _table2.Constraints.Clear ();
402                                 AssertEquals ("A2", 0, _table2.Constraints.Count);
403                         }
404                         catch (Exception e) {
405                                 Console.WriteLine (e);
406                         }
407
408                 }
409
410                 [Test]
411                 [Ignore ("This never works on MS.NET (and it should not)")]
412                 public void CanRemove()
413                 {
414                         AssertEquals ("A1", false, _table.Constraints.CanRemove (_table.Constraints [0]));
415
416                 }
417
418                 [Test]
419                 public void CollectionChanged()
420                 {
421                 }
422
423                 [Test]
424                 [Ignore ("MS.NET fails this test (and it should fail)")]
425                 public void RemoveAt()
426                 {
427                          _table2.Constraints.RemoveAt (1); //Remove constraint and again add it
428                          AssertEquals ("A1", 1, _table2.Constraints.Count);                                                  UniqueConstraint _constraint4  = new UniqueConstraint ("UK2", _table2.Columns [1]);
429                          // Add the constraints.
430                          Constraint [] constraints = {_constraint4};
431                          _table.Constraints.AddRange (constraints);
432
433                 }
434
435                 [Test]
436                 [Ignore ("MS.NET fails this test (and it should fail)")]
437                 public void Remove()
438                 {
439                         _table2.Constraints.Remove (_table2.Constraints [1]); //Remove constraint and again add it
440                         AssertEquals ("A1", 1, _table2.Constraints.Count);                      
441                         UniqueConstraint _constraint4 = new UniqueConstraint ("UK2", _table2.Columns [1]);                                                                               
442                         // Add the constraints.
443                         Constraint [] constraints = {_constraint4};
444                         _table2.Constraints.AddRange (constraints);
445                 }
446
447                 [Test]
448                 public void RemoveExceptions()
449                 {
450                         try {
451                                 //Remove constraint that cannot be removed
452                                 _table.Constraints.Remove (_table.Constraints [0]);
453                                 Fail ("A1");
454                         } catch (Exception e) {
455                                 AssertEquals ("A2", typeof (IndexOutOfRangeException), e.GetType ());
456                         }
457                 }
458
459         }
460         
461 }