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;
32 using MonoTests.System.Data.Utils;
34 namespace MonoTests_System.Data
36 [TestFixture] public class DataTableCollectionTest2
38 private int counter = 0;
40 [Test] public void Add()
42 // Adding computed column to a data set
43 DataSet ds = new DataSet();
44 ds.Tables.Add(new DataTable("Table"));
45 ds.Tables[0].Columns.Add(new DataColumn("EmployeeNo", typeof(string)));
46 ds.Tables[0].Rows.Add(new object[] {"Maciek"});
47 ds.Tables[0].Columns.Add("ComputedColumn", typeof(object), "EmployeeNo");
49 Assert.AreEqual("EmployeeNo", ds.Tables[0].Columns["ComputedColumn"].Expression, "DTC1");
53 public void AddTwoTables()
55 DataSet ds = new DataSet();
57 Assert.AreEqual("Table1", ds.Tables[0].TableName , "DTC2");
58 //Assert.AreEqual(ds.Tables[0].TableName,"Table1");
60 Assert.AreEqual("Table2", ds.Tables[1].TableName , "DTC3");
61 //Assert.AreEqual(ds.Tables[1].TableName,"Table2");
65 public void AddRange()
67 DataSet ds = new DataSet();
69 DataTable[] arr = new DataTable[2];
71 arr[0] = new DataTable("NewTable1");
72 arr[1] = new DataTable("NewTable2");
74 ds.Tables.AddRange(arr);
75 Assert.AreEqual("NewTable1", ds.Tables[0].TableName, "DTC4");
76 Assert.AreEqual("NewTable2", ds.Tables[1].TableName, "DTC5");
79 public void AddRange_NullValue()
81 DataSet ds = new DataSet();
82 ds.Tables.AddRange(null);
86 public void AddRange_ArrayWithNull()
88 DataSet ds = new DataSet();
89 DataTable[] arr = new DataTable[2];
90 arr[0] = new DataTable("NewTable1");
91 arr[1] = (DataTable)null ;
92 ds.Tables.AddRange(arr);
93 Assert.AreEqual("NewTable1", ds.Tables[0].TableName, "DTC6");
94 Assert.AreEqual(1, ds.Tables.Count, "DTC7");
98 public void CanRemove()
100 DataSet ds = new DataSet();
102 Assert.AreEqual(true, ds.Tables.CanRemove(ds.Tables[0]), "DTC8");
106 public void CanRemove_NullValue()
108 DataSet ds = new DataSet();
109 Assert.AreEqual(false, ds.Tables.CanRemove(null), "DTC9");
113 public void CanRemove_TableDoesntBelong()
115 DataSet ds = new DataSet();
116 DataSet ds1 = new DataSet();
118 Assert.AreEqual(false, ds.Tables.CanRemove(ds1.Tables[0]), "DTC10");
122 public void CanRemove_PartOfRelation()
124 DataSet ds = new DataSet();
125 ds.Tables.Add(DataProvider.CreateParentDataTable());
126 ds.Tables.Add(DataProvider.CreateChildDataTable());
128 ds.Relations.Add("rel",ds.Tables[0].Columns["ParentId"],ds.Tables[1].Columns["ParentId"],false);
130 Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[0]), "DTC11");
131 Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[1]), "DTC12");
134 public void CanRemove_PartOfConstraint()
136 DataSet ds = DataProvider.CreateForigenConstraint();
137 Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[0]), "DTC13"); //Unique
138 Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[1]), "DTC14"); //Foreign
142 public void CollectionChanged()
145 DataSet ds = new DataSet();
146 ds.Tables.CollectionChanged+=new System.ComponentModel.CollectionChangeEventHandler(Tables_CollectionChanged);
149 Assert.AreEqual(2, counter, "DTC15");
151 ds.Tables.Remove(ds.Tables[0]);
152 ds.Tables.Remove(ds.Tables[0]);
153 Assert.AreEqual(4, counter, "DTC16");
156 private void Tables_CollectionChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e)
162 public void CollectionChanging()
165 DataSet ds = new DataSet();
166 ds.Tables.CollectionChanging+=new System.ComponentModel.CollectionChangeEventHandler(Tables_CollectionChanging);
169 Assert.AreEqual(2, counter, "DTC17");
171 ds.Tables.Remove(ds.Tables[0]);
172 ds.Tables.Remove(ds.Tables[0]);
173 Assert.AreEqual(4, counter, "DTC18");
176 private void Tables_CollectionChanging(object sender, System.ComponentModel.CollectionChangeEventArgs e)
182 public void Contains()
184 DataSet ds = new DataSet();
185 ds.Tables.Add("NewTable1");
186 ds.Tables.Add("NewTable2");
188 Assert.AreEqual(true, ds.Tables.Contains("NewTable1"), "DTC19");
189 Assert.AreEqual(true, ds.Tables.Contains("NewTable2"), "DTC20");
190 Assert.AreEqual(false, ds.Tables.Contains("NewTable3"), "DTC21");
192 ds.Tables["NewTable1"].TableName = "Tbl1";
193 Assert.AreEqual(false, ds.Tables.Contains("NewTable1"), "DTC22");
194 Assert.AreEqual(true, ds.Tables.Contains("Tbl1"), "DTC23");
200 DataSet ds = new DataSet();
203 DataTable[] arr = new DataTable[2];
204 ds.Tables.CopyTo(arr,0);
205 Assert.AreEqual("Table1", ((DataTable)arr[0]).TableName, "DTC24");
206 Assert.AreEqual("Table2", ((DataTable)arr[1]).TableName, "DTC25");
212 DataSet ds = new DataSet();
213 Assert.AreEqual(0, ds.Tables.Count, "DTC26");
216 Assert.AreEqual(1, ds.Tables.Count, "DTC27");
219 Assert.AreEqual(2, ds.Tables.Count, "DTC28");
221 ds.Tables.Remove("Table1");
222 Assert.AreEqual(1, ds.Tables.Count, "DTC29");
224 ds.Tables.Remove("Table2");
225 Assert.AreEqual(0, ds.Tables.Count, "DTC30");
229 public void GetEnumerator()
231 DataSet ds = new DataSet();
236 System.Collections.IEnumerator myEnumerator = ds.Tables.GetEnumerator();
238 while (myEnumerator.MoveNext())
240 Assert.AreEqual("Table", ((DataTable) myEnumerator.Current).TableName.Substring(0,5), "DTC31");
243 Assert.AreEqual(2, count, "DTC32");
245 public void IndexOf_ByDataTable()
247 DataSet ds = new DataSet();
248 DataTable dt = new DataTable("NewTable1");
249 DataTable dt1 = new DataTable("NewTable2");
250 ds.Tables.AddRange(new DataTable[] {dt,dt1});
252 Assert.AreEqual(0, ds.Tables.IndexOf(dt), "DTC33");
253 Assert.AreEqual(1, ds.Tables.IndexOf(dt1), "DTC34");
255 ds.Tables.IndexOf((DataTable)null);
257 DataTable dt2 = new DataTable("NewTable2");
259 Assert.AreEqual(-1, ds.Tables.IndexOf(dt2), "DTC35");
262 public void IndexOf_ByName()
264 DataSet ds = new DataSet();
265 DataTable dt = new DataTable("NewTable1");
266 DataTable dt1 = new DataTable("NewTable2");
267 ds.Tables.AddRange(new DataTable[] {dt,dt1});
269 Assert.AreEqual(0, ds.Tables.IndexOf("NewTable1"), "DTC36");
270 Assert.AreEqual(1, ds.Tables.IndexOf("NewTable2"), "DTC37");
272 ds.Tables.IndexOf((string)null);
274 Assert.AreEqual(-1, ds.Tables.IndexOf("NewTable3"), "DTC38");
280 DataSet ds = new DataSet();
281 DataTable dt = new DataTable("NewTable1");
282 DataTable dt1 = new DataTable("NewTable2");
283 ds.Tables.AddRange(new DataTable[] {dt,dt1});
285 Assert.AreEqual(dt, ds.Tables[0], "DTC39");
286 Assert.AreEqual(dt1, ds.Tables[1], "DTC40");
287 Assert.AreEqual(dt, ds.Tables["NewTable1"], "DTC41");
288 Assert.AreEqual(dt1, ds.Tables["NewTable2"], "DTC42");
292 public void DataTableCollection_Add_D1()
294 DataSet ds = new DataSet();
295 DataTable dt = new DataTable("NewTable1");
297 Assert.AreEqual("NewTable1",ds.Tables[0].TableName,"DTC43");
301 [ExpectedException(typeof(ArgumentNullException))]
302 public void DataTableCollection_Add_D2()
304 DataSet ds = new DataSet();
306 ds.Tables.Add((DataTable)null);
310 [ExpectedException(typeof(ArgumentException))]
311 public void DataTableCollection_Add_D3()
313 DataSet ds = new DataSet();
314 DataSet ds1 = new DataSet();
317 ds.Tables.Add(ds1.Tables[0]);
321 [ExpectedException(typeof(DuplicateNameException))]
322 public void DataTableCollection_Add_D4()
324 DataSet ds = new DataSet();
327 DataTable dt = new DataTable("Table1");
332 public void DataTableCollection_Add_S1()
334 DataSet ds = new DataSet();
335 ds.Tables.Add("NewTable1");
336 Assert.AreEqual("NewTable1",ds.Tables[0].TableName,"DTC44");
337 ds.Tables.Add("NewTable2");
338 Assert.AreEqual("NewTable2",ds.Tables[1].TableName,"DTC45");
342 [ExpectedException(typeof(DuplicateNameException))]
343 public void DataTableCollection_Add_S2()
345 DataSet ds = new DataSet();
346 ds.Tables.Add("NewTable1");
348 ds.Tables.Add("NewTable1");
352 public void DataTableCollection_Clear1()
354 DataSet ds = new DataSet();
358 Assert.AreEqual(0,ds.Tables.Count,"DTC46");
363 [ExpectedException(typeof(IndexOutOfRangeException))]
364 public void DataTableCollection_Clear2()
366 DataSet ds = new DataSet();
371 ds.Tables[0].TableName = "Error";
375 public void DataTableCollection_Remove_D1()
377 DataSet ds = new DataSet();
378 DataTable dt = new DataTable("NewTable1");
379 DataTable dt1 = new DataTable("NewTable2");
380 ds.Tables.AddRange(new DataTable[] {dt,dt1});
382 ds.Tables.Remove(dt);
383 Assert.AreEqual(1,ds.Tables.Count,"DTC47");
384 Assert.AreEqual(dt1,ds.Tables[0],"DTC48");
385 ds.Tables.Remove(dt1);
386 Assert.AreEqual(0,ds.Tables.Count,"DTC49");
390 [ExpectedException(typeof(ArgumentException))]
391 public void DataTableCollection_Remove_D2()
393 DataSet ds = new DataSet();
394 DataTable dt = new DataTable("NewTable1");
396 ds.Tables.Remove(dt);
400 [ExpectedException(typeof(ArgumentNullException))]
401 public void DataTableCollection_Remove_D3()
403 DataSet ds = new DataSet();
405 ds.Tables.Remove((DataTable)null);
409 public void DataTableCollection_Remove_S1()
411 DataSet ds = new DataSet();
412 DataTable dt = new DataTable("NewTable1");
413 DataTable dt1 = new DataTable("NewTable2");
414 ds.Tables.AddRange(new DataTable[] {dt,dt1});
416 ds.Tables.Remove("NewTable1");
417 Assert.AreEqual(1,ds.Tables.Count,"DTC50");
418 Assert.AreEqual(dt1,ds.Tables[0],"DTC51");
419 ds.Tables.Remove("NewTable2");
420 Assert.AreEqual(0,ds.Tables.Count,"DTC52");
424 [ExpectedException(typeof(ArgumentException))]
425 public void DataTableCollection_Remove_S2()
427 DataSet ds = new DataSet();
429 ds.Tables.Remove("NewTable2");
433 [ExpectedException(typeof(ArgumentException))]
434 public void DataTableCollection_Remove_S3()
436 DataSet ds = new DataSet();
438 ds.Tables.Remove((string)null);
442 public void DataTableCollection_RemoveAt_I1()
444 DataSet ds = new DataSet();
445 DataTable dt = new DataTable("NewTable1");
446 DataTable dt1 = new DataTable("NewTable2");
447 ds.Tables.AddRange(new DataTable[] {dt,dt1});
449 ds.Tables.RemoveAt(1);
450 Assert.AreEqual(dt,ds.Tables[0],"DTC53");
451 ds.Tables.RemoveAt(0);
452 Assert.AreEqual(0,ds.Tables.Count,"DTC54");
456 [ExpectedException(typeof(IndexOutOfRangeException))]
457 public void DataTableCollection_RemoveAt_I2()
459 DataSet ds = new DataSet();
461 ds.Tables.RemoveAt(-1);
465 [ExpectedException(typeof(ArgumentException))]
466 public void DataTableCollection_RemoveAt_I3()
468 DataSet ds = DataProvider.CreateForigenConstraint();
470 ds.Tables.RemoveAt(0); //Parent table
475 public void AddTable_DiffNamespaceTest ()
477 DataSet ds = new DataSet ();
478 ds.Tables.Add ("table", "namespace1");
479 ds.Tables.Add ("table", "namespace2");
480 Assert.AreEqual (2, ds.Tables.Count, "#1");
483 ds.Tables.Add ("table", "namespace1");
485 } catch (DuplicateNameException e) { }
487 ds.Tables.Add ("table");
489 ds.Tables.Add ("table", null);
491 } catch (DuplicateNameException e) { }
495 public void Contains_DiffNamespaceTest ()
497 DataSet ds = new DataSet ();
498 ds.Tables.Add ("table");
499 Assert.IsTrue (ds.Tables.Contains ("table"), "#1");
501 ds.Tables.Add ("table", "namespace1");
502 ds.Tables.Add ("table", "namespace2");
504 // Should fail if it cannot be resolved to a single table
505 Assert.IsFalse (ds.Tables.Contains ("table"));
508 ds.Tables.Contains ("table", null);
510 } catch (ArgumentNullException e) { }
512 Assert.IsTrue (ds.Tables.Contains ("table", "namespace1"), "#4");
513 Assert.IsFalse (ds.Tables.Contains ("table", "namespace3"), "#5");
517 public void IndexOf_DiffNamespaceTest ()
519 DataSet ds = new DataSet ();
520 ds.Tables.Add ("table");
521 Assert.AreEqual (0, ds.Tables.IndexOf ("table"), "#1");
522 ds.Tables.Add ("table", "namespace1");
523 ds.Tables.Add ("table", "namespace2");
524 Assert.AreEqual (-1, ds.Tables.IndexOf ("table"), "#2");
525 Assert.AreEqual (2, ds.Tables.IndexOf ("table", "namespace2"), "#3");
526 Assert.AreEqual (1, ds.Tables.IndexOf ("table", "namespace1"), "#4");
530 public void Remove_DiffNamespaceTest ()
532 DataSet ds = new DataSet ();
533 ds.Tables.Add ("table");
534 ds.Tables.Add ("table", "namespace1");
535 ds.Tables.Add ("table", "namespace2");
538 ds.Tables.Remove ("table");
540 } catch (ArgumentException e) { }
542 ds.Tables.Remove ("table", "namespace2");
543 Assert.AreEqual (2, ds.Tables.Count, "#3");
544 Assert.AreEqual ("namespace1", ds.Tables [1].Namespace, "#4");
547 ds.Tables.Remove ("table", "namespace2");
549 } catch (ArgumentException e) { }