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
11 using NUnit.Framework;
14 using System.Globalization;
16 using System.Runtime.Serialization.Formatters.Binary;
19 namespace MonoTests.System.Data
22 public class DataTableTest : Assertion
27 DataTable dt = new DataTable();
29 AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
30 Assert("Col",dt.Columns != null);
31 //Assert(dt.ChildRelations != null);
32 Assert("Const", dt.Constraints != null);
33 Assert("ds", dt.DataSet == null);
34 Assert("dv", dt.DefaultView != null);
35 Assert("de", dt.DisplayExpression == "");
36 Assert("ep", dt.ExtendedProperties != null);
37 Assert("he", dt.HasErrors == false);
38 Assert("lc", dt.Locale != null);
39 Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
40 Assert("ns", dt.Namespace == "");
41 //Assert(dt.ParentRelations != null);
42 Assert("pf", dt.Prefix == "");
43 Assert("pk", dt.PrimaryKey != null);
44 Assert("rows", dt.Rows != null);
45 Assert("Site", dt.Site == null);
46 Assert("tname", dt.TableName == "");
53 DataSet Set = new DataSet ();
54 DataTable Mom = new DataTable ("Mom");
55 DataTable Child = new DataTable ("Child");
57 Set.Tables.Add (Child);
59 DataColumn Col = new DataColumn ("Name");
60 DataColumn Col2 = new DataColumn ("ChildName");
61 Mom.Columns.Add (Col);
62 Mom.Columns.Add (Col2);
64 DataColumn Col3 = new DataColumn ("Name");
65 DataColumn Col4 = new DataColumn ("Age");
66 Col4.DataType = Type.GetType ("System.Int16");
67 Child.Columns.Add (Col3);
68 Child.Columns.Add (Col4);
70 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
71 Set.Relations.Add (Relation);
73 DataRow Row = Mom.NewRow ();
98 Row = Child.NewRow ();
101 Child.Rows.Add (Row);
103 Row = Child.NewRow ();
106 Child.Rows.Add (Row);
108 Row = Child.NewRow ();
111 Child.Rows.Add (Row);
113 Row = Child.NewRow ();
116 Child.Rows.Add (Row);
118 Row = Child.NewRow ();
121 Child.Rows.Add (Row);
123 Row = Child.NewRow ();
126 Child.Rows.Add (Row);
128 DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
129 AssertEquals ("test#01", 2, Rows.Length);
131 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
132 AssertEquals ("test#02", 0, Rows.Length);
134 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
135 AssertEquals ("test#03", 1, Rows.Length);
137 Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
138 AssertEquals ("test#04", "Mack", Rows [0] [1]);
140 Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
141 AssertEquals ("test#05", 5, Rows.Length);
143 Rows = Child.Select ("age = 20 - 1");
144 AssertEquals ("test#06", 1, Rows.Length);
146 Rows = Child.Select ("age <= 20");
147 AssertEquals ("test#07", 3, Rows.Length);
149 Rows = Child.Select ("age >= 20");
150 AssertEquals ("test#08", 3, Rows.Length);
152 Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
153 AssertEquals ("test#09", 2, Rows.Length);
155 Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
156 AssertEquals ("test#10", 1, Rows.Length);
157 AssertEquals ("test#11", "Mack", Rows [0] [0]);
159 Rows = Child.Select ("not (Name = 'Jack')");
160 AssertEquals ("test#12", 5, Rows.Length);
164 public void Select2 ()
166 DataSet Set = new DataSet ();
167 DataTable Child = new DataTable ("Child");
169 Set.Tables.Add (Child);
171 DataColumn Col3 = new DataColumn ("Name");
172 DataColumn Col4 = new DataColumn ("Age");
173 Col4.DataType = Type.GetType ("System.Int16");
174 Child.Columns.Add (Col3);
175 Child.Columns.Add (Col4);
177 DataRow Row = Child.NewRow ();
180 Child.Rows.Add (Row);
182 Row = Child.NewRow ();
185 Child.Rows.Add (Row);
187 Row = Child.NewRow ();
190 Child.Rows.Add (Row);
192 Row = Child.NewRow ();
195 Child.Rows.Add (Row);
197 Row = Child.NewRow ();
200 Child.Rows.Add (Row);
202 Row = Child.NewRow ();
205 Child.Rows.Add (Row);
207 DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
208 AssertEquals ("test#01", 3, Rows.Length);
209 AssertEquals ("test#02", "Mack", Rows [0] [0]);
210 AssertEquals ("test#03", "Mick", Rows [1] [0]);
211 AssertEquals ("test#04", "Dick", Rows [2] [0]);
213 Rows = Child.Select ("age >= 20", "age asc");
214 AssertEquals ("test#05", 3, Rows.Length);
215 AssertEquals ("test#06", "Dick", Rows [0] [0]);
216 AssertEquals ("test#07", "Mick", Rows [1] [0]);
217 AssertEquals ("test#08", "Mack", Rows [2] [0]);
219 Rows = Child.Select ("age >= 20", "name asc");
220 AssertEquals ("test#09", 3, Rows.Length);
221 AssertEquals ("test#10", "Dick", Rows [0] [0]);
222 AssertEquals ("test#11", "Mack", Rows [1] [0]);
223 AssertEquals ("test#12", "Mick", Rows [2] [0]);
225 Rows = Child.Select ("age >= 20", "name desc");
226 AssertEquals ("test#09", 3, Rows.Length);
227 AssertEquals ("test#10", "Mick", Rows [0] [0]);
228 AssertEquals ("test#11", "Mack", Rows [1] [0]);
229 AssertEquals ("test#12", "Dick", Rows [2] [0]);
234 public void SelectParsing ()
236 DataTable T = new DataTable ("test");
237 DataColumn C = new DataColumn ("name");
239 C = new DataColumn ("age");
240 C.DataType = typeof (int);
242 C = new DataColumn ("id");
245 DataSet Set = new DataSet ("TestSet");
249 for (int i = 0; i < 100; i++) {
251 Row [0] = "human" + i;
263 AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
265 AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
268 T.Select ("name = 1human ");
270 } catch (Exception e) {
272 // missing operand after 'human' operand
273 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
277 T.Select ("name = 1");
279 } catch (Exception e) {
281 // Cannot perform '=' operation between string and Int32
282 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
285 AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
290 public void SelectOperators ()
292 DataTable T = new DataTable ("test");
293 DataColumn C = new DataColumn ("name");
295 C = new DataColumn ("age");
296 C.DataType = typeof (int);
298 C = new DataColumn ("id");
301 DataSet Set = new DataSet ("TestSet");
305 for (int i = 0; i < 100; i++) {
307 Row [0] = "human" + i;
319 AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
320 AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);
321 AssertEquals ("test#03", 12, T.Select ("age< =10").Length);
322 AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
323 AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);
324 AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
325 AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
326 AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
327 // FIXME: Somebody explain how this can be possible.
328 // it seems that it is no matter between 10 - 30. The
329 // result is allways 25 :-P
330 //AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
335 public void SelectExceptions ()
337 DataTable T = new DataTable ("test");
338 DataColumn C = new DataColumn ("name");
340 C = new DataColumn ("age");
341 C.DataType = typeof (int);
343 C = new DataColumn ("id");
346 for (int i = 0; i < 100; i++) {
347 DataRow Row = T.NewRow ();
348 Row [0] = "human" + i;
355 T.Select ("name = human1");
357 } catch (Exception e) {
359 // column name human not found
360 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
363 AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
364 AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
367 T.Select ("id = 1k3");
369 } catch (Exception e) {
371 // no operands after k3 operator
372 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
377 public void SelectStringOperators ()
379 DataTable T = new DataTable ("test");
380 DataColumn C = new DataColumn ("name");
382 C = new DataColumn ("age");
383 C.DataType = typeof (int);
385 C = new DataColumn ("id");
388 DataSet Set = new DataSet ("TestSet");
392 for (int i = 0; i < 100; i++) {
394 Row [0] = "human" + i;
405 AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
407 AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);
408 AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
409 AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);
410 AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
411 AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
413 AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
415 Set.CaseSensitive = true;
416 AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
418 T.CaseSensitive = false;
419 AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
421 T.CaseSensitive = true;
422 AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
424 Set.CaseSensitive = false;
425 AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
427 T.CaseSensitive = false;
428 AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
430 AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
431 AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
432 AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
435 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
437 } catch (Exception e) {
439 // 'h*an1' is invalid
440 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
444 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
446 } catch (Exception e) {
448 // 'h%an1' is invalid
449 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
452 AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
453 AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
458 public void SelectAggregates ()
460 DataTable T = new DataTable ("test");
461 DataColumn C = new DataColumn ("name");
463 C = new DataColumn ("age");
464 C.DataType = typeof (int);
466 C = new DataColumn ("id");
470 for (int i = 0; i < 1000; i++) {
472 Row [0] = "human" + i;
478 AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
479 AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
480 AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
481 AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
482 AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
483 AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
484 AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
488 public void SelectFunctions ()
490 DataTable T = new DataTable ("test");
491 DataColumn C = new DataColumn ("name");
493 C = new DataColumn ("age");
494 C.DataType = typeof (int);
496 C = new DataColumn ("id");
500 for (int i = 0; i < 1000; i++) {
502 Row [0] = "human" + i;
509 Row [0] = "human" + "test";
510 Row [1] = DBNull.Value;
511 Row [2] = DBNull.Value;
514 //TODO: How to test Convert-function
515 AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);
516 AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
517 AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
518 AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);
519 AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);
520 AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
521 AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
522 AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
523 AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
524 AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
529 public void SelectRelations ()
531 DataSet Set = new DataSet ();
532 DataTable Mom = new DataTable ("Mom");
533 DataTable Child = new DataTable ("Child");
535 Set.Tables.Add (Mom);
536 Set.Tables.Add (Child);
538 DataColumn Col = new DataColumn ("Name");
539 DataColumn Col2 = new DataColumn ("ChildName");
540 Mom.Columns.Add (Col);
541 Mom.Columns.Add (Col2);
543 DataColumn Col3 = new DataColumn ("Name");
544 DataColumn Col4 = new DataColumn ("Age");
545 Col4.DataType = Type.GetType ("System.Int16");
546 Child.Columns.Add (Col3);
547 Child.Columns.Add (Col4);
549 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
550 Set.Relations.Add (Relation);
552 DataRow Row = Mom.NewRow ();
577 Row = Child.NewRow ();
580 Child.Rows.Add (Row);
582 Row = Child.NewRow ();
585 Child.Rows.Add (Row);
587 Row = Child.NewRow ();
590 Child.Rows.Add (Row);
592 Row = Child.NewRow ();
595 Child.Rows.Add (Row);
597 Row = Child.NewRow ();
600 Child.Rows.Add (Row);
602 Row = Child.NewRow ();
605 Child.Rows.Add (Row);
607 DataRow [] Rows = Child.Select ("name = Parent.Childname");
608 AssertEquals ("test#01", 6, Rows.Length);
609 Rows = Child.Select ("Parent.childname = 'Jack'");
610 AssertEquals ("test#02", 1, Rows.Length);
614 // FIXME: LAMESPEC: Why the exception is thrown why... why...
615 Mom.Select ("Child.Name = 'Jack'");
617 } catch (Exception e) {
618 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
619 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
623 Rows = Child.Select ("Parent.name = 'Laura'");
624 AssertEquals ("test#06", 3, Rows.Length);
626 DataTable Parent2 = new DataTable ("Parent2");
627 Col = new DataColumn ("Name");
628 Col2 = new DataColumn ("ChildName");
630 Parent2.Columns.Add (Col);
631 Parent2.Columns.Add (Col2);
632 Set.Tables.Add (Parent2);
634 Row = Parent2.NewRow ();
637 Parent2.Rows.Add (Row);
639 Row = Parent2.NewRow ();
642 Parent2.Rows.Add (Row);
644 Row = Parent2.NewRow ();
647 Parent2.Rows.Add (Row);
649 Row = Parent2.NewRow ();
652 Parent2.Rows.Add (Row);
654 Row = Parent2.NewRow ();
657 Parent2.Rows.Add (Row);
659 Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
660 Set.Relations.Add (Relation);
663 Rows = Child.Select ("Parent.ChildName = 'Jack'");
665 } catch (Exception e) {
666 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
667 //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);
670 Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
671 AssertEquals ("test#10", 1, Rows.Length);
673 Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
674 AssertEquals ("test#10", 1, Rows.Length);
677 Mom.Select ("Parent.name = 'John'");
678 } catch (Exception e) {
679 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
680 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
686 public void ToStringTest()
688 DataTable dt = new DataTable();
689 dt.Columns.Add("Col1",typeof(int));
691 dt.TableName = "Mytable";
692 dt.DisplayExpression = "Col1";
695 string cmpr = dt.TableName + " + " + dt.DisplayExpression;
696 AssertEquals(cmpr,dt.ToString());
700 public void PrimaryKey ()
702 DataTable dt = new DataTable ();
703 DataColumn Col = new DataColumn ();
704 Col.AllowDBNull = false;
705 Col.DataType = typeof (int);
706 dt.Columns.Add (Col);
711 AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
713 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
714 AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
715 AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
717 dt.PrimaryKey = null;
718 AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
720 Col = new DataColumn ("failed");
723 dt.PrimaryKey = new DataColumn [] {Col};
725 } catch (Exception e) {
726 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
727 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
730 DataTable dt2 = new DataTable ();
734 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
736 } catch (Exception e) {
737 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
738 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
742 AssertEquals ("test#11", 0, dt.Constraints.Count);
744 dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
745 AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
746 AssertEquals ("test#13", 1, dt.Constraints.Count);
747 AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
748 AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
749 AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);
754 public void PropertyExceptions ()
756 DataSet set = new DataSet ();
757 DataTable table = new DataTable ();
758 DataTable table1 = new DataTable ();
759 set.Tables.Add (table);
760 set.Tables.Add (table1);
762 DataColumn col = new DataColumn ();
763 col.ColumnName = "Id";
764 col.DataType = Type.GetType ("System.Int32");
765 table.Columns.Add (col);
766 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
767 table.Constraints.Add (uc);
768 table.CaseSensitive = false;
770 col = new DataColumn ();
771 col.ColumnName = "Name";
772 col.DataType = Type.GetType ("System.String");
773 table.Columns.Add (col);
775 col = new DataColumn ();
776 col.ColumnName = "Id";
777 col.DataType = Type.GetType ("System.Int32");
778 table1.Columns.Add (col);
779 col = new DataColumn ();
780 col.ColumnName = "Name";
781 col.DataType = Type.GetType ("System.String");
782 table1.Columns.Add (col);
784 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
785 set.Relations.Add (dr);
788 table.CaseSensitive = true;
789 table1.CaseSensitive = true;
792 catch (Exception e) {
793 if (e.GetType () != typeof (AssertionException))
794 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);
796 Console.WriteLine (e);
799 CultureInfo cultureInfo = new CultureInfo ("en-gb");
800 table.Locale = cultureInfo;
801 table1.Locale = cultureInfo;
804 catch (Exception e) {
805 if (e.GetType () != typeof (AssertionException))
806 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);
808 Console.WriteLine (e);
811 table.Prefix = "Prefix#1";
815 if (e.GetType () != typeof (AssertionException))
816 AssertEquals ("#A06", "Prefix 'Prefix#1' is not valid, because it contains special characters.",e.Message);
818 Console.WriteLine (e);
824 public void GetErrors ()
826 DataTable table = new DataTable ();
828 DataColumn col = new DataColumn ();
829 col.ColumnName = "Id";
830 col.DataType = Type.GetType ("System.Int32");
831 table.Columns.Add (col);
833 col = new DataColumn ();
834 col.ColumnName = "Name";
835 col.DataType = Type.GetType ("System.String");
836 table.Columns.Add (col);
838 DataRow row = table.NewRow ();
840 row ["name"] = "Abc";
841 row.RowError = "Error#1";
842 table.Rows.Add (row);
844 AssertEquals ("#A01", 1, table.GetErrors ().Length);
845 AssertEquals ("#A02", "Error#1", (table.GetErrors ())[0].RowError);
848 public void CloneCopyTest ()
850 DataTable table = new DataTable ();
851 table.TableName = "Table#1";
852 DataTable table1 = new DataTable ();
853 table1.TableName = "Table#2";
855 table.AcceptChanges ();
857 DataSet set = new DataSet ("Data Set#1");
858 set.DataSetName = "Dataset#1";
859 set.Tables.Add (table);
860 set.Tables.Add (table1);
862 DataColumn col = new DataColumn ();
863 col.ColumnName = "Id";
864 col.DataType = Type.GetType ("System.Int32");
865 table.Columns.Add (col);
866 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
867 table.Constraints.Add (uc);
869 col = new DataColumn ();
870 col.ColumnName = "Id";
871 col.DataType = Type.GetType ("System.Int32");
872 table1.Columns.Add (col);
874 col = new DataColumn ();
875 col.ColumnName = "Name";
876 col.DataType = Type.GetType ("System.String");
877 table.Columns.Add (col);
879 col = new DataColumn ();
880 col.ColumnName = "Name";
881 col.DataType = Type.GetType ("System.String");
882 table1.Columns.Add (col);
883 DataRow row = table.NewRow ();
885 row ["name"] = "Abc";
886 row.RowError = "Error#1";
887 table.Rows.Add (row);
889 row = table.NewRow ();
891 row ["name"] = "Efg";
892 table.Rows.Add (row);
893 table.AcceptChanges ();
895 table.CaseSensitive = true;
896 table1.CaseSensitive = true;
897 table.MinimumCapacity = 100;
898 table.Prefix = "PrefixNo:1";
899 table.Namespace = "Namespace#1";
900 table.DisplayExpression = "Id / Name + (Id * Id)";
901 DataColumn[] colArray = {table.Columns[0]};
902 table.PrimaryKey = colArray;
903 table.ExtendedProperties.Add ("TimeStamp", DateTime.Now);
904 #if NET_1_1 // This prevents further tests after .NET 1.1.
906 CultureInfo cultureInfo = new CultureInfo ("en-gb");
907 table.Locale = cultureInfo;
910 row = table1.NewRow ();
911 row ["Name"] = "Abc";
913 table1.Rows.Add (row);
915 row = table1.NewRow ();
917 row ["Name"] = "Efg";
918 table1.Rows.Add (row);
920 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
921 set.Relations.Add (dr);
923 //Testing properties of clone
924 DataTable cloneTable = table.Clone ();
925 AssertEquals ("#A01",true ,cloneTable.CaseSensitive);
926 AssertEquals ("#A02", 0 , cloneTable.ChildRelations.Count);
927 AssertEquals ("#A03", 0 , cloneTable.ParentRelations.Count);
928 AssertEquals ("#A04", 2, cloneTable.Columns.Count);
929 AssertEquals ("#A05", 1, cloneTable.Constraints.Count);
930 AssertEquals ("#A06", "Id / Name + (Id * Id)", cloneTable.DisplayExpression);
931 AssertEquals ("#A07", 1 ,cloneTable.ExtendedProperties.Count);
932 AssertEquals ("#A08", false ,cloneTable.HasErrors);
935 AssertEquals ("#A09", 2057, cloneTable.Locale.LCID);
937 AssertEquals ("#A10", 100, cloneTable.MinimumCapacity);
938 AssertEquals ("#A11","Namespace#1", cloneTable.Namespace);
939 AssertEquals ("#A12", "PrefixNo:1",cloneTable.Prefix);
940 AssertEquals ("#A13", "Id", cloneTable.PrimaryKey[0].ColumnName);
941 AssertEquals ("#A14",0 , cloneTable.Rows.Count );
942 AssertEquals ("#A15", "Table#1", cloneTable.TableName);
944 //Testing properties of copy
945 DataTable copyTable = table.Copy ();
946 AssertEquals ("#A16",true ,copyTable.CaseSensitive);
947 AssertEquals ("#A17", 0 , copyTable.ChildRelations.Count);
948 AssertEquals ("#A18", 0 , copyTable.ParentRelations.Count);
949 AssertEquals ("#A19", 2, copyTable.Columns.Count);
950 AssertEquals ("#A20", 1, copyTable.Constraints.Count);
951 AssertEquals ("#A21", "Id / Name + (Id * Id)", copyTable.DisplayExpression);
952 AssertEquals ("#A22", 1 ,copyTable.ExtendedProperties.Count);
953 AssertEquals ("#A23", true ,copyTable.HasErrors);
956 AssertEquals ("#A24", 2057, copyTable.Locale.LCID);
958 AssertEquals ("#A25", 100, copyTable.MinimumCapacity);
959 AssertEquals ("#A26","Namespace#1", copyTable.Namespace);
960 AssertEquals ("#A27", "PrefixNo:1",copyTable.Prefix);
961 AssertEquals ("#A28", "Id", copyTable.PrimaryKey[0].ColumnName);
962 AssertEquals ("#A29", 2 , copyTable.Rows.Count );
963 AssertEquals ("#A30", "Table#1", copyTable.TableName);
967 public void LoadDataException ()
969 DataTable table = new DataTable ();
970 DataColumn col = new DataColumn ();
971 col.ColumnName = "Id";
972 col.DataType = Type.GetType ("System.Int32");
973 col.DefaultValue = 47;
974 table.Columns.Add (col);
975 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
976 table.Constraints.Add (uc);
978 col = new DataColumn ();
979 col.ColumnName = "Name";
980 col.DataType = Type.GetType ("System.String");
981 col.DefaultValue = "Hello";
982 table.Columns.Add (col);
984 table.BeginLoadData();
985 object[] row = {147, "Abc"};
986 DataRow newRow = table.LoadDataRow (row, true);
988 object[] row1 = {147, "Efg"};
989 DataRow newRow1 = table.LoadDataRow (row1, true);
991 object[] row2 = {143, "Hij"};
992 DataRow newRow2 = table.LoadDataRow (row2, true);
995 table.EndLoadData ();
998 catch (ConstraintException) {
1002 public void Changes () //To test GetChanges and RejectChanges
1004 DataTable table = new DataTable ();
1006 DataColumn col = new DataColumn ();
1007 col.ColumnName = "Id";
1008 col.DataType = Type.GetType ("System.Int32");
1009 table.Columns.Add (col);
1010 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1011 table.Constraints.Add (uc);
1013 col = new DataColumn ();
1014 col.ColumnName = "Name";
1015 col.DataType = Type.GetType ("System.String");
1016 table.Columns.Add (col);
1018 DataRow row = table.NewRow ();
1020 row ["name"] = "Abc";
1021 table.Rows.Add (row);
1022 table.AcceptChanges ();
1024 row = table.NewRow ();
1026 row ["name"] = "Efg";
1027 table.Rows.Add (row);
1029 //Testing GetChanges
1030 DataTable changesTable = table.GetChanges ();
1031 AssertEquals ("#A01", 1 ,changesTable.Rows.Count);
1032 AssertEquals ("#A02","Efg" ,changesTable.Rows[0]["Name"]);
1033 table.AcceptChanges ();
1034 changesTable = table.GetChanges ();
1036 int cnt = changesTable.Rows.Count;
1038 catch(Exception e) {
1039 if (e.GetType () != typeof (AssertionException))
1040 AssertEquals ("#A03",typeof(NullReferenceException) ,e.GetType ());
1042 Console.WriteLine (e);
1045 //Testing RejectChanges
1046 row = table.NewRow ();
1048 row ["name"] = "Hij";
1049 table.Rows.Add (row);
1051 (table.Rows [0])["Name"] = "AaBbCc";
1052 table.RejectChanges ();
1053 AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]);
1054 AssertEquals ("#A04", 2, table.Rows.Count);
1057 public void ImportRow ()
1059 DataTable table = new DataTable ();
1060 DataColumn col = new DataColumn ();
1061 col.ColumnName = "Id";
1062 col.DataType = Type.GetType ("System.Int32");
1063 table.Columns.Add (col);
1065 col = new DataColumn ();
1066 col.ColumnName = "Name";
1067 col.DataType = Type.GetType ("System.String");
1068 table.Columns.Add (col);
1070 DataRow row = table.NewRow ();
1072 row ["name"] = "Abc";
1073 table.Rows.Add (row);
1074 table.AcceptChanges ();
1076 row = table.NewRow ();
1078 row ["name"] = "Efg";
1079 table.Rows.Add (row);
1081 (table.Rows [0]) ["Name"] = "AaBbCc";
1083 table.ImportRow (table.Rows [0]);
1084 table.ImportRow (table.Rows [1]);
1086 AssertEquals ("#A01", 147, table.Rows [2]["Id"]);
1087 AssertEquals ("#A02", 47, table.Rows [3]["Id"]);
1088 AssertEquals ("#A03", DataRowState.Modified, table.Rows [2].RowState);
1089 AssertEquals ("#A04", DataRowState.Added, table.Rows [3].RowState);
1092 public void ClearReset () //To test Clear and Reset methods
1094 DataTable table = new DataTable ();
1095 DataTable table1 = new DataTable ();
1097 DataSet set = new DataSet ();
1098 set.Tables.Add (table);
1099 set.Tables.Add (table1);
1101 DataColumn col = new DataColumn ();
1102 col.ColumnName = "Id";
1103 col.DataType = Type.GetType ("System.Int32");
1104 table.Columns.Add (col);
1105 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1106 table.Constraints.Add (uc);
1107 table.CaseSensitive = false;
1109 col = new DataColumn ();
1110 col.ColumnName = "Name";
1111 col.DataType = Type.GetType ("System.String");
1112 table.Columns.Add (col);
1114 col = new DataColumn ();
1115 col.ColumnName = "Id";
1116 col.DataType = Type.GetType ("System.Int32");
1117 table1.Columns.Add (col);
1119 col = new DataColumn ();
1120 col.ColumnName = "Name";
1121 col.DataType = Type.GetType ("System.String");
1122 table1.Columns.Add (col);
1124 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
1125 set.Relations.Add (dr);
1127 DataRow row = table.NewRow ();
1129 row ["name"] = "Roopa";
1130 table.Rows.Add (row);
1132 row = table.NewRow ();
1134 row ["Name"] = "roopa";
1135 table.Rows.Add (row);
1141 catch (ArgumentException) {
1150 catch (Exception e) {
1154 if (e.GetType () != typeof (AssertionException)) {
1155 // FIXME: Don't depend on localizable error messages.
1156 AssertEquals ("#A04", "Cannot clear table Parent because ForeignKeyConstraint DR enforces Child.", e.Message);
1162 AssertEquals ("#A05", 0, table1.Rows.Count);
1163 AssertEquals ("#A06", 0, table1.Constraints.Count);
1164 AssertEquals ("#A07", 0, table1.ParentRelations.Count);
1167 AssertEquals ("#A08", 0, table.Rows.Count);
1169 AssertEquals ("#A09", 0, table.Constraints.Count);
1171 AssertEquals ("#A09", 1, table.Constraints.Count);
1173 AssertEquals ("#A05", 0, table.ChildRelations.Count);
1177 public void Serialize ()
1179 MemoryStream fs = new MemoryStream ();
1181 // Construct a BinaryFormatter and use it
1182 // to serialize the data to the stream.
1183 BinaryFormatter formatter = new BinaryFormatter();
1185 // Create an array with multiple elements refering to
1186 // the one Singleton object.
1187 DataTable dt = new DataTable();
1190 dt.Columns.Add(new DataColumn("Id", typeof(string)));
1191 dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
1192 dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
1193 dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
1194 dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
1197 loRowToAdd = dt.NewRow();
1198 loRowToAdd[0] = "a";
1199 loRowToAdd[1] = "b";
1200 loRowToAdd[2] = "c";
1201 loRowToAdd[3] = "d";
1202 loRowToAdd[4] = "e";
1204 dt.Rows.Add(loRowToAdd);
1206 DataTable[] dtarr = new DataTable[] {dt};
1208 // Serialize the array elements.
1209 formatter.Serialize(fs, dtarr);
1211 // Deserialize the array elements.
1213 DataTable[] a2 = (DataTable[]) formatter.Deserialize(fs);
1215 DataSet ds = new DataSet();
1216 ds.Tables.Add(a2[0]);
1218 StringWriter sw = new StringWriter ();
1220 XmlDocument doc = new XmlDocument ();
1221 doc.LoadXml (sw.ToString ());
1222 AssertEquals (5, doc.DocumentElement.FirstChild.ChildNodes.Count);