* Removed AllTests.cs, System.Data/AllTests.cs,
[mono.git] / mcs / class / System.Data / Test / System.Data / DataRowTest.cs
1 // DataRowTest.cs - NUnit Test Cases for System.DataRow
2 //
3 // Authors:
4 //   Franklin Wise (gracenote@earthlink.net)
5 //   Daniel Morgan <danmorg@sc.rr.com>
6 //
7 // (C) Copyright 2002 Franklin Wise
8 // (C) Copyright 2003 Daniel Morgan
9 // (C) Copyright 2003 Martin Willemoes Hansen
10 // 
11
12 using NUnit.Framework;
13 using System;
14 using System.Data;
15
16 namespace MonoTests.System.Data
17 {
18         [TestFixture]
19         public class DataRowTest {
20         
21                 private DataTable _tbl; 
22
23                 [SetUp]
24                 public void GetReady() {
25                         _tbl = new DataTable();
26                 }
27
28                 // tests item at row, column in table to be DBNull.Value
29                 private void DBNullTest (string message, DataTable dt, int row, int column) 
30                 {
31                         object val = dt.Rows[row].ItemArray[column];
32                         Assertion.AssertEquals(message, DBNull.Value, val);
33                 }
34
35                 // tests item at row, column in table to be null
36                 private void NullTest (string message, DataTable dt, int row, int column) 
37                 {
38                         object val = dt.Rows[row].ItemArray[column];
39                         Assertion.AssertEquals(message, null, val);
40                 }
41
42                 // tests item at row, column in table to be 
43                 private void ValueTest (string message, DataTable dt, int row, int column, object value) 
44                 {
45                         object val = dt.Rows[row].ItemArray[column];
46                         Assertion.AssertEquals(message, value, val);
47                 }
48
49                 // test set null, DBNull.Value, and ItemArray short count
50                 [Test]
51                 public void NullInItemArray () 
52                 {
53                         string zero = "zero";
54                         string one = "one";
55                         string two = "two";
56
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)));
61
62                         object[] obj = new object[3];
63                         // -- normal -----------------
64                         obj[0] = zero;
65                         obj[1] = one;
66                         obj[2] = two;
67                         // results:
68                         //   table.Rows[0].ItemArray.ItemArray[0] = "zero"
69                         //   table.Rows[0].ItemArray.ItemArray[1] = "one"
70                         //   table.Rows[0].ItemArray.ItemArray[2] = "two"
71                         
72                         DataRow row = table.NewRow();
73                         
74                         try {
75                                 row.ItemArray = obj;
76                         }
77                         catch(Exception e1) {
78                                 Assertion.Fail("DR1: Exception Caught: " + e1);
79                         }
80                         
81                         table.Rows.Add(row);
82
83                         // -- null ----------
84                         obj[1] = null;
85                         // results:
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"
89
90                         row = table.NewRow();
91                         
92                         try {
93                                 row.ItemArray = obj;
94                         }
95                         catch(Exception e2) {
96                                 Assertion.Fail("DR2: Exception Caught: " + e2);
97                         }
98                         
99                         table.Rows.Add(row);
100
101                         // -- DBNull.Value -------------
102                         obj[1] = DBNull.Value;
103                         // results:
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"
107
108                         row = table.NewRow();
109                         
110                         try {
111                                 row.ItemArray = obj;
112                         }
113                         catch(Exception e3) {
114                                 Assertion.Fail("DR3: Exception Caught: " + e3);
115                         }
116                         
117                         table.Rows.Add(row);
118
119                         // -- object array smaller than number of columns -----
120                         string abc = "abc";
121                         string def = "def";
122                         obj = new object[2];
123                         obj[0] = abc;
124                         obj[1] = def;
125                         // results:
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;
129                         
130                         row = table.NewRow();
131                         
132                         try {
133                                 row.ItemArray = obj;
134                         }
135                         catch(Exception e3) {
136                                 Assertion.Fail("DR4: Exception Caught: " + e3);
137                         }
138                         
139                         table.Rows.Add(row);
140
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);
145
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);
150
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);
155
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);
160                 }
161         
162                 // test DefaultValue when setting ItemArray
163                 [Test]
164                 public void DefaultValueInItemArray () {                
165                         string zero = "zero";
166
167                         DataTable table = new DataTable();
168                         table.Columns.Add(new DataColumn("zero", typeof(string)));              
169                         
170                         DataColumn column = new DataColumn("num", typeof(int));
171                         column.DefaultValue = 15;
172                         table.Columns.Add(column);
173                         
174                         object[] obj = new object[2];
175                         // -- normal -----------------
176                         obj[0] = "zero";
177                         obj[1] = 8;
178                         // results:
179                         //   table.Rows[0].ItemArray.ItemArray[0] = "zero"
180                         //   table.Rows[0].ItemArray.ItemArray[1] = 8
181                                                 
182                         DataRow row = table.NewRow();
183                         
184                         try {
185                                 row.ItemArray = obj;
186                         }
187                         catch(Exception e1) {
188                                 Assertion.Fail("DR17: Exception Caught: " + e1);
189                         }
190                         
191                         table.Rows.Add(row);
192
193                         // -- null ----------
194                         obj[1] = null;
195                         // results:
196                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
197                         //   table.Rows[1].ItemArray.ItemArray[1] = 15
198                         
199                         row = table.NewRow();
200                         
201                         try {
202                                 row.ItemArray = obj;
203                         }
204                         catch(Exception e2) {
205                                 Assertion.Fail("DR18: Exception Caught: " + e2);
206                         }
207                         
208                         table.Rows.Add(row);
209
210                         // -- DBNull.Value -------------
211                         obj[1] = DBNull.Value;
212                         // results:
213                         //   table.Rows[2].ItemArray.ItemArray[0] = "zero"
214                         //   table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
215                         //      even though internally, the v
216                         
217                         row = table.NewRow();
218                         
219                         try {
220                                 row.ItemArray = obj;
221                         }
222                         catch(Exception e3) {
223                                 Assertion.Fail("DR19: Exception Caught: " + e3);
224                         }
225                         
226                         table.Rows.Add(row);
227
228                         // -- object array smaller than number of columns -----
229                         string abc = "abc";
230                         string def = "def";
231                         obj = new object[2];
232                         obj[0] = abc;
233                         // results:
234                         //   table.Rows[3].ItemArray.ItemArray[0] = "abc"
235                         //   table.Rows[3].ItemArray.ItemArray[1] = DBNull.Value
236                                                 
237                         row = table.NewRow();
238                         
239                         try {
240                                 row.ItemArray = obj;
241                         }
242                         catch(Exception e3) {
243                                 Assertion.Fail("DR20: Exception Caught: " + e3);
244                         }
245                         
246                         table.Rows.Add(row);
247
248                         // -- normal -----------------
249                         ValueTest("DR20: normal value test", table, 0, 0, zero);
250                         ValueTest("DR21: normal value test", table, 0, 1, 8);
251                         
252                         // -- null ----------
253                         ValueTest("DR22: null value test", table, 1, 0, zero);
254                         ValueTest("DR23: null value test", table, 1, 1, 15);
255                         
256                         // -- DBNull.Value -------------
257                         ValueTest("DR24: DBNull.Value value test", table, 2, 0, zero);
258                         DBNullTest("DR25: DBNull.Value value test", table, 2, 1);
259                         
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);
263                 }
264
265                 // test AutoIncrement when setting ItemArray
266                 [Test]
267                 public void AutoIncrementInItemArray () {
268                         string zero = "zero";
269                         string num = "num";
270                         
271                         DataTable table = new DataTable();
272                         table.Columns.Add(new DataColumn(zero, typeof(string)));                
273                         
274                         DataColumn column = new DataColumn("num", typeof(int));
275                         column.AutoIncrement = true;
276                         table.Columns.Add(column);
277                         
278                         object[] obj = new object[2];
279                         // -- normal -----------------
280                         obj[0] = "zero";
281                         obj[1] = 8;
282                         // results:
283                         //   table.Rows[0].ItemArray.ItemArray[0] = "zero"
284                         //   table.Rows[0].ItemArray.ItemArray[1] = 8
285                                                 
286                         DataRow row = table.NewRow();
287                         
288                         try {
289                                 row.ItemArray = obj;
290                         }
291                         catch(Exception e1) {
292                                 Assertion.Fail("DR28:  Exception Caught: " + e1);
293                         }
294                         
295                         table.Rows.Add(row);
296
297                         // -- null 1----------
298                         obj[1] = null;
299                         // results:
300                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
301                         //   table.Rows[1].ItemArray.ItemArray[1] = 9
302                         
303                         row = table.NewRow();
304                         
305                         try {
306                                 row.ItemArray = obj;
307                         }
308                         catch(Exception e2) {
309                                 Assertion.Fail("DR29:  Exception Caught: " + e2);
310                         }
311                         
312                         table.Rows.Add(row);
313
314                         // -- null 2----------
315                         obj[1] = null;
316                         // results:
317                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
318                         //   table.Rows[1].ItemArray.ItemArray[1] = 10
319                         
320                         row = table.NewRow();
321                         
322                         try {
323                                 row.ItemArray = obj;
324                         }
325                         catch(Exception e2) {
326                                 Assertion.Fail("DR30: Exception Caught: " + e2);
327                         }
328                         
329                         table.Rows.Add(row);
330
331                         // -- null 3----------
332                         obj[1] = null;
333                         // results:
334                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
335                         //   table.Rows[1].ItemArray.ItemArray[1] = 11
336                         
337                         row = table.NewRow();
338                         
339                         try {
340                                 row.ItemArray = obj;
341                         }
342                         catch(Exception e2) {
343                                 Assertion.Fail("DR31: Exception Caught: " + e2);
344                         }
345                         
346                         table.Rows.Add(row);
347
348                         // -- DBNull.Value -------------
349                         obj[1] = DBNull.Value;
350                         // results:
351                         //   table.Rows[2].ItemArray.ItemArray[0] = "zero"
352                         //   table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
353                         //      even though internally, the AutoIncrement value
354                         //      is incremented
355                         
356                         row = table.NewRow();
357                         
358                         try {
359                                 row.ItemArray = obj;
360                         }
361                         catch(Exception e3) {
362                                 Assertion.Fail("DR32: Exception Caught: " + e3);
363                         }
364                         
365                         table.Rows.Add(row);
366
367                         // -- null 4----------
368                         obj[1] = null;
369                         // results:
370                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
371                         //   table.Rows[1].ItemArray.ItemArray[1] = 13
372                         
373                         row = table.NewRow();
374                         
375                         try {
376                                 row.ItemArray = obj;
377                         }
378                         catch(Exception e2) {
379                                 Assertion.Fail("DR48: Exception Caught: " + e2);
380                         }
381                         
382                         table.Rows.Add(row);
383
384                         // -- object array smaller than number of columns -----
385                         string abc = "abc";
386                         string def = "def";
387                         obj = new object[2];
388                         obj[0] = abc;
389                         // results:
390                         //   table.Rows[3].ItemArray.ItemArray[0] = "abc"
391                         //   table.Rows[3].ItemArray.ItemArray[1] = 14
392                                                 
393                         row = table.NewRow();
394                         
395                         try {
396                                 row.ItemArray = obj;
397                         }
398                         catch(Exception e3) {
399                                 Assertion.Fail("DR33: Exception Caught: " + e3);
400                         }
401                         
402                         table.Rows.Add(row);
403
404                         // -- normal -----------------
405                         ValueTest("DR34: normal value test", table, 0, 0, zero);
406                         ValueTest("DR35: normal value test", table, 0, 1, 8);
407                         
408                         // -- null 1----------
409                         ValueTest("DR36: null value test", table, 1, 0, zero);
410                         ValueTest("DR37: null value test", table, 1, 1, 9);
411
412                         // -- null 2----------
413                         ValueTest("DR38: null value test", table, 2, 0, zero);
414                         ValueTest("DR39: null value test", table, 2, 1, 10);
415
416                         // -- null 3----------
417                         ValueTest("DR40: null value test", table, 3, 0, zero);
418                         ValueTest("DR41: null value test", table, 3, 1, 11);
419
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);
423
424                         // -- null 4----------
425                         ValueTest("DR44: null value test", table, 5, 0, zero);
426                         ValueTest("DR45: null value test", table, 5, 1, 13);
427
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);
431                 }
432         }
433 }