2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Data / Test / System.Data / DataRowViewTest.cs
1 //
2 // DataRowViewTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // (C) 2005 Novell Inc,
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.Data;
34 using System.ComponentModel;
35
36 namespace MonoTests.System.Data
37 {
38         [TestFixture]
39         public class DataRowViewTest : Assertion
40         {
41                 private DataView CreateTestView ()
42                 {
43                         DataTable dt1 = new DataTable ("table1");
44                         DataColumn c1 = new DataColumn ("col");
45                         dt1.Columns.Add (c1);
46                         dt1.Rows.Add (new object [] {"1"});
47                         return new DataView (dt1);
48                 }
49
50                 [Test]
51                 [ExpectedException (typeof (ArgumentException))]
52                 public void CreateChildViewNullStringArg ()
53                 {
54                         DataView dv = CreateTestView ();
55                         DataRowView dvr = dv [0];
56                         dvr.CreateChildView ((string) null);
57                 }
58
59                 [Test]
60                 [ExpectedException (typeof (ArgumentException))]
61                 public void CreateChildViewNullDataRelationArg ()
62                 {
63                         DataView dv = CreateTestView ();
64                         DataRowView dvr = dv [0];
65                         dvr.CreateChildView ((DataRelation) null);
66                 }
67
68                 [Test]
69                 [ExpectedException (typeof (ArgumentException))]
70                 public void CreateChildViewNonExistentName ()
71                 {
72                         DataView dv = CreateTestView ();
73                         DataRowView dvr = dv [0];
74                         dvr.CreateChildView ("nothing");
75                 }
76
77                 [Test]
78                 public void CreateChildViewSimple ()
79                 {
80                         DataSet ds = new DataSet ();
81
82                         DataTable dt1 = new DataTable ("table1");
83                         ds.Tables.Add (dt1);
84                         DataColumn c1 = new DataColumn ("col");
85                         dt1.Columns.Add (c1);
86                         dt1.Rows.Add (new object [] {"1"});
87
88                         DataTable dt2 = new DataTable ("table2");
89                         ds.Tables.Add (dt2);
90                         DataColumn c2 = new DataColumn ("col");
91                         dt2.Columns.Add (c2);
92                         dt2.Rows.Add (new object [] {"1"});
93
94                         DataRelation dr = new DataRelation ("dr", c1, c2);
95
96                         DataView dv = new DataView (dt1);
97                         DataRowView dvr = dv [0];
98                         DataView v = dvr.CreateChildView (dr);
99                         AssertEquals ("RowFilter", "", v.RowFilter);
100                         AssertEquals ("Sort", "", v.Sort);
101                 }
102
103                 [Test]
104                 public void IsEdit ()
105                 {
106                         DataTable dt = new DataTable ("table");
107                         dt.Columns.Add ("col");
108                         dt.Rows.Add ((new object [] {"val"}));
109
110                         DataView dv = new DataView (dt);
111                         DataRowView drv = dv [0];
112                         dt.Rows [0].BeginEdit ();
113                         AssertEquals ("DataView.Item", true, drv.IsEdit);
114
115                         drv = dv.AddNew ();
116                         drv.Row ["col"] = "test";
117                         drv.Row.CancelEdit ();
118                         AssertEquals ("AddNew", false, drv.IsEdit);
119                 }
120
121                 [Test]
122                 public void Item ()
123                 {
124                         DataTable dt = new DataTable ("table");
125                         dt.Columns.Add ("col");
126                         dt.Rows.Add ((new object [] {"val"}));
127                         DataView dv = new DataView (dt);
128                         DataRowView drv = dv [0];
129                         dt.Rows [0].BeginEdit ();
130                         AssertEquals ("DataView.Item", "val", drv ["col"]);
131                 }
132
133                 [Test]
134                 [ExpectedException (typeof (RowNotInTableException))]
135 //              [NUnit.Framework.Category ("NotWorking")]
136                 public void ItemException ()
137                 {
138                         DataTable dt = new DataTable ("table");
139                         dt.Columns.Add ("col");
140                         dt.Rows.Add ((new object [] {"val"}));
141                         DataView dv = new DataView (dt);
142                         DataRowView drv = dv.AddNew ();
143                         drv.Row ["col"] = "test";
144                         drv.Row.CancelEdit ();
145                         object o = drv ["col"];
146                 }
147
148                 [Test]
149                 public void RowVersion1 ()
150                 {
151                         // I guess we could write better tests.
152                         DataTable dt = new DataTable ("table");
153                         dt.Columns.Add ("col");
154                         dt.Rows.Add (new object [] {1});
155                         DataView dv = new DataView (dt);
156                         DataRowView drv = dv.AddNew ();
157                         AssertEquals (DataRowVersion.Current, drv.RowVersion);
158                         AssertEquals (DataRowVersion.Current, dv [0].RowVersion);
159                         drv ["col"] = "mod";
160                         AssertEquals (DataRowVersion.Current, drv.RowVersion);
161                         AssertEquals (DataRowVersion.Current, dv [0].RowVersion);
162                         dt.AcceptChanges ();
163                         AssertEquals (DataRowVersion.Current, drv.RowVersion);
164                         AssertEquals (DataRowVersion.Current, dv [0].RowVersion);
165                         drv.EndEdit ();
166                         dv [0].EndEdit ();
167                         AssertEquals (DataRowVersion.Current, drv.RowVersion);
168                         AssertEquals (DataRowVersion.Current, dv [0].RowVersion);
169                 }
170         }
171 }