1 // DataTableTest.cs - NUnit Test Cases for testing the DataTable
4 // Franklin Wise (gracenote@earthlink.net)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
8 // (C) 2003 Martin Willemoes Hansen
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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
;
37 using System
.Globalization
;
39 using System
.Runtime
.Serialization
.Formatters
.Binary
;
42 namespace MonoTests
.System
.Data
45 public class DataTableTest
: Assertion
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
== "");
76 DataSet Set
= new DataSet ();
77 DataTable Mom
= new DataTable ("Mom");
78 DataTable Child
= new DataTable ("Child");
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 ();
121 Row
= Child
.NewRow ();
124 Child
.Rows
.Add (Row
);
126 Row
= Child
.NewRow ();
129 Child
.Rows
.Add (Row
);
131 Row
= Child
.NewRow ();
134 Child
.Rows
.Add (Row
);
136 Row
= Child
.NewRow ();
139 Child
.Rows
.Add (Row
);
141 Row
= Child
.NewRow ();
144 Child
.Rows
.Add (Row
);
146 Row
= Child
.NewRow ();
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
);
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 ();
203 Child
.Rows
.Add (Row
);
205 Row
= Child
.NewRow ();
208 Child
.Rows
.Add (Row
);
210 Row
= Child
.NewRow ();
213 Child
.Rows
.Add (Row
);
215 Row
= Child
.NewRow ();
218 Child
.Rows
.Add (Row
);
220 Row
= Child
.NewRow ();
223 Child
.Rows
.Add (Row
);
225 Row
= Child
.NewRow ();
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]);
257 public void SelectParsing ()
259 DataTable T
= new DataTable ("test");
260 DataColumn C
= new DataColumn ("name");
262 C
= new DataColumn ("age");
263 C
.DataType
= typeof (int);
265 C
= new DataColumn ("id");
268 DataSet Set
= new DataSet ("TestSet");
272 for (int i
= 0; i
< 100; i
++) {
274 Row
[0] = "human" + i
;
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
);
291 T
.Select ("name = 1human ");
293 } catch (Exception e
) {
295 // missing operand after 'human' operand
296 AssertEquals ("test#04", typeof (SyntaxErrorException
), e
.GetType ());
300 T
.Select ("name = 1");
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
);
313 public void SelectOperators ()
315 DataTable T
= new DataTable ("test");
316 DataColumn C
= new DataColumn ("name");
318 C
= new DataColumn ("age");
319 C
.DataType
= typeof (int);
321 C
= new DataColumn ("id");
324 DataSet Set
= new DataSet ("TestSet");
328 for (int i
= 0; i
< 100; i
++) {
330 Row
[0] = "human" + i
;
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);
358 public void SelectExceptions ()
360 DataTable T
= new DataTable ("test");
361 DataColumn C
= new DataColumn ("name");
363 C
= new DataColumn ("age");
364 C
.DataType
= typeof (int);
366 C
= new DataColumn ("id");
369 for (int i
= 0; i
< 100; i
++) {
370 DataRow Row
= T
.NewRow ();
371 Row
[0] = "human" + i
;
378 T
.Select ("name = human1");
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
);
390 T
.Select ("id = 1k3");
392 } catch (Exception e
) {
394 // no operands after k3 operator
395 AssertEquals ("test#07", typeof (SyntaxErrorException
), e
.GetType ());
400 public void SelectStringOperators ()
402 DataTable T
= new DataTable ("test");
403 DataColumn C
= new DataColumn ("name");
405 C
= new DataColumn ("age");
406 C
.DataType
= typeof (int);
408 C
= new DataColumn ("id");
411 DataSet Set
= new DataSet ("TestSet");
415 for (int i
= 0; i
< 100; i
++) {
417 Row
[0] = "human" + i
;
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
);
458 AssertEquals ("test#16", 11, T
.Select ("name like 'h*an1'").Length
);
460 } catch (Exception e
) {
462 // 'h*an1' is invalid
463 AssertEquals ("test#17", typeof (EvaluateException
), e
.GetType ());
467 AssertEquals ("test#18", 11, T
.Select ("name like 'h%an1'").Length
);
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
);
481 public void SelectAggregates ()
483 DataTable T
= new DataTable ("test");
484 DataColumn C
= new DataColumn ("name");
486 C
= new DataColumn ("age");
487 C
.DataType
= typeof (int);
489 C
= new DataColumn ("id");
493 for (int i
= 0; i
< 1000; i
++) {
495 Row
[0] = "human" + i
;
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
);
511 public void SelectFunctions ()
513 DataTable T
= new DataTable ("test");
514 DataColumn C
= new DataColumn ("name");
516 C
= new DataColumn ("age");
517 C
.DataType
= typeof (int);
519 C
= new DataColumn ("id");
523 for (int i
= 0; i
< 1000; i
++) {
525 Row
[0] = "human" + i
;
532 Row
[0] = "human" + "test";
533 Row
[1] = DBNull
.Value
;
534 Row
[2] = DBNull
.Value
;
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"]);
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 ();
600 Row
= Child
.NewRow ();
603 Child
.Rows
.Add (Row
);
605 Row
= Child
.NewRow ();
608 Child
.Rows
.Add (Row
);
610 Row
= Child
.NewRow ();
613 Child
.Rows
.Add (Row
);
615 Row
= Child
.NewRow ();
618 Child
.Rows
.Add (Row
);
620 Row
= Child
.NewRow ();
623 Child
.Rows
.Add (Row
);
625 Row
= Child
.NewRow ();
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
);
637 // FIXME: LAMESPEC: Why the exception is thrown why... why...
638 Mom.Select ("Child.Name = 'Jack'");
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 ();
660 Parent2
.Rows
.Add (Row
);
662 Row
= Parent2
.NewRow ();
665 Parent2
.Rows
.Add (Row
);
667 Row
= Parent2
.NewRow ();
670 Parent2
.Rows
.Add (Row
);
672 Row
= Parent2
.NewRow ();
675 Parent2
.Rows
.Add (Row
);
677 Row
= Parent2
.NewRow ();
680 Parent2
.Rows
.Add (Row
);
682 Relation
= new DataRelation ("Rel2", Parent2
.Columns
[1], Child
.Columns
[0]);
683 Set
.Relations
.Add (Relation
);
686 Rows
= Child
.Select ("Parent.ChildName = 'Jack'");
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
);
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
);
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());
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
);
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");
746 dt
.PrimaryKey
= new DataColumn
[] {Col}
;
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 ();
757 dt
.PrimaryKey
= new DataColumn
[] {dt2.Columns [0]}
;
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
);
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
);
811 table
.CaseSensitive
= true;
812 table1
.CaseSensitive
= true;
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
);
819 Console
.WriteLine (e
);
822 CultureInfo cultureInfo
= new CultureInfo ("en-gb");
823 table
.Locale
= cultureInfo
;
824 table1
.Locale
= cultureInfo
;
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
);
831 Console
.WriteLine (e
);
834 table
.Prefix
= "Prefix#1";
838 if (e
.GetType () != typeof (AssertionException
))
839 AssertEquals ("#A06", "Prefix 'Prefix#1' is not valid, because it contains special characters.",e
.Message
);
841 Console
.WriteLine (e
);
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 ();
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
);
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 ();
908 row
["name"] = "Abc";
909 row
.RowError
= "Error#1";
910 table
.Rows
.Add (row
);
912 row
= table
.NewRow ();
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.
929 CultureInfo cultureInfo
= new CultureInfo ("en-gb");
930 table
.Locale
= cultureInfo
;
933 row
= table1
.NewRow ();
934 row
["Name"] = "Abc";
936 table1
.Rows
.Add (row
);
938 row
= table1
.NewRow ();
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
);
958 AssertEquals ("#A09", 2057, cloneTable
.Locale
.LCID
);
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
);
979 AssertEquals ("#A24", 2057, copyTable
.Locale
.LCID
);
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
);
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);
1018 table
.EndLoadData ();
1021 catch (ConstraintException
) {
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 ();
1043 row
["name"] = "Abc";
1044 table
.Rows
.Add (row
);
1045 table
.AcceptChanges ();
1047 row
= table
.NewRow ();
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 ();
1059 int cnt
= changesTable
.Rows
.Count
;
1061 catch(Exception e
) {
1062 if (e
.GetType () != typeof (AssertionException
))
1063 AssertEquals ("#A03",typeof(NullReferenceException
) ,e
.GetType ());
1065 Console
.WriteLine (e
);
1068 //Testing RejectChanges
1069 row
= table
.NewRow ();
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
);
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 ();
1095 row
["name"] = "Abc";
1096 table
.Rows
.Add (row
);
1097 table
.AcceptChanges ();
1099 row
= table
.NewRow ();
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
);
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 ();
1152 row
["name"] = "Roopa";
1153 table
.Rows
.Add (row
);
1155 row
= table
.NewRow ();
1157 row
["Name"] = "roopa";
1158 table
.Rows
.Add (row
);
1164 catch (InvalidConstraintException
) {
1173 catch (Exception e
) {
1177 AssertEquals ("#A04", "Cannot clear table Parent because ForeignKeyConstraint DR enforces Child.", e
.Message
);
1178 AssertEquals ("#A11", typeof (InvalidConstraintException
), e
.GetType());
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
);
1188 AssertEquals ("#A05", 0, table1
.Rows
.Count
);
1189 AssertEquals ("#A06", 0, table1
.Constraints
.Count
);
1190 AssertEquals ("#A07", 0, table1
.ParentRelations
.Count
);
1193 AssertEquals ("#A08", 0, table
.Rows
.Count
);
1195 AssertEquals ("#A09", 1, table
.Constraints
.Count
);
1197 AssertEquals ("#A09", 1, table
.Constraints
.Count
);
1199 AssertEquals ("#A10", 0, table
.ChildRelations
.Count
);
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)));
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.
1239 DataTable
[] a2
= (DataTable
[]) formatter
.Deserialize(fs
);
1241 DataSet ds
= new DataSet();
1242 ds
.Tables
.Add(a2
[0]);
1244 StringWriter sw
= new StringWriter ();
1246 XmlDocument doc
= new XmlDocument ();
1247 doc
.LoadXml (sw
.ToString ());
1248 AssertEquals (5, doc
.DocumentElement
.FirstChild
.ChildNodes
.Count
);
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() {