Merge pull request #3091 from alexanderkyte/mobile_static_fix_mcs_tests
[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 #if !MOBILE
37 using NUnit.Framework.SyntaxHelpers;
38 #endif
39
40 namespace MonoTests.System.Data
41 {
42         [TestFixture]
43         public class UniqueConstraintTest
44         {
45                 private DataTable _table;
46
47                 [SetUp]
48                 public void GetReady ()
49                 {
50
51                         //Setup DataTable
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));
56
57                 }  
58
59                 [Test]
60                 public void CtorExceptions ()
61                 {
62                         //UniqueConstraint(string name, DataColumn column, bool isPrimaryKey)
63
64                         UniqueConstraint cst;
65                         
66                         //must have DataTable exception
67                         try {
68                                 //Should throw an ArgumentException
69                                 //Can only add DataColumns that are attached
70                                 //to a DataTable
71                                 cst = new UniqueConstraint (new DataColumn (""));
72
73                                 Assert.Fail ("Failed to throw ArgumentException.");
74                         } catch (Exception e) {
75                                 Assert.That (e, Is.TypeOf (typeof(ArgumentException)), "test#02");
76                         }        
77
78                         //Null exception
79                         try {
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");
84                         }
85                         
86                         try {
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 [] {});
91
92                                 Assert.Fail ("B1: Failed to throw InvalidConstraintException.");
93                         } catch (InvalidConstraintException) {
94                         } catch (AssertionException exc) {
95                                 throw exc;
96                         } catch {
97                                 Assert.Fail ("A3: Wrong Exception type.");
98                         }
99
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));
104
105                         DataSet ds = new DataSet ();
106                         ds.Tables.Add (dt);
107                         ds.Tables.Add (dt2);
108
109                         //columns from two different tables.
110                         try {
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]});
115
116                                 Assert.Fail ("B2: Failed to throw InvalidConstraintException");
117                         } catch (InvalidConstraintException) {
118                         } catch (AssertionException exc) {
119                                 throw exc;
120                         } catch {
121                                 Assert.Fail ("A4: Wrong Exception type.");
122                         }
123                 }
124
125                 [Test]
126                 public void Ctor ()
127                 {
128                         UniqueConstraint cst;
129                 
130                         //Success case
131                         try {
132                                 cst = new UniqueConstraint (_table.Columns [0]);
133                         } catch (Exception exc) {
134                                 Assert.Fail ("A1: Failed to ctor. " + exc.ToString ());
135                         }
136
137                         try {
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 ());
142                         }
143
144                         //table is set on ctor
145                         cst = new UniqueConstraint (_table.Columns [0]);
146                         
147                         Assert.That (cst.Table, Is.SameAs (_table), "B1");
148
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");
153
154                         cst = new UniqueConstraint ("MyName", _table.Columns [0], true);
155
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.");
159                 
160                         _table.Constraints.Add (cst);
161
162                         Assert.That (cst.IsPrimaryKey, Is.True, "IsPrimaryKey not set set.");
163                         
164                         Assert.That (_table.PrimaryKey.Length, Is.EqualTo (1), "PrimaryKey not set.");
165                         Assert.That (_table.PrimaryKey [0].Unique, Is.True, "Not unigue.");
166                 }
167
168                 [Test]
169                 public void Unique ()
170                 {                                                     
171                         UniqueConstraint U = new UniqueConstraint (_table.Columns [0]);
172                         Assert.That (_table.Columns [0].Unique, Is.False, "test#01"); 
173                         
174                         U = new UniqueConstraint (new DataColumn [] {_table.Columns [0],_table.Columns [1]});     
175                         
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");
179                         
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");
184                 }                                                     
185
186                 [Test]
187                 public void EqualsAndHashCode ()
188                 {
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]});
193
194                         UniqueConstraint cst3 = new UniqueConstraint (_table.Columns [0]);
195                         UniqueConstraint cst4 = new UniqueConstraint (_table.Columns [2]);
196                         
197                         //true
198                         Assert.That (cst.Equals (cst2), Is.True, "A0");
199                         
200                         //false
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");
205
206                         //false... but it should be true (FXDG violation)
207                         //Assert.That (cst.GetHashCode (), Is.Not.EqualTo (cst2.GetHashCode ()), "HashEquals");
208
209                         //false
210                         Assert.That (cst.GetHashCode (), Is.Not.EqualTo (cst3.GetHashCode ()), "Hash Not Equals");
211                 }
212
213                 [Test]
214                 public void DBNullAllowed ()
215                 {
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});
222                 }
223         }
224 }