Merge pull request #3142 from henricm/fix-for-win-mono_string_to_utf8
[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 
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                         Assert.AreEqual ("", v.RowFilter, "RowFilter");
100                         Assert.AreEqual ("", v.Sort, "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                         Assert.AreEqual (true, drv.IsEdit, "DataView.Item");
114
115                         drv = dv.AddNew ();
116                         drv.Row ["col"] = "test";
117                         drv.Row.CancelEdit ();
118                         Assert.AreEqual (false, drv.IsEdit, "AddNew");
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                         Assert.AreEqual ("val", drv ["col"], "DataView.Item");
131                 }
132
133                 [Test]
134                 [ExpectedException (typeof (RowNotInTableException))]
135                 public void ItemException ()
136                 {
137                         DataTable dt = new DataTable ("table");
138                         dt.Columns.Add ("col");
139                         dt.Rows.Add ((new object [] {"val"}));
140                         DataView dv = new DataView (dt);
141                         DataRowView drv = dv.AddNew ();
142                         drv.Row ["col"] = "test";
143                         drv.Row.CancelEdit ();
144                         object o = drv ["col"];
145                 }
146
147                 [Test]
148                 public void RowVersion1 ()
149                 {
150                         // I guess we could write better tests.
151                         DataTable dt = new DataTable ("table");
152                         dt.Columns.Add ("col");
153                         dt.Rows.Add (new object [] {1});
154                         DataView dv = new DataView (dt);
155                         DataRowView drv = dv.AddNew ();
156                         Assert.AreEqual (DataRowVersion.Current, drv.RowVersion);
157                         Assert.AreEqual (DataRowVersion.Current, dv [0].RowVersion);
158                         drv ["col"] = "mod";
159                         Assert.AreEqual (DataRowVersion.Current, drv.RowVersion);
160                         Assert.AreEqual (DataRowVersion.Current, dv [0].RowVersion);
161                         dt.AcceptChanges ();
162                         Assert.AreEqual (DataRowVersion.Current, drv.RowVersion);
163                         Assert.AreEqual (DataRowVersion.Current, dv [0].RowVersion);
164                         drv.EndEdit ();
165                         dv [0].EndEdit ();
166                         Assert.AreEqual (DataRowVersion.Current, drv.RowVersion);
167                         Assert.AreEqual (DataRowVersion.Current, dv [0].RowVersion);
168                 }
169         }
170 }