1 // UniqueConstraintTest.cs - NUnit Test Cases for testing the class System.Data.UniqueConstraint
4 // Franklin Wise <gracenote@earthlink.net>
5 // Martin Willemoes Hansen <mwh@sysrq.dk>
7 // (C) 2002 Franklin Wise
8 // (C) 2003 Martin Willemoes Hansen
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using NUnit
.Framework
;
38 namespace MonoTests
.System
.Data
41 public class UniqueConstraintTest
: Assertion
43 private DataTable _table
;
46 public void GetReady() {
49 _table
= new DataTable("TestTable");
50 _table
.Columns
.Add("Col1",typeof(int));
51 _table
.Columns
.Add("Col2",typeof(int));
52 _table
.Columns
.Add("Col3",typeof(int));
57 public void CtorExceptions() {
58 //UniqueConstraint(string name, DataColumn column, bool isPrimaryKey)
62 //must have DataTable exception
64 //Should throw an ArgumentException
65 //Can only add DataColumns that are attached
67 cst
= new UniqueConstraint(new DataColumn(""));
69 Fail("Failed to throw ArgumentException.");
72 AssertEquals ("test#02", typeof (ArgumentException
), e
.GetType ());
73 AssertEquals ("test#03", "Column must belong to a table.", e
.Message
);
78 //Should throw argument null exception
79 cst
= new UniqueConstraint((DataColumn
)null);
82 AssertEquals ("test#05", typeof (NullReferenceException
), e
.GetType ());
83 AssertEquals ("test#06", "Object reference not set to an instance of an object.", e
.Message
);
87 //Should throw exception
88 //must have at least one valid column
89 //InvalidConstraintException is thrown by msft ver
90 cst
= new UniqueConstraint(new DataColumn
[] {});
92 Fail("B1: Failed to throw InvalidConstraintException.");
94 catch (InvalidConstraintException
) {}
95 catch (AssertionException exc
) {throw exc;}
97 Fail("A3: Wrong Exception type.");
100 DataTable dt
= new DataTable("Table1");
101 dt
.Columns
.Add("Col1",typeof(int));
102 DataTable dt2
= new DataTable("Table2");
103 dt2
.Columns
.Add("Col1",typeof(int));
105 DataSet ds
= new DataSet();
109 //columns from two different tables.
111 //next line should throw
112 //can't have columns from two different tables
113 cst
= new UniqueConstraint(new DataColumn
[] {
114 dt
.Columns
[0], dt2
.Columns
[0]});
116 Fail("B2: Failed to throw InvalidConstraintException");
118 catch (InvalidConstraintException
) {}
119 catch (AssertionException exc
) {throw exc;}
121 Fail("A4: Wrong Exception type.");
131 UniqueConstraint cst
;
135 cst
= new UniqueConstraint(_table
.Columns
[0]);
137 catch (Exception exc
) {
138 Fail("A1: Failed to ctor. " + exc
.ToString());
143 cst
= new UniqueConstraint( new DataColumn
[] {
144 _table
.Columns
[0], _table
.Columns
[1]});
146 catch (Exception exc
) {
147 Fail("A2: Failed to ctor. " + exc
.ToString());
151 //table is set on ctor
152 cst
= new UniqueConstraint(_table
.Columns
[0]);
154 AssertSame("B1", cst
.Table
, _table
);
156 //table is set on ctor
157 cst
= new UniqueConstraint( new DataColumn
[] {
158 _table
.Columns
[0], _table
.Columns
[1]});
159 AssertSame ("B2", cst
.Table
, _table
);
161 cst
= new UniqueConstraint("MyName",_table
.Columns
[0],true);
163 //Test ctor parm set for ConstraintName & IsPrimaryKey
164 AssertEquals("ConstraintName not set in ctor.",
165 "MyName", cst
.ConstraintName
);
166 AssertEquals("IsPrimaryKey already set.",
167 false, cst
.IsPrimaryKey
);
169 _table
.Constraints
.Add (cst
);
171 AssertEquals("IsPrimaryKey not set set.",
172 true, cst
.IsPrimaryKey
);
174 AssertEquals("PrimaryKey not set.", 1, _table
.PrimaryKey
.Length
);
175 AssertEquals("Not unigue.", true, _table
.PrimaryKey
[0].Unique
);
180 public void Unique ()
182 UniqueConstraint U
= new UniqueConstraint (_table
.Columns
[0]);
183 AssertEquals ("test#01", false, _table
.Columns
[0].Unique
);
185 U
= new UniqueConstraint (new DataColumn
[] {_table.Columns [0],_table.Columns [1]}
);
187 AssertEquals ("test#02", false, _table
.Columns
[0].Unique
);
188 AssertEquals ("test#03", false, _table
.Columns
[1].Unique
);
189 AssertEquals ("test#04", false, _table
.Columns
[2].Unique
);
191 _table
.Constraints
.Add (U
);
192 AssertEquals ("test#05", false, _table
.Columns
[0].Unique
);
193 AssertEquals ("test#06", false, _table
.Columns
[1].Unique
);
194 AssertEquals ("test#07", false, _table
.Columns
[2].Unique
);
198 public void EqualsAndHashCode() {
199 UniqueConstraint cst
= new UniqueConstraint( new DataColumn
[] {
200 _table
.Columns
[0], _table
.Columns
[1]});
201 UniqueConstraint cst2
= new UniqueConstraint( new DataColumn
[] {
202 _table
.Columns
[1], _table
.Columns
[0]});
204 UniqueConstraint cst3
= new UniqueConstraint(_table
.Columns
[0]);
205 UniqueConstraint cst4
= new UniqueConstraint(_table
.Columns
[2]);
208 Assert(cst
.Equals(cst2
) == true);
211 Assert("A1", cst
.Equals(23) == false);
212 Assert("A2", cst
.Equals(cst3
) == false);
213 Assert("A3", cst3
.Equals(cst
) == false);
214 Assert("A4", cst
.Equals(cst4
) == false);
217 Assert("HashEquals", cst
.GetHashCode() == cst2
.GetHashCode());
220 Assert("Hash Not Equals", (cst
.GetHashCode() == cst3
.GetHashCode()) == false);