Standardized Mainsoft System.Data exception tests.
[mono.git] / mcs / class / System.Data / Test / System.Data / ConstraintExceptionTest.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
5 //   Ofer Borstein
6 //
7 // Copyright (c) 2004 Mainsoft Co.
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 using NUnit.Framework;
30 using System;
31 using System.Text;
32 using System.IO;
33 using System.Data;
34 using MonoTests.System.Data.Test.Utils;
35
36 namespace MonoTests_System.Data
37 {
38         [TestFixture] public class ConstraintExceptionTest
39         {
40                 [Test] public void Generate()
41                 {
42                         DataTable dtParent= GHTUtils.DataProvider.CreateParentDataTable(); 
43                         DataTable dtChild = GHTUtils.DataProvider.CreateChildDataTable(); 
44
45                         DataSet ds = new DataSet();
46                         ds.Tables.Add(dtChild);
47                         ds.Tables.Add(dtParent);
48
49                         //------ check UniqueConstraint ---------
50
51                         //create unique constraint
52                         UniqueConstraint uc; 
53
54                         //Column type = int
55                         uc = new UniqueConstraint(dtParent.Columns[0]); 
56                         dtParent.Constraints.Add(uc);
57                         // UniqueConstraint Exception - Column type = int
58                         try 
59                         {
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.");
63                         }
64                         catch (ConstraintException) {}
65                         catch (AssertionException) { throw; }
66                         catch (Exception exc)
67                         {
68                                 Assert.Fail("CNE2: Rows.Add wrong exception type. Got: " + exc);
69                         }
70
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
76                         try 
77                         {
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.");
81                         }
82                         catch (ConstraintException) {}
83                         catch (AssertionException) { throw; }
84                         catch (Exception exc)
85                         {
86                                 Assert.Fail("CNE4: Rows.Add wrong exception type. Got: " + exc);
87                         }
88
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
94                         try 
95                         {
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.");
99                         }
100                         catch (ConstraintException) {}
101                         catch (AssertionException) { throw; }
102                         catch (Exception exc)
103                         {
104                                 Assert.Fail("CNE6: Rows.Add wrong exception type. Got: " + exc);
105                         }
106
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
112                         try 
113                         {
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.");
117                         }
118                         catch (ConstraintException) {}
119                         catch (AssertionException) { throw; }
120                         catch (Exception exc)
121                         {
122                                 Assert.Fail("CNE8: Rows.Add wrong exception type. Got: " + exc);
123                         }
124
125                         //Column type = string, ds.CaseSensitive = false;
126                         ds.CaseSensitive = false;
127
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();
134
135                         // UniqueConstraint Exception - Column type = String, CaseSensitive = false;
136                         try 
137                         {
138                                 dtParent.Rows.Add(dr);
139                                 Assert.Fail("CNE9: Rows.Add failed to raise ConstraintException.");
140                         }
141                         catch (ConstraintException) {}
142                         catch (AssertionException) { throw; }
143                         catch (Exception exc)
144                         {
145                                 Assert.Fail("CNE10: Rows.Add wrong exception type. Got: " + exc);
146                         }
147
148                         //Column type = string, ds.CaseSensitive = true;
149                         ds.CaseSensitive = true;
150
151                         dtParent.Constraints.Clear();
152                         uc = new UniqueConstraint(dtParent.Columns["String1"]); 
153                         dtParent.Constraints.Add(uc);
154
155                         // No UniqueConstraint Exception - Column type = String, CaseSensitive = true;
156                         dtParent.Rows.Add(dr);
157
158                         // Column type = string, ds.CaseSensitive = false;
159                         // UniqueConstraint Exception - Column type = String, Enable CaseSensitive = true;
160                         try 
161                         {
162                                 ds.CaseSensitive = false;
163                                 Assert.Fail("CNE13: CaseSensitive failed to raise ConstraintException.");
164                         }
165                         catch (ConstraintException) {}
166                         catch (AssertionException) { throw; }
167                         catch (Exception exc)
168                         {
169                                 Assert.Fail("CNE14: CaseSensitive wrong exception type. Got: " + exc);
170                         }
171
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);
175
176                         // UniqueConstraint Exception - ds.EnforceConstraints 
177                         try 
178                         {
179                                 ds.EnforceConstraints = true;
180                                 Assert.Fail("CNE15: EnforceConstraints failed to raise ConstraintException.");
181                         }
182                         catch (ConstraintException) {}
183                         catch (AssertionException) { throw; }
184                         catch (Exception exc)
185                         {
186                                 Assert.Fail("CNE16: EnforceConstraints wrong exception type. Got: " + exc);
187                         }
188                 }
189         }
190 }