2 // Rafael Mizrahi <rafim@mainsoft.com>
3 // Erez Lotan <erezl@mainsoft.com>
4 // Oren Gurfinkel <oreng@mainsoft.com>
7 // Copyright (c) 2004 Mainsoft Co.
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using NUnit.Framework;
34 using MonoTests.System.Data.Test.Utils;
36 namespace MonoTests_System.Data
38 [TestFixture] public class ConstraintExceptionTest
40 [Test] public void Generate()
42 DataTable dtParent= GHTUtils.DataProvider.CreateParentDataTable();
43 DataTable dtChild = GHTUtils.DataProvider.CreateChildDataTable();
45 DataSet ds = new DataSet();
46 ds.Tables.Add(dtChild);
47 ds.Tables.Add(dtParent);
49 //------ check UniqueConstraint ---------
51 //create unique constraint
55 uc = new UniqueConstraint(dtParent.Columns[0]);
56 dtParent.Constraints.Add(uc);
57 // UniqueConstraint Exception - Column type = int
60 //add exisiting value - will raise exception
61 dtParent.Rows.Add(dtParent.Rows[0].ItemArray);
62 Assert.Fail("CNE1: Rows.Add failed to raise ConstraintException.");
64 catch (ConstraintException) {}
65 catch (AssertionException) { throw; }
68 Assert.Fail("CNE2: Rows.Add wrong exception type. Got: " + exc);
71 //Column type = DateTime
72 dtParent.Constraints.Clear();
73 uc = new UniqueConstraint(dtParent.Columns["ParentDateTime"]);
74 dtParent.Constraints.Add(uc);
75 // UniqueConstraint Exception - Column type = DateTime
78 //add exisiting value - will raise exception
79 dtParent.Rows.Add(dtParent.Rows[0].ItemArray);
80 Assert.Fail("CNE3: Rows.Add failed to raise ConstraintException.");
82 catch (ConstraintException) {}
83 catch (AssertionException) { throw; }
86 Assert.Fail("CNE4: Rows.Add wrong exception type. Got: " + exc);
89 //Column type = double
90 dtParent.Constraints.Clear();
91 uc = new UniqueConstraint(dtParent.Columns["ParentDouble"]);
92 dtParent.Constraints.Add(uc);
93 // UniqueConstraint Exception - Column type = double
96 //add exisiting value - will raise exception
97 dtParent.Rows.Add(dtParent.Rows[0].ItemArray);
98 Assert.Fail("CNE5: Rows.Add failed to raise ConstraintException.");
100 catch (ConstraintException) {}
101 catch (AssertionException) { throw; }
102 catch (Exception exc)
104 Assert.Fail("CNE6: Rows.Add wrong exception type. Got: " + exc);
107 //Column type = string
108 dtParent.Constraints.Clear();
109 uc = new UniqueConstraint(dtParent.Columns["String1"]);
110 dtParent.Constraints.Add(uc);
111 // UniqueConstraint Exception - Column type = String
114 //add exisiting value - will raise exception
115 dtParent.Rows.Add(dtParent.Rows[0].ItemArray);
116 Assert.Fail("CNE7: Rows.Add failed to raise ConstraintException.");
118 catch (ConstraintException) {}
119 catch (AssertionException) { throw; }
120 catch (Exception exc)
122 Assert.Fail("CNE8: Rows.Add wrong exception type. Got: " + exc);
125 //Column type = string, ds.CaseSensitive = false;
126 ds.CaseSensitive = false;
128 dtParent.Constraints.Clear();
129 uc = new UniqueConstraint(dtParent.Columns["String1"]);
130 dtParent.Constraints.Add(uc);
131 DataRow dr = dtParent.NewRow();
132 dr.ItemArray = dtParent.Rows[0].ItemArray ;
133 dr["String1"] = dr["String1"].ToString().ToUpper();
135 // UniqueConstraint Exception - Column type = String, CaseSensitive = false;
138 dtParent.Rows.Add(dr);
139 Assert.Fail("CNE9: Rows.Add failed to raise ConstraintException.");
141 catch (ConstraintException) {}
142 catch (AssertionException) { throw; }
143 catch (Exception exc)
145 Assert.Fail("CNE10: Rows.Add wrong exception type. Got: " + exc);
148 //Column type = string, ds.CaseSensitive = true;
149 ds.CaseSensitive = true;
151 dtParent.Constraints.Clear();
152 uc = new UniqueConstraint(dtParent.Columns["String1"]);
153 dtParent.Constraints.Add(uc);
155 // No UniqueConstraint Exception - Column type = String, CaseSensitive = true;
156 dtParent.Rows.Add(dr);
158 // Column type = string, ds.CaseSensitive = false;
159 // UniqueConstraint Exception - Column type = String, Enable CaseSensitive = true;
162 ds.CaseSensitive = false;
163 Assert.Fail("CNE13: CaseSensitive failed to raise ConstraintException.");
165 catch (ConstraintException) {}
166 catch (AssertionException) { throw; }
167 catch (Exception exc)
169 Assert.Fail("CNE14: CaseSensitive wrong exception type. Got: " + exc);
172 dtChild.Constraints.Add(new UniqueConstraint(new DataColumn[] {dtChild.Columns[0],dtChild.Columns[1]}));
173 ds.EnforceConstraints = false;
174 dtChild.Rows.Add(dtChild.Rows[0].ItemArray);
176 // UniqueConstraint Exception - ds.EnforceConstraints
179 ds.EnforceConstraints = true;
180 Assert.Fail("CNE15: EnforceConstraints failed to raise ConstraintException.");
182 catch (ConstraintException) {}
183 catch (AssertionException) { throw; }
184 catch (Exception exc)
186 Assert.Fail("CNE16: EnforceConstraints wrong exception type. Got: " + exc);