Merge pull request #3913 from omwok/master
[mono.git] / mcs / class / System.Data / Test / System.Data / UniqueConstraintTest.cs
1 // UniqueConstraintTest.cs - NUnit Test Cases for testing the class System.Data.UniqueConstraint
2 //
3 // Authors:
4 //   Franklin Wise <gracenote@earthlink.net>
5 //   Martin Willemoes Hansen <mwh@sysrq.dk>
6 //
7 // (C) 2002 Franklin Wise
8 // (C) 2003 Martin Willemoes Hansen
9 // 
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33 using NUnit.Framework;
34 using System;
35 using System.Data;
36
37 namespace MonoTests.System.Data
38 {
39         [TestFixture]
40         public class UniqueConstraintTest
41         {
42                 private DataTable _table;
43
44                 [SetUp]
45                 public void GetReady ()
46                 {
47
48                         //Setup DataTable
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));
53
54                 }  
55
56                 [Test]
57                 public void CtorExceptions ()
58                 {
59                         //UniqueConstraint(string name, DataColumn column, bool isPrimaryKey)
60
61                         UniqueConstraint cst;
62                         
63                         //must have DataTable exception
64                         try {
65                                 //Should throw an ArgumentException
66                                 //Can only add DataColumns that are attached
67                                 //to a DataTable
68                                 cst = new UniqueConstraint (new DataColumn (""));
69
70                                 Assert.Fail ("Failed to throw ArgumentException.");
71                         } catch (Exception e) {
72                                 Assert.That (e, Is.TypeOf (typeof(ArgumentException)), "test#02");
73                         }        
74
75                         //Null exception
76                         try {
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");
81                         }
82                         
83                         try {
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 [] {});
88
89                                 Assert.Fail ("B1: Failed to throw InvalidConstraintException.");
90                         } catch (InvalidConstraintException) {
91                         } catch (AssertionException exc) {
92                                 throw exc;
93                         } catch {
94                                 Assert.Fail ("A3: Wrong Exception type.");
95                         }
96
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));
101
102                         DataSet ds = new DataSet ();
103                         ds.Tables.Add (dt);
104                         ds.Tables.Add (dt2);
105
106                         //columns from two different tables.
107                         try {
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]});
112
113                                 Assert.Fail ("B2: Failed to throw InvalidConstraintException");
114                         } catch (InvalidConstraintException) {
115                         } catch (AssertionException exc) {
116                                 throw exc;
117                         } catch {
118                                 Assert.Fail ("A4: Wrong Exception type.");
119                         }
120                 }
121
122                 [Test]
123                 public void Ctor ()
124                 {
125                         UniqueConstraint cst;
126                 
127                         //Success case
128                         try {
129                                 cst = new UniqueConstraint (_table.Columns [0]);
130                         } catch (Exception exc) {
131                                 Assert.Fail ("A1: Failed to ctor. " + exc.ToString ());
132                         }
133
134                         try {
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 ());
139                         }
140
141                         //table is set on ctor
142                         cst = new UniqueConstraint (_table.Columns [0]);
143                         
144                         Assert.That (cst.Table, Is.SameAs (_table), "B1");
145
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");
150
151                         cst = new UniqueConstraint ("MyName", _table.Columns [0], true);
152
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.");
156                 
157                         _table.Constraints.Add (cst);
158
159                         Assert.That (cst.IsPrimaryKey, Is.True, "IsPrimaryKey not set set.");
160                         
161                         Assert.That (_table.PrimaryKey.Length, Is.EqualTo (1), "PrimaryKey not set.");
162                         Assert.That (_table.PrimaryKey [0].Unique, Is.True, "Not unigue.");
163                 }
164
165                 [Test]
166                 public void Unique ()
167                 {                                                     
168                         UniqueConstraint U = new UniqueConstraint (_table.Columns [0]);
169                         Assert.That (_table.Columns [0].Unique, Is.False, "test#01"); 
170                         
171                         U = new UniqueConstraint (new DataColumn [] {_table.Columns [0],_table.Columns [1]});     
172                         
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");
176                         
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");
181                 }                                                     
182
183                 [Test]
184                 public void EqualsAndHashCode ()
185                 {
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]});
190
191                         UniqueConstraint cst3 = new UniqueConstraint (_table.Columns [0]);
192                         UniqueConstraint cst4 = new UniqueConstraint (_table.Columns [2]);
193                         
194                         //true
195                         Assert.That (cst.Equals (cst2), Is.True, "A0");
196                         
197                         //false
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");
202
203                         //false... but it should be true (FXDG violation)
204                         //Assert.That (cst.GetHashCode (), Is.Not.EqualTo (cst2.GetHashCode ()), "HashEquals");
205
206                         //false
207                         Assert.That (cst.GetHashCode (), Is.Not.EqualTo (cst3.GetHashCode ()), "Hash Not Equals");
208                 }
209
210                 [Test]
211                 public void DBNullAllowed ()
212                 {
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});
219                 }
220         }
221 }