[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / System.Data / Test / System.Data / UniqueConstraintTest2.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.Data;
32 using MonoTests.System.Data.Utils;
33
34 namespace MonoTests_System.Data
35 {
36         [TestFixture] public class UniqueConstraintTest2
37         {
38                 [Test] public void Columns()
39                 {
40                         DataTable dtParent = DataProvider.CreateParentDataTable();
41
42                         UniqueConstraint uc = null;
43                         uc = new UniqueConstraint(dtParent.Columns[0]);
44
45                         // Columns 1
46                         Assert.AreEqual(1, uc.Columns.Length  , "UC1");
47
48                         // Columns 2
49                         Assert.AreEqual(dtParent.Columns[0], uc.Columns[0], "UC2");
50                 }
51
52                 [Test] public void Equals_O()
53                 {
54                         DataSet ds = new DataSet();
55                         DataTable dtParent = DataProvider.CreateParentDataTable();
56                         ds.Tables.Add(dtParent);
57
58                         UniqueConstraint  uc1,uc2;
59                         uc1 = new UniqueConstraint(dtParent.Columns[0]);
60
61                         uc2 = new UniqueConstraint(dtParent.Columns[1]);
62                         // different columnn
63                         Assert.AreEqual(false, uc1.Equals(uc2), "UC3");
64
65                         //Two System.Data.ForeignKeyConstraint are equal if they constrain the same columns.
66                         // same column
67                         uc2 = new UniqueConstraint(dtParent.Columns[0]);
68                         Assert.AreEqual(true, uc1.Equals(uc2), "UC4");
69                 }
70
71                 [Test] public void IsPrimaryKey()
72                 {
73                         DataTable dtParent = DataProvider.CreateParentDataTable();
74
75                         UniqueConstraint uc = null;
76                         uc = new UniqueConstraint(dtParent.Columns[0],false);
77                         dtParent.Constraints.Add(uc);
78
79                         // primary key 1
80                         Assert.AreEqual(false, uc.IsPrimaryKey , "UC5");
81
82                         dtParent.Constraints.Remove(uc);
83                         uc = new UniqueConstraint(dtParent.Columns[0],true);
84                         dtParent.Constraints.Add(uc);
85
86                         // primary key 2
87                         Assert.AreEqual(true, uc.IsPrimaryKey , "UC6");
88                 }
89
90                 [Test] public void Table()
91                 {
92                         DataSet ds = new DataSet();
93                         DataTable dtParent = DataProvider.CreateParentDataTable();
94                         ds.Tables.Add(dtParent);
95                         UniqueConstraint uc = null;
96                         uc = new UniqueConstraint(dtParent.Columns[0]);
97
98                         // Table
99                         Assert.AreEqual(dtParent , uc.Table , "UC7");
100                 }
101
102                 [Test] public new void ToString()
103                 {
104                         DataTable dtParent = DataProvider.CreateParentDataTable();
105
106                         UniqueConstraint uc = null;
107                         uc = new UniqueConstraint(dtParent.Columns[0],false);
108
109                         // ToString - default
110                         Assert.AreEqual(string.Empty , uc.ToString(), "UC8");
111
112                         uc = new UniqueConstraint("myConstraint",dtParent.Columns[0],false);
113                         // Tostring - Constraint name
114                         Assert.AreEqual("myConstraint", uc.ToString(), "UC9");
115                 }
116
117                 [Test] public void constraintName()
118                 {
119                         DataTable dtParent = DataProvider.CreateParentDataTable();
120
121                         UniqueConstraint uc = null;
122                         uc = new UniqueConstraint(dtParent.Columns[0]);
123
124                         // default 
125                         Assert.AreEqual(string.Empty , uc.ConstraintName , "UC10");
126
127                         uc.ConstraintName  = "myConstraint";
128
129                         // set/get 
130                         Assert.AreEqual("myConstraint" , uc.ConstraintName , "UC11");
131                 }
132
133                 [Test] public void ctor_DataColumn()
134                 {
135                         Exception tmpEx = new Exception();
136
137                         DataSet ds = new DataSet();
138                         DataTable dtParent = DataProvider.CreateParentDataTable();
139                         ds.Tables.Add(dtParent);
140                         ds.EnforceConstraints = true;
141
142                         UniqueConstraint uc = null;
143
144                         // DataColumn.Unique - without constraint
145                         Assert.AreEqual(false, dtParent.Columns[0].Unique , "UC12");
146
147                         uc = new UniqueConstraint(dtParent.Columns[0]);
148
149                         // Ctor
150                         Assert.AreEqual(false , uc == null , "UC13");
151
152                         // DataColumn.Unique - with constraint
153                         Assert.AreEqual(false, dtParent.Columns[0].Unique , "UC14");
154
155                         // Ctor - add exisiting column
156                         dtParent.Rows.Add(new object[] {99,"str1","str2"});
157                         dtParent.Constraints.Add(uc);
158                         try 
159                         {
160                                 dtParent.Rows.Add(new object[] {99,"str1","str2"});
161                                 Assert.Fail("DS333: Rows.Add Failed to throw ConstraintException");
162                         }
163                         catch (ConstraintException) {}
164                         catch (AssertionException exc) {throw  exc;}
165                         catch (Exception exc)
166                         {
167                                 Assert.Fail("DS334: Rows.Add. Wrong exception type. Got:" + exc);
168                         }
169
170                         DataTable dtChild = DataProvider.CreateChildDataTable(); 
171                         uc = new UniqueConstraint(dtChild.Columns[1]);
172
173                         //Column[1] is not unique, will throw exception
174                         // ArgumentException 
175                         try 
176                         {
177                                 dtChild.Constraints.Add(uc);        
178                                 Assert.Fail("DS333: Constraints.Add Failed to throw ArgumentException");
179                         }
180                         catch (ArgumentException) {}
181                         catch (AssertionException exc) {throw  exc;}
182                         catch (Exception exc)
183                         {
184                                 Assert.Fail("DS334: Constraints.Add. Wrong exception type. Got:" + exc);
185                         }
186
187                         //reset the table
188                         dtParent = DataProvider.CreateParentDataTable();
189
190                         // DataColumn.Unique = true, will add UniqueConstraint
191                         dtParent.Columns[0].Unique = true;
192                         Assert.AreEqual(1, dtParent.Constraints.Count , "UC15");
193
194                         // Check the created UniqueConstraint
195                         dtParent.Columns[0].Unique = true;
196                         Assert.AreEqual(typeof(UniqueConstraint).FullName, dtParent.Constraints[0].GetType().FullName , "UC16");
197
198                         // add UniqueConstarint that don't belong to the table
199                         try 
200                         {
201                                 dtParent.Constraints.Add(uc);
202                                 Assert.Fail("DS333: Constraints.Add Failed to throw ArgumentException");
203                         }
204                         catch (ArgumentException) {}
205                         catch (AssertionException exc) {throw  exc;}
206                         catch (Exception exc)
207                         {
208                                 Assert.Fail("DS334: Constraints.Add. Wrong exception type. Got:" + exc);
209                         }
210                 }
211
212                 [Test] public void ctor_DataColumnNoPrimary()
213                 {
214                         DataTable dtParent = DataProvider.CreateParentDataTable();
215
216                         UniqueConstraint uc = null;
217                         uc = new UniqueConstraint(dtParent.Columns[0],false);
218                         dtParent.Constraints.Add(uc);
219
220                         // Ctor
221                         Assert.AreEqual(false , uc == null , "UC17");
222
223                         // primary key 1
224                         Assert.AreEqual(0, dtParent.PrimaryKey.Length  , "UC18");
225
226                         dtParent.Constraints.Remove(uc);
227                         uc = new UniqueConstraint(dtParent.Columns[0],true);
228                         dtParent.Constraints.Add(uc);
229
230                         // primary key 2
231                         Assert.AreEqual(1, dtParent.PrimaryKey.Length  , "UC19");
232                 }
233
234                 [Test] public void ctor_DataColumns()
235                 {
236                         Exception tmpEx = new Exception();
237                         DataTable dtParent = DataProvider.CreateParentDataTable();
238
239                         UniqueConstraint uc = null;
240                         uc = new UniqueConstraint(new DataColumn[] {dtParent.Columns[0],dtParent.Columns[1]});
241
242                         // Ctor - parent
243                         Assert.AreEqual(false , uc == null , "UC20");
244
245                         // Ctor - add exisiting column
246                         dtParent.Rows.Add(new object[] {99,"str1","str2"});
247                         dtParent.Constraints.Add(uc);
248                         try 
249                         {
250                                 dtParent.Rows.Add(new object[] {99,"str1","str2"});
251                                 Assert.Fail("DS333: Rows.Add Failed to throw ConstraintException");
252                         }
253                         catch (ConstraintException) {}
254                         catch (AssertionException exc) {throw  exc;}
255                         catch (Exception exc)
256                         {
257                                 Assert.Fail("DS334: Rows.Add. Wrong exception type. Got:" + exc);
258                         }
259
260                         DataTable dtChild = DataProvider.CreateChildDataTable(); 
261                         uc = new UniqueConstraint(new DataColumn[] {dtChild.Columns[0],dtChild.Columns[1]});
262                         dtChild.Constraints.Add(uc);
263
264                         // Ctor - child
265                         Assert.AreEqual(false , uc == null , "UC21");
266
267                         dtChild.Constraints.Clear();
268                         uc = new UniqueConstraint(new DataColumn[] {dtChild.Columns[1],dtChild.Columns[2]});
269
270                         //target columnn are not unnique, will throw an exception
271                         // ArgumentException - child
272                         try 
273                         {
274                                 dtChild.Constraints.Add(uc);        
275                                 Assert.Fail("DS333: Constraints.Add Failed to throw ArgumentException");
276                         }
277                         catch (ArgumentException) {}
278                         catch (AssertionException exc) {throw  exc;}
279                         catch (Exception exc)
280                         {
281                                 Assert.Fail("DS334: Constraints.Add. Wrong exception type. Got:" + exc);
282                         }
283                 }
284
285                 [Test] public void ctor_DataColumnPrimary()
286                 {
287                         DataTable dtParent = DataProvider.CreateParentDataTable();
288
289                         UniqueConstraint uc = null;
290                         uc = new UniqueConstraint(dtParent.Columns[0],false);
291                         dtParent.Constraints.Add(uc);
292
293                         // Ctor
294                         Assert.AreEqual(false , uc == null , "UC22");
295
296                         // primary key 1
297                         Assert.AreEqual(0, dtParent.PrimaryKey.Length  , "UC23");
298
299                         dtParent.Constraints.Remove(uc);
300                         uc = new UniqueConstraint(dtParent.Columns[0],true);
301                         dtParent.Constraints.Add(uc);
302
303                         // primary key 2
304                         Assert.AreEqual(1, dtParent.PrimaryKey.Length  , "UC24");
305                 }
306
307                 [Test] public void ctor_NameDataColumn()
308                 {
309                         DataTable dtParent = DataProvider.CreateParentDataTable();
310
311                         UniqueConstraint uc = null;
312                         uc = new UniqueConstraint("myConstraint",dtParent.Columns[0]);
313
314                         // Ctor
315                         Assert.AreEqual(false , uc == null , "UC25");
316
317                         // Ctor name
318                         Assert.AreEqual("myConstraint", uc.ConstraintName , "UC26");
319                 }
320
321                 [Test] public void ctor_NameDataColumnPrimary()
322                 {
323                         DataTable dtParent = DataProvider.CreateParentDataTable();
324
325                         UniqueConstraint uc = null;
326                         uc = new UniqueConstraint("myConstraint",dtParent.Columns[0],false);
327                         dtParent.Constraints.Add(uc);
328
329                         // Ctor
330                         Assert.AreEqual(false , uc == null , "UC27");
331
332                         // primary key 1
333                         Assert.AreEqual(0, dtParent.PrimaryKey.Length  , "UC28");
334
335                         // Ctor name 1
336                         Assert.AreEqual("myConstraint", uc.ConstraintName , "UC29");
337
338                         dtParent.Constraints.Remove(uc);
339                         uc = new UniqueConstraint("myConstraint",dtParent.Columns[0],true);
340                         dtParent.Constraints.Add(uc);
341
342                         // primary key 2
343                         Assert.AreEqual(1, dtParent.PrimaryKey.Length  , "UC30");
344
345                         // Ctor name 2
346                         Assert.AreEqual("myConstraint", uc.ConstraintName , "UC31");
347                 }
348
349                 [Test] public void ctor_NameDataColumns()
350                 {
351                         DataTable dtParent = DataProvider.CreateParentDataTable();
352
353                         UniqueConstraint uc = null;
354                         uc = new UniqueConstraint("myConstraint",new DataColumn[] {dtParent.Columns[0],dtParent.Columns[1]});
355
356                         // Ctor
357                         Assert.AreEqual(false , uc == null , "UC32");
358
359                         // Ctor name
360                         Assert.AreEqual("myConstraint", uc.ConstraintName , "UC33");
361                 }
362
363                 [Test] public void ctor_NameDataColumnsPrimary()
364                 {
365                         DataTable dtParent = DataProvider.CreateParentDataTable();
366
367                         UniqueConstraint uc = null;
368                         uc = new UniqueConstraint("myConstraint",new DataColumn[] {dtParent.Columns[0]},false);
369                         dtParent.Constraints.Add(uc);
370
371                         // Ctor
372                         Assert.AreEqual(false , uc == null , "UC34");
373
374                         // primary key 1
375                         Assert.AreEqual(0, dtParent.PrimaryKey.Length  , "UC35");
376
377                         // Ctor name 1
378                         Assert.AreEqual("myConstraint", uc.ConstraintName , "UC36");
379
380                         dtParent.Constraints.Remove(uc);
381                         uc = new UniqueConstraint("myConstraint",new DataColumn[] {dtParent.Columns[0]},true);
382                         dtParent.Constraints.Add(uc);
383
384                         // primary key 2
385                         Assert.AreEqual(1, dtParent.PrimaryKey.Length  , "UC37");
386
387                         // Ctor name 2
388                         Assert.AreEqual("myConstraint", uc.ConstraintName , "UC38");
389                 }
390
391                 [Test] public void extendedProperties()
392                 {
393                         DataTable dtParent = DataProvider.CreateParentDataTable();
394
395                         UniqueConstraint uc = null;
396                         uc = new UniqueConstraint(dtParent.Columns[0]);
397                         PropertyCollection pc = uc.ExtendedProperties ;
398
399                         // Checking ExtendedProperties default 
400                         Assert.AreEqual(true, pc != null, "UC39");
401
402                         // Checking ExtendedProperties count 
403                         Assert.AreEqual(0, pc.Count , "UC40");
404                 }
405         }
406 }