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.ComponentModel;
34 using MonoTests.System.Data.Utils;
36 namespace MonoTests.System.Data
38 [TestFixture] public class DataRowViewTest2
40 [Test] public void BeginEdit()
42 DataTable dt = DataProvider.CreateParentDataTable();
43 DataView dv = new DataView(dt);
45 DataRowView drv = dv[0];
48 drv["String1"] = "ChangeValue";
50 // check Proposed value
51 Assert.AreEqual("ChangeValue" , dt.Rows[0]["String1",DataRowVersion.Proposed] , "DRV1");
53 // check Original value
54 Assert.AreEqual("1-String1" , dt.Rows[0]["String1",DataRowVersion.Original ] , "DRV2");
57 Assert.AreEqual(true, drv.IsEdit , "DRV3");
59 // check IsEdit - change another row
60 dv[1]["String1"] = "something";
61 Assert.AreEqual(true, drv.IsEdit , "DRV4");
64 [Test] public void CancelEdit()
66 DataTable dt = DataProvider.CreateParentDataTable();
67 DataView dv = new DataView(dt);
69 DataRowView drv = dv[0];
72 drv["String1"] = "ChangeValue";
74 // check Proposed value
75 Assert.AreEqual("ChangeValue" , dt.Rows[0]["String1",DataRowVersion.Proposed] , "DRV5");
78 Assert.AreEqual(true, drv.IsEdit , "DRV6");
80 // check Proposed value
82 Assert.AreEqual(false, dt.Rows[0].HasVersion(DataRowVersion.Proposed) , "DRV7");
84 // check current value
85 Assert.AreEqual("1-String1" , dt.Rows[0]["String1",DataRowVersion.Current ] , "DRV8");
87 // check IsEdit after cancel edit
88 Assert.AreEqual(false, drv.IsEdit , "DRV9");
91 [Test] public void CreateChildView_ByDataRelation()
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);
102 //DataView dvChild = null;
103 DataView dvParent = new DataView(dtParent);
105 DataView dvTmp1 = dvParent[0].CreateChildView(drel);
106 DataView dvTmp2 = dvParent[3].CreateChildView(drel);
109 Assert.AreEqual(true, dvTmp1!=null, "DRV10");
111 // Child view table = ChildTable
112 Assert.AreEqual(dtChild , dvTmp1.Table , "DRV11");
114 // ChildView1.Table = ChildView2.Table
115 Assert.AreEqual(dvTmp2.Table, dvTmp1.Table , "DRV12");
117 //the child dataview are different
118 // Child DataViews different
119 Assert.AreEqual(false, dvTmp1.Equals(dvTmp2), "DRV13");
122 [Test] public void CreateChildView_ByName()
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);
133 //DataView dvChild = null;
134 DataView dvParent = new DataView(dtParent);
136 DataView dvTmp1 = dvParent[0].CreateChildView("ParentChild");
137 DataView dvTmp2 = dvParent[3].CreateChildView("ParentChild");
140 Assert.AreEqual(true, dvTmp1!=null, "DRV14");
142 // Child view table = ChildTable
143 Assert.AreEqual(dtChild , dvTmp1.Table , "DRV15");
145 // ChildView1.Table = ChildView2.Table
146 Assert.AreEqual(dvTmp2.Table, dvTmp1.Table , "DRV16");
148 //the child dataview are different
149 // Child DataViews different
150 Assert.AreEqual(false, dvTmp1.Equals(dvTmp2), "DRV17");
153 [Test] public void DataView()
155 DataTable dt = DataProvider.CreateParentDataTable();
156 DataView dv = new DataView(dt);
158 DataRowView drv1 = dv[0];
159 DataRowView drv2 = dv[4];
161 // check DataRowView.DataView
162 Assert.AreEqual(dv, drv1.DataView , "DRV18");
164 // compare DataRowView.DataView
165 Assert.AreEqual(drv2.DataView, drv1.DataView , "DRV19");
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
171 // Console.WriteLine("*********" + (drv1.DataView == null));
172 Assert.AreEqual(true, drv1.DataView == dv , "DRV20");
174 //check that the DataRowView has a new DataView
175 // check that the DataRowView has a new DataView
177 Assert.AreEqual(true, drv1.DataView.Equals(dv) , "DRV21");
180 [Test] public void Delete()
182 DataTable dt = DataProvider.CreateParentDataTable();
183 DataView dv = new DataView(dt);
185 DataRowView drv = dv[0];
186 int TableRowsCount = dt.Rows.Count;
187 int ViewRowCount = dv.Count;
191 Assert.AreEqual(dv.Count, ViewRowCount-1, "DRV22");
193 //the table count should stay the same until EndEdit is invoked
195 Assert.AreEqual(TableRowsCount, dt.Rows.Count, "DRV23");
197 // DataRowState deleted
198 Assert.AreEqual(DataRowState.Deleted , drv.Row.RowState , "DRV24");
201 [Test] public void EndEdit()
203 DataTable dt = DataProvider.CreateParentDataTable();
204 DataView dv = new DataView(dt);
206 DataRowView drv = dv[0];
209 drv["String1"] = "ChangeValue";
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");
216 // check if has Proposed version
218 Assert.AreEqual(false, dt.Rows[0].HasVersion(DataRowVersion.Proposed) , "DRV26");
220 // check Current value
221 Assert.AreEqual("ChangeValue" , dt.Rows[0]["String1",DataRowVersion.Current] , "DRV27");
224 Assert.AreEqual(false, drv.IsEdit , "DRV28");
227 [Test] public void Equals()
229 DataTable dt = DataProvider.CreateParentDataTable();
230 DataView dv = new DataView(dt);
232 DataRowView d1 = dv[0];
233 DataRowView d2 = dv[0];
235 // DataRowView.Equals
236 Assert.AreEqual(d2, d1, "DRV29");
239 [Test] public void IsEdit()
241 DataTable dt = DataProvider.CreateParentDataTable();
242 DataView dv = new DataView(dt);
244 DataRowView drv = dv[0];
247 Assert.AreEqual(false, drv.IsEdit, "DRV30");
251 Assert.AreEqual(true, drv.IsEdit, "DRV31");
255 Assert.AreEqual(false, drv.IsEdit, "DRV32");
257 // after BeginEdit again
259 Assert.AreEqual(true, drv.IsEdit, "DRV33");
263 Assert.AreEqual(false, drv.IsEdit, "DRV34");
266 [Test] public void IsNew()
268 DataTable dt = DataProvider.CreateParentDataTable();
269 DataView dv = new DataView(dt);
271 DataRowView drv = dv[0];
274 Assert.AreEqual(false, drv.IsNew , "DRV35");
278 Assert.AreEqual(true, drv.IsNew , "DRV36");
281 [Test] public void Item()
283 DataTable dt = DataProvider.CreateParentDataTable();
284 DataView dv = new DataView(dt);
286 DataRowView drv = dv[0];
289 Assert.AreEqual(dt.Rows[0][0], drv[0], "DRV37");
292 Assert.AreEqual(dt.Rows[0][4], drv[4], "DRV38");
294 // Item -1 - excpetion
297 Assert.Fail("DRV39: Indexer Failed to throw IndexOutOfRangeException");
299 catch (IndexOutOfRangeException) {}
300 catch (AssertionException exc) {throw exc;}
301 catch (Exception exc)
303 Assert.Fail("DRV40: Indexer. Wrong exception type. Got:" + exc);
307 [Test] public void Item_Property()
309 DataTable dt = DataProvider.CreateParentDataTable();
310 DataView dv = new DataView(dt);
312 DataRowView drv = dv[0];
315 Assert.AreEqual(dt.Rows[0]["ParentId"], drv["ParentId"], "DRV41");
317 // Item 'ParentDateTime'
318 Assert.AreEqual(dt.Rows[0]["ParentDateTime"], drv["ParentDateTime"], "DRV42");
320 // Item invalid - excpetion
322 object o = drv["something"];
323 Assert.Fail("DRV43: Indexer Failed to throw ArgumentException");
325 catch (ArgumentException) {}
326 catch (AssertionException exc) {throw exc;}
327 catch (Exception exc)
329 Assert.Fail("DRV44: Indexer. Wrong exception type. Got:" + exc);
333 [Test] public void Row()
335 DataTable dt = DataProvider.CreateParentDataTable();
336 DataView dv = new DataView(dt);
337 DataRowView drv = null;
339 // Compare DataRowView.Row to table row
341 Assert.AreEqual(dt.Rows[3], drv.Row, "DRV45");
344 [Test] public void RowVersion()
346 DataTable dt = DataProvider.CreateParentDataTable();
347 DataView dv = new DataView(dt);
348 dt.Columns[1].DefaultValue = "default";
349 DataRowView drv = dv[0];
351 dt.Rows.Add(new object[] {99});
353 dt.Rows[2].BeginEdit();
354 dt.Rows[2][1] = "aaa";
356 dv.RowStateFilter=DataViewRowState.CurrentRows ;
358 Assert.AreEqual(DataRowVersion.Current, drv.RowVersion, "DRV46");
360 dv.RowStateFilter=DataViewRowState.Deleted ;
362 Assert.AreEqual(DataRowVersion.Current , drv.RowVersion, "DRV47");