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 namespace MonoTests.System.Data
40 public class UniqueConstraintTest
42 private DataTable _table;
45 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 ()
59 //UniqueConstraint(string name, DataColumn column, bool isPrimaryKey)
63 //must have DataTable exception
65 //Should throw an ArgumentException
66 //Can only add DataColumns that are attached
68 cst = new UniqueConstraint (new DataColumn (""));
70 Assert.Fail ("Failed to throw ArgumentException.");
71 } catch (Exception e) {
72 Assert.That (e, Is.TypeOf (typeof(ArgumentException)), "test#02");
77 //Should throw argument null exception
78 cst = new UniqueConstraint ((DataColumn)null);
79 } catch (Exception e) {
80 Assert.That (e, Is.TypeOf (typeof(NullReferenceException)), "test#05");
84 //Should throw exception
85 //must have at least one valid column
86 //InvalidConstraintException is thrown by msft ver
87 cst = new UniqueConstraint (new DataColumn [] {});
89 Assert.Fail ("B1: Failed to throw InvalidConstraintException.");
90 } catch (InvalidConstraintException) {
91 } catch (AssertionException exc) {
94 Assert.Fail ("A3: Wrong Exception type.");
97 DataTable dt = new DataTable ("Table1");
98 dt.Columns.Add ("Col1", typeof(int));
99 DataTable dt2 = new DataTable ("Table2");
100 dt2.Columns.Add ("Col1", typeof(int));
102 DataSet ds = new DataSet ();
106 //columns from two different tables.
108 //next line should throw
109 //can't have columns from two different tables
110 cst = new UniqueConstraint (new DataColumn [] {
111 dt.Columns[0], dt2.Columns[0]});
113 Assert.Fail ("B2: Failed to throw InvalidConstraintException");
114 } catch (InvalidConstraintException) {
115 } catch (AssertionException exc) {
118 Assert.Fail ("A4: Wrong Exception type.");
125 UniqueConstraint cst;
129 cst = new UniqueConstraint (_table.Columns [0]);
130 } catch (Exception exc) {
131 Assert.Fail ("A1: Failed to ctor. " + exc.ToString ());
135 cst = new UniqueConstraint (new DataColumn [] {
136 _table.Columns[0], _table.Columns[1]});
137 } catch (Exception exc) {
138 Assert.Fail ("A2: Failed to ctor. " + exc.ToString ());
141 //table is set on ctor
142 cst = new UniqueConstraint (_table.Columns [0]);
144 Assert.That (cst.Table, Is.SameAs (_table), "B1");
146 //table is set on ctor
147 cst = new UniqueConstraint (new DataColumn [] {
148 _table.Columns[0], _table.Columns[1]});
149 Assert.That (cst.Table, Is.SameAs (_table), "B2");
151 cst = new UniqueConstraint ("MyName", _table.Columns [0], true);
153 //Test ctor parm set for ConstraintName & IsPrimaryKey
154 Assert.That (cst.ConstraintName, Is.EqualTo ("MyName"), "ConstraintName not set in ctor.");
155 Assert.That (cst.IsPrimaryKey, Is.False, "IsPrimaryKey already set.");
157 _table.Constraints.Add (cst);
159 Assert.That (cst.IsPrimaryKey, Is.True, "IsPrimaryKey not set set.");
161 Assert.That (_table.PrimaryKey.Length, Is.EqualTo (1), "PrimaryKey not set.");
162 Assert.That (_table.PrimaryKey [0].Unique, Is.True, "Not unigue.");
166 public void Unique ()
168 UniqueConstraint U = new UniqueConstraint (_table.Columns [0]);
169 Assert.That (_table.Columns [0].Unique, Is.False, "test#01");
171 U = new UniqueConstraint (new DataColumn [] {_table.Columns [0],_table.Columns [1]});
173 Assert.That (_table.Columns [0].Unique, Is.False, "test#02");
174 Assert.That (_table.Columns [1].Unique, Is.False, "test#03");
175 Assert.That (_table.Columns [2].Unique, Is.False, "test#04");
177 _table.Constraints.Add (U);
178 Assert.That (_table.Columns [0].Unique, Is.False, "test#05");
179 Assert.That (_table.Columns [1].Unique, Is.False, "test#06");
180 Assert.That (_table.Columns [2].Unique, Is.False, "test#07");
184 public void EqualsAndHashCode ()
186 UniqueConstraint cst = new UniqueConstraint (new DataColumn [] {
187 _table.Columns[0], _table.Columns[1]});
188 UniqueConstraint cst2 = new UniqueConstraint (new DataColumn [] {
189 _table.Columns[1], _table.Columns[0]});
191 UniqueConstraint cst3 = new UniqueConstraint (_table.Columns [0]);
192 UniqueConstraint cst4 = new UniqueConstraint (_table.Columns [2]);
195 Assert.That (cst.Equals (cst2), Is.True, "A0");
198 Assert.That (cst.Equals (23), Is.False, "A1");
199 Assert.That (cst.Equals (cst3), Is.False, "A2");
200 Assert.That (cst3.Equals (cst), Is.False, "A3");
201 Assert.That (cst.Equals (cst4), Is.False, "A4");
203 //false... but it should be true (FXDG violation)
204 //Assert.That (cst.GetHashCode (), Is.Not.EqualTo (cst2.GetHashCode ()), "HashEquals");
207 Assert.That (cst.GetHashCode (), Is.Not.EqualTo (cst3.GetHashCode ()), "Hash Not Equals");
211 public void DBNullAllowed ()
213 DataTable dt = new DataTable ("table");
214 dt.Columns.Add ("col1");
215 dt.Columns.Add ("col2");
216 dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
217 dt.Rows.Add (new object [] {1, 3});
218 dt.Rows.Add (new object [] {DBNull.Value, 3});