File: DataRowViewTest2.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (365 lines) | stat: -rw-r--r-- 10,573 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Authors:
//   Rafael Mizrahi   <rafim@mainsoft.com>
//   Erez Lotan       <erezl@mainsoft.com>
//   Oren Gurfinkel   <oreng@mainsoft.com>
//   Ofer Borstein
// 
// Copyright (c) 2004 Mainsoft Co.
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using NUnit.Framework;
using System;
using System.IO;
using System.ComponentModel;
using System.Data;
using MonoTests.System.Data.Utils;

namespace MonoTests.System.Data
{
	[TestFixture] public class DataRowViewTest2
	{
		[Test] public void BeginEdit()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);

			DataRowView drv = dv[0];

			drv.BeginEdit();
			drv["String1"] = "ChangeValue";

			// check Proposed value
			Assert.AreEqual("ChangeValue" , dt.Rows[0]["String1",DataRowVersion.Proposed] , "DRV1");

			// check Original value
			Assert.AreEqual("1-String1" , dt.Rows[0]["String1",DataRowVersion.Original ] , "DRV2");

			// check IsEdit
			Assert.AreEqual(true, drv.IsEdit , "DRV3");

			// check IsEdit - change another row
			dv[1]["String1"] = "something";
			Assert.AreEqual(true, drv.IsEdit , "DRV4");
		}

		[Test] public void CancelEdit()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);

			DataRowView drv = dv[0];

			drv.BeginEdit();
			drv["String1"] = "ChangeValue";

			// check Proposed value
			Assert.AreEqual("ChangeValue" , dt.Rows[0]["String1",DataRowVersion.Proposed] , "DRV5");

			// check IsEdit
			Assert.AreEqual(true, drv.IsEdit , "DRV6");

			// check Proposed value
			drv.CancelEdit();
			Assert.AreEqual(false, dt.Rows[0].HasVersion(DataRowVersion.Proposed) , "DRV7");

			// check current value
			Assert.AreEqual("1-String1" , dt.Rows[0]["String1",DataRowVersion.Current ] , "DRV8");

			// check IsEdit after cancel edit
			Assert.AreEqual(false, drv.IsEdit , "DRV9");
		}

		[Test] public void CreateChildView_ByDataRelation()
		{
			//create a dataset with two tables, with a DataRelation between them
			DataTable dtParent = DataProvider.CreateParentDataTable();
			DataTable dtChild = DataProvider.CreateChildDataTable();
			DataSet ds = new DataSet();
			ds.Tables.Add(dtParent);
			ds.Tables.Add(dtChild);
			DataRelation drel = new DataRelation("ParentChild",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
			ds.Relations.Add(drel);

			//DataView dvChild = null;
			DataView dvParent = new DataView(dtParent);

			DataView dvTmp1 = dvParent[0].CreateChildView(drel);
			DataView dvTmp2 = dvParent[3].CreateChildView(drel);

			// ChildView != null
			Assert.AreEqual(true, dvTmp1!=null, "DRV10");

			// Child view table = ChildTable
			Assert.AreEqual(dtChild , dvTmp1.Table , "DRV11");

			// ChildView1.Table = ChildView2.Table
			Assert.AreEqual(dvTmp2.Table, dvTmp1.Table , "DRV12");

			//the child dataview are different
			// Child DataViews different 
			Assert.AreEqual(false, dvTmp1.Equals(dvTmp2), "DRV13");
		}

		[Test] public void CreateChildView_ByName()
		{
			//create a dataset with two tables, with a DataRelation between them
			DataTable dtParent = DataProvider.CreateParentDataTable();
			DataTable dtChild = DataProvider.CreateChildDataTable();
			DataSet ds = new DataSet();
			ds.Tables.Add(dtParent);
			ds.Tables.Add(dtChild);
			DataRelation drel = new DataRelation("ParentChild",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
			ds.Relations.Add(drel);

			//DataView dvChild = null;
			DataView dvParent = new DataView(dtParent);

			DataView dvTmp1 = dvParent[0].CreateChildView("ParentChild");
			DataView dvTmp2 = dvParent[3].CreateChildView("ParentChild");

			// ChildView != null
			Assert.AreEqual(true, dvTmp1!=null, "DRV14");

			// Child view table = ChildTable
			Assert.AreEqual(dtChild , dvTmp1.Table , "DRV15");

			// ChildView1.Table = ChildView2.Table
			Assert.AreEqual(dvTmp2.Table, dvTmp1.Table , "DRV16");

			//the child dataview are different
			// Child DataViews different 
			Assert.AreEqual(false, dvTmp1.Equals(dvTmp2), "DRV17");
		}

		[Test] public void DataView()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);

			DataRowView drv1 = dv[0];
			DataRowView drv2 = dv[4];

			// check DataRowView.DataView 
			Assert.AreEqual(dv, drv1.DataView , "DRV18");

			// compare DataRowView.DataView 
			Assert.AreEqual(drv2.DataView, drv1.DataView , "DRV19");

			//check that the DataRowView still has the same DataView even when the source table changed
			// check that the DataRowView still has the same DataView
			dv.Table = null;

			// Console.WriteLine("*********" + (drv1.DataView == null));
			Assert.AreEqual(true, drv1.DataView == dv , "DRV20");

			//check that the DataRowView has a new DataView
			// check that the DataRowView has a new DataView
			dv = new DataView();
			Assert.AreEqual(true, drv1.DataView.Equals(dv) , "DRV21");
		}

		[Test] public void Delete()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);

			DataRowView drv = dv[0];
			int TableRowsCount = dt.Rows.Count;
			int ViewRowCount = dv.Count;

			// DataView Count
			drv.Delete();
			Assert.AreEqual(dv.Count, ViewRowCount-1, "DRV22");

			//the table count should stay the same until EndEdit is invoked
			// Table Count
			Assert.AreEqual(TableRowsCount, dt.Rows.Count, "DRV23");

			// DataRowState deleted
			Assert.AreEqual(DataRowState.Deleted , drv.Row.RowState , "DRV24");
		}

		[Test] public void EndEdit()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);

			DataRowView drv = dv[0];

			drv.BeginEdit();
			drv["String1"] = "ChangeValue";

			//the row should be stay in edit mode event if changing other rows
			// check IsEdit - change another row
			dv[1]["String1"] = "something";
			Assert.AreEqual(true, drv.IsEdit , "DRV25");

			// check if has Proposed version
			drv.EndEdit();
			Assert.AreEqual(false, dt.Rows[0].HasVersion(DataRowVersion.Proposed) , "DRV26");

			// check Current value
			Assert.AreEqual("ChangeValue" , dt.Rows[0]["String1",DataRowVersion.Current] , "DRV27");

			// check IsEdit
			Assert.AreEqual(false, drv.IsEdit , "DRV28");
		}

		[Test] public void Equals()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);

			DataRowView d1 = dv[0];
			DataRowView d2 = dv[0];

			// DataRowView.Equals
			Assert.AreEqual(d2, d1, "DRV29");
		}

		[Test] public void IsEdit()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);

			DataRowView drv = dv[0];

			// default value
			Assert.AreEqual(false, drv.IsEdit, "DRV30"); 

			// after BeginEdit
			drv.BeginEdit();
			Assert.AreEqual(true, drv.IsEdit, "DRV31"); 

			// after CancelEdit
			drv.CancelEdit();
			Assert.AreEqual(false, drv.IsEdit, "DRV32"); 

			// after BeginEdit again
			drv.BeginEdit();
			Assert.AreEqual(true, drv.IsEdit, "DRV33"); 

			// after EndEdit 
			drv.EndEdit();
			Assert.AreEqual(false, drv.IsEdit, "DRV34"); 
		}

		[Test] public void IsNew()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);

			DataRowView drv = dv[0];

			// existing row
			Assert.AreEqual(false, drv.IsNew , "DRV35"); 

			// add new row
			drv = dv.AddNew();
			Assert.AreEqual(true, drv.IsNew , "DRV36"); 
		}

		[Test] public void Item()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);

			DataRowView drv = dv[0];

			// Item 0
			Assert.AreEqual(dt.Rows[0][0], drv[0], "DRV37"); 

			// Item 4
			Assert.AreEqual(dt.Rows[0][4], drv[4], "DRV38"); 

			// Item -1 - excpetion
			try {
				object o = drv[-1];
				Assert.Fail("DRV39: Indexer Failed to throw IndexOutOfRangeException");
			}
			catch (IndexOutOfRangeException) {}
			catch (AssertionException exc) {throw  exc;}
			catch (Exception exc)
			{
				Assert.Fail("DRV40: Indexer. Wrong exception type. Got:" + exc);
			}
		}

		[Test] public void Item_Property()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);

			DataRowView drv = dv[0];

			// Item 'ParentId'
			Assert.AreEqual(dt.Rows[0]["ParentId"], drv["ParentId"], "DRV41"); 

			// Item 'ParentDateTime'
			Assert.AreEqual(dt.Rows[0]["ParentDateTime"], drv["ParentDateTime"], "DRV42"); 

			// Item invalid - excpetion
			try {
				object o = drv["something"];
				Assert.Fail("DRV43: Indexer Failed to throw ArgumentException");
			}
			catch (ArgumentException) {}
			catch (AssertionException exc) {throw  exc;}
			catch (Exception exc)
			{
				Assert.Fail("DRV44: Indexer. Wrong exception type. Got:" + exc);
			}
		}

		[Test] public void Row()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);
			DataRowView drv = null;

			// Compare DataRowView.Row to table row
			drv = dv[3];
			Assert.AreEqual(dt.Rows[3], drv.Row, "DRV45"); 
		}

		[Test] public void RowVersion()
		{
			DataTable dt = DataProvider.CreateParentDataTable();
			DataView dv = new DataView(dt);
			dt.Columns[1].DefaultValue = "default";
			DataRowView drv = dv[0];

			dt.Rows.Add(new object[] {99});
			dt.Rows[1].Delete();
			dt.Rows[2].BeginEdit();
			dt.Rows[2][1] = "aaa";

			dv.RowStateFilter=DataViewRowState.CurrentRows ;
			// check Current
			Assert.AreEqual(DataRowVersion.Current, drv.RowVersion, "DRV46");

			dv.RowStateFilter=DataViewRowState.Deleted ;
			// check Original
			Assert.AreEqual(DataRowVersion.Current , drv.RowVersion, "DRV47");
		}
	}
}