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 System.Collections;
33 using System.ComponentModel;
35 using MonoTests.System.Data.Utils;
37 namespace MonoTests.System.Data
39 [TestFixture] public class DataRowCollectionTest2
41 [Test] public void CopyTo()
43 DataTable dt = DataProvider.CreateParentDataTable();
44 DataRow[] arr = new DataRow[dt.Rows.Count];
45 dt.Rows.CopyTo(arr,0);
46 Assert.AreEqual(dt.Rows.Count, arr.Length, "DRWC1");
49 foreach (DataRow dr in dt.Rows)
51 Assert.AreEqual(dr, arr[index], "DRWC2");
56 [Test] public void Count()
58 DataTable dt = DataProvider.CreateParentDataTable();
59 Assert.AreEqual(6, dt.Rows.Count, "DRWC3");
60 dt.Rows.Remove(dt.Rows[0]);
61 Assert.AreEqual(5, dt.Rows.Count, "DRWC4");
62 dt.Rows.Add(new object[] {1,"1-String1","1-String2",new DateTime(2005,1,1,0,0,0,0),1.534,true});
63 Assert.AreEqual(6, dt.Rows.Count, "DRWC5");
66 [Test] public void GetEnumerator()
68 DataTable dt = DataProvider.CreateParentDataTable();
69 IEnumerator myEnumerator = dt.Rows.GetEnumerator();
71 while (myEnumerator.MoveNext())
73 Assert.AreEqual(dt.Rows[index], (DataRow)myEnumerator.Current, "DRWC6");
76 Assert.AreEqual(index, dt.Rows.Count, "DRWC7");
79 [Test] public void RemoveAt_ByIndex()
81 DataTable dt = DataProvider.CreateParentDataTable();
82 int counter = dt.Rows.Count;
83 dt.PrimaryKey= new DataColumn[] {dt.Columns[0]};
85 Assert.AreEqual(counter-1, dt.Rows.Count, "DRWC8");
86 Assert.AreEqual(null, dt.Rows.Find(4), "DRWC9");
89 [Test] public void Remove_ByDataRow()
91 DataTable dt = DataProvider.CreateParentDataTable();
92 int counter = dt.Rows.Count;
93 dt.PrimaryKey= new DataColumn[] {dt.Columns[0]};
94 Assert.AreEqual(dt.Rows[0], dt.Rows.Find(1), "DRWC10");
95 dt.Rows.Remove(dt.Rows[0]);
96 Assert.AreEqual(counter-1, dt.Rows.Count, "DRWC11");
97 Assert.AreEqual(null, dt.Rows.Find(1), "DRWC12");
101 public void DataRowCollection_Add_D1()
103 DataTable dt = DataProvider.CreateParentDataTable();
105 DataRow dr = dt.NewRow();
107 dr["String1"] = "string1";
108 dr["String2"] = string.Empty;
109 dr["ParentDateTime"] = new DateTime(2004,12,15);
110 dr["ParentDouble"] = 3.14;
111 dr["ParentBool"] = false;
115 Assert.AreEqual(1,dt.Rows.Count,"RDWC13");
116 Assert.AreEqual(dr,dt.Rows[0],"DRWC14");
120 [ExpectedException(typeof(ArgumentException))]
121 public void DataRowCollection_Add_D2()
123 DataTable dt = DataProvider.CreateParentDataTable();
124 dt.Rows.Add(dt.Rows[0]);
128 [ExpectedException(typeof(ArgumentNullException))]
129 public void DataRowCollection_Add_D3()
131 DataTable dt = DataProvider.CreateParentDataTable();
132 dt.Rows.Add((DataRow)null);
136 [ExpectedException(typeof(ArgumentException))]
137 public void DataRowCollection_Add_D4()
139 DataTable dt = DataProvider.CreateParentDataTable();
140 DataTable dt1 = DataProvider.CreateParentDataTable();
142 dt.Rows.Add(dt1.Rows[0]);
146 public void DataRowCollection_Add_O1()
148 DataTable dt = DataProvider.CreateParentDataTable();
150 dt.Rows.Add(new object[] {1,"1-String1","1-String2",new DateTime(2005,1,1,0,0,0,0),1.534,true});
151 Assert.AreEqual(1,dt.Rows.Count,"DRWC15");
152 Assert.AreEqual(1,dt.Rows[0]["ParentId"],"DRWC16");
153 Assert.AreEqual("1-String1",dt.Rows[0]["String1"],"DRWC17");
154 Assert.AreEqual("1-String2",dt.Rows[0]["String2"],"DRWC18");
155 Assert.AreEqual(new DateTime(2005,1,1,0,0,0,0),dt.Rows[0]["ParentDateTime"],"DRWC19");
156 Assert.AreEqual(1.534,dt.Rows[0]["ParentDouble"],"DRWC20");
157 Assert.AreEqual(true,dt.Rows[0]["ParentBool"],"DRWC21");
162 public void DataRowCollection_Add_O2()
164 DataTable dt = DataProvider.CreateParentDataTable();
165 int count = dt.Rows.Count;
166 dt.Rows.Add(new object[] {8,"1-String1","1-String2",new DateTime(2005,1,1,0,0,0,0),1.534});
167 Assert.AreEqual(count+1,dt.Rows.Count,"DRWC14");
171 // [ExpectedException(typeof(ArgumentException))]
172 // public void DataRowCollection_Add_O3()
174 // DataTable dt = DataProvider.CreateParentDataTable();
175 // dt.Rows.Add(new object[] {8,"1-String1","1-String2",new DateTime(2005,1,1,0,0,0,0),1.534});
179 [ExpectedException(typeof(NullReferenceException))]
180 public void DataRowCollection_Add_O4()
182 DataTable dt = DataProvider.CreateParentDataTable();
183 dt.Rows.Add((Object[])null);
187 public void FindByKey ()
189 DataTable table = new DataTable ();
190 table.Columns.Add ("col1", typeof (int));
191 table.PrimaryKey = new DataColumn[] {table.Columns [0]};
193 table.Rows.Add (new object[] {1});
194 table.Rows.Add (new object[] {2});
195 table.Rows.Add (new object[] {3});
196 table.AcceptChanges ();
198 Assert.IsNotNull (table.Rows.Find (new object[] {1}), "#1");
200 table.Rows[0].Delete ();
201 Assert.IsNull (table.Rows.Find (new object[] {1}), "#2");
203 table.RejectChanges ();
204 Assert.IsNotNull (table.Rows.Find (new object[] {1}), "#3");
208 public void FindByKey_VerifyOrder ()
210 DataTable table = new DataTable ();
211 table.Columns.Add ("col1", typeof (int));
212 table.PrimaryKey = new DataColumn[] {table.Columns [0]};
214 table.Rows.Add (new object[] {1});
215 table.Rows.Add (new object[] {2});
216 table.Rows.Add (new object[] {1000});
217 table.AcceptChanges ();
219 table.Rows [1][0] = 100;
220 Assert.IsNotNull (table.Rows.Find (100), "#1");
222 table.Rows [2][0] = 999;
223 Assert.IsNotNull (table.Rows.Find (999), "#2");
224 Assert.IsNotNull (table.Rows.Find (100), "#3");
228 public void FindByKey_DuringDataLoad ()
230 DataTable table = new DataTable ();
231 table.Columns.Add ("col1", typeof (int));
232 table.PrimaryKey = new DataColumn[] {table.Columns [0]};
234 table.Rows.Add (new object[] {1});
235 table.Rows.Add (new object[] {2});
236 table.AcceptChanges ();
238 table.BeginLoadData ();
239 table.LoadDataRow (new object[] {1000}, false);
240 Assert.IsNotNull (table.Rows.Find (1), "#1");
241 Assert.IsNotNull (table.Rows.Find (1000), "#2");
242 table.EndLoadData ();
243 Assert.IsNotNull (table.Rows.Find (1000), "#3");
247 public void DataRowCollection_Clear1()
249 DataTable dt = DataProvider.CreateParentDataTable();
250 int count = dt.Rows.Count;
251 Assert.AreEqual(count !=0,true);
253 Assert.AreEqual(0,dt.Rows.Count,"DRWC15");
257 [ExpectedException(typeof(InvalidConstraintException))]
258 public void DataRowCollection_Clear2()
260 DataSet ds = DataProvider.CreateForigenConstraint();
262 ds.Tables[0].Rows.Clear(); //Try to clear the parent table
266 public void DataRowCollection_Contains_O1()
268 DataTable dt = DataProvider.CreateParentDataTable();
269 dt.PrimaryKey= new DataColumn[] {dt.Columns[0]};
271 Assert.AreEqual(true,dt.Rows.Contains(1),"DRWC16");
272 Assert.AreEqual(false,dt.Rows.Contains(10),"DRWC17");
276 [ExpectedException(typeof(MissingPrimaryKeyException))]
277 public void DataRowCollection_Contains_O2()
279 DataTable dt = DataProvider.CreateParentDataTable();
280 Assert.AreEqual(false,dt.Rows.Contains(1),"DRWC18");
284 public void DataRowCollection_Contains_O3()
286 DataTable dt = DataProvider.CreateParentDataTable();
287 dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
289 //Prepare values array
290 object[] arr = new object[2];
292 arr[1] = "1-String1";
294 Assert.AreEqual(true,dt.Rows.Contains( (object[])arr),"DRWC19");
298 Assert.AreEqual(false,dt.Rows.Contains( (object[])arr),"DRWC20");
303 [ExpectedException(typeof(ArgumentException))]
304 public void DataRowCollection_Contains_O4()
306 DataTable dt = DataProvider.CreateParentDataTable();
307 dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
309 //Prepare values array
310 object[] arr = new object[1];
313 Assert.AreEqual(false,dt.Rows.Contains((object[]) arr),"DRWC21");
317 public void DataRowCollection_Find_O1()
319 DataTable dt = DataProvider.CreateParentDataTable();
320 dt.PrimaryKey= new DataColumn[] {dt.Columns[0]};
322 Assert.AreEqual(dt.Rows[0],dt.Rows.Find(1),"DRWC22");
323 Assert.AreEqual(null,dt.Rows.Find(10),"DRWC23");
327 [ExpectedException(typeof(MissingPrimaryKeyException))]
328 public void DataRowCollection_Find_O2()
330 DataTable dt = DataProvider.CreateParentDataTable();
332 Assert.AreEqual(null,dt.Rows.Find(1),"DRWC24");
336 public void DataRowCollection_Find_O3()
338 DataTable dt = DataProvider.CreateParentDataTable();
339 dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
341 //Prepare values array
342 object[] arr = new object[2];
344 arr[1] = "2-String1";
346 Assert.AreEqual(dt.Rows[1],dt.Rows.Find( (object[])arr),"DRWC25");
350 Assert.AreEqual(null,dt.Rows.Find( (object[])arr),"DRWC26");
355 [ExpectedException(typeof(ArgumentException))]
356 public void DataRowCollection_Find_O4()
358 DataTable dt = DataProvider.CreateParentDataTable();
359 dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
361 //Prepare values array
362 object[] arr = new object[1];
365 Assert.AreEqual(null,dt.Rows.Find((object[]) arr),"DRWC27");
369 public void DataRowCollection_InsertAt_DI1()
371 DataTable dt = DataProvider.CreateParentDataTable();
372 DataRow dr = GetNewDataRow(dt);
373 dt.Rows.InsertAt(dr,0);
375 Assert.AreEqual(dr,dt.Rows[0],"DRWC28"); //Begin
379 public void DataRowCollection_InsertAt_DI2()
381 DataTable dt = DataProvider.CreateParentDataTable();
382 DataRow dr = GetNewDataRow(dt);
383 dt.Rows.InsertAt(dr,3);
385 Assert.AreEqual(dr,dt.Rows[3],"DRWC29"); //Middle
389 public void DataRowCollection_InsertAt_DI3()
391 DataTable dt = DataProvider.CreateParentDataTable();
392 DataRow dr = GetNewDataRow(dt);
393 dt.Rows.InsertAt(dr,300);
395 Assert.AreEqual(dr,dt.Rows[dt.Rows.Count-1],"DRWC30"); //End
399 [ExpectedException(typeof(IndexOutOfRangeException))]
400 public void DataRowCollection_InsertAt_DI4()
402 DataTable dt = DataProvider.CreateParentDataTable();
403 DataRow dr = GetNewDataRow(dt);
405 dt.Rows.InsertAt(dr,-1);
408 private DataRow GetNewDataRow(DataTable dt)
410 DataRow dr = dt.NewRow();
412 dr["String1"] = "string1";
413 dr["String2"] = string.Empty;
414 dr["ParentDateTime"] = new DateTime(2004,12,15);
415 dr["ParentDouble"] = 3.14;
416 dr["ParentBool"] = false;
421 public void DataRowCollection_Item1()
423 DataTable dt = DataProvider.CreateParentDataTable();
426 foreach (DataRow dr in dt.Rows)
428 Assert.AreEqual(dr,dt.Rows[index],"DRWC31");
434 [ExpectedException(typeof(IndexOutOfRangeException))]
435 public void DataRowCollection_Item2()
437 DataTable dt = DataProvider.CreateParentDataTable();
439 DataRow dr = dt.Rows[-1];