**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Data / Test / System.Data / DataTableTest.cs
blob91cc0cf6cde807803e2b864caab45a5af28f750a
1 // DataTableTest.cs - NUnit Test Cases for testing the DataTable
2 //
3 // Authors:
4 // Franklin Wise (gracenote@earthlink.net)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 //
7 // (C) Franklin Wise
8 // (C) 2003 Martin Willemoes Hansen
9 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using NUnit.Framework;
35 using System;
36 using System.Data;
37 using System.Globalization;
38 using System.IO;
39 using System.Runtime.Serialization.Formatters.Binary;
40 using System.Xml;
42 namespace MonoTests.System.Data
44 [TestFixture]
45 public class DataTableTest : Assertion
47 [Test]
48 public void Ctor()
50 DataTable dt = new DataTable();
52 AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
53 Assert("Col",dt.Columns != null);
54 //Assert(dt.ChildRelations != null);
55 Assert("Const", dt.Constraints != null);
56 Assert("ds", dt.DataSet == null);
57 Assert("dv", dt.DefaultView != null);
58 Assert("de", dt.DisplayExpression == "");
59 Assert("ep", dt.ExtendedProperties != null);
60 Assert("he", dt.HasErrors == false);
61 Assert("lc", dt.Locale != null);
62 Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
63 Assert("ns", dt.Namespace == "");
64 //Assert(dt.ParentRelations != null);
65 Assert("pf", dt.Prefix == "");
66 Assert("pk", dt.PrimaryKey != null);
67 Assert("rows", dt.Rows != null);
68 Assert("Site", dt.Site == null);
69 Assert("tname", dt.TableName == "");
73 [Test]
74 public void Select ()
76 DataSet Set = new DataSet ();
77 DataTable Mom = new DataTable ("Mom");
78 DataTable Child = new DataTable ("Child");
79 Set.Tables.Add (Mom);
80 Set.Tables.Add (Child);
82 DataColumn Col = new DataColumn ("Name");
83 DataColumn Col2 = new DataColumn ("ChildName");
84 Mom.Columns.Add (Col);
85 Mom.Columns.Add (Col2);
87 DataColumn Col3 = new DataColumn ("Name");
88 DataColumn Col4 = new DataColumn ("Age");
89 Col4.DataType = Type.GetType ("System.Int16");
90 Child.Columns.Add (Col3);
91 Child.Columns.Add (Col4);
93 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
94 Set.Relations.Add (Relation);
96 DataRow Row = Mom.NewRow ();
97 Row [0] = "Laura";
98 Row [1] = "Nick";
99 Mom.Rows.Add (Row);
101 Row = Mom.NewRow ();
102 Row [0] = "Laura";
103 Row [1] = "Dick";
104 Mom.Rows.Add (Row);
106 Row = Mom.NewRow ();
107 Row [0] = "Laura";
108 Row [1] = "Mick";
109 Mom.Rows.Add (Row);
111 Row = Mom.NewRow ();
112 Row [0] = "Teresa";
113 Row [1] = "Jack";
114 Mom.Rows.Add (Row);
116 Row = Mom.NewRow ();
117 Row [0] = "Teresa";
118 Row [1] = "Mack";
119 Mom.Rows.Add (Row);
121 Row = Child.NewRow ();
122 Row [0] = "Nick";
123 Row [1] = 15;
124 Child.Rows.Add (Row);
126 Row = Child.NewRow ();
127 Row [0] = "Dick";
128 Row [1] = 25;
129 Child.Rows.Add (Row);
131 Row = Child.NewRow ();
132 Row [0] = "Mick";
133 Row [1] = 35;
134 Child.Rows.Add (Row);
136 Row = Child.NewRow ();
137 Row [0] = "Jack";
138 Row [1] = 10;
139 Child.Rows.Add (Row);
141 Row = Child.NewRow ();
142 Row [0] = "Mack";
143 Row [1] = 19;
144 Child.Rows.Add (Row);
146 Row = Child.NewRow ();
147 Row [0] = "Mack";
148 Row [1] = 99;
149 Child.Rows.Add (Row);
151 DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
152 AssertEquals ("test#01", 2, Rows.Length);
154 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
155 AssertEquals ("test#02", 0, Rows.Length);
157 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
158 AssertEquals ("test#03", 1, Rows.Length);
160 Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
161 AssertEquals ("test#04", "Mack", Rows [0] [1]);
163 Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
164 AssertEquals ("test#05", 5, Rows.Length);
166 Rows = Child.Select ("age = 20 - 1");
167 AssertEquals ("test#06", 1, Rows.Length);
169 Rows = Child.Select ("age <= 20");
170 AssertEquals ("test#07", 3, Rows.Length);
172 Rows = Child.Select ("age >= 20");
173 AssertEquals ("test#08", 3, Rows.Length);
175 Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
176 AssertEquals ("test#09", 2, Rows.Length);
178 Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
179 AssertEquals ("test#10", 1, Rows.Length);
180 AssertEquals ("test#11", "Mack", Rows [0] [0]);
182 Rows = Child.Select ("not (Name = 'Jack')");
183 AssertEquals ("test#12", 5, Rows.Length);
186 [Test]
187 public void Select2 ()
189 DataSet Set = new DataSet ();
190 DataTable Child = new DataTable ("Child");
192 Set.Tables.Add (Child);
194 DataColumn Col3 = new DataColumn ("Name");
195 DataColumn Col4 = new DataColumn ("Age");
196 Col4.DataType = Type.GetType ("System.Int16");
197 Child.Columns.Add (Col3);
198 Child.Columns.Add (Col4);
200 DataRow Row = Child.NewRow ();
201 Row [0] = "Nick";
202 Row [1] = 15;
203 Child.Rows.Add (Row);
205 Row = Child.NewRow ();
206 Row [0] = "Dick";
207 Row [1] = 25;
208 Child.Rows.Add (Row);
210 Row = Child.NewRow ();
211 Row [0] = "Mick";
212 Row [1] = 35;
213 Child.Rows.Add (Row);
215 Row = Child.NewRow ();
216 Row [0] = "Jack";
217 Row [1] = 10;
218 Child.Rows.Add (Row);
220 Row = Child.NewRow ();
221 Row [0] = "Mack";
222 Row [1] = 19;
223 Child.Rows.Add (Row);
225 Row = Child.NewRow ();
226 Row [0] = "Mack";
227 Row [1] = 99;
228 Child.Rows.Add (Row);
230 DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
231 AssertEquals ("test#01", 3, Rows.Length);
232 AssertEquals ("test#02", "Mack", Rows [0] [0]);
233 AssertEquals ("test#03", "Mick", Rows [1] [0]);
234 AssertEquals ("test#04", "Dick", Rows [2] [0]);
236 Rows = Child.Select ("age >= 20", "age asc");
237 AssertEquals ("test#05", 3, Rows.Length);
238 AssertEquals ("test#06", "Dick", Rows [0] [0]);
239 AssertEquals ("test#07", "Mick", Rows [1] [0]);
240 AssertEquals ("test#08", "Mack", Rows [2] [0]);
242 Rows = Child.Select ("age >= 20", "name asc");
243 AssertEquals ("test#09", 3, Rows.Length);
244 AssertEquals ("test#10", "Dick", Rows [0] [0]);
245 AssertEquals ("test#11", "Mack", Rows [1] [0]);
246 AssertEquals ("test#12", "Mick", Rows [2] [0]);
248 Rows = Child.Select ("age >= 20", "name desc");
249 AssertEquals ("test#09", 3, Rows.Length);
250 AssertEquals ("test#10", "Mick", Rows [0] [0]);
251 AssertEquals ("test#11", "Mack", Rows [1] [0]);
252 AssertEquals ("test#12", "Dick", Rows [2] [0]);
256 [Test]
257 public void SelectParsing ()
259 DataTable T = new DataTable ("test");
260 DataColumn C = new DataColumn ("name");
261 T.Columns.Add (C);
262 C = new DataColumn ("age");
263 C.DataType = typeof (int);
264 T.Columns.Add (C);
265 C = new DataColumn ("id");
266 T.Columns.Add (C);
268 DataSet Set = new DataSet ("TestSet");
269 Set.Tables.Add (T);
271 DataRow Row = null;
272 for (int i = 0; i < 100; i++) {
273 Row = T.NewRow ();
274 Row [0] = "human" + i;
275 Row [1] = i;
276 Row [2] = i;
277 T.Rows.Add (Row);
280 Row = T.NewRow ();
281 Row [0] = "h*an";
282 Row [1] = 1;
283 Row [2] = 1;
284 T.Rows.Add (Row);
286 AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
288 AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
290 try {
291 T.Select ("name = 1human ");
292 Fail ("test#03");
293 } catch (Exception e) {
295 // missing operand after 'human' operand
296 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
299 try {
300 T.Select ("name = 1");
301 Fail ("test#05");
302 } catch (Exception e) {
304 // Cannot perform '=' operation between string and Int32
305 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
308 AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
312 [Test]
313 public void SelectOperators ()
315 DataTable T = new DataTable ("test");
316 DataColumn C = new DataColumn ("name");
317 T.Columns.Add (C);
318 C = new DataColumn ("age");
319 C.DataType = typeof (int);
320 T.Columns.Add (C);
321 C = new DataColumn ("id");
322 T.Columns.Add (C);
324 DataSet Set = new DataSet ("TestSet");
325 Set.Tables.Add (T);
327 DataRow Row = null;
328 for (int i = 0; i < 100; i++) {
329 Row = T.NewRow ();
330 Row [0] = "human" + i;
331 Row [1] = i;
332 Row [2] = i;
333 T.Rows.Add (Row);
336 Row = T.NewRow ();
337 Row [0] = "h*an";
338 Row [1] = 1;
339 Row [2] = 1;
340 T.Rows.Add (Row);
342 AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
343 AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);
344 AssertEquals ("test#03", 12, T.Select ("age< =10").Length);
345 AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
346 AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);
347 AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
348 AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
349 AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
350 // FIXME: Somebody explain how this can be possible.
351 // it seems that it is no matter between 10 - 30. The
352 // result is allways 25 :-P
353 //AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
357 [Test]
358 public void SelectExceptions ()
360 DataTable T = new DataTable ("test");
361 DataColumn C = new DataColumn ("name");
362 T.Columns.Add (C);
363 C = new DataColumn ("age");
364 C.DataType = typeof (int);
365 T.Columns.Add (C);
366 C = new DataColumn ("id");
367 T.Columns.Add (C);
369 for (int i = 0; i < 100; i++) {
370 DataRow Row = T.NewRow ();
371 Row [0] = "human" + i;
372 Row [1] = i;
373 Row [2] = i;
374 T.Rows.Add (Row);
377 try {
378 T.Select ("name = human1");
379 Fail ("test#01");
380 } catch (Exception e) {
382 // column name human not found
383 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
386 AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
387 AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
389 try {
390 T.Select ("id = 1k3");
391 Fail ("test#06");
392 } catch (Exception e) {
394 // no operands after k3 operator
395 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
399 [Test]
400 public void SelectStringOperators ()
402 DataTable T = new DataTable ("test");
403 DataColumn C = new DataColumn ("name");
404 T.Columns.Add (C);
405 C = new DataColumn ("age");
406 C.DataType = typeof (int);
407 T.Columns.Add (C);
408 C = new DataColumn ("id");
409 T.Columns.Add (C);
411 DataSet Set = new DataSet ("TestSet");
412 Set.Tables.Add (T);
414 DataRow Row = null;
415 for (int i = 0; i < 100; i++) {
416 Row = T.NewRow ();
417 Row [0] = "human" + i;
418 Row [1] = i;
419 Row [2] = i;
420 T.Rows.Add (Row);
422 Row = T.NewRow ();
423 Row [0] = "h*an";
424 Row [1] = 1;
425 Row [2] = 1;
426 T.Rows.Add (Row);
428 AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
430 AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);
431 AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
432 AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);
433 AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
434 AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
436 AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
438 Set.CaseSensitive = true;
439 AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
441 T.CaseSensitive = false;
442 AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
444 T.CaseSensitive = true;
445 AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
447 Set.CaseSensitive = false;
448 AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
450 T.CaseSensitive = false;
451 AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
453 AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
454 AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
455 AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
457 try {
458 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
459 Fail ("test#16");
460 } catch (Exception e) {
462 // 'h*an1' is invalid
463 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
466 try {
467 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
468 Fail ("test#19");
469 } catch (Exception e) {
471 // 'h%an1' is invalid
472 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
475 AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
476 AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
480 [Test]
481 public void SelectAggregates ()
483 DataTable T = new DataTable ("test");
484 DataColumn C = new DataColumn ("name");
485 T.Columns.Add (C);
486 C = new DataColumn ("age");
487 C.DataType = typeof (int);
488 T.Columns.Add (C);
489 C = new DataColumn ("id");
490 T.Columns.Add (C);
491 DataRow Row = null;
493 for (int i = 0; i < 1000; i++) {
494 Row = T.NewRow ();
495 Row [0] = "human" + i;
496 Row [1] = i;
497 Row [2] = i;
498 T.Rows.Add (Row);
501 AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
502 AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
503 AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
504 AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
505 AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
506 AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
507 AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
510 [Test]
511 public void SelectFunctions ()
513 DataTable T = new DataTable ("test");
514 DataColumn C = new DataColumn ("name");
515 T.Columns.Add (C);
516 C = new DataColumn ("age");
517 C.DataType = typeof (int);
518 T.Columns.Add (C);
519 C = new DataColumn ("id");
520 T.Columns.Add (C);
521 DataRow Row = null;
523 for (int i = 0; i < 1000; i++) {
524 Row = T.NewRow ();
525 Row [0] = "human" + i;
526 Row [1] = i;
527 Row [2] = i;
528 T.Rows.Add (Row);
531 Row = T.NewRow ();
532 Row [0] = "human" + "test";
533 Row [1] = DBNull.Value;
534 Row [2] = DBNull.Value;
535 T.Rows.Add (Row);
537 //TODO: How to test Convert-function
538 AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);
539 AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
540 AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
541 AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);
542 AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);
543 AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
544 AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
545 AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
546 AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
547 AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
551 [Test]
552 public void SelectRelations ()
554 DataSet Set = new DataSet ();
555 DataTable Mom = new DataTable ("Mom");
556 DataTable Child = new DataTable ("Child");
558 Set.Tables.Add (Mom);
559 Set.Tables.Add (Child);
561 DataColumn Col = new DataColumn ("Name");
562 DataColumn Col2 = new DataColumn ("ChildName");
563 Mom.Columns.Add (Col);
564 Mom.Columns.Add (Col2);
566 DataColumn Col3 = new DataColumn ("Name");
567 DataColumn Col4 = new DataColumn ("Age");
568 Col4.DataType = Type.GetType ("System.Int16");
569 Child.Columns.Add (Col3);
570 Child.Columns.Add (Col4);
572 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
573 Set.Relations.Add (Relation);
575 DataRow Row = Mom.NewRow ();
576 Row [0] = "Laura";
577 Row [1] = "Nick";
578 Mom.Rows.Add (Row);
580 Row = Mom.NewRow ();
581 Row [0] = "Laura";
582 Row [1] = "Dick";
583 Mom.Rows.Add (Row);
585 Row = Mom.NewRow ();
586 Row [0] = "Laura";
587 Row [1] = "Mick";
588 Mom.Rows.Add (Row);
590 Row = Mom.NewRow ();
591 Row [0] = "Teresa";
592 Row [1] = "Jack";
593 Mom.Rows.Add (Row);
595 Row = Mom.NewRow ();
596 Row [0] = "Teresa";
597 Row [1] = "Mack";
598 Mom.Rows.Add (Row);
600 Row = Child.NewRow ();
601 Row [0] = "Nick";
602 Row [1] = 15;
603 Child.Rows.Add (Row);
605 Row = Child.NewRow ();
606 Row [0] = "Dick";
607 Row [1] = 25;
608 Child.Rows.Add (Row);
610 Row = Child.NewRow ();
611 Row [0] = "Mick";
612 Row [1] = 35;
613 Child.Rows.Add (Row);
615 Row = Child.NewRow ();
616 Row [0] = "Jack";
617 Row [1] = 10;
618 Child.Rows.Add (Row);
620 Row = Child.NewRow ();
621 Row [0] = "Mack";
622 Row [1] = 19;
623 Child.Rows.Add (Row);
625 Row = Child.NewRow ();
626 Row [0] = "Mack";
627 Row [1] = 99;
628 Child.Rows.Add (Row);
630 DataRow [] Rows = Child.Select ("name = Parent.Childname");
631 AssertEquals ("test#01", 6, Rows.Length);
632 Rows = Child.Select ("Parent.childname = 'Jack'");
633 AssertEquals ("test#02", 1, Rows.Length);
636 try {
637 // FIXME: LAMESPEC: Why the exception is thrown why... why...
638 Mom.Select ("Child.Name = 'Jack'");
639 Fail ("test#03");
640 } catch (Exception e) {
641 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
642 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
646 Rows = Child.Select ("Parent.name = 'Laura'");
647 AssertEquals ("test#06", 3, Rows.Length);
649 DataTable Parent2 = new DataTable ("Parent2");
650 Col = new DataColumn ("Name");
651 Col2 = new DataColumn ("ChildName");
653 Parent2.Columns.Add (Col);
654 Parent2.Columns.Add (Col2);
655 Set.Tables.Add (Parent2);
657 Row = Parent2.NewRow ();
658 Row [0] = "Laura";
659 Row [1] = "Nick";
660 Parent2.Rows.Add (Row);
662 Row = Parent2.NewRow ();
663 Row [0] = "Laura";
664 Row [1] = "Dick";
665 Parent2.Rows.Add (Row);
667 Row = Parent2.NewRow ();
668 Row [0] = "Laura";
669 Row [1] = "Mick";
670 Parent2.Rows.Add (Row);
672 Row = Parent2.NewRow ();
673 Row [0] = "Teresa";
674 Row [1] = "Jack";
675 Parent2.Rows.Add (Row);
677 Row = Parent2.NewRow ();
678 Row [0] = "Teresa";
679 Row [1] = "Mack";
680 Parent2.Rows.Add (Row);
682 Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
683 Set.Relations.Add (Relation);
685 try {
686 Rows = Child.Select ("Parent.ChildName = 'Jack'");
687 Fail ("test#07");
688 } catch (Exception e) {
689 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
690 //AssertEquals ("test#09", "The table [Child] involved in more than one relation. You must explicitly mention a relation name in the expression 'parent.[ChildName]'.", e.Message);
693 Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
694 AssertEquals ("test#10", 1, Rows.Length);
696 Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
697 AssertEquals ("test#10", 1, Rows.Length);
699 try {
700 Mom.Select ("Parent.name = 'John'");
701 } catch (Exception e) {
702 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
703 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
708 [Test]
709 public void ToStringTest()
711 DataTable dt = new DataTable();
712 dt.Columns.Add("Col1",typeof(int));
714 dt.TableName = "Mytable";
715 dt.DisplayExpression = "Col1";
718 string cmpr = dt.TableName + " + " + dt.DisplayExpression;
719 AssertEquals(cmpr,dt.ToString());
722 [Test]
723 public void PrimaryKey ()
725 DataTable dt = new DataTable ();
726 DataColumn Col = new DataColumn ();
727 Col.AllowDBNull = false;
728 Col.DataType = typeof (int);
729 dt.Columns.Add (Col);
730 dt.Columns.Add ();
731 dt.Columns.Add ();
732 dt.Columns.Add ();
734 AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
736 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
737 AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
738 AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
740 dt.PrimaryKey = null;
741 AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
743 Col = new DataColumn ("failed");
745 try {
746 dt.PrimaryKey = new DataColumn [] {Col};
747 Fail ("test#05");
748 } catch (Exception e) {
749 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
750 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
753 DataTable dt2 = new DataTable ();
754 dt2.Columns.Add ();
756 try {
757 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
758 Fail ("test#08");
759 } catch (Exception e) {
760 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
761 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
765 AssertEquals ("test#11", 0, dt.Constraints.Count);
767 dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
768 AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
769 AssertEquals ("test#13", 1, dt.Constraints.Count);
770 AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
771 AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
772 AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);
776 [Test]
777 public void PropertyExceptions ()
779 DataSet set = new DataSet ();
780 DataTable table = new DataTable ();
781 DataTable table1 = new DataTable ();
782 set.Tables.Add (table);
783 set.Tables.Add (table1);
785 DataColumn col = new DataColumn ();
786 col.ColumnName = "Id";
787 col.DataType = Type.GetType ("System.Int32");
788 table.Columns.Add (col);
789 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
790 table.Constraints.Add (uc);
791 table.CaseSensitive = false;
793 col = new DataColumn ();
794 col.ColumnName = "Name";
795 col.DataType = Type.GetType ("System.String");
796 table.Columns.Add (col);
798 col = new DataColumn ();
799 col.ColumnName = "Id";
800 col.DataType = Type.GetType ("System.Int32");
801 table1.Columns.Add (col);
802 col = new DataColumn ();
803 col.ColumnName = "Name";
804 col.DataType = Type.GetType ("System.String");
805 table1.Columns.Add (col);
807 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
808 set.Relations.Add (dr);
810 try {
811 table.CaseSensitive = true;
812 table1.CaseSensitive = true;
813 Fail ("#A01");
815 catch (Exception e) {
816 if (e.GetType () != typeof (AssertionException))
817 AssertEquals ("#A02", "Cannot change CaseSensitive or Locale property. This change would lead to at least one DataRelation or Constraint to have different Locale or CaseSensitive settings between its related tables.",e.Message);
818 else
819 Console.WriteLine (e);
821 try {
822 CultureInfo cultureInfo = new CultureInfo ("en-gb");
823 table.Locale = cultureInfo;
824 table1.Locale = cultureInfo;
825 Fail ("#A03");
827 catch (Exception e) {
828 if (e.GetType () != typeof (AssertionException))
829 AssertEquals ("#A04", "Cannot change CaseSensitive or Locale property. This change would lead to at least one DataRelation or Constraint to have different Locale or CaseSensitive settings between its related tables.",e.Message);
830 else
831 Console.WriteLine (e);
833 try {
834 table.Prefix = "Prefix#1";
835 Fail ("#A05");
837 catch (Exception e){
838 if (e.GetType () != typeof (AssertionException))
839 AssertEquals ("#A06", "Prefix 'Prefix#1' is not valid, because it contains special characters.",e.Message);
840 else
841 Console.WriteLine (e);
846 [Test]
847 public void GetErrors ()
849 DataTable table = new DataTable ();
851 DataColumn col = new DataColumn ();
852 col.ColumnName = "Id";
853 col.DataType = Type.GetType ("System.Int32");
854 table.Columns.Add (col);
856 col = new DataColumn ();
857 col.ColumnName = "Name";
858 col.DataType = Type.GetType ("System.String");
859 table.Columns.Add (col);
861 DataRow row = table.NewRow ();
862 row ["Id"] = 147;
863 row ["name"] = "Abc";
864 row.RowError = "Error#1";
865 table.Rows.Add (row);
867 AssertEquals ("#A01", 1, table.GetErrors ().Length);
868 AssertEquals ("#A02", "Error#1", (table.GetErrors ())[0].RowError);
870 [Test]
871 public void CloneCopyTest ()
873 DataTable table = new DataTable ();
874 table.TableName = "Table#1";
875 DataTable table1 = new DataTable ();
876 table1.TableName = "Table#2";
878 table.AcceptChanges ();
880 DataSet set = new DataSet ("Data Set#1");
881 set.DataSetName = "Dataset#1";
882 set.Tables.Add (table);
883 set.Tables.Add (table1);
885 DataColumn col = new DataColumn ();
886 col.ColumnName = "Id";
887 col.DataType = Type.GetType ("System.Int32");
888 table.Columns.Add (col);
889 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
890 table.Constraints.Add (uc);
892 col = new DataColumn ();
893 col.ColumnName = "Id";
894 col.DataType = Type.GetType ("System.Int32");
895 table1.Columns.Add (col);
897 col = new DataColumn ();
898 col.ColumnName = "Name";
899 col.DataType = Type.GetType ("System.String");
900 table.Columns.Add (col);
902 col = new DataColumn ();
903 col.ColumnName = "Name";
904 col.DataType = Type.GetType ("System.String");
905 table1.Columns.Add (col);
906 DataRow row = table.NewRow ();
907 row ["Id"] = 147;
908 row ["name"] = "Abc";
909 row.RowError = "Error#1";
910 table.Rows.Add (row);
912 row = table.NewRow ();
913 row ["Id"] = 47;
914 row ["name"] = "Efg";
915 table.Rows.Add (row);
916 table.AcceptChanges ();
918 table.CaseSensitive = true;
919 table1.CaseSensitive = true;
920 table.MinimumCapacity = 100;
921 table.Prefix = "PrefixNo:1";
922 table.Namespace = "Namespace#1";
923 table.DisplayExpression = "Id / Name + (Id * Id)";
924 DataColumn[] colArray = {table.Columns[0]};
925 table.PrimaryKey = colArray;
926 table.ExtendedProperties.Add ("TimeStamp", DateTime.Now);
927 #if NET_1_1 // This prevents further tests after .NET 1.1.
928 #else
929 CultureInfo cultureInfo = new CultureInfo ("en-gb");
930 table.Locale = cultureInfo;
931 #endif
933 row = table1.NewRow ();
934 row ["Name"] = "Abc";
935 row ["Id"] = 147;
936 table1.Rows.Add (row);
938 row = table1.NewRow ();
939 row ["Id"] = 47;
940 row ["Name"] = "Efg";
941 table1.Rows.Add (row);
943 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
944 set.Relations.Add (dr);
946 //Testing properties of clone
947 DataTable cloneTable = table.Clone ();
948 AssertEquals ("#A01",true ,cloneTable.CaseSensitive);
949 AssertEquals ("#A02", 0 , cloneTable.ChildRelations.Count);
950 AssertEquals ("#A03", 0 , cloneTable.ParentRelations.Count);
951 AssertEquals ("#A04", 2, cloneTable.Columns.Count);
952 AssertEquals ("#A05", 1, cloneTable.Constraints.Count);
953 AssertEquals ("#A06", "Id / Name + (Id * Id)", cloneTable.DisplayExpression);
954 AssertEquals ("#A07", 1 ,cloneTable.ExtendedProperties.Count);
955 AssertEquals ("#A08", false ,cloneTable.HasErrors);
956 #if NET_1_1
957 #else
958 AssertEquals ("#A09", 2057, cloneTable.Locale.LCID);
959 #endif
960 AssertEquals ("#A10", 100, cloneTable.MinimumCapacity);
961 AssertEquals ("#A11","Namespace#1", cloneTable.Namespace);
962 AssertEquals ("#A12", "PrefixNo:1",cloneTable.Prefix);
963 AssertEquals ("#A13", "Id", cloneTable.PrimaryKey[0].ColumnName);
964 AssertEquals ("#A14",0 , cloneTable.Rows.Count );
965 AssertEquals ("#A15", "Table#1", cloneTable.TableName);
967 //Testing properties of copy
968 DataTable copyTable = table.Copy ();
969 AssertEquals ("#A16",true ,copyTable.CaseSensitive);
970 AssertEquals ("#A17", 0 , copyTable.ChildRelations.Count);
971 AssertEquals ("#A18", 0 , copyTable.ParentRelations.Count);
972 AssertEquals ("#A19", 2, copyTable.Columns.Count);
973 AssertEquals ("#A20", 1, copyTable.Constraints.Count);
974 AssertEquals ("#A21", "Id / Name + (Id * Id)", copyTable.DisplayExpression);
975 AssertEquals ("#A22", 1 ,copyTable.ExtendedProperties.Count);
976 AssertEquals ("#A23", true ,copyTable.HasErrors);
977 #if NET_1_1
978 #else
979 AssertEquals ("#A24", 2057, copyTable.Locale.LCID);
980 #endif
981 AssertEquals ("#A25", 100, copyTable.MinimumCapacity);
982 AssertEquals ("#A26","Namespace#1", copyTable.Namespace);
983 AssertEquals ("#A27", "PrefixNo:1",copyTable.Prefix);
984 AssertEquals ("#A28", "Id", copyTable.PrimaryKey[0].ColumnName);
985 AssertEquals ("#A29", 2 , copyTable.Rows.Count );
986 AssertEquals ("#A30", "Table#1", copyTable.TableName);
989 [Test]
990 public void LoadDataException ()
992 DataTable table = new DataTable ();
993 DataColumn col = new DataColumn ();
994 col.ColumnName = "Id";
995 col.DataType = Type.GetType ("System.Int32");
996 col.DefaultValue = 47;
997 table.Columns.Add (col);
998 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
999 table.Constraints.Add (uc);
1001 col = new DataColumn ();
1002 col.ColumnName = "Name";
1003 col.DataType = Type.GetType ("System.String");
1004 col.DefaultValue = "Hello";
1005 table.Columns.Add (col);
1007 table.BeginLoadData();
1008 object[] row = {147, "Abc"};
1009 DataRow newRow = table.LoadDataRow (row, true);
1011 object[] row1 = {147, "Efg"};
1012 DataRow newRow1 = table.LoadDataRow (row1, true);
1014 object[] row2 = {143, "Hij"};
1015 DataRow newRow2 = table.LoadDataRow (row2, true);
1017 try {
1018 table.EndLoadData ();
1019 Fail ("#A01");
1021 catch (ConstraintException) {
1024 [Test]
1025 public void Changes () //To test GetChanges and RejectChanges
1027 DataTable table = new DataTable ();
1029 DataColumn col = new DataColumn ();
1030 col.ColumnName = "Id";
1031 col.DataType = Type.GetType ("System.Int32");
1032 table.Columns.Add (col);
1033 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1034 table.Constraints.Add (uc);
1036 col = new DataColumn ();
1037 col.ColumnName = "Name";
1038 col.DataType = Type.GetType ("System.String");
1039 table.Columns.Add (col);
1041 DataRow row = table.NewRow ();
1042 row ["Id"] = 147;
1043 row ["name"] = "Abc";
1044 table.Rows.Add (row);
1045 table.AcceptChanges ();
1047 row = table.NewRow ();
1048 row ["Id"] = 47;
1049 row ["name"] = "Efg";
1050 table.Rows.Add (row);
1052 //Testing GetChanges
1053 DataTable changesTable = table.GetChanges ();
1054 AssertEquals ("#A01", 1 ,changesTable.Rows.Count);
1055 AssertEquals ("#A02","Efg" ,changesTable.Rows[0]["Name"]);
1056 table.AcceptChanges ();
1057 changesTable = table.GetChanges ();
1058 try {
1059 int cnt = changesTable.Rows.Count;
1061 catch(Exception e) {
1062 if (e.GetType () != typeof (AssertionException))
1063 AssertEquals ("#A03",typeof(NullReferenceException) ,e.GetType ());
1064 else
1065 Console.WriteLine (e);
1068 //Testing RejectChanges
1069 row = table.NewRow ();
1070 row ["Id"] = 247;
1071 row ["name"] = "Hij";
1072 table.Rows.Add (row);
1074 (table.Rows [0])["Name"] = "AaBbCc";
1075 table.RejectChanges ();
1076 AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]);
1077 AssertEquals ("#A04", 2, table.Rows.Count);
1079 [Test]
1080 public void ImportRow ()
1082 DataTable table = new DataTable ();
1083 DataColumn col = new DataColumn ();
1084 col.ColumnName = "Id";
1085 col.DataType = Type.GetType ("System.Int32");
1086 table.Columns.Add (col);
1088 col = new DataColumn ();
1089 col.ColumnName = "Name";
1090 col.DataType = Type.GetType ("System.String");
1091 table.Columns.Add (col);
1093 DataRow row = table.NewRow ();
1094 row ["Id"] = 147;
1095 row ["name"] = "Abc";
1096 table.Rows.Add (row);
1097 table.AcceptChanges ();
1099 row = table.NewRow ();
1100 row ["Id"] = 47;
1101 row ["name"] = "Efg";
1102 table.Rows.Add (row);
1104 (table.Rows [0]) ["Name"] = "AaBbCc";
1106 table.ImportRow (table.Rows [0]);
1107 table.ImportRow (table.Rows [1]);
1109 AssertEquals ("#A01", 147, table.Rows [2]["Id"]);
1110 AssertEquals ("#A02", 47, table.Rows [3]["Id"]);
1111 AssertEquals ("#A03", DataRowState.Modified, table.Rows [2].RowState);
1112 AssertEquals ("#A04", DataRowState.Added, table.Rows [3].RowState);
1114 [Test]
1115 public void ClearReset () //To test Clear and Reset methods
1117 DataTable table = new DataTable ();
1118 DataTable table1 = new DataTable ();
1120 DataSet set = new DataSet ();
1121 set.Tables.Add (table);
1122 set.Tables.Add (table1);
1124 DataColumn col = new DataColumn ();
1125 col.ColumnName = "Id";
1126 col.DataType = Type.GetType ("System.Int32");
1127 table.Columns.Add (col);
1128 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1129 table.Constraints.Add (uc);
1130 table.CaseSensitive = false;
1132 col = new DataColumn ();
1133 col.ColumnName = "Name";
1134 col.DataType = Type.GetType ("System.String");
1135 table.Columns.Add (col);
1137 col = new DataColumn ();
1138 col.ColumnName = "Id";
1139 col.DataType = Type.GetType ("System.Int32");
1140 table1.Columns.Add (col);
1142 col = new DataColumn ();
1143 col.ColumnName = "Name";
1144 col.DataType = Type.GetType ("System.String");
1145 table1.Columns.Add (col);
1147 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
1148 set.Relations.Add (dr);
1150 DataRow row = table.NewRow ();
1151 row ["Id"] = 147;
1152 row ["name"] = "Roopa";
1153 table.Rows.Add (row);
1155 row = table.NewRow ();
1156 row ["Id"] = 47;
1157 row ["Name"] = "roopa";
1158 table.Rows.Add (row);
1160 try {
1161 table.Reset ();
1162 Fail ("#A01");
1164 catch (InvalidConstraintException) {
1166 try {
1167 table.Clear ();
1168 //#if NET_1_1
1169 //#else
1170 Fail ("#A03");
1171 //#endif
1173 catch (Exception e) {
1175 #if NET_1_1
1177 AssertEquals ("#A04", "Cannot clear table Parent because ForeignKeyConstraint DR enforces Child.", e.Message);
1178 AssertEquals ("#A11", typeof (InvalidConstraintException), e.GetType());
1179 #else
1180 if (e.GetType () != typeof (AssertionException)) {
1181 // FIXME: Don't depend on localizable error messages.
1182 AssertEquals ("#A04", "Cannot clear table Parent because ForeignKeyConstraint DR enforces Child.", e.Message);
1184 else throw e;
1185 #endif
1187 table1.Reset ();
1188 AssertEquals ("#A05", 0, table1.Rows.Count);
1189 AssertEquals ("#A06", 0, table1.Constraints.Count);
1190 AssertEquals ("#A07", 0, table1.ParentRelations.Count);
1192 table.Clear ();
1193 AssertEquals ("#A08", 0, table.Rows.Count);
1194 #if NET_1_1
1195 AssertEquals ("#A09", 1, table.Constraints.Count);
1196 #else
1197 AssertEquals ("#A09", 1, table.Constraints.Count);
1198 #endif
1199 AssertEquals ("#A10", 0, table.ChildRelations.Count);
1202 [Test]
1203 public void Serialize ()
1205 MemoryStream fs = new MemoryStream ();
1207 // Construct a BinaryFormatter and use it
1208 // to serialize the data to the stream.
1209 BinaryFormatter formatter = new BinaryFormatter();
1211 // Create an array with multiple elements refering to
1212 // the one Singleton object.
1213 DataTable dt = new DataTable();
1216 dt.Columns.Add(new DataColumn("Id", typeof(string)));
1217 dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
1218 dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
1219 dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
1220 dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
1222 DataRow loRowToAdd;
1223 loRowToAdd = dt.NewRow();
1224 loRowToAdd[0] = "a";
1225 loRowToAdd[1] = "b";
1226 loRowToAdd[2] = "c";
1227 loRowToAdd[3] = "d";
1228 loRowToAdd[4] = "e";
1230 dt.Rows.Add(loRowToAdd);
1232 DataTable[] dtarr = new DataTable[] {dt};
1234 // Serialize the array elements.
1235 formatter.Serialize(fs, dtarr);
1237 // Deserialize the array elements.
1238 fs.Position = 0;
1239 DataTable[] a2 = (DataTable[]) formatter.Deserialize(fs);
1241 DataSet ds = new DataSet();
1242 ds.Tables.Add(a2[0]);
1244 StringWriter sw = new StringWriter ();
1245 ds.WriteXml(sw);
1246 XmlDocument doc = new XmlDocument ();
1247 doc.LoadXml (sw.ToString ());
1248 AssertEquals (5, doc.DocumentElement.FirstChild.ChildNodes.Count);
1251 [Test]
1252 public void CloneSubClassTest()
1254 MyDataTable dt1 = new MyDataTable();
1255 MyDataTable dt = (MyDataTable)(dt1.Clone());
1256 AssertEquals("A#01",2,MyDataTable.count);
1262 public class MyDataTable:DataTable {
1264 public static int count = 0;
1266 public MyDataTable() {
1268 count++;