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 [NUnit.Framework.Category ("NotWorking")]
229 public void FindByKey_DuringDataLoad ()
231 DataTable table = new DataTable ();
232 table.Columns.Add ("col1", typeof (int));
233 table.PrimaryKey = new DataColumn[] {table.Columns [0]};
235 table.Rows.Add (new object[] {1});
236 table.Rows.Add (new object[] {2});
237 table.AcceptChanges ();
239 table.BeginLoadData ();
240 table.LoadDataRow (new object[] {1000}, false);
241 Assert.IsNotNull (table.Rows.Find (1), "#1");
242 Assert.IsNotNull (table.Rows.Find (1000), "#2");
243 table.EndLoadData ();
244 Assert.IsNotNull (table.Rows.Find (1000), "#3");
248 public void DataRowCollection_Clear1()
\r
250 DataTable dt = DataProvider.CreateParentDataTable();
\r
251 int count = dt.Rows.Count;
\r
252 Assert.AreEqual(count !=0,true);
\r
254 Assert.AreEqual(0,dt.Rows.Count,"DRWC15");
\r
258 [ExpectedException(typeof(InvalidConstraintException))]
\r
259 public void DataRowCollection_Clear2()
\r
261 DataSet ds = DataProvider.CreateForigenConstraint();
\r
263 ds.Tables[0].Rows.Clear(); //Try to clear the parent table
\r
267 public void DataRowCollection_Contains_O1()
\r
269 DataTable dt = DataProvider.CreateParentDataTable();
\r
270 dt.PrimaryKey= new DataColumn[] {dt.Columns[0]};
\r
272 Assert.AreEqual(true,dt.Rows.Contains(1),"DRWC16");
\r
273 Assert.AreEqual(false,dt.Rows.Contains(10),"DRWC17");
\r
277 [ExpectedException(typeof(MissingPrimaryKeyException))]
\r
278 public void DataRowCollection_Contains_O2()
\r
280 DataTable dt = DataProvider.CreateParentDataTable();
\r
281 Assert.AreEqual(false,dt.Rows.Contains(1),"DRWC18");
\r
285 public void DataRowCollection_Contains_O3()
\r
287 DataTable dt = DataProvider.CreateParentDataTable();
\r
288 dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
\r
290 //Prepare values array
\r
291 object[] arr = new object[2];
\r
293 arr[1] = "1-String1";
\r
295 Assert.AreEqual(true,dt.Rows.Contains( (object[])arr),"DRWC19");
\r
299 Assert.AreEqual(false,dt.Rows.Contains( (object[])arr),"DRWC20");
\r
304 [ExpectedException(typeof(ArgumentException))]
\r
305 public void DataRowCollection_Contains_O4()
\r
307 DataTable dt = DataProvider.CreateParentDataTable();
\r
308 dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
\r
310 //Prepare values array
\r
311 object[] arr = new object[1];
\r
314 Assert.AreEqual(false,dt.Rows.Contains((object[]) arr),"DRWC21");
\r
318 public void DataRowCollection_Find_O1()
\r
320 DataTable dt = DataProvider.CreateParentDataTable();
\r
321 dt.PrimaryKey= new DataColumn[] {dt.Columns[0]};
\r
323 Assert.AreEqual(dt.Rows[0],dt.Rows.Find(1),"DRWC22");
\r
324 Assert.AreEqual(null,dt.Rows.Find(10),"DRWC23");
\r
328 [ExpectedException(typeof(MissingPrimaryKeyException))]
\r
329 public void DataRowCollection_Find_O2()
\r
331 DataTable dt = DataProvider.CreateParentDataTable();
\r
333 Assert.AreEqual(null,dt.Rows.Find(1),"DRWC24");
\r
337 public void DataRowCollection_Find_O3()
\r
339 DataTable dt = DataProvider.CreateParentDataTable();
\r
340 dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
\r
342 //Prepare values array
\r
343 object[] arr = new object[2];
\r
345 arr[1] = "2-String1";
\r
347 Assert.AreEqual(dt.Rows[1],dt.Rows.Find( (object[])arr),"DRWC25");
\r
351 Assert.AreEqual(null,dt.Rows.Find( (object[])arr),"DRWC26");
\r
356 [ExpectedException(typeof(ArgumentException))]
\r
357 public void DataRowCollection_Find_O4()
\r
359 DataTable dt = DataProvider.CreateParentDataTable();
\r
360 dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
\r
362 //Prepare values array
\r
363 object[] arr = new object[1];
\r
366 Assert.AreEqual(null,dt.Rows.Find((object[]) arr),"DRWC27");
\r
370 public void DataRowCollection_InsertAt_DI1()
\r
372 DataTable dt = DataProvider.CreateParentDataTable();
\r
373 DataRow dr = GetNewDataRow(dt);
\r
374 dt.Rows.InsertAt(dr,0);
\r
376 Assert.AreEqual(dr,dt.Rows[0],"DRWC28"); //Begin
\r
380 public void DataRowCollection_InsertAt_DI2()
\r
382 DataTable dt = DataProvider.CreateParentDataTable();
\r
383 DataRow dr = GetNewDataRow(dt);
\r
384 dt.Rows.InsertAt(dr,3);
\r
386 Assert.AreEqual(dr,dt.Rows[3],"DRWC29"); //Middle
\r
390 public void DataRowCollection_InsertAt_DI3()
\r
392 DataTable dt = DataProvider.CreateParentDataTable();
\r
393 DataRow dr = GetNewDataRow(dt);
\r
394 dt.Rows.InsertAt(dr,300);
\r
396 Assert.AreEqual(dr,dt.Rows[dt.Rows.Count-1],"DRWC30"); //End
\r
400 [ExpectedException(typeof(IndexOutOfRangeException))]
\r
401 public void DataRowCollection_InsertAt_DI4()
\r
403 DataTable dt = DataProvider.CreateParentDataTable();
\r
404 DataRow dr = GetNewDataRow(dt);
\r
406 dt.Rows.InsertAt(dr,-1);
\r
409 private DataRow GetNewDataRow(DataTable dt)
\r
411 DataRow dr = dt.NewRow();
\r
412 dr["ParentId"] = 10;
\r
413 dr["String1"] = "string1";
\r
414 dr["String2"] = string.Empty;
\r
415 dr["ParentDateTime"] = new DateTime(2004,12,15);
\r
416 dr["ParentDouble"] = 3.14;
\r
417 dr["ParentBool"] = false;
\r
422 public void DataRowCollection_Item1()
\r
424 DataTable dt = DataProvider.CreateParentDataTable();
\r
427 foreach (DataRow dr in dt.Rows)
\r
429 Assert.AreEqual(dr,dt.Rows[index],"DRWC31");
\r
435 [ExpectedException(typeof(IndexOutOfRangeException))]
\r
436 public void DataRowCollection_Item2()
\r
438 DataTable dt = DataProvider.CreateParentDataTable();
\r
440 DataRow dr = dt.Rows[-1];
\r