1 // ConstraintCollection.cs - NUnit Test Cases for testing the ConstraintCollection
5 // Franklin Wise (gracenote@earthlink.net)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Roopa Wilson (rowilson@novell.com)
10 // (C) 2003 Martin Willemoes Hansen
15 using NUnit.Framework;
19 namespace MonoTests.System.Data
22 public class ConstraintCollectionTest : Assertion {
23 private DataTable _table;
24 private DataTable _table2;
25 private Constraint _constraint1;
26 private Constraint _constraint2;
29 public void GetReady()
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));
37 _table2 = new DataTable("TestTable");
38 _table2.Columns.Add("Col1",typeof(int));
39 _table2.Columns.Add("Col2",typeof(int));
41 //Use UniqueConstraint to test Constraint Base Class
42 _constraint1 = new UniqueConstraint(_table.Columns[0],false);
43 _constraint2 = new UniqueConstraint(_table.Columns[1],false);
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();
54 ConstraintCollection col = _table.Constraints;
55 col.Add(_constraint1);
56 col.Add(_constraint2);
58 AssertEquals("Count doesn't equal added.",2, col.Count);
62 public void AddExceptions()
64 ConstraintCollection col = _table.Constraints;
70 Fail("B1: Failed to throw ArgumentNullException.");
72 catch (ArgumentNullException) {}
73 catch (AssertionException exc) {throw exc;}
76 Fail("A1: Wrong exception type");
82 _constraint1.ConstraintName = "Dog";
83 _constraint2.ConstraintName = "dog"; //case insensitive
84 col.Add(_constraint1);
85 col.Add(_constraint2);
88 Fail("Failed to throw Duplicate name exception.");
90 col.Remove (_constraint2); // only for !1.0
91 col.Remove (_constraint1);
94 catch (DuplicateNameException) {
97 catch (AssertionException exc) {throw exc;}
98 /* Don't use such catch. They cover our eyes from the exact exception location.
101 Fail("A2: Wrong exception type. " + exc.ToString());
104 //Constraint Already exists
107 col.Add(_constraint1);
110 Fail("B2: Failed to throw ArgumentException.");
112 col.Remove (_constraint1);
114 catch (ArgumentException) {
120 catch (AssertionException exc) {throw exc;}
123 Fail("A3: Wrong exception type");
128 public void Indexer()
130 Constraint c1 = new UniqueConstraint(_table.Columns[0]);
131 Constraint c2 = new UniqueConstraint(_table.Columns[1]);
133 c1.ConstraintName = "first";
134 c2.ConstraintName = "second";
137 _table.Constraints.Add(c1);
138 _table.Constraints.Add(c2);
140 AssertSame("A1", c1, _table.Constraints[0]);
141 AssertSame("A2", c2, _table.Constraints[1]);
143 AssertSame("A3", c1, _table.Constraints["first"]);
144 AssertSame("A4", c2, _table.Constraints["sEcond"]); //case insensitive
149 public void IndexOf()
151 Constraint c1 = new UniqueConstraint(_table.Columns[0]);
152 Constraint c2 = new UniqueConstraint(_table.Columns[1]);
154 c1.ConstraintName = "first";
155 c2.ConstraintName = "second";
157 _table.Constraints.Add(c1);
158 _table.Constraints.Add(c2);
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"));
167 public void Contains()
169 Constraint c1 = new UniqueConstraint(_table.Columns[0]);
170 Constraint c2 = new UniqueConstraint(_table.Columns[1]);
172 c1.ConstraintName = "first";
173 c2.ConstraintName = "second";
175 _table.Constraints.Add(c1);
177 Assert("A1", _table.Constraints.Contains(c1.ConstraintName)); //true
178 Assert("A2", _table.Constraints.Contains(c2.ConstraintName) == false); //doesn't contain
182 public void IndexerFailures()
184 _table.Constraints.Add(new UniqueConstraint(_table.Columns[0]));
187 AssertNull(_table.Constraints["notInCollection"]);
192 Constraint c = _table.Constraints[_table.Constraints.Count];
193 Fail("B1: Failed to throw IndexOutOfRangeException.");
195 catch (IndexOutOfRangeException) {}
196 catch (AssertionException exc) {throw exc;}
199 Fail("A1: Wrong exception type");
205 Constraint c = _table.Constraints[-1];
206 Fail("B2: Failed to throw IndexOutOfRangeException.");
208 catch (IndexOutOfRangeException) {}
209 catch (AssertionException exc) {throw exc;}
212 Fail("A2: Wrong exception type");
218 public void AddFkException1()
220 DataSet ds = new DataSet();
221 ds.Tables.Add(_table);
222 _table2.TableName = "TestTable2";
223 ds.Tables.Add(_table2);
225 _table.Rows.Add(new object [] {1});
226 _table.Rows.Add(new object [] {1});
228 //FKC: can't create unique constraint because duplicate values already exist
231 ForeignKeyConstraint fkc = new ForeignKeyConstraint( _table.Columns[0],
234 _table2.Constraints.Add(fkc); //should throw
235 Fail("B1: Failed to throw ArgumentException.");
237 catch (ArgumentException) {}
238 catch (AssertionException exc) {throw exc;}
239 catch (Exception exc)
241 Fail("A1: Wrong Exception type. " + exc.ToString());
249 public void AddFkException2()
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);
258 _table.Rows.Add(new object [] {1});
260 // will need a matching parent value in
262 _table2.Rows.Add(new object [] {3});
265 //FKC: no matching parent value
268 ForeignKeyConstraint fkc = new ForeignKeyConstraint( _table.Columns[0],
271 _table2.Constraints.Add(fkc); //should throw
272 Fail("B1: Failed to throw ArgumentException.");
274 catch (ArgumentException) {}
275 catch (AssertionException exc) {throw exc;}
276 catch (Exception exc)
278 Fail("A1: Wrong Exception type. " + exc.ToString());
286 public void AddUniqueExceptions()
290 //UC: can't create unique constraint because duplicate values already exist
293 _table.Rows.Add(new object [] {1});
294 _table.Rows.Add(new object [] {1});
295 UniqueConstraint uc = new UniqueConstraint( _table.Columns[0]);
297 _table.Constraints.Add(uc); //should throw
298 Fail("B1: Failed to throw ArgumentException.");
300 catch (ArgumentException) {}
301 catch (AssertionException exc) {throw exc;}
302 catch (Exception exc)
304 Fail("A1: Wrong Exception type. " + exc.ToString());
309 //Tests AddRange (), CanRemove (), RemoveAt (), Remove (), Exceptions of Remove(), and Clear ()
310 public void AddRemoveTest ()
313 // CanRemove (); This test is ignored
315 // RemoveAt (); This test is ignored
317 // This test is expected to be failed, so don't reuse it.
318 // RemoveExceptions ();
319 _table.Constraints.Remove (_table.Constraints [0]);
325 public void AddRange()
327 _constraint1.ConstraintName = "UK1";
328 _constraint2.ConstraintName = "UK12";
330 ForeignKeyConstraint _constraint3 = new ForeignKeyConstraint ("FK2", _table.Columns [0],
331 _table2.Columns [0]);
332 UniqueConstraint _constraint4=new UniqueConstraint("UK2", _table2.Columns [1]);
334 // Add the constraints.
335 Constraint [] constraints = {_constraint1, _constraint2};
336 _table.Constraints.AddRange (constraints);
338 Constraint [] constraints1 = {_constraint3, _constraint4};
339 _table2.Constraints.AddRange (constraints1);
341 AssertEquals ("A1", "UK1", _table.Constraints [0].ConstraintName);
342 AssertEquals ("A2", "UK12", _table.Constraints [1].ConstraintName);
344 AssertEquals ("A3", "FK2", _table2.Constraints [0].ConstraintName);
345 AssertEquals ("A4", "UK2", _table2.Constraints [1].ConstraintName);
350 public void TestAddRange2()
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"};
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);
367 //Console.WriteLine(table.InitStatus == DataTable.initStatus.BeginInit);
368 table.Constraints.AddRange (constraints);
370 //Check the table property of UniqueConstraint Object
372 Assertion.AssertNull ("#01", constraints [2].Table);
374 catch (Exception e) {
375 Assertion.Assert ("#A02", "System.NullReferenceException".Equals (e.GetType().ToString()));
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"));
389 Assertion.Assert ("#A06", table.Constraints.Contains ("Unique3"));
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);
404 catch (Exception e) {
405 Console.WriteLine (e);
411 [Ignore ("This never works on MS.NET (and it should not)")]
412 public void CanRemove()
414 AssertEquals ("A1", false, _table.Constraints.CanRemove (_table.Constraints [0]));
419 public void CollectionChanged()
424 [Ignore ("MS.NET fails this test (and it should fail)")]
425 public void RemoveAt()
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);
436 [Ignore ("MS.NET fails this test (and it should fail)")]
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);
448 public void RemoveExceptions()
451 //Remove constraint that cannot be removed
452 _table.Constraints.Remove (_table.Constraints [0]);
454 } catch (Exception e) {
455 AssertEquals ("A2", typeof (IndexOutOfRangeException), e.GetType ());