Standardized Mainsoft ConstraintCollection tests.
[mono.git] / mcs / class / System.Data / Test / System.Data / ConstraintCollectionTest2.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
5 //   Ofer Borstein
6 // 
7 // Copyright (c) 2004 Mainsoft Co.
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30 using MonoTests.System.Data.Test.Utils;
31 using System;
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Data;
35
36 namespace MonoTests.System.Data
37 {
38         [TestFixture] public class ConstraintCollectionTest2
39         {
40                 private bool CollectionChangedFlag = false;
41
42                 [Test] public void CanRemove_ParentForeign()
43                 {
44                         DataSet ds = DataProvider.CreateForigenConstraint();
45                         Assert.AreEqual(false, ds.Tables["parent"].Constraints.CanRemove(ds.Tables["parent"].Constraints[0]), "CN1");
46                 }
47
48                 [Test] public void CanRemove_ChildForeign()
49                 {
50                         DataSet ds = DataProvider.CreateForigenConstraint();
51                         Assert.AreEqual(true, ds.Tables["child"].Constraints.CanRemove(ds.Tables["child"].Constraints[0]), "CN2");
52                 }
53
54                 [Test] public void CanRemove_ParentAndChildForeign()
55                 {
56                         DataSet ds = DataProvider.CreateForigenConstraint();
57                         //remove the forigen and ask about the unique
58                         ds.Tables["child"].Constraints.Remove(ds.Tables["child"].Constraints[0]);
59                         Assert.AreEqual(true, ds.Tables["parent"].Constraints.CanRemove(ds.Tables["parent"].Constraints[0]), "CN3");
60                 }
61
62                 // FIXME. This test isn't complete.
63                 public void CanRemove_Unique()
64                 {
65                         DataTable dt = DataProvider.CreateUniqueConstraint();
66                         //remove the forigen and ask about the unique
67                         dt.Constraints.Remove(dt.Constraints[0]);
68                         Assert.AreEqual(true, dt.Constraints.CanRemove(dt.Constraints[0]), "CN4");
69                 }
70
71                 [Test] public void Clear_Foreign()
72                 {
73                         DataSet ds = DataProvider.CreateForigenConstraint();
74                         foreach(DataTable dt in ds.Tables)
75                         {
76                                 dt.Constraints.Clear();
77                         }
78                         Assert.AreEqual(0, ds.Tables[0].Constraints.Count, "CN5");
79                         Assert.AreEqual(0, ds.Tables[0].Constraints.Count, "CN6");
80
81                 }
82
83                 [Test] public void Clear_Unique()
84                 {
85                         DataTable dt = DataProvider.CreateUniqueConstraint();
86                         int rowsCount = dt.Rows.Count;
87                         dt.Constraints.Clear();
88                         DataRow dr = dt.NewRow();
89                         dr[0] = 1;
90                         dt.Rows.Add(dr);
91                         Assert.AreEqual(rowsCount+1, dt.Rows.Count, "CN7"); //Just checking that no expection ocuured
92                 }
93
94                 [Test] public void CollectionChanged()
95                 {
96                         DataTable dt = DataProvider.CreateParentDataTable();
97                         CollectionChangedFlag = false;
98                         dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);     
99                         dt = DataProvider.CreateUniqueConstraint(dt);
100                         Assert.AreEqual(true, CollectionChangedFlag, "CN8"); 
101                 }
102
103                 [Test] public void Contains_ByName()
104                 {
105                         DataSet ds =  DataProvider.CreateForigenConstraint();
106          
107                         //changing the constraints's name
108
109                         ds.Tables["child"].Constraints[0].ConstraintName = "name1";
110                         ds.Tables["parent"].Constraints[0].ConstraintName = "name2";
111
112
113                         Assert.AreEqual(true, ds.Tables["child"].Constraints.Contains("name1"), "CN9");
114                         Assert.AreEqual(false, ds.Tables["child"].Constraints.Contains("xxx"), "CN10");
115                         Assert.AreEqual(true, ds.Tables["parent"].Constraints.Contains("name2"), "CN11");
116                         Assert.AreEqual(false, ds.Tables["parent"].Constraints.Contains("xxx"), "CN12");
117
118                 }
119
120                 [Test] public void CopyTo()
121                 {
122                         DataTable dt = DataProvider.CreateUniqueConstraint();
123                         dt.Constraints.Add("constraint2",dt.Columns["String1"],true);
124
125                         object[] ar = new object[2];
126
127                         dt.Constraints.CopyTo(ar,0);
128                         Assert.AreEqual(2, ar.Length, "CN13");
129                 }
130
131                 [Test] public void Count()
132                 {
133                         DataTable dt = DataProvider.CreateUniqueConstraint();
134                         Assert.AreEqual(1, dt.Constraints.Count, "CN14");
135
136                         //Add
137
138                         dt.Constraints.Add("constraint2",dt.Columns["String1"],false);
139                         Assert.AreEqual(2, dt.Constraints.Count, "CN15");
140
141                         //Remove
142
143                         dt.Constraints.Remove("constraint2");
144                         Assert.AreEqual(1, dt.Constraints.Count, "CN16");
145                 }
146
147                 [Test] public void GetEnumerator()
148                 {
149                         DataTable dt = DataProvider.CreateUniqueConstraint();
150                         dt.Constraints.Add("constraint2",dt.Columns["String1"],false);
151
152                         int counter=0;
153                         IEnumerator myEnumerator = dt.Constraints.GetEnumerator();
154                         while (myEnumerator.MoveNext())
155                         {
156                                 counter++;
157
158                         }
159                         Assert.AreEqual(2, counter, "CN17");
160                 }
161
162                 [Test] public void IndexOf()
163                 {
164                         DataTable dt = DataProvider.CreateUniqueConstraint();
165                         Assert.AreEqual(0, dt.Constraints.IndexOf(dt.Constraints[0]), "CN18");
166
167                         //Add new constraint
168                         Constraint con = new UniqueConstraint(dt.Columns["String1"],false);
169
170                         dt.Constraints.Add(con);
171                         Assert.AreEqual(1, dt.Constraints.IndexOf(con), "CN19");
172
173                         //Remove it and try to look for it 
174
175                         dt.Constraints.Remove(con);
176                         Assert.AreEqual(-1, dt.Constraints.IndexOf(con), "CN20");
177
178                 }
179
180                 [Test] public void IndexOf_ByName()
181                 {
182                         DataTable dt = DataProvider.CreateUniqueConstraint();
183                         dt.Constraints[0].ConstraintName="name1";
184                         Assert.AreEqual(0, dt.Constraints.IndexOf("name1"), "CN21");
185
186                         //Add new constraint
187                         Constraint con = new UniqueConstraint(dt.Columns["String1"],false);
188                         con.ConstraintName="name2";
189
190                         dt.Constraints.Add(con);
191                         Assert.AreEqual(1, dt.Constraints.IndexOf("name2"), "CN22");
192
193                         //Remove it and try to look for it 
194
195                         dt.Constraints.Remove(con);
196                         Assert.AreEqual(-1, dt.Constraints.IndexOf("name2"), "CN23");
197
198                 }
199
200                 [Test] public void IsReadOnly()
201                 {
202                         DataTable dt = DataProvider.CreateUniqueConstraint();
203                         Assert.AreEqual(false, dt.Constraints.IsReadOnly, "CN24"); 
204                 }
205
206                 [Test] public void IsSynchronized()
207                 {
208                         DataTable dt = DataProvider.CreateUniqueConstraint();
209                         Assert.AreEqual(false, dt.Constraints.IsSynchronized, "CN25");
210          
211                         ConstraintCollection col = (ConstraintCollection)dt.Constraints.SyncRoot;
212
213         //              lock(dt.Constraints.SyncRoot)
214         //              {
215                         //      Assert.AreEqual(true, col.IsSynchronized, "CN26");
216
217                         //}
218                 }
219
220                 [Test] public void Remove()
221                 {
222                         DataTable dt = DataProvider.CreateUniqueConstraint();
223                         dt.Constraints.Remove(dt.Constraints[0]);
224                         Assert.AreEqual(0, dt.Constraints.Count, "CN27");
225                 }
226
227                 [Test] public void Remove_ByNameSimple()
228                 {
229                         DataTable dt = DataProvider.CreateUniqueConstraint();
230                         dt.Constraints[0].ConstraintName = "constraint1";
231                         dt.Constraints.Remove("constraint1");
232                         Assert.AreEqual(0, dt.Constraints.Count, "CN28");
233                 }
234
235                 [Test] public void Remove_ByNameWithAdd()
236                 {
237                         DataTable dt = DataProvider.CreateUniqueConstraint();
238                         dt.Constraints[0].ConstraintName = "constraint1";
239                         Constraint con = new UniqueConstraint(dt.Columns["String1"],false);
240                         dt.Constraints.Add(con);
241                         dt.Constraints.Remove(con);
242
243                         Assert.AreEqual(1, dt.Constraints.Count, "CN29");
244                         Assert.AreEqual("constraint1", dt.Constraints[0].ConstraintName, "CN30");
245                 }
246
247                 [Test] public void Remove_CollectionChangedEvent()
248                 {
249                         DataTable dt = DataProvider.CreateUniqueConstraint();
250                         CollectionChangedFlag = false;
251                         dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);
252                         dt.Constraints.Remove(dt.Constraints[0]);
253                         Assert.AreEqual(true, CollectionChangedFlag, "CN31"); //Checking that event has raised
254                 }
255
256                 [Test] public void Remove_ByNameCollectionChangedEvent()
257                 {
258                         DataTable dt = DataProvider.CreateUniqueConstraint();
259                         CollectionChangedFlag = false;
260                         dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);
261                         dt.Constraints.Remove("constraint1");
262                         Assert.AreEqual(true, CollectionChangedFlag, "CN32"); //Checking that event has raised
263
264                 }
265
266                 [Test] public void add_CollectionChanged()
267                 {
268                         DataTable dt = DataProvider.CreateParentDataTable();
269                         CollectionChangedFlag = false;
270                         dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);     
271                         dt = DataProvider.CreateUniqueConstraint(dt);
272                         Assert.AreEqual(true, CollectionChangedFlag, "CN33"); 
273                 }
274
275                 private void Constraints_CollectionChangedHandler(object sender, CollectionChangeEventArgs e)
276                 {
277                         CollectionChangedFlag = true;
278                 }
279         }
280 }