2 // DataRelationTest.cs - NUnit Test Cases for DataRelation
5 // Ville Palo (vi64pa@koti.soon.fi)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
9 // (C) 2003 Martin Willemoes Hansen
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using NUnit
.Framework
;
39 namespace MonoTests
.System
.Data
42 public class DataRelationTest
: Assertion
44 private DataSet Set
= null;
45 private DataTable Mom
= null;
46 private DataTable Child
= null;
49 public void GetReady()
52 Mom
= new DataTable ("Mom");
53 Child
= new DataTable ("Child");
55 Set
.Tables
.Add (Child
);
57 DataColumn Col
= new DataColumn ("Name");
58 DataColumn Col2
= new DataColumn ("ChildName");
59 Mom
.Columns
.Add (Col
);
60 Mom
.Columns
.Add (Col2
);
62 DataColumn Col3
= new DataColumn ("Name");
63 DataColumn Col4
= new DataColumn ("Age");
64 Col4
.DataType
= Type
.GetType ("System.Int16");
65 Child
.Columns
.Add (Col3
);
66 Child
.Columns
.Add (Col4
);
70 public void Foreign ()
72 DataRelation Relation
= new DataRelation ("Rel", Mom
.Columns
[1], Child
.Columns
[0]);
73 Set
.Relations
.Add (Relation
);
75 DataRow Row
= Mom
.NewRow ();
89 Row
= Child
.NewRow ();
94 Row
= Child
.NewRow ();
99 AssertEquals ("test#01", 2, Child
.Rows
.Count
);
104 AssertEquals ("test#02", 1, Child
.Rows
.Count
);
113 } catch (Exception e
) {
114 AssertEquals ("test#04", typeof (ConstraintException
), e
.GetType ());
115 AssertEquals ("test#05", "Column 'ChildName' is constrained to be unique. Value 'Dick' is already present.", e
.Message
);
122 AssertEquals ("test#06", 1, Child
.Rows
.Count
);
124 Row
= Child
.NewRow ();
129 Child
.Rows
.Add (Row
);
131 } catch (Exception e
) {
132 AssertEquals ("test#08", typeof (InvalidConstraintException
), e
.GetType ());
133 AssertEquals ("test#09", "ForeignKeyConstraint Rel requires the child key values (Jack) to exist in the parent table.", e
.Message
);
139 [ExpectedException (typeof(InvalidConstraintException
))]
140 public void InvalidConstraintException ()
142 // Parent Columns and Child Columns don't have type-matching columns.
143 DataRelation Relation
= new DataRelation ("Rel", Mom
.Columns
[1], Child
.Columns
[1], true);
147 public void InvalidConstraintException2 ()
149 // Parent Columns and Child Columns don't have type-matching columns.
150 Child
.Columns
[1].DataType
= Mom
.Columns
[1].DataType
;
152 DataRelation Relation
= new DataRelation ("Rel", Mom
.Columns
[1], Child
.Columns
[1], true);
153 Set
.Relations
.Add (Relation
);
154 AssertEquals("test#01", 1, Set
.Relations
.Count
);
156 Child
.Columns
[1].DataType
= Type
.GetType ("System.Double");
160 public void DataSetRelations ()
162 DataRelation Relation
;
163 AssertEquals ("test#01", 0, Set
.Relations
.Count
);
164 AssertEquals ("test#02", 0, Mom
.ParentRelations
.Count
);
165 AssertEquals ("test#03", 0, Mom
.ChildRelations
.Count
);
166 AssertEquals ("test#04", 0, Child
.ParentRelations
.Count
);
167 AssertEquals ("test#05", 0, Child
.ChildRelations
.Count
);
169 Relation
= new DataRelation ("Rel", Mom
.Columns
[1], Child
.Columns
[0]);
170 Set
.Relations
.Add (Relation
);
172 AssertEquals ("test#06", 1, Set
.Relations
.Count
);
173 AssertEquals ("test#07", 0, Mom
.ParentRelations
.Count
);
174 AssertEquals ("test#08", 1, Mom
.ChildRelations
.Count
);
175 AssertEquals ("test#09", 1, Child
.ParentRelations
.Count
);
176 AssertEquals ("test#10", 0, Child
.ChildRelations
.Count
);
178 Relation
= Set
.Relations
[0];
179 AssertEquals ("test#11", 1, Relation
.ParentColumns
.Length
);
180 AssertEquals ("test#12", 1, Relation
.ChildColumns
.Length
);
181 AssertEquals ("test#13", "Rel", Relation
.ChildKeyConstraint
.ConstraintName
);
182 AssertEquals ("test#14", "Constraint1", Relation
.ParentKeyConstraint
.ConstraintName
);
186 public void Constraints ()
189 AssertEquals ("test#01", 0, Mom
.Constraints
.Count
);
190 AssertEquals ("test#02", 0, Child
.Constraints
.Count
);
192 DataRelation Relation
= new DataRelation ("Rel", Mom
.Columns
[1], Child
.Columns
[0]);
193 Set
.Relations
.Add (Relation
);
195 AssertEquals ("test#03", 1, Mom
.Constraints
.Count
);
196 AssertEquals ("test#04", 1, Child
.Constraints
.Count
);
197 AssertEquals ("test#05", typeof (ForeignKeyConstraint
), Child
.Constraints
[0].GetType ());
198 AssertEquals ("test#05", typeof (UniqueConstraint
), Mom
.Constraints
[0].GetType ());
203 public void Creation ()
206 DataRelation Relation
= new DataRelation ("Rel", Mom
.Columns
[1], Child
.Columns
[0]);
207 Set
.Relations
.Add (Relation
);
208 DataRelation Test
= null;
209 AssertEquals ("test#01", 1, Mom
.ChildRelations
.Count
);
210 AssertEquals ("test#02", 0, Child
.ChildRelations
.Count
);
211 AssertEquals ("test#03", 0, Mom
.ParentRelations
.Count
);
212 AssertEquals ("test#04", 1, Child
.ParentRelations
.Count
);
214 Test
= Child
.ParentRelations
[0];
215 AssertEquals ("test#05", "Rel", Test
.ToString ());
216 AssertEquals ("test#06", "Rel", Test
.RelationName
);
217 AssertEquals ("test#07", "Mom", Test
.ParentTable
.TableName
);
218 AssertEquals ("test#08", 1, Test
.ParentKeyConstraint
.Columns
.Length
);
219 AssertEquals ("test#09", false, Test
.ParentKeyConstraint
.IsPrimaryKey
);
220 AssertEquals ("test#10", 1, Test
.ParentColumns
.Length
);
221 AssertEquals ("test#11", false, Test
.Nested
);
222 AssertEquals ("test#12", 0, Test
.ExtendedProperties
.Count
);
223 AssertEquals ("test#13", "Child", Test
.ChildTable
.TableName
);
224 AssertEquals ("test#14", "Rel", Test
.ChildKeyConstraint
.ConstraintName
);
225 AssertEquals ("test#15", 1, Test
.ChildColumns
.Length
);
229 public void Creation2 ()
231 DataSet Set
= new DataSet ();
232 DataTable Mom2
= new DataTable ("Mom");
233 DataTable Child2
= new DataTable ("Child");
234 DataTable Hubby
= new DataTable ("Hubby");
235 Set
.Tables
.Add (Mom2
);
236 Set
.Tables
.Add (Child2
);
237 Set
.Tables
.Add (Hubby
);
239 DataColumn Col
= new DataColumn ("Name");
240 DataColumn Col2
= new DataColumn ("ChildName");
241 DataColumn Col3
= new DataColumn ("hubby");
242 Mom2
.Columns
.Add (Col
);
243 Mom2
.Columns
.Add (Col2
);
244 Mom2
.Columns
.Add (Col3
);
246 DataColumn Col4
= new DataColumn ("Name");
247 DataColumn Col5
= new DataColumn ("Age");
248 DataColumn Col6
= new DataColumn ("father");
249 Child2
.Columns
.Add (Col4
);
250 Child2
.Columns
.Add (Col5
);
251 Child2
.Columns
.Add (Col6
);
254 DataColumn Col7
= new DataColumn ("Name");
255 DataColumn Col8
= new DataColumn ("Age");
256 Hubby
.Columns
.Add (Col7
);
257 Hubby
.Columns
.Add (Col8
);
260 DataColumn
[] Parents
= new DataColumn
[2];
263 DataColumn
[] Childs
= new DataColumn
[2];
267 DataRelation Relation
= null;
269 Relation
= new DataRelation ("Rel", Parents
, Childs
);
271 } catch (InvalidConstraintException e
) {
272 // AssertEquals ("test#02", typeof (InvalidConstraintException), e.GetType ());
273 // AssertEquals ("test#03", "Cannot create a Key from Columns that belong to different tables.", e.Message);
277 Relation
= new DataRelation ("Rel", Parents
, Childs
);
279 Set
.Relations
.Add (Relation
);
281 DataRelation Test
= null;
282 AssertEquals ("test#01", 1, Mom2
.ChildRelations
.Count
);
283 AssertEquals ("test#02", 0, Child2
.ChildRelations
.Count
);
284 AssertEquals ("test#03", 0, Mom2
.ParentRelations
.Count
);
285 AssertEquals ("test#04", 1, Child2
.ParentRelations
.Count
);
287 Test
= Child2
.ParentRelations
[0];
288 AssertEquals ("test#05", "Rel", Test
.ToString ());
289 AssertEquals ("test#06", "Rel", Test
.RelationName
);
290 AssertEquals ("test#07", "Mom", Test
.ParentTable
.TableName
);
291 AssertEquals ("test#08", 2, Test
.ParentKeyConstraint
.Columns
.Length
);
292 AssertEquals ("test#09", false, Test
.ParentKeyConstraint
.IsPrimaryKey
);
293 AssertEquals ("test#10", 2, Test
.ParentColumns
.Length
);
294 AssertEquals ("test#11", false, Test
.Nested
);
295 AssertEquals ("test#12", 0, Test
.ExtendedProperties
.Count
);
296 AssertEquals ("test#13", "Child", Test
.ChildTable
.TableName
);
297 AssertEquals ("test#14", "Rel", Test
.ChildKeyConstraint
.ConstraintName
);
298 AssertEquals ("test#15", 2, Test
.ChildColumns
.Length
);
299 AssertEquals ("test#16", 1, Mom2
.Constraints
.Count
);
300 AssertEquals ("test#17", "Constraint1", Mom2
.Constraints
[0].ToString ());
301 AssertEquals ("test#18", 1, Child2
.Constraints
.Count
);
302 AssertEquals ("test#19", 0, Hubby
.Constraints
.Count
);
306 public void Creation3 ()
309 DataRelation Relation
= new DataRelation ("Rel", Mom
.Columns
[1], Child
.Columns
[0], false);
310 Set
.Relations
.Add (Relation
);
311 DataRelation Test
= null;
313 AssertEquals ("test#01", 1, Mom
.ChildRelations
.Count
);
314 AssertEquals ("test#02", 0, Child
.ChildRelations
.Count
);
315 AssertEquals ("test#03", 0, Mom
.ParentRelations
.Count
);
316 AssertEquals ("test#04", 1, Child
.ParentRelations
.Count
);
318 Test
= Child
.ParentRelations
[0];
320 AssertEquals ("test#05", "Rel", Test
.ToString ());
322 AssertEquals ("test#06", "Rel", Test
.RelationName
);
323 AssertEquals ("test#07", "Mom", Test
.ParentTable
.TableName
);
325 Assert ("test#08", Test
.ParentKeyConstraint
== null);
327 Assert ("test#09", Test
.ParentKeyConstraint
== null);
329 AssertEquals ("test#10", 1, Test
.ParentColumns
.Length
);
330 AssertEquals ("test#11", false, Test
.Nested
);
331 AssertEquals ("test#12", 0, Test
.ExtendedProperties
.Count
);
332 AssertEquals ("test#13", "Child", Test
.ChildTable
.TableName
);
334 Assert ("test#14", Test
.ChildKeyConstraint
== null);
335 AssertEquals ("test#15", 1, Test
.ChildColumns
.Length
);
336 AssertEquals ("test#16", 0, Mom
.Constraints
.Count
);
337 AssertEquals ("test#17", 0, Child
.Constraints
.Count
);
342 [Ignore ("Set.Relations.AddRange() fails, but why?")]
343 public void Creation4 ()
346 DataRelation Relation
= new DataRelation ("Rel", "Mom", "Child",
347 new string [] {"ChildName"}
,
348 new string [] {"Name"}
, true);
351 Set
.Relations
.Add (Relation
);
353 } catch (Exception e
) {
354 AssertEquals ("test#02", typeof (NullReferenceException
), e
.GetType ());
358 Set
.Relations
.AddRange (new DataRelation
[] {Relation}
);
360 } catch (Exception e
) {
361 AssertEquals ("test#04", typeof (NullReferenceException
), e
.GetType ());
365 Set
.Relations
.AddRange (new DataRelation
[] {Relation}
);
368 DataRelation Test
= null;
369 AssertEquals ("test#01", 1, Mom
.ChildRelations
.Count
);
370 AssertEquals ("test#02", 0, Child
.ChildRelations
.Count
);
371 AssertEquals ("test#03", 0, Mom
.ParentRelations
.Count
);
372 AssertEquals ("test#04", 1, Child
.ParentRelations
.Count
);
374 Test
= Child
.ParentRelations
[0];
375 AssertEquals ("test#05", "Rel", Test
.ToString ());
376 AssertEquals ("test#06", "Rel", Test
.RelationName
);
377 AssertEquals ("test#07", "Mom", Test
.ParentTable
.TableName
);
379 AssertEquals ("test#08", true, Test
.ParentKeyConstraint
== null);
381 AssertEquals ("test#10", 1, Test
.ParentColumns
.Length
);
382 AssertEquals ("test#11", true, Test
.Nested
);
383 AssertEquals ("test#12", 0, Test
.ExtendedProperties
.Count
);
384 AssertEquals ("test#13", "Child", Test
.ChildTable
.TableName
);
385 AssertEquals ("test#14", true, Test
.ChildKeyConstraint
== null);
386 AssertEquals ("test#15", 1, Test
.ChildColumns
.Length
);
391 public void RelationFromSchema ()
393 DataSet Set
= new DataSet ();
394 Set
.ReadXmlSchema ("Test/System.Data/store.xsd");
395 DataTable Table
= Set
.Tables
[0];
397 AssertEquals ("test#01", false, Table
.CaseSensitive
);
398 AssertEquals ("test#02", 1, Table
.ChildRelations
.Count
);
399 AssertEquals ("test#03", 0, Table
.ParentRelations
.Count
);
400 AssertEquals ("test#04", 1, Table
.Constraints
.Count
);
401 AssertEquals ("test#05", 1, Table
.PrimaryKey
.Length
);
402 AssertEquals ("test#06", 0, Table
.Rows
.Count
);
403 AssertEquals ("test#07", "bookstore", Table
.TableName
);
404 AssertEquals ("test#08", 1, Table
.Columns
.Count
);
406 DataRelation Relation
= Table
.ChildRelations
[0];
407 AssertEquals ("test#09", 1, Relation
.ChildColumns
.Length
);
408 AssertEquals ("test#10", "bookstore_book", Relation
.ChildKeyConstraint
.ConstraintName
);
409 AssertEquals ("test#11", 1, Relation
.ChildKeyConstraint
.Columns
.Length
);
410 AssertEquals ("test#12", "book", Relation
.ChildTable
.TableName
);
411 AssertEquals ("test#13", "NewDataSet", Relation
.DataSet
.DataSetName
);
412 AssertEquals ("test#14", 0, Relation
.ExtendedProperties
.Count
);
413 AssertEquals ("test#15", true, Relation
.Nested
);
414 AssertEquals ("test#16", 1, Relation
.ParentColumns
.Length
);
415 AssertEquals ("test#17", "Constraint1", Relation
.ParentKeyConstraint
.ConstraintName
);
416 AssertEquals ("test#18", "bookstore", Relation
.ParentTable
.TableName
);
417 AssertEquals ("test#19", "bookstore_book", Relation
.RelationName
);
419 Table
= Set
.Tables
[1];
421 AssertEquals ("test#20", false, Table
.CaseSensitive
);
422 AssertEquals ("test#21", 1, Table
.ChildRelations
.Count
);
423 AssertEquals ("test#22", 1, Table
.ParentRelations
.Count
);
424 AssertEquals ("test#23", 2, Table
.Constraints
.Count
);
425 AssertEquals ("test#24", 1, Table
.PrimaryKey
.Length
);
426 AssertEquals ("test#25", 0, Table
.Rows
.Count
);
427 AssertEquals ("test#26", "book", Table
.TableName
);
428 AssertEquals ("test#27", 5, Table
.Columns
.Count
);
430 Relation
= Table
.ChildRelations
[0];
431 AssertEquals ("test#28", 1, Relation
.ChildColumns
.Length
);
432 AssertEquals ("test#29", "book_author", Relation
.ChildKeyConstraint
.ConstraintName
);
433 AssertEquals ("test#30", 1, Relation
.ChildKeyConstraint
.Columns
.Length
);
434 AssertEquals ("test#31", "author", Relation
.ChildTable
.TableName
);
435 AssertEquals ("test#32", "NewDataSet", Relation
.DataSet
.DataSetName
);
436 AssertEquals ("test#33", 0, Relation
.ExtendedProperties
.Count
);
437 AssertEquals ("test#34", true, Relation
.Nested
);
438 AssertEquals ("test#35", 1, Relation
.ParentColumns
.Length
);
439 AssertEquals ("test#36", "Constraint1", Relation
.ParentKeyConstraint
.ConstraintName
);
440 AssertEquals ("test#37", "book", Relation
.ParentTable
.TableName
);
441 AssertEquals ("test#38", "book_author", Relation
.RelationName
);
443 Table
= Set
.Tables
[2];
444 AssertEquals ("test#39", false, Table
.CaseSensitive
);
445 AssertEquals ("test#40", 0, Table
.ChildRelations
.Count
);
446 AssertEquals ("test#41", 1, Table
.ParentRelations
.Count
);
447 AssertEquals ("test#42", 1, Table
.Constraints
.Count
);
448 AssertEquals ("test#43", 0, Table
.PrimaryKey
.Length
);
449 AssertEquals ("test#44", 0, Table
.Rows
.Count
);
450 AssertEquals ("test#45", "author", Table
.TableName
);
451 AssertEquals ("test#46", 3, Table
.Columns
.Count
);
455 public void ChildRows ()
458 DataRelation Relation
= new DataRelation ("Rel", Mom
.Columns
[1], Child
.Columns
[0]);
459 Set
.Relations
.Add (Relation
);
461 DataRow TempRow
= Mom
.NewRow ();
462 TempRow
[0] = "teresa";
463 TempRow
[1] = "john";
464 Mom
.Rows
.Add (TempRow
);
466 TempRow
= Mom
.NewRow ();
467 TempRow
[0] = "teresa";
468 TempRow
[1] = "Dick";
469 Mom
.Rows
.Add (TempRow
);
471 TempRow
= Child
.NewRow ();
472 TempRow
[0] = "john";
474 Child
.Rows
.Add (TempRow
);
476 TempRow
= Child
.NewRow ();
477 TempRow
[0] = "Dick";
479 Child
.Rows
.Add (TempRow
);
481 DataRow Row
= Mom
.Rows
[1];
482 TempRow
= Row
.GetChildRows ("Rel") [0];
483 AssertEquals ("test#01", "Dick", TempRow
[0]);
484 AssertEquals ("test#02", "10", TempRow
[1].ToString ());
485 TempRow
= TempRow
.GetParentRow ("Rel");
486 AssertEquals ("test#03", "teresa", TempRow
[0]);
487 AssertEquals ("test#04", "Dick", TempRow
[1]);
489 Row
= Child
.Rows
[0];
490 TempRow
= Row
.GetParentRows ("Rel") [0];
491 AssertEquals ("test#05", "teresa", TempRow
[0]);
492 AssertEquals ("test#06", "john", TempRow
[1]);