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;
32 using MonoTests.System.Data.Utils;
34 namespace MonoTests_System.Data
36 [TestFixture] public class UniqueConstraintTest2
38 [Test] public void Columns()
40 DataTable dtParent = DataProvider.CreateParentDataTable();
42 UniqueConstraint uc = null;
43 uc = new UniqueConstraint(dtParent.Columns[0]);
46 Assert.AreEqual(1, uc.Columns.Length , "UC1");
49 Assert.AreEqual(dtParent.Columns[0], uc.Columns[0], "UC2");
52 [Test] public void Equals_O()
54 DataSet ds = new DataSet();
55 DataTable dtParent = DataProvider.CreateParentDataTable();
56 ds.Tables.Add(dtParent);
58 UniqueConstraint uc1,uc2;
59 uc1 = new UniqueConstraint(dtParent.Columns[0]);
61 uc2 = new UniqueConstraint(dtParent.Columns[1]);
63 Assert.AreEqual(false, uc1.Equals(uc2), "UC3");
65 //Two System.Data.ForeignKeyConstraint are equal if they constrain the same columns.
67 uc2 = new UniqueConstraint(dtParent.Columns[0]);
68 Assert.AreEqual(true, uc1.Equals(uc2), "UC4");
71 [Test] public void IsPrimaryKey()
73 DataTable dtParent = DataProvider.CreateParentDataTable();
75 UniqueConstraint uc = null;
76 uc = new UniqueConstraint(dtParent.Columns[0],false);
77 dtParent.Constraints.Add(uc);
80 Assert.AreEqual(false, uc.IsPrimaryKey , "UC5");
82 dtParent.Constraints.Remove(uc);
83 uc = new UniqueConstraint(dtParent.Columns[0],true);
84 dtParent.Constraints.Add(uc);
87 Assert.AreEqual(true, uc.IsPrimaryKey , "UC6");
90 [Test] public void Table()
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]);
99 Assert.AreEqual(dtParent , uc.Table , "UC7");
102 [Test] public new void ToString()
104 DataTable dtParent = DataProvider.CreateParentDataTable();
106 UniqueConstraint uc = null;
107 uc = new UniqueConstraint(dtParent.Columns[0],false);
109 // ToString - default
110 Assert.AreEqual(string.Empty , uc.ToString(), "UC8");
112 uc = new UniqueConstraint("myConstraint",dtParent.Columns[0],false);
113 // Tostring - Constraint name
114 Assert.AreEqual("myConstraint", uc.ToString(), "UC9");
117 [Test] public void constraintName()
119 DataTable dtParent = DataProvider.CreateParentDataTable();
121 UniqueConstraint uc = null;
122 uc = new UniqueConstraint(dtParent.Columns[0]);
125 Assert.AreEqual(string.Empty , uc.ConstraintName , "UC10");
127 uc.ConstraintName = "myConstraint";
130 Assert.AreEqual("myConstraint" , uc.ConstraintName , "UC11");
133 [Test] public void ctor_DataColumn()
135 Exception tmpEx = new Exception();
137 DataSet ds = new DataSet();
138 DataTable dtParent = DataProvider.CreateParentDataTable();
139 ds.Tables.Add(dtParent);
140 ds.EnforceConstraints = true;
142 UniqueConstraint uc = null;
144 // DataColumn.Unique - without constraint
145 Assert.AreEqual(false, dtParent.Columns[0].Unique , "UC12");
147 uc = new UniqueConstraint(dtParent.Columns[0]);
150 Assert.AreEqual(false , uc == null , "UC13");
152 // DataColumn.Unique - with constraint
153 Assert.AreEqual(false, dtParent.Columns[0].Unique , "UC14");
155 // Ctor - add exisiting column
156 dtParent.Rows.Add(new object[] {99,"str1","str2"});
157 dtParent.Constraints.Add(uc);
160 dtParent.Rows.Add(new object[] {99,"str1","str2"});
161 Assert.Fail("DS333: Rows.Add Failed to throw ConstraintException");
163 catch (ConstraintException) {}
164 catch (AssertionException exc) {throw exc;}
165 catch (Exception exc)
167 Assert.Fail("DS334: Rows.Add. Wrong exception type. Got:" + exc);
170 DataTable dtChild = DataProvider.CreateChildDataTable();
171 uc = new UniqueConstraint(dtChild.Columns[1]);
173 //Column[1] is not unique, will throw exception
177 dtChild.Constraints.Add(uc);
178 Assert.Fail("DS333: Constraints.Add Failed to throw ArgumentException");
180 catch (ArgumentException) {}
181 catch (AssertionException exc) {throw exc;}
182 catch (Exception exc)
184 Assert.Fail("DS334: Constraints.Add. Wrong exception type. Got:" + exc);
188 dtParent = DataProvider.CreateParentDataTable();
190 // DataColumn.Unique = true, will add UniqueConstraint
191 dtParent.Columns[0].Unique = true;
192 Assert.AreEqual(1, dtParent.Constraints.Count , "UC15");
194 // Check the created UniqueConstraint
195 dtParent.Columns[0].Unique = true;
196 Assert.AreEqual(typeof(UniqueConstraint).FullName, dtParent.Constraints[0].GetType().FullName , "UC16");
198 // add UniqueConstarint that don't belong to the table
201 dtParent.Constraints.Add(uc);
202 Assert.Fail("DS333: Constraints.Add Failed to throw ArgumentException");
204 catch (ArgumentException) {}
205 catch (AssertionException exc) {throw exc;}
206 catch (Exception exc)
208 Assert.Fail("DS334: Constraints.Add. Wrong exception type. Got:" + exc);
212 [Test] public void ctor_DataColumnNoPrimary()
214 DataTable dtParent = DataProvider.CreateParentDataTable();
216 UniqueConstraint uc = null;
217 uc = new UniqueConstraint(dtParent.Columns[0],false);
218 dtParent.Constraints.Add(uc);
221 Assert.AreEqual(false , uc == null , "UC17");
224 Assert.AreEqual(0, dtParent.PrimaryKey.Length , "UC18");
226 dtParent.Constraints.Remove(uc);
227 uc = new UniqueConstraint(dtParent.Columns[0],true);
228 dtParent.Constraints.Add(uc);
231 Assert.AreEqual(1, dtParent.PrimaryKey.Length , "UC19");
234 [Test] public void ctor_DataColumns()
236 Exception tmpEx = new Exception();
237 DataTable dtParent = DataProvider.CreateParentDataTable();
239 UniqueConstraint uc = null;
240 uc = new UniqueConstraint(new DataColumn[] {dtParent.Columns[0],dtParent.Columns[1]});
243 Assert.AreEqual(false , uc == null , "UC20");
245 // Ctor - add exisiting column
246 dtParent.Rows.Add(new object[] {99,"str1","str2"});
247 dtParent.Constraints.Add(uc);
250 dtParent.Rows.Add(new object[] {99,"str1","str2"});
251 Assert.Fail("DS333: Rows.Add Failed to throw ConstraintException");
253 catch (ConstraintException) {}
254 catch (AssertionException exc) {throw exc;}
255 catch (Exception exc)
257 Assert.Fail("DS334: Rows.Add. Wrong exception type. Got:" + exc);
260 DataTable dtChild = DataProvider.CreateChildDataTable();
261 uc = new UniqueConstraint(new DataColumn[] {dtChild.Columns[0],dtChild.Columns[1]});
262 dtChild.Constraints.Add(uc);
265 Assert.AreEqual(false , uc == null , "UC21");
267 dtChild.Constraints.Clear();
268 uc = new UniqueConstraint(new DataColumn[] {dtChild.Columns[1],dtChild.Columns[2]});
270 //target columnn are not unnique, will throw an exception
271 // ArgumentException - child
274 dtChild.Constraints.Add(uc);
275 Assert.Fail("DS333: Constraints.Add Failed to throw ArgumentException");
277 catch (ArgumentException) {}
278 catch (AssertionException exc) {throw exc;}
279 catch (Exception exc)
281 Assert.Fail("DS334: Constraints.Add. Wrong exception type. Got:" + exc);
285 [Test] public void ctor_DataColumnPrimary()
287 DataTable dtParent = DataProvider.CreateParentDataTable();
289 UniqueConstraint uc = null;
290 uc = new UniqueConstraint(dtParent.Columns[0],false);
291 dtParent.Constraints.Add(uc);
294 Assert.AreEqual(false , uc == null , "UC22");
297 Assert.AreEqual(0, dtParent.PrimaryKey.Length , "UC23");
299 dtParent.Constraints.Remove(uc);
300 uc = new UniqueConstraint(dtParent.Columns[0],true);
301 dtParent.Constraints.Add(uc);
304 Assert.AreEqual(1, dtParent.PrimaryKey.Length , "UC24");
307 [Test] public void ctor_NameDataColumn()
309 DataTable dtParent = DataProvider.CreateParentDataTable();
311 UniqueConstraint uc = null;
312 uc = new UniqueConstraint("myConstraint",dtParent.Columns[0]);
315 Assert.AreEqual(false , uc == null , "UC25");
318 Assert.AreEqual("myConstraint", uc.ConstraintName , "UC26");
321 [Test] public void ctor_NameDataColumnPrimary()
323 DataTable dtParent = DataProvider.CreateParentDataTable();
325 UniqueConstraint uc = null;
326 uc = new UniqueConstraint("myConstraint",dtParent.Columns[0],false);
327 dtParent.Constraints.Add(uc);
330 Assert.AreEqual(false , uc == null , "UC27");
333 Assert.AreEqual(0, dtParent.PrimaryKey.Length , "UC28");
336 Assert.AreEqual("myConstraint", uc.ConstraintName , "UC29");
338 dtParent.Constraints.Remove(uc);
339 uc = new UniqueConstraint("myConstraint",dtParent.Columns[0],true);
340 dtParent.Constraints.Add(uc);
343 Assert.AreEqual(1, dtParent.PrimaryKey.Length , "UC30");
346 Assert.AreEqual("myConstraint", uc.ConstraintName , "UC31");
349 [Test] public void ctor_NameDataColumns()
351 DataTable dtParent = DataProvider.CreateParentDataTable();
353 UniqueConstraint uc = null;
354 uc = new UniqueConstraint("myConstraint",new DataColumn[] {dtParent.Columns[0],dtParent.Columns[1]});
357 Assert.AreEqual(false , uc == null , "UC32");
360 Assert.AreEqual("myConstraint", uc.ConstraintName , "UC33");
363 [Test] public void ctor_NameDataColumnsPrimary()
365 DataTable dtParent = DataProvider.CreateParentDataTable();
367 UniqueConstraint uc = null;
368 uc = new UniqueConstraint("myConstraint",new DataColumn[] {dtParent.Columns[0]},false);
369 dtParent.Constraints.Add(uc);
372 Assert.AreEqual(false , uc == null , "UC34");
375 Assert.AreEqual(0, dtParent.PrimaryKey.Length , "UC35");
378 Assert.AreEqual("myConstraint", uc.ConstraintName , "UC36");
380 dtParent.Constraints.Remove(uc);
381 uc = new UniqueConstraint("myConstraint",new DataColumn[] {dtParent.Columns[0]},true);
382 dtParent.Constraints.Add(uc);
385 Assert.AreEqual(1, dtParent.PrimaryKey.Length , "UC37");
388 Assert.AreEqual("myConstraint", uc.ConstraintName , "UC38");
391 [Test] public void extendedProperties()
393 DataTable dtParent = DataProvider.CreateParentDataTable();
395 UniqueConstraint uc = null;
396 uc = new UniqueConstraint(dtParent.Columns[0]);
397 PropertyCollection pc = uc.ExtendedProperties ;
399 // Checking ExtendedProperties default
400 Assert.AreEqual(true, pc != null, "UC39");
402 // Checking ExtendedProperties count
403 Assert.AreEqual(0, pc.Count , "UC40");