Merge pull request #3091 from alexanderkyte/mobile_static_fix_mcs_tests
[mono.git] / mcs / class / System.Data / Test / System.Data / DataRowCollectionTest2.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 System;
31 using System.IO;
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Data;
35 using MonoTests.System.Data.Utils;
36
37 namespace MonoTests.System.Data
38 {
39         [TestFixture] public class DataRowCollectionTest2
40         {
41                 [Test] public void CopyTo()
42                 {
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");
47
48                         int index=0;
49                         foreach (DataRow dr in dt.Rows)
50                         {
51                                 Assert.AreEqual(dr, arr[index], "DRWC2");
52                                 index++;
53                         }
54                 }
55
56                 [Test] public void Count()
57                 {
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");
64                 }
65
66                 [Test] public void GetEnumerator()
67                 {
68                         DataTable dt = DataProvider.CreateParentDataTable();
69                         IEnumerator myEnumerator = dt.Rows.GetEnumerator();
70                         int index=0;
71                         while (myEnumerator.MoveNext())
72                         {
73                                 Assert.AreEqual(dt.Rows[index], (DataRow)myEnumerator.Current, "DRWC6");
74                                 index++;
75                         }
76                         Assert.AreEqual(index, dt.Rows.Count, "DRWC7");
77                 }
78
79                 [Test] public void RemoveAt_ByIndex()
80                 {
81                         DataTable dt = DataProvider.CreateParentDataTable();
82                         int counter = dt.Rows.Count;
83                         dt.PrimaryKey=  new DataColumn[] {dt.Columns[0]};
84                         dt.Rows.RemoveAt(3);
85                         Assert.AreEqual(counter-1, dt.Rows.Count, "DRWC8");
86                         Assert.AreEqual(null, dt.Rows.Find(4), "DRWC9");
87                 }
88
89                 [Test] public void Remove_ByDataRow()
90                 {
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");
98                 }
99
100                 [Test]
101                 public void DataRowCollection_Add_D1()
102                 {
103                         DataTable dt = DataProvider.CreateParentDataTable();
104                         dt.Rows.Clear();
105                         DataRow dr = dt.NewRow();
106                         dr["ParentId"] = 10;
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;
112
113                         dt.Rows.Add(dr);
114
115                         Assert.AreEqual(1,dt.Rows.Count,"RDWC13");
116                         Assert.AreEqual(dr,dt.Rows[0],"DRWC14");
117                 }
118
119                 [Test]
120                 [ExpectedException(typeof(ArgumentException))]
121                 public void DataRowCollection_Add_D2()
122                 {
123                         DataTable dt = DataProvider.CreateParentDataTable();
124                         dt.Rows.Add(dt.Rows[0]);                        
125                 }
126
127                 [Test]
128                 [ExpectedException(typeof(ArgumentNullException))]
129                 public void DataRowCollection_Add_D3()
130                 {
131                         DataTable dt = DataProvider.CreateParentDataTable();
132                         dt.Rows.Add((DataRow)null);
133                 }
134
135                 [Test]
136                 [ExpectedException(typeof(ArgumentException))]
137                 public void DataRowCollection_Add_D4()
138                 {
139                         DataTable dt = DataProvider.CreateParentDataTable();
140                         DataTable dt1 = DataProvider.CreateParentDataTable();
141
142                         dt.Rows.Add(dt1.Rows[0]);
143                 }
144
145                 [Test]
146                 public void DataRowCollection_Add_O1()
147                 {
148                         DataTable dt = DataProvider.CreateParentDataTable();
149                         dt.Rows.Clear();
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");
158
159                 }
160
161                 [Test]
162                 public void DataRowCollection_Add_O2()
163                 {               
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");
168                 }
169
170 //              [Test]
171 //              [ExpectedException(typeof(ArgumentException))]
172 //              public void DataRowCollection_Add_O3()
173 //              {
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});                     
176 //              }
177
178                 [Test]
179                 [ExpectedException(typeof(NullReferenceException))]
180                 public void DataRowCollection_Add_O4()
181                 {
182                         DataTable dt = DataProvider.CreateParentDataTable();
183                         dt.Rows.Add((Object[])null);
184                 }
185
186                 [Test]
187                 public void FindByKey ()
188                 {
189                         DataTable table = new DataTable ();
190                         table.Columns.Add ("col1", typeof (int));
191                         table.PrimaryKey = new DataColumn[] {table.Columns [0]};
192
193                         table.Rows.Add (new object[] {1});
194                         table.Rows.Add (new object[] {2});
195                         table.Rows.Add (new object[] {3});
196                         table.AcceptChanges ();
197
198                         Assert.IsNotNull (table.Rows.Find (new object[] {1}), "#1");
199
200                         table.Rows[0].Delete ();
201                         Assert.IsNull (table.Rows.Find (new object[] {1}), "#2");
202
203                         table.RejectChanges ();
204                         Assert.IsNotNull (table.Rows.Find (new object[] {1}), "#3");
205                 }
206
207                 [Test]
208                 public void FindByKey_VerifyOrder ()
209                 {
210                         DataTable table = new DataTable ();
211                         table.Columns.Add ("col1", typeof (int));
212                         table.PrimaryKey = new DataColumn[] {table.Columns [0]};
213
214                         table.Rows.Add (new object[] {1});
215                         table.Rows.Add (new object[] {2});
216                         table.Rows.Add (new object[] {1000});
217                         table.AcceptChanges ();
218
219                         table.Rows [1][0] = 100;
220                         Assert.IsNotNull (table.Rows.Find (100), "#1");
221
222                         table.Rows [2][0] = 999;
223                         Assert.IsNotNull (table.Rows.Find (999), "#2");
224                         Assert.IsNotNull (table.Rows.Find (100), "#3");
225                 }
226
227                 [Test]
228                 public void FindByKey_DuringDataLoad ()
229                 {
230                         DataTable table = new DataTable ();
231                         table.Columns.Add ("col1", typeof (int));
232                         table.PrimaryKey = new DataColumn[] {table.Columns [0]};
233
234                         table.Rows.Add (new object[] {1});
235                         table.Rows.Add (new object[] {2});
236                         table.AcceptChanges ();
237
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");
244                 }
245
246                 [Test]
247                 public void DataRowCollection_Clear1()
248                 {
249                         DataTable dt = DataProvider.CreateParentDataTable();
250                         int count = dt.Rows.Count;
251                         Assert.AreEqual(count !=0,true);
252                         dt.Rows.Clear();
253                         Assert.AreEqual(0,dt.Rows.Count,"DRWC15");
254                 }
255
256                 [Test]
257                 [ExpectedException(typeof(InvalidConstraintException))]
258                 public void DataRowCollection_Clear2()
259                 {
260                         DataSet ds = DataProvider.CreateForigenConstraint();
261
262                         ds.Tables[0].Rows.Clear(); //Try to clear the parent table                      
263                 }
264
265                 [Test]
266                 public void DataRowCollection_Contains_O1()
267                 {
268                         DataTable dt = DataProvider.CreateParentDataTable();
269                         dt.PrimaryKey=  new DataColumn[] {dt.Columns[0]};
270                         
271                         Assert.AreEqual(true,dt.Rows.Contains(1),"DRWC16");
272                         Assert.AreEqual(false,dt.Rows.Contains(10),"DRWC17");
273                 }
274
275                 [Test]
276                 [ExpectedException(typeof(MissingPrimaryKeyException))]
277                 public void DataRowCollection_Contains_O2()
278                 {
279                         DataTable dt = DataProvider.CreateParentDataTable();
280                         Assert.AreEqual(false,dt.Rows.Contains(1),"DRWC18");
281                 }
282
283                 [Test]
284                 public void DataRowCollection_Contains_O3()
285                 {
286                         DataTable dt = DataProvider.CreateParentDataTable();
287                         dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
288
289                         //Prepare values array
290                         object[] arr = new object[2];
291                         arr[0] = 1;
292                         arr[1] = "1-String1";
293
294                         Assert.AreEqual(true,dt.Rows.Contains( (object[])arr),"DRWC19");
295
296                         arr[0] = 8;
297
298                         Assert.AreEqual(false,dt.Rows.Contains( (object[])arr),"DRWC20");
299
300                 }
301
302                 [Test]
303                 [ExpectedException(typeof(ArgumentException))]
304                 public void DataRowCollection_Contains_O4()
305                 {
306                         DataTable dt = DataProvider.CreateParentDataTable();
307                         dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
308
309                         //Prepare values array
310                         object[] arr = new object[1];
311                         arr[0] = 1;
312
313                         Assert.AreEqual(false,dt.Rows.Contains((object[]) arr),"DRWC21");
314                 }
315
316                 [Test]
317                 public void DataRowCollection_Find_O1()
318                 {
319                         DataTable dt = DataProvider.CreateParentDataTable();
320                         dt.PrimaryKey=  new DataColumn[] {dt.Columns[0]};
321                         
322                         Assert.AreEqual(dt.Rows[0],dt.Rows.Find(1),"DRWC22");
323                         Assert.AreEqual(null,dt.Rows.Find(10),"DRWC23");
324                 }
325
326                 [Test]
327                 [ExpectedException(typeof(MissingPrimaryKeyException))]
328                 public void DataRowCollection_Find_O2()
329                 {
330                         DataTable dt = DataProvider.CreateParentDataTable();
331
332                         Assert.AreEqual(null,dt.Rows.Find(1),"DRWC24");
333                 }
334
335                 [Test]
336                 public void DataRowCollection_Find_O3()
337                 {
338                         DataTable dt = DataProvider.CreateParentDataTable();
339                         dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
340
341                         //Prepare values array
342                         object[] arr = new object[2];
343                         arr[0] = 2;
344                         arr[1] = "2-String1";
345
346                         Assert.AreEqual(dt.Rows[1],dt.Rows.Find( (object[])arr),"DRWC25");
347
348                         arr[0] = 8;
349
350                         Assert.AreEqual(null,dt.Rows.Find( (object[])arr),"DRWC26");
351
352                 }
353
354                 [Test]
355                 [ExpectedException(typeof(ArgumentException))]
356                 public void DataRowCollection_Find_O4()
357                 {
358                         DataTable dt = DataProvider.CreateParentDataTable();
359                         dt.PrimaryKey= new DataColumn[] {dt.Columns[0],dt.Columns[1]};
360
361                         //Prepare values array
362                         object[] arr = new object[1];
363                         arr[0] = 1;
364                         
365                         Assert.AreEqual(null,dt.Rows.Find((object[]) arr),"DRWC27");
366                 }
367
368                 [Test]
369                 public void DataRowCollection_InsertAt_DI1()
370                 {
371                         DataTable dt = DataProvider.CreateParentDataTable();
372                         DataRow dr =  GetNewDataRow(dt);
373                         dt.Rows.InsertAt(dr,0);
374
375                         Assert.AreEqual(dr,dt.Rows[0],"DRWC28"); //Begin
376                 }
377
378                 [Test]
379                 public void DataRowCollection_InsertAt_DI2()
380                 {
381                         DataTable dt = DataProvider.CreateParentDataTable();
382                         DataRow dr =  GetNewDataRow(dt);
383                         dt.Rows.InsertAt(dr,3);
384
385                         Assert.AreEqual(dr,dt.Rows[3],"DRWC29"); //Middle
386                 }
387
388                 [Test]
389                 public void DataRowCollection_InsertAt_DI3()
390                 {
391                         DataTable dt = DataProvider.CreateParentDataTable();
392                         DataRow dr =  GetNewDataRow(dt);
393                         dt.Rows.InsertAt(dr,300);
394
395                         Assert.AreEqual(dr,dt.Rows[dt.Rows.Count-1],"DRWC30"); //End
396                 }
397
398                 [Test]
399                 [ExpectedException(typeof(IndexOutOfRangeException))]
400                 public void DataRowCollection_InsertAt_DI4()
401                 {
402                         DataTable dt = DataProvider.CreateParentDataTable();
403                         DataRow dr =  GetNewDataRow(dt);
404
405                         dt.Rows.InsertAt(dr,-1);
406                 }
407
408                 private DataRow  GetNewDataRow(DataTable dt)
409                 {
410                         DataRow dr = dt.NewRow();
411                         dr["ParentId"] = 10;
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;
417                         return dr;
418                 }
419
420                 [Test]
421                 public void DataRowCollection_Item1()
422                 {
423                         DataTable dt = DataProvider.CreateParentDataTable();
424                         int index=0;
425
426                         foreach (DataRow dr in dt.Rows)
427                         {
428                                 Assert.AreEqual(dr,dt.Rows[index],"DRWC31");
429                                 index++;
430                         }
431                 }
432
433                 [Test]
434                 [ExpectedException(typeof(IndexOutOfRangeException))]
435                 public void DataRowCollection_Item2()
436                 {
437                         DataTable dt = DataProvider.CreateParentDataTable();
438
439                         DataRow dr =  dt.Rows[-1];
440                 }
441         }
442 }