1 // Licensed to the .NET Foundation under one or more agreements.
2 // See the LICENSE file in the project root for more information.
3 
4 // Copyright (c) 2004 Mainsoft Co.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25 
26 using Xunit;
27 
28 
29 namespace System.Data.Tests
30 {
31     public class InvalidConstraintExceptionTest
32     {
33         [Fact]
Generate()34         public void Generate()
35         {
36             DataTable dtParent;
37             dtParent = DataProvider.CreateParentDataTable();
38             Exception tmpEx = new Exception();
39 
40             //------ check ForeignKeyConstraint  ---------
41             DataTable dtChild = DataProvider.CreateChildDataTable();
42             var ds = new DataSet();
43             ds.Tables.Add(dtChild);
44             ds.Tables.Add(dtParent);
45 
46             ds.Relations.Add(new DataRelation("myRelation", dtParent.Columns[0], dtChild.Columns[0], true));
47 
48             //update to value which is not exists in Parent table
49             // InvalidConstraintException - update child row
50             Assert.Throws<InvalidConstraintException>(() =>
51             {
52                 dtChild.Rows[0]["ParentId"] = 99;
53             });
54 
55             //Add another relation to the same column of the existing relation in child table
56             // InvalidConstraintException - Add Relation Child
57             Assert.Throws<InvalidConstraintException>(() =>
58             {
59                 ds.Relations.Add(new DataRelation("test", dtParent.Columns[2], dtChild.Columns[0], true));
60             });
61 
62             //Attempt to clear rows from parent table
63             // InvalidConstraintException - RowsCollection.Clear
64             Assert.Throws<InvalidConstraintException>(() =>
65             {
66                 dtParent.Rows.Clear();
67             });
68 
69             //try to run commands on two different datasets
70             DataSet ds1 = new DataSet();
71             ds1.Tables.Add(dtParent.Copy());
72 
73             // InvalidConstraintException - Add relation with two DataSets
74             Assert.Throws<InvalidConstraintException>(() =>
75             {
76                 ds.Relations.Add(new DataRelation("myRelation", ds1.Tables[0].Columns[0], dtChild.Columns[0], true));
77             });
78         }
79     }
80 }
81