1 // DataView.cs - Nunit Test Cases for for testing the DataView
4 // Punit Kumar Todi ( punit_todi@da-iict.org )
5 // Patrick Kalkman kalkman@cistron.nl
6 // Umadevi S (sumadevi@novell.com)
8 // (C) 2003 Patrick Kalkman
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using NUnit
.Framework
;
37 using System
.ComponentModel
;
39 namespace MonoTests
.System
.Data
42 public class DataViewTest
: Assertion
48 ListChangedEventArgs listChangedArgs
;
50 public void GetReady ()
52 dataTable
= new DataTable ("itemTable");
53 DataColumn dc1
= new DataColumn ("itemId");
54 DataColumn dc2
= new DataColumn ("itemName");
55 DataColumn dc3
= new DataColumn ("itemPrice");
56 DataColumn dc4
= new DataColumn ("itemCategory");
58 dataTable
.Columns
.Add (dc1
);
59 dataTable
.Columns
.Add (dc2
);
60 dataTable
.Columns
.Add (dc3
);
61 dataTable
.Columns
.Add (dc4
);
65 rndm
= new Random (seed
);
66 for (int i
= 1; i
<= rowCount
; i
++) {
67 dr
= dataTable
.NewRow ();
68 dr
["itemId"] = "item " + i
;
69 dr
["itemName"] = "name " + rndm
.Next ();
70 dr
["itemPrice"] = "Rs. " + (rndm
.Next () % 1000);
71 dr
["itemCategory"] = "Cat " + ((rndm
.Next () % 10) + 1);
72 dataTable
.Rows
.Add (dr
);
74 dataTable
.AcceptChanges ();
75 dataView
= new DataView (dataTable
);
76 dataView
.ListChanged
+= new ListChangedEventHandler (OnListChanged
);
77 listChangedArgs
= null;
80 protected void OnListChanged (object sender
, ListChangedEventArgs args
)
82 listChangedArgs
= args
;
84 /*Console.WriteLine("EventType :: " + listChangedArgs.ListChangedType +
85 " oldIndex :: " + listChangedArgs.OldIndex +
86 " NewIndex :: " + listChangedArgs.NewIndex);*/
89 private void PrintTableOrView (DataTable t
, string label
)
91 Console
.WriteLine ("\n" + label
);
92 for (int i
= 0; i
<t
.Rows
.Count
; i
++){
93 foreach (DataColumn dc
in t
.Columns
)
94 Console
.Write (t
.Rows
[i
][dc
] + "\t");
95 Console
.WriteLine ("");
100 private void PrintTableOrView (DataView dv
, string label
)
102 Console
.WriteLine ("\n" + label
);
103 Console
.WriteLine ("Sort Key :: " + dv
.Sort
);
104 for (int i
= 0; i
< dv
.Count
; i
++) {
105 foreach (DataColumn dc
in dv
.Table
.Columns
)
106 Console
.Write (dv
[i
].Row
[dc
] + "\t");
107 Console
.WriteLine ("");
109 Console
.WriteLine ();
120 public void DataView ()
122 DataView dv1
,dv2
,dv3
;
123 dv1
= new DataView ();
124 // AssertEquals ("test#01",null,dv1.Table);
125 AssertEquals ("test#02",true,dv1
.AllowNew
);
126 AssertEquals ("test#03",true,dv1
.AllowEdit
);
127 AssertEquals ("test#04",true,dv1
.AllowDelete
);
128 AssertEquals ("test#05",false,dv1
.ApplyDefaultSort
);
129 AssertEquals ("test#06",string.Empty
,dv1
.RowFilter
);
130 AssertEquals ("test#07",DataViewRowState
.CurrentRows
,dv1
.RowStateFilter
);
131 AssertEquals ("test#08",string.Empty
,dv1
.Sort
);
133 dv2
= new DataView (dataTable
);
134 AssertEquals ("test#09","itemTable",dv2
.Table
.TableName
);
135 AssertEquals ("test#10",string.Empty
,dv2
.Sort
);
136 AssertEquals ("test#11",false,dv2
.ApplyDefaultSort
);
137 AssertEquals ("test#12",dataTable
.Rows
[0],dv2
[0].Row
);
139 dv3
= new DataView (dataTable
,"","itemId DESC",DataViewRowState
.CurrentRows
);
140 AssertEquals ("test#13","",dv3
.RowFilter
);
141 AssertEquals ("test#14","itemId DESC",dv3
.Sort
);
142 AssertEquals ("test#15",DataViewRowState
.CurrentRows
,dv3
.RowStateFilter
);
143 //AssertEquals ("test#16",dataTable.Rows.[(dataTable.Rows.Count-1)],dv3[0]);
147 public void TestValue ()
149 DataView TestView
= new DataView (dataTable
);
150 Assertion
.AssertEquals ("Dv #1", "item 1", TestView
[0]["itemId"]);
154 public void TestCount ()
156 DataView TestView
= new DataView (dataTable
);
157 Assertion
.AssertEquals ("Dv #3", 5, TestView
.Count
);
163 public void AllowNew ()
165 AssertEquals ("test#01",true,dataView
.AllowNew
);
168 public void ApplyDefaultSort ()
170 UniqueConstraint uc
= new UniqueConstraint (dataTable
.Columns
["itemId"]);
171 dataTable
.Constraints
.Add (uc
);
172 dataView
.ApplyDefaultSort
= true;
173 // dataView.Sort = "itemName";
174 // AssertEquals ("test#01","item 1",dataView[0]["itemId"]);
175 AssertEquals ("test#02",ListChangedType
.Reset
,listChangedArgs
.ListChangedType
);
176 // UnComment the line below to see if dataView is sorted
177 // PrintTableOrView (dataView,"* OnApplyDefaultSort");
180 [Ignore("Test code not implemented")]
181 public void RowStateFilter ()
184 AssertEquals ("test#01",ListChangedType
.Reset
,listChangedArgs
.ListChangedType
);
189 dataView
.Sort
= "itemName DESC";
190 AssertEquals ("test#01",ListChangedType
.Reset
,listChangedArgs
.ListChangedType
);
191 // UnComment the line below to see if dataView is sorted
192 // PrintTableOrView (dataView);
196 [ExpectedException(typeof(DataException
))]
197 public void AddNew_1 ()
199 dataView
.AllowNew
= false;
200 DataRowView drv
= dataView
.AddNew ();
204 public void AddNew_2 ()
206 dataView
.AllowNew
= true;
207 DataRowView drv
= dataView
.AddNew ();
208 AssertEquals ("test#01",ListChangedType
.ItemAdded
,listChangedArgs
.ListChangedType
);
209 AssertEquals ("test#02",drv
["itemName"],dataView
[dataView
.Count
- 1]["itemName"]);
210 drv
["itemId"] = "item " + 1001;
211 drv
["itemName"] = "name " + rndm
.Next();
212 drv
["itemPrice"] = "Rs. " + (rndm
.Next() % 1000);
213 drv
["itemCategory"] = "Cat " + ((rndm
.Next() % 10) + 1);
214 AssertEquals ("test#01",ListChangedType
.ItemChanged
,listChangedArgs
.ListChangedType
);
218 [Ignore("Test code not implemented")]
219 public void BeginInit ()
225 [ExpectedException(typeof(ArgumentException
))]
226 public void Find_1 ()
228 /* since the sort key is not specified. Must raise a ArgumentException */
229 int sIndex
= dataView
.Find ("abc");
233 public void Find_2 ()
237 randInt
= rndm
.Next () % rowCount
;
238 dataView
.Sort
= "itemId";
239 drv
= dataView
[randInt
];
240 AssertEquals ("test#01",randInt
,dataView
.Find (drv
["itemId"]));
242 dataView
.Sort
= "itemId DESC";
243 drv
= dataView
[randInt
];
244 AssertEquals ("test#02",randInt
,dataView
.Find (drv
["itemId"]));
246 dataView
.Sort
= "itemId, itemName";
247 drv
= dataView
[randInt
];
248 object [] keys
= new object [2];
249 keys
[0] = drv
["itemId"];
250 keys
[1] = drv
["itemName"];
251 AssertEquals ("test#03",randInt
,dataView
.Find (keys
));
253 dataView
.Sort
= "itemId";
254 AssertEquals ("test#04",-1,dataView
.Find("no item"));
258 [ExpectedException (typeof (ArgumentException
))]
259 public void Find_3 ()
261 dataView
.Sort
= "itemID, itemName";
262 /* expecting order key count mismatch */
263 dataView
.Find ("itemValue");
267 [Ignore("Test code not implemented")]
268 public void GetEnumerator ()
273 public void ToStringTest ()
275 AssertEquals ("test#01","System.Data.DataView",dataView
.ToString());
278 public void TestingEventHandling ()
280 dataView
.Sort
= "itemId";
282 dr
= dataTable
.NewRow ();
283 dr
["itemId"] = "item 0";
284 dr
["itemName"] = "name " + rndm
.Next ();
285 dr
["itemPrice"] = "Rs. " + (rndm
.Next () % 1000);
286 dr
["itemCategory"] = "Cat " + ((rndm
.Next () % 10) + 1);
287 dataTable
.Rows
.Add(dr
);
289 //PrintTableOrView(dataView, "ItemAdded");
290 AssertEquals ("test#01",ListChangedType
.ItemAdded
,listChangedArgs
.ListChangedType
);
292 dr
["itemId"] = "aitem 0";
293 // PrintTableOrView(dataView, "ItemChanged");
294 AssertEquals ("test#02",ListChangedType
.ItemChanged
,listChangedArgs
.ListChangedType
);
296 dr
["itemId"] = "zitem 0";
297 // PrintTableOrView(dataView, "ItemMoved");
298 AssertEquals ("test#03",ListChangedType
.ItemMoved
,listChangedArgs
.ListChangedType
);
300 dataTable
.Rows
.Remove (dr
);
301 // PrintTableOrView(dataView, "ItemDeleted");
302 AssertEquals ("test#04",ListChangedType
.ItemDeleted
,listChangedArgs
.ListChangedType
);
304 DataColumn dc5
= new DataColumn ("itemDesc");
305 dataTable
.Columns
.Add (dc5
);
306 // PrintTableOrView(dataView, "PropertyDescriptorAdded");
307 AssertEquals ("test#05",ListChangedType
.PropertyDescriptorAdded
,listChangedArgs
.ListChangedType
);
309 dc5
.ColumnName
= "itemDescription";
310 // PrintTableOrView(dataView, "PropertyDescriptorChanged");
311 // AssertEquals ("test#06",ListChangedType.PropertyDescriptorChanged,listChangedArgs.ListChangedType);
313 dataTable
.Columns
.Remove (dc5
);
314 // PrintTableOrView(dataView, "PropertyDescriptorDeleted");
315 AssertEquals ("test#07",ListChangedType
.PropertyDescriptorDeleted
,listChangedArgs
.ListChangedType
);
319 public void TestFindRows ()
321 DataView TestView
= new DataView (dataTable
);
322 TestView
.Sort
= "itemId";
323 DataRowView
[] Result
= TestView
.FindRows ("itemId");
324 Assertion
.AssertEquals ("Dv #1", 1, Result
.Length
);
325 Assertion
.AssertEquals ("Dv #2", "item 3", Result
[0]["itemId"]);
329 [ExpectedException (typeof (DeletedRowInaccessibleException
))]
330 public void TestDelete ()
332 DataView TestView
= new DataView (dataTable
);
334 DataRow r
= TestView
.Table
.Rows
[0];
335 Assertion
.Assert ("Dv #1", !(r
["itemId"] == "item 1"));
339 [ExpectedException (typeof (IndexOutOfRangeException
))]
340 public void TestDeleteOutOfBounds ()
342 DataView TestView
= new DataView (dataTable
);
343 TestView
.Delete (100);
347 [ExpectedException (typeof (DataException
))]
348 public void TestDeleteNotAllowed ()
350 DataView TestView
= new DataView (dataTable
);
351 TestView
.AllowDelete
= false;
356 [ExpectedException (typeof (DataException
))]
357 public void TestDeleteClosed ()
359 DataView TestView
= new DataView (dataTable
);
360 TestView
.Dispose (); // Close the table