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.
33 using NUnit.Framework;
37 using NUnit.Framework.SyntaxHelpers;
40 namespace MonoTests.System.Data
43 public class UniqueConstraintTest
45 private DataTable _table;
48 public void GetReady ()
52 _table = new DataTable ("TestTable");
53 _table.Columns.Add ("Col1", typeof(int));
54 _table.Columns.Add ("Col2", typeof(int));
55 _table.Columns.Add ("Col3", typeof(int));
60 public void CtorExceptions ()
62 //UniqueConstraint(string name, DataColumn column, bool isPrimaryKey)
66 //must have DataTable exception
68 //Should throw an ArgumentException
69 //Can only add DataColumns that are attached
71 cst = new UniqueConstraint (new DataColumn (""));
73 Assert.Fail ("Failed to throw ArgumentException.");
74 } catch (Exception e) {
75 Assert.That (e, Is.TypeOf (typeof(ArgumentException)), "test#02");
80 //Should throw argument null exception
81 cst = new UniqueConstraint ((DataColumn)null);
82 } catch (Exception e) {
83 Assert.That (e, Is.TypeOf (typeof(NullReferenceException)), "test#05");
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 Assert.Fail ("B1: Failed to throw InvalidConstraintException.");
93 } catch (InvalidConstraintException) {
94 } catch (AssertionException exc) {
97 Assert.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 Assert.Fail ("B2: Failed to throw InvalidConstraintException");
117 } catch (InvalidConstraintException) {
118 } catch (AssertionException exc) {
121 Assert.Fail ("A4: Wrong Exception type.");
128 UniqueConstraint cst;
132 cst = new UniqueConstraint (_table.Columns [0]);
133 } catch (Exception exc) {
134 Assert.Fail ("A1: Failed to ctor. " + exc.ToString ());
138 cst = new UniqueConstraint (new DataColumn [] {
139 _table.Columns[0], _table.Columns[1]});
140 } catch (Exception exc) {
141 Assert.Fail ("A2: Failed to ctor. " + exc.ToString ());
144 //table is set on ctor
145 cst = new UniqueConstraint (_table.Columns [0]);
147 Assert.That (cst.Table, Is.SameAs (_table), "B1");
149 //table is set on ctor
150 cst = new UniqueConstraint (new DataColumn [] {
151 _table.Columns[0], _table.Columns[1]});
152 Assert.That (cst.Table, Is.SameAs (_table), "B2");
154 cst = new UniqueConstraint ("MyName", _table.Columns [0], true);
156 //Test ctor parm set for ConstraintName & IsPrimaryKey
157 Assert.That (cst.ConstraintName, Is.EqualTo ("MyName"), "ConstraintName not set in ctor.");
158 Assert.That (cst.IsPrimaryKey, Is.False, "IsPrimaryKey already set.");
160 _table.Constraints.Add (cst);
162 Assert.That (cst.IsPrimaryKey, Is.True, "IsPrimaryKey not set set.");
164 Assert.That (_table.PrimaryKey.Length, Is.EqualTo (1), "PrimaryKey not set.");
165 Assert.That (_table.PrimaryKey [0].Unique, Is.True, "Not unigue.");
169 public void Unique ()
171 UniqueConstraint U = new UniqueConstraint (_table.Columns [0]);
172 Assert.That (_table.Columns [0].Unique, Is.False, "test#01");
174 U = new UniqueConstraint (new DataColumn [] {_table.Columns [0],_table.Columns [1]});
176 Assert.That (_table.Columns [0].Unique, Is.False, "test#02");
177 Assert.That (_table.Columns [1].Unique, Is.False, "test#03");
178 Assert.That (_table.Columns [2].Unique, Is.False, "test#04");
180 _table.Constraints.Add (U);
181 Assert.That (_table.Columns [0].Unique, Is.False, "test#05");
182 Assert.That (_table.Columns [1].Unique, Is.False, "test#06");
183 Assert.That (_table.Columns [2].Unique, Is.False, "test#07");
187 public void EqualsAndHashCode ()
189 UniqueConstraint cst = new UniqueConstraint (new DataColumn [] {
190 _table.Columns[0], _table.Columns[1]});
191 UniqueConstraint cst2 = new UniqueConstraint (new DataColumn [] {
192 _table.Columns[1], _table.Columns[0]});
194 UniqueConstraint cst3 = new UniqueConstraint (_table.Columns [0]);
195 UniqueConstraint cst4 = new UniqueConstraint (_table.Columns [2]);
198 Assert.That (cst.Equals (cst2), Is.True, "A0");
201 Assert.That (cst.Equals (23), Is.False, "A1");
202 Assert.That (cst.Equals (cst3), Is.False, "A2");
203 Assert.That (cst3.Equals (cst), Is.False, "A3");
204 Assert.That (cst.Equals (cst4), Is.False, "A4");
206 //false... but it should be true (FXDG violation)
207 //Assert.That (cst.GetHashCode (), Is.Not.EqualTo (cst2.GetHashCode ()), "HashEquals");
210 Assert.That (cst.GetHashCode (), Is.Not.EqualTo (cst3.GetHashCode ()), "Hash Not Equals");
214 public void DBNullAllowed ()
216 DataTable dt = new DataTable ("table");
217 dt.Columns.Add ("col1");
218 dt.Columns.Add ("col2");
219 dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
220 dt.Rows.Add (new object [] {1, 3});
221 dt.Rows.Add (new object [] {DBNull.Value, 3});