Merge pull request #1685 from esdrubal/touint64
[mono.git] / mcs / class / System.Data / Test / System.Data / DataRowViewTest2.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.ComponentModel;
33 using System.Data;
34 using MonoTests.System.Data.Utils;
35
36 namespace MonoTests.System.Data
37 {
38         [TestFixture] public class DataRowViewTest2
39         {
40                 [Test] public void BeginEdit()
41                 {
42                         DataTable dt = DataProvider.CreateParentDataTable();
43                         DataView dv = new DataView(dt);
44
45                         DataRowView drv = dv[0];
46
47                         drv.BeginEdit();
48                         drv["String1"] = "ChangeValue";
49
50                         // check Proposed value
51                         Assert.AreEqual("ChangeValue" , dt.Rows[0]["String1",DataRowVersion.Proposed] , "DRV1");
52
53                         // check Original value
54                         Assert.AreEqual("1-String1" , dt.Rows[0]["String1",DataRowVersion.Original ] , "DRV2");
55
56                         // check IsEdit
57                         Assert.AreEqual(true, drv.IsEdit , "DRV3");
58
59                         // check IsEdit - change another row
60                         dv[1]["String1"] = "something";
61                         Assert.AreEqual(true, drv.IsEdit , "DRV4");
62                 }
63
64                 [Test] public void CancelEdit()
65                 {
66                         DataTable dt = DataProvider.CreateParentDataTable();
67                         DataView dv = new DataView(dt);
68
69                         DataRowView drv = dv[0];
70
71                         drv.BeginEdit();
72                         drv["String1"] = "ChangeValue";
73
74                         // check Proposed value
75                         Assert.AreEqual("ChangeValue" , dt.Rows[0]["String1",DataRowVersion.Proposed] , "DRV5");
76
77                         // check IsEdit
78                         Assert.AreEqual(true, drv.IsEdit , "DRV6");
79
80                         // check Proposed value
81                         drv.CancelEdit();
82                         Assert.AreEqual(false, dt.Rows[0].HasVersion(DataRowVersion.Proposed) , "DRV7");
83
84                         // check current value
85                         Assert.AreEqual("1-String1" , dt.Rows[0]["String1",DataRowVersion.Current ] , "DRV8");
86
87                         // check IsEdit after cancel edit
88                         Assert.AreEqual(false, drv.IsEdit , "DRV9");
89                 }
90
91                 [Test] public void CreateChildView_ByDataRelation()
92                 {
93                         //create a dataset with two tables, with a DataRelation between them
94                         DataTable dtParent = DataProvider.CreateParentDataTable();
95                         DataTable dtChild = DataProvider.CreateChildDataTable();
96                         DataSet ds = new DataSet();
97                         ds.Tables.Add(dtParent);
98                         ds.Tables.Add(dtChild);
99                         DataRelation drel = new DataRelation("ParentChild",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
100                         ds.Relations.Add(drel);
101
102                         //DataView dvChild = null;
103                         DataView dvParent = new DataView(dtParent);
104
105                         DataView dvTmp1 = dvParent[0].CreateChildView(drel);
106                         DataView dvTmp2 = dvParent[3].CreateChildView(drel);
107
108                         // ChildView != null
109                         Assert.AreEqual(true, dvTmp1!=null, "DRV10");
110
111                         // Child view table = ChildTable
112                         Assert.AreEqual(dtChild , dvTmp1.Table , "DRV11");
113
114                         // ChildView1.Table = ChildView2.Table
115                         Assert.AreEqual(dvTmp2.Table, dvTmp1.Table , "DRV12");
116
117                         //the child dataview are different
118                         // Child DataViews different 
119                         Assert.AreEqual(false, dvTmp1.Equals(dvTmp2), "DRV13");
120                 }
121
122                 [Test] public void CreateChildView_ByName()
123                 {
124                         //create a dataset with two tables, with a DataRelation between them
125                         DataTable dtParent = DataProvider.CreateParentDataTable();
126                         DataTable dtChild = DataProvider.CreateChildDataTable();
127                         DataSet ds = new DataSet();
128                         ds.Tables.Add(dtParent);
129                         ds.Tables.Add(dtChild);
130                         DataRelation drel = new DataRelation("ParentChild",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
131                         ds.Relations.Add(drel);
132
133                         //DataView dvChild = null;
134                         DataView dvParent = new DataView(dtParent);
135
136                         DataView dvTmp1 = dvParent[0].CreateChildView("ParentChild");
137                         DataView dvTmp2 = dvParent[3].CreateChildView("ParentChild");
138
139                         // ChildView != null
140                         Assert.AreEqual(true, dvTmp1!=null, "DRV14");
141
142                         // Child view table = ChildTable
143                         Assert.AreEqual(dtChild , dvTmp1.Table , "DRV15");
144
145                         // ChildView1.Table = ChildView2.Table
146                         Assert.AreEqual(dvTmp2.Table, dvTmp1.Table , "DRV16");
147
148                         //the child dataview are different
149                         // Child DataViews different 
150                         Assert.AreEqual(false, dvTmp1.Equals(dvTmp2), "DRV17");
151                 }
152
153                 [Test] public void DataView()
154                 {
155                         DataTable dt = DataProvider.CreateParentDataTable();
156                         DataView dv = new DataView(dt);
157
158                         DataRowView drv1 = dv[0];
159                         DataRowView drv2 = dv[4];
160
161                         // check DataRowView.DataView 
162                         Assert.AreEqual(dv, drv1.DataView , "DRV18");
163
164                         // compare DataRowView.DataView 
165                         Assert.AreEqual(drv2.DataView, drv1.DataView , "DRV19");
166
167                         //check that the DataRowView still has the same DataView even when the source table changed
168                         // check that the DataRowView still has the same DataView
169                         dv.Table = null;
170
171                         // Console.WriteLine("*********" + (drv1.DataView == null));
172                         Assert.AreEqual(true, drv1.DataView == dv , "DRV20");
173
174                         //check that the DataRowView has a new DataView
175                         // check that the DataRowView has a new DataView
176                         dv = new DataView();
177                         Assert.AreEqual(true, drv1.DataView.Equals(dv) , "DRV21");
178                 }
179
180                 [Test] public void Delete()
181                 {
182                         DataTable dt = DataProvider.CreateParentDataTable();
183                         DataView dv = new DataView(dt);
184
185                         DataRowView drv = dv[0];
186                         int TableRowsCount = dt.Rows.Count;
187                         int ViewRowCount = dv.Count;
188
189                         // DataView Count
190                         drv.Delete();
191                         Assert.AreEqual(dv.Count, ViewRowCount-1, "DRV22");
192
193                         //the table count should stay the same until EndEdit is invoked
194                         // Table Count
195                         Assert.AreEqual(TableRowsCount, dt.Rows.Count, "DRV23");
196
197                         // DataRowState deleted
198                         Assert.AreEqual(DataRowState.Deleted , drv.Row.RowState , "DRV24");
199                 }
200
201                 [Test] public void EndEdit()
202                 {
203                         DataTable dt = DataProvider.CreateParentDataTable();
204                         DataView dv = new DataView(dt);
205
206                         DataRowView drv = dv[0];
207
208                         drv.BeginEdit();
209                         drv["String1"] = "ChangeValue";
210
211                         //the row should be stay in edit mode event if changing other rows
212                         // check IsEdit - change another row
213                         dv[1]["String1"] = "something";
214                         Assert.AreEqual(true, drv.IsEdit , "DRV25");
215
216                         // check if has Proposed version
217                         drv.EndEdit();
218                         Assert.AreEqual(false, dt.Rows[0].HasVersion(DataRowVersion.Proposed) , "DRV26");
219
220                         // check Current value
221                         Assert.AreEqual("ChangeValue" , dt.Rows[0]["String1",DataRowVersion.Current] , "DRV27");
222
223                         // check IsEdit
224                         Assert.AreEqual(false, drv.IsEdit , "DRV28");
225                 }
226
227                 [Test] public void Equals()
228                 {
229                         DataTable dt = DataProvider.CreateParentDataTable();
230                         DataView dv = new DataView(dt);
231
232                         DataRowView d1 = dv[0];
233                         DataRowView d2 = dv[0];
234
235                         // DataRowView.Equals
236                         Assert.AreEqual(d2, d1, "DRV29");
237                 }
238
239                 [Test] public void IsEdit()
240                 {
241                         DataTable dt = DataProvider.CreateParentDataTable();
242                         DataView dv = new DataView(dt);
243
244                         DataRowView drv = dv[0];
245
246                         // default value
247                         Assert.AreEqual(false, drv.IsEdit, "DRV30"); 
248
249                         // after BeginEdit
250                         drv.BeginEdit();
251                         Assert.AreEqual(true, drv.IsEdit, "DRV31"); 
252
253                         // after CancelEdit
254                         drv.CancelEdit();
255                         Assert.AreEqual(false, drv.IsEdit, "DRV32"); 
256
257                         // after BeginEdit again
258                         drv.BeginEdit();
259                         Assert.AreEqual(true, drv.IsEdit, "DRV33"); 
260
261                         // after EndEdit 
262                         drv.EndEdit();
263                         Assert.AreEqual(false, drv.IsEdit, "DRV34"); 
264                 }
265
266                 [Test] public void IsNew()
267                 {
268                         DataTable dt = DataProvider.CreateParentDataTable();
269                         DataView dv = new DataView(dt);
270
271                         DataRowView drv = dv[0];
272
273                         // existing row
274                         Assert.AreEqual(false, drv.IsNew , "DRV35"); 
275
276                         // add new row
277                         drv = dv.AddNew();
278                         Assert.AreEqual(true, drv.IsNew , "DRV36"); 
279                 }
280
281                 [Test] public void Item()
282                 {
283                         DataTable dt = DataProvider.CreateParentDataTable();
284                         DataView dv = new DataView(dt);
285
286                         DataRowView drv = dv[0];
287
288                         // Item 0
289                         Assert.AreEqual(dt.Rows[0][0], drv[0], "DRV37"); 
290
291                         // Item 4
292                         Assert.AreEqual(dt.Rows[0][4], drv[4], "DRV38"); 
293
294                         // Item -1 - excpetion
295                         try {
296                                 object o = drv[-1];
297                                 Assert.Fail("DRV39: Indexer Failed to throw IndexOutOfRangeException");
298                         }
299                         catch (IndexOutOfRangeException) {}
300                         catch (AssertionException exc) {throw  exc;}
301                         catch (Exception exc)
302                         {
303                                 Assert.Fail("DRV40: Indexer. Wrong exception type. Got:" + exc);
304                         }
305                 }
306
307                 [Test] public void Item_Property()
308                 {
309                         DataTable dt = DataProvider.CreateParentDataTable();
310                         DataView dv = new DataView(dt);
311
312                         DataRowView drv = dv[0];
313
314                         // Item 'ParentId'
315                         Assert.AreEqual(dt.Rows[0]["ParentId"], drv["ParentId"], "DRV41"); 
316
317                         // Item 'ParentDateTime'
318                         Assert.AreEqual(dt.Rows[0]["ParentDateTime"], drv["ParentDateTime"], "DRV42"); 
319
320                         // Item invalid - excpetion
321                         try {
322                                 object o = drv["something"];
323                                 Assert.Fail("DRV43: Indexer Failed to throw ArgumentException");
324                         }
325                         catch (ArgumentException) {}
326                         catch (AssertionException exc) {throw  exc;}
327                         catch (Exception exc)
328                         {
329                                 Assert.Fail("DRV44: Indexer. Wrong exception type. Got:" + exc);
330                         }
331                 }
332
333                 [Test] public void Row()
334                 {
335                         DataTable dt = DataProvider.CreateParentDataTable();
336                         DataView dv = new DataView(dt);
337                         DataRowView drv = null;
338
339                         // Compare DataRowView.Row to table row
340                         drv = dv[3];
341                         Assert.AreEqual(dt.Rows[3], drv.Row, "DRV45"); 
342                 }
343
344                 [Test] public void RowVersion()
345                 {
346                         DataTable dt = DataProvider.CreateParentDataTable();
347                         DataView dv = new DataView(dt);
348                         dt.Columns[1].DefaultValue = "default";
349                         DataRowView drv = dv[0];
350
351                         dt.Rows.Add(new object[] {99});
352                         dt.Rows[1].Delete();
353                         dt.Rows[2].BeginEdit();
354                         dt.Rows[2][1] = "aaa";
355
356                         dv.RowStateFilter=DataViewRowState.CurrentRows ;
357                         // check Current
358                         Assert.AreEqual(DataRowVersion.Current, drv.RowVersion, "DRV46");
359
360                         dv.RowStateFilter=DataViewRowState.Deleted ;
361                         // check Original
362                         Assert.AreEqual(DataRowVersion.Current , drv.RowVersion, "DRV47");
363                 }
364         }
365 }