1 // DataRowTest.cs - NUnit Test Cases for System.DataRow
4 // Franklin Wise (gracenote@earthlink.net)
5 // Daniel Morgan <danmorg@sc.rr.com>
7 // (C) Copyright 2002 Franklin Wise
8 // (C) Copyright 2003 Daniel Morgan
9 // (C) Copyright 2003 Martin Willemoes Hansen
12 using NUnit.Framework;
16 namespace MonoTests.System.Data
19 public class DataRowTest {
21 private DataTable _tbl;
24 public void GetReady() {
25 _tbl = new DataTable();
28 // tests item at row, column in table to be DBNull.Value
29 private void DBNullTest (string message, DataTable dt, int row, int column)
31 object val = dt.Rows[row].ItemArray[column];
32 Assertion.AssertEquals(message, DBNull.Value, val);
35 // tests item at row, column in table to be null
36 private void NullTest (string message, DataTable dt, int row, int column)
38 object val = dt.Rows[row].ItemArray[column];
39 Assertion.AssertEquals(message, null, val);
42 // tests item at row, column in table to be
43 private void ValueTest (string message, DataTable dt, int row, int column, object value)
45 object val = dt.Rows[row].ItemArray[column];
46 Assertion.AssertEquals(message, value, val);
49 // test set null, DBNull.Value, and ItemArray short count
51 public void NullInItemArray ()
57 DataTable table = new DataTable();
58 table.Columns.Add(new DataColumn(zero, typeof(string)));
59 table.Columns.Add(new DataColumn(one, typeof(string)));
60 table.Columns.Add(new DataColumn(two, typeof(string)));
62 object[] obj = new object[3];
63 // -- normal -----------------
68 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
69 // table.Rows[0].ItemArray.ItemArray[1] = "one"
70 // table.Rows[0].ItemArray.ItemArray[2] = "two"
72 DataRow row = table.NewRow();
78 Assertion.Fail("DR1: Exception Caught: " + e1);
86 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
87 // table.Rows[1].ItemArray.ItemArray[1] = DBNull.Value
88 // table.Rows[1].ItemArray.ItemArray[2] = "two"
96 Assertion.Fail("DR2: Exception Caught: " + e2);
101 // -- DBNull.Value -------------
102 obj[1] = DBNull.Value;
104 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
105 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
106 // table.Rows[2].ItemArray.ItemArray[2] = "two"
108 row = table.NewRow();
113 catch(Exception e3) {
114 Assertion.Fail("DR3: Exception Caught: " + e3);
119 // -- object array smaller than number of columns -----
126 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
127 // table.Rows[3].ItemArray.ItemArray[1] = "def"
128 // table.Rows[3].ItemArray.ItemArray[2] = DBNull.Value;
130 row = table.NewRow();
135 catch(Exception e3) {
136 Assertion.Fail("DR4: Exception Caught: " + e3);
141 // -- normal -----------------
142 ValueTest("DR5: normal value test", table, 0, 0, zero);
143 ValueTest("DR6: normal value test", table, 0, 1, one);
144 ValueTest("DR7: normal value test", table, 0, 2, two);
146 // -- null ----------
147 ValueTest("DR8: null value test", table, 1, 0, zero);
148 ValueTest("DR9: null value test", table, 1, 1, DBNull.Value);
149 ValueTest("DR10: null value test", table, 1, 2, two);
151 // -- DBNull.Value -------------
152 ValueTest("DR11: DBNull.Value value test", table, 2, 0, zero);
153 ValueTest("DR12: DBNull.Value value test", table, 2, 1, DBNull.Value);
154 ValueTest("DR13: DBNull.Value value test", table, 2, 2, two);
156 // -- object array smaller than number of columns -----
157 ValueTest("DR14: array smaller value test", table, 3, 0, abc);
158 ValueTest("DR15: array smaller value test", table, 3, 1, def);
159 ValueTest("DR16: array smaller value test", table, 3, 2, DBNull.Value);
162 // test DefaultValue when setting ItemArray
164 public void DefaultValueInItemArray () {
165 string zero = "zero";
167 DataTable table = new DataTable();
168 table.Columns.Add(new DataColumn("zero", typeof(string)));
170 DataColumn column = new DataColumn("num", typeof(int));
171 column.DefaultValue = 15;
172 table.Columns.Add(column);
174 object[] obj = new object[2];
175 // -- normal -----------------
179 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
180 // table.Rows[0].ItemArray.ItemArray[1] = 8
182 DataRow row = table.NewRow();
187 catch(Exception e1) {
188 Assertion.Fail("DR17: Exception Caught: " + e1);
193 // -- null ----------
196 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
197 // table.Rows[1].ItemArray.ItemArray[1] = 15
199 row = table.NewRow();
204 catch(Exception e2) {
205 Assertion.Fail("DR18: Exception Caught: " + e2);
210 // -- DBNull.Value -------------
211 obj[1] = DBNull.Value;
213 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
214 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
215 // even though internally, the v
217 row = table.NewRow();
222 catch(Exception e3) {
223 Assertion.Fail("DR19: Exception Caught: " + e3);
228 // -- object array smaller than number of columns -----
234 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
235 // table.Rows[3].ItemArray.ItemArray[1] = DBNull.Value
237 row = table.NewRow();
242 catch(Exception e3) {
243 Assertion.Fail("DR20: Exception Caught: " + e3);
248 // -- normal -----------------
249 ValueTest("DR20: normal value test", table, 0, 0, zero);
250 ValueTest("DR21: normal value test", table, 0, 1, 8);
252 // -- null ----------
253 ValueTest("DR22: null value test", table, 1, 0, zero);
254 ValueTest("DR23: null value test", table, 1, 1, 15);
256 // -- DBNull.Value -------------
257 ValueTest("DR24: DBNull.Value value test", table, 2, 0, zero);
258 DBNullTest("DR25: DBNull.Value value test", table, 2, 1);
260 // -- object array smaller than number of columns -----
261 ValueTest("DR26: array smaller value test", table, 3, 0, abc);
262 ValueTest("DR27: array smaller value test", table, 3, 1, 15);
265 // test AutoIncrement when setting ItemArray
267 public void AutoIncrementInItemArray () {
268 string zero = "zero";
271 DataTable table = new DataTable();
272 table.Columns.Add(new DataColumn(zero, typeof(string)));
274 DataColumn column = new DataColumn("num", typeof(int));
275 column.AutoIncrement = true;
276 table.Columns.Add(column);
278 object[] obj = new object[2];
279 // -- normal -----------------
283 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
284 // table.Rows[0].ItemArray.ItemArray[1] = 8
286 DataRow row = table.NewRow();
291 catch(Exception e1) {
292 Assertion.Fail("DR28: Exception Caught: " + e1);
297 // -- null 1----------
300 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
301 // table.Rows[1].ItemArray.ItemArray[1] = 9
303 row = table.NewRow();
308 catch(Exception e2) {
309 Assertion.Fail("DR29: Exception Caught: " + e2);
314 // -- null 2----------
317 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
318 // table.Rows[1].ItemArray.ItemArray[1] = 10
320 row = table.NewRow();
325 catch(Exception e2) {
326 Assertion.Fail("DR30: Exception Caught: " + e2);
331 // -- null 3----------
334 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
335 // table.Rows[1].ItemArray.ItemArray[1] = 11
337 row = table.NewRow();
342 catch(Exception e2) {
343 Assertion.Fail("DR31: Exception Caught: " + e2);
348 // -- DBNull.Value -------------
349 obj[1] = DBNull.Value;
351 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
352 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
353 // even though internally, the AutoIncrement value
356 row = table.NewRow();
361 catch(Exception e3) {
362 Assertion.Fail("DR32: Exception Caught: " + e3);
367 // -- null 4----------
370 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
371 // table.Rows[1].ItemArray.ItemArray[1] = 13
373 row = table.NewRow();
378 catch(Exception e2) {
379 Assertion.Fail("DR48: Exception Caught: " + e2);
384 // -- object array smaller than number of columns -----
390 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
391 // table.Rows[3].ItemArray.ItemArray[1] = 14
393 row = table.NewRow();
398 catch(Exception e3) {
399 Assertion.Fail("DR33: Exception Caught: " + e3);
404 // -- normal -----------------
405 ValueTest("DR34: normal value test", table, 0, 0, zero);
406 ValueTest("DR35: normal value test", table, 0, 1, 8);
408 // -- null 1----------
409 ValueTest("DR36: null value test", table, 1, 0, zero);
410 ValueTest("DR37: null value test", table, 1, 1, 9);
412 // -- null 2----------
413 ValueTest("DR38: null value test", table, 2, 0, zero);
414 ValueTest("DR39: null value test", table, 2, 1, 10);
416 // -- null 3----------
417 ValueTest("DR40: null value test", table, 3, 0, zero);
418 ValueTest("DR41: null value test", table, 3, 1, 11);
420 // -- DBNull.Value -------------
421 ValueTest("DR42: DBNull.Value value test", table, 4, 0, zero);
422 ValueTest("DR43: DBNull.Value value test", table, 4, 1, DBNull.Value);
424 // -- null 4----------
425 ValueTest("DR44: null value test", table, 5, 0, zero);
426 ValueTest("DR45: null value test", table, 5, 1, 13);
428 // -- object array smaller than number of columns -----
429 ValueTest("DR46: array smaller value test", table, 6, 0, abc);
430 ValueTest("DR47: array smaller value test", table, 6, 1, 14);