2 // Rafael Mizrahi <rafim@mainsoft.com>
3 // Erez Lotan <erezl@mainsoft.com>
4 // Oren Gurfinkel <oreng@mainsoft.com>
7 // Copyright (c) 2004 Mainsoft Co.
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using NUnit.Framework;
31 using System.Collections;
32 using System.ComponentModel;
34 using MonoTests.System.Data.Utils;
36 namespace MonoTests.System.Data
38 [TestFixture] public class DataTableCollectionTest2
40 private int counter = 0;
42 [Test] public void Add()
44 // Adding computed column to a data set
45 DataSet ds = new DataSet();
46 ds.Tables.Add(new DataTable("Table"));
47 ds.Tables[0].Columns.Add(new DataColumn("EmployeeNo", typeof(string)));
48 ds.Tables[0].Rows.Add(new object[] {"Maciek"});
49 ds.Tables[0].Columns.Add("ComputedColumn", typeof(object), "EmployeeNo");
51 Assert.AreEqual("EmployeeNo", ds.Tables[0].Columns["ComputedColumn"].Expression, "DTC1");
55 public void AddTwoTables()
57 DataSet ds = new DataSet();
59 Assert.AreEqual("Table1", ds.Tables[0].TableName , "DTC2");
60 //Assert.AreEqual(ds.Tables[0].TableName,"Table1");
62 Assert.AreEqual("Table2", ds.Tables[1].TableName , "DTC3");
63 //Assert.AreEqual(ds.Tables[1].TableName,"Table2");
67 public void AddRange()
69 DataSet ds = new DataSet();
71 DataTable[] arr = new DataTable[2];
73 arr[0] = new DataTable("NewTable1");
74 arr[1] = new DataTable("NewTable2");
76 ds.Tables.AddRange(arr);
77 Assert.AreEqual("NewTable1", ds.Tables[0].TableName, "DTC4");
78 Assert.AreEqual("NewTable2", ds.Tables[1].TableName, "DTC5");
81 public void AddRange_NullValue()
83 DataSet ds = new DataSet();
84 ds.Tables.AddRange(null);
88 public void AddRange_ArrayWithNull()
90 DataSet ds = new DataSet();
91 DataTable[] arr = new DataTable[2];
92 arr[0] = new DataTable("NewTable1");
93 arr[1] = (DataTable)null ;
94 ds.Tables.AddRange(arr);
95 Assert.AreEqual("NewTable1", ds.Tables[0].TableName, "DTC6");
96 Assert.AreEqual(1, ds.Tables.Count, "DTC7");
100 public void CanRemove()
102 DataSet ds = new DataSet();
104 Assert.AreEqual(true, ds.Tables.CanRemove(ds.Tables[0]), "DTC8");
108 public void CanRemove_NullValue()
110 DataSet ds = new DataSet();
111 Assert.AreEqual(false, ds.Tables.CanRemove(null), "DTC9");
115 public void CanRemove_TableDoesntBelong()
117 DataSet ds = new DataSet();
118 DataSet ds1 = new DataSet();
120 Assert.AreEqual(false, ds.Tables.CanRemove(ds1.Tables[0]), "DTC10");
124 public void CanRemove_PartOfRelation()
126 DataSet ds = new DataSet();
127 ds.Tables.Add(DataProvider.CreateParentDataTable());
128 ds.Tables.Add(DataProvider.CreateChildDataTable());
130 ds.Relations.Add("rel",ds.Tables[0].Columns["ParentId"],ds.Tables[1].Columns["ParentId"],false);
132 Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[0]), "DTC11");
133 Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[1]), "DTC12");
136 public void CanRemove_PartOfConstraint()
138 DataSet ds = DataProvider.CreateForigenConstraint();
139 Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[0]), "DTC13"); //Unique
140 Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[1]), "DTC14"); //Foreign
144 public void CollectionChanged()
147 DataSet ds = new DataSet();
148 ds.Tables.CollectionChanged+=new CollectionChangeEventHandler(Tables_CollectionChanged);
151 Assert.AreEqual(2, counter, "DTC15");
153 ds.Tables.Remove(ds.Tables[0]);
154 ds.Tables.Remove(ds.Tables[0]);
155 Assert.AreEqual(4, counter, "DTC16");
158 private void Tables_CollectionChanged(object sender, CollectionChangeEventArgs e)
164 public void CollectionChanging()
167 DataSet ds = new DataSet();
168 ds.Tables.CollectionChanging+=new CollectionChangeEventHandler(Tables_CollectionChanging);
171 Assert.AreEqual(2, counter, "DTC17");
173 ds.Tables.Remove(ds.Tables[0]);
174 ds.Tables.Remove(ds.Tables[0]);
175 Assert.AreEqual(4, counter, "DTC18");
178 private void Tables_CollectionChanging(object sender, CollectionChangeEventArgs e)
184 public void Contains()
186 DataSet ds = new DataSet();
187 ds.Tables.Add("NewTable1");
188 ds.Tables.Add("NewTable2");
190 Assert.AreEqual(true, ds.Tables.Contains("NewTable1"), "DTC19");
191 Assert.AreEqual(true, ds.Tables.Contains("NewTable2"), "DTC20");
192 Assert.AreEqual(false, ds.Tables.Contains("NewTable3"), "DTC21");
194 ds.Tables["NewTable1"].TableName = "Tbl1";
195 Assert.AreEqual(false, ds.Tables.Contains("NewTable1"), "DTC22");
196 Assert.AreEqual(true, ds.Tables.Contains("Tbl1"), "DTC23");
202 DataSet ds = new DataSet();
205 DataTable[] arr = new DataTable[2];
206 ds.Tables.CopyTo(arr,0);
207 Assert.AreEqual("Table1", ((DataTable)arr[0]).TableName, "DTC24");
208 Assert.AreEqual("Table2", ((DataTable)arr[1]).TableName, "DTC25");
214 DataSet ds = new DataSet();
215 Assert.AreEqual(0, ds.Tables.Count, "DTC26");
218 Assert.AreEqual(1, ds.Tables.Count, "DTC27");
221 Assert.AreEqual(2, ds.Tables.Count, "DTC28");
223 ds.Tables.Remove("Table1");
224 Assert.AreEqual(1, ds.Tables.Count, "DTC29");
226 ds.Tables.Remove("Table2");
227 Assert.AreEqual(0, ds.Tables.Count, "DTC30");
231 public void GetEnumerator()
233 DataSet ds = new DataSet();
238 IEnumerator myEnumerator = ds.Tables.GetEnumerator();
240 while (myEnumerator.MoveNext())
242 Assert.AreEqual("Table", ((DataTable) myEnumerator.Current).TableName.Substring(0,5), "DTC31");
245 Assert.AreEqual(2, count, "DTC32");
247 public void IndexOf_ByDataTable()
249 DataSet ds = new DataSet();
250 DataTable dt = new DataTable("NewTable1");
251 DataTable dt1 = new DataTable("NewTable2");
252 ds.Tables.AddRange(new DataTable[] {dt,dt1});
254 Assert.AreEqual(0, ds.Tables.IndexOf(dt), "DTC33");
255 Assert.AreEqual(1, ds.Tables.IndexOf(dt1), "DTC34");
257 ds.Tables.IndexOf((DataTable)null);
259 DataTable dt2 = new DataTable("NewTable2");
261 Assert.AreEqual(-1, ds.Tables.IndexOf(dt2), "DTC35");
264 public void IndexOf_ByName()
266 DataSet ds = new DataSet();
267 DataTable dt = new DataTable("NewTable1");
268 DataTable dt1 = new DataTable("NewTable2");
269 ds.Tables.AddRange(new DataTable[] {dt,dt1});
271 Assert.AreEqual(0, ds.Tables.IndexOf("NewTable1"), "DTC36");
272 Assert.AreEqual(1, ds.Tables.IndexOf("NewTable2"), "DTC37");
274 ds.Tables.IndexOf((string)null);
276 Assert.AreEqual(-1, ds.Tables.IndexOf("NewTable3"), "DTC38");
282 DataSet ds = new DataSet();
283 DataTable dt = new DataTable("NewTable1");
284 DataTable dt1 = new DataTable("NewTable2");
285 ds.Tables.AddRange(new DataTable[] {dt,dt1});
287 Assert.AreEqual(dt, ds.Tables[0], "DTC39");
288 Assert.AreEqual(dt1, ds.Tables[1], "DTC40");
289 Assert.AreEqual(dt, ds.Tables["NewTable1"], "DTC41");
290 Assert.AreEqual(dt1, ds.Tables["NewTable2"], "DTC42");
294 public void DataTableCollection_Add_D1()
296 DataSet ds = new DataSet();
297 DataTable dt = new DataTable("NewTable1");
299 Assert.AreEqual("NewTable1",ds.Tables[0].TableName,"DTC43");
303 [ExpectedException(typeof(ArgumentNullException))]
304 public void DataTableCollection_Add_D2()
306 DataSet ds = new DataSet();
308 ds.Tables.Add((DataTable)null);
312 [ExpectedException(typeof(ArgumentException))]
313 public void DataTableCollection_Add_D3()
315 DataSet ds = new DataSet();
316 DataSet ds1 = new DataSet();
319 ds.Tables.Add(ds1.Tables[0]);
323 [ExpectedException(typeof(DuplicateNameException))]
324 public void DataTableCollection_Add_D4()
326 DataSet ds = new DataSet();
329 DataTable dt = new DataTable("Table1");
334 public void DataTableCollection_Add_S1()
336 DataSet ds = new DataSet();
337 ds.Tables.Add("NewTable1");
338 Assert.AreEqual("NewTable1",ds.Tables[0].TableName,"DTC44");
339 ds.Tables.Add("NewTable2");
340 Assert.AreEqual("NewTable2",ds.Tables[1].TableName,"DTC45");
344 [ExpectedException(typeof(DuplicateNameException))]
345 public void DataTableCollection_Add_S2()
347 DataSet ds = new DataSet();
348 ds.Tables.Add("NewTable1");
350 ds.Tables.Add("NewTable1");
354 public void DataTableCollection_Clear1()
356 DataSet ds = new DataSet();
360 Assert.AreEqual(0,ds.Tables.Count,"DTC46");
365 [ExpectedException(typeof(IndexOutOfRangeException))]
366 public void DataTableCollection_Clear2()
368 DataSet ds = new DataSet();
373 ds.Tables[0].TableName = "Error";
377 public void DataTableCollection_Remove_D1()
379 DataSet ds = new DataSet();
380 DataTable dt = new DataTable("NewTable1");
381 DataTable dt1 = new DataTable("NewTable2");
382 ds.Tables.AddRange(new DataTable[] {dt,dt1});
384 ds.Tables.Remove(dt);
385 Assert.AreEqual(1,ds.Tables.Count,"DTC47");
386 Assert.AreEqual(dt1,ds.Tables[0],"DTC48");
387 ds.Tables.Remove(dt1);
388 Assert.AreEqual(0,ds.Tables.Count,"DTC49");
392 [ExpectedException(typeof(ArgumentException))]
393 public void DataTableCollection_Remove_D2()
395 DataSet ds = new DataSet();
396 DataTable dt = new DataTable("NewTable1");
398 ds.Tables.Remove(dt);
402 [ExpectedException(typeof(ArgumentNullException))]
403 public void DataTableCollection_Remove_D3()
405 DataSet ds = new DataSet();
407 ds.Tables.Remove((DataTable)null);
411 public void DataTableCollection_Remove_S1()
413 DataSet ds = new DataSet();
414 DataTable dt = new DataTable("NewTable1");
415 DataTable dt1 = new DataTable("NewTable2");
416 ds.Tables.AddRange(new DataTable[] {dt,dt1});
418 ds.Tables.Remove("NewTable1");
419 Assert.AreEqual(1,ds.Tables.Count,"DTC50");
420 Assert.AreEqual(dt1,ds.Tables[0],"DTC51");
421 ds.Tables.Remove("NewTable2");
422 Assert.AreEqual(0,ds.Tables.Count,"DTC52");
426 [ExpectedException(typeof(ArgumentException))]
427 public void DataTableCollection_Remove_S2()
429 DataSet ds = new DataSet();
431 ds.Tables.Remove("NewTable2");
435 [ExpectedException(typeof(ArgumentException))]
436 public void DataTableCollection_Remove_S3()
438 DataSet ds = new DataSet();
440 ds.Tables.Remove((string)null);
444 public void DataTableCollection_RemoveAt_I1()
446 DataSet ds = new DataSet();
447 DataTable dt = new DataTable("NewTable1");
448 DataTable dt1 = new DataTable("NewTable2");
449 ds.Tables.AddRange(new DataTable[] {dt,dt1});
451 ds.Tables.RemoveAt(1);
452 Assert.AreEqual(dt,ds.Tables[0],"DTC53");
453 ds.Tables.RemoveAt(0);
454 Assert.AreEqual(0,ds.Tables.Count,"DTC54");
458 [ExpectedException(typeof(IndexOutOfRangeException))]
459 public void DataTableCollection_RemoveAt_I2()
461 DataSet ds = new DataSet();
463 ds.Tables.RemoveAt(-1);
467 [ExpectedException(typeof(ArgumentException))]
468 public void DataTableCollection_RemoveAt_I3()
470 DataSet ds = DataProvider.CreateForigenConstraint();
472 ds.Tables.RemoveAt(0); //Parent table
477 public void AddTable_DiffNamespaceTest ()
479 DataSet ds = new DataSet ();
480 ds.Tables.Add ("table", "namespace1");
481 ds.Tables.Add ("table", "namespace2");
482 Assert.AreEqual (2, ds.Tables.Count, "#1");
485 ds.Tables.Add ("table", "namespace1");
487 } catch (DuplicateNameException e) { }
489 ds.Tables.Add ("table");
491 ds.Tables.Add ("table", null);
493 } catch (DuplicateNameException e) { }
497 public void Contains_DiffNamespaceTest ()
499 DataSet ds = new DataSet ();
500 ds.Tables.Add ("table");
501 Assert.IsTrue (ds.Tables.Contains ("table"), "#1");
503 ds.Tables.Add ("table", "namespace1");
504 ds.Tables.Add ("table", "namespace2");
506 // Should fail if it cannot be resolved to a single table
507 Assert.IsFalse (ds.Tables.Contains ("table"));
510 ds.Tables.Contains ("table", null);
512 } catch (ArgumentNullException e) { }
514 Assert.IsTrue (ds.Tables.Contains ("table", "namespace1"), "#4");
515 Assert.IsFalse (ds.Tables.Contains ("table", "namespace3"), "#5");
519 public void IndexOf_DiffNamespaceTest ()
521 DataSet ds = new DataSet ();
522 ds.Tables.Add ("table");
523 Assert.AreEqual (0, ds.Tables.IndexOf ("table"), "#1");
524 ds.Tables.Add ("table", "namespace1");
525 ds.Tables.Add ("table", "namespace2");
526 Assert.AreEqual (-1, ds.Tables.IndexOf ("table"), "#2");
527 Assert.AreEqual (2, ds.Tables.IndexOf ("table", "namespace2"), "#3");
528 Assert.AreEqual (1, ds.Tables.IndexOf ("table", "namespace1"), "#4");
532 public void Remove_DiffNamespaceTest ()
534 DataSet ds = new DataSet ();
535 ds.Tables.Add ("table");
536 ds.Tables.Add ("table", "namespace1");
537 ds.Tables.Add ("table", "namespace2");
540 ds.Tables.Remove ("table");
542 } catch (ArgumentException e) { }
544 ds.Tables.Remove ("table", "namespace2");
545 Assert.AreEqual (2, ds.Tables.Count, "#3");
546 Assert.AreEqual ("namespace1", ds.Tables [1].Namespace, "#4");
549 ds.Tables.Remove ("table", "namespace2");
551 } catch (ArgumentException e) { }