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.Data.SqlTypes;
38 using System.Globalization;
40 using System.Runtime.Serialization.Formatters.Binary;
43 namespace MonoTests.System.Data
46 public class DataTableTest : Assertion
52 DataTable dt = new DataTable();
54 AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
55 Assert("Col",dt.Columns != null);
56 //Assert(dt.ChildRelations != null);
57 Assert("Const", dt.Constraints != null);
58 Assert("ds", dt.DataSet == null);
59 Assert("dv", dt.DefaultView != null);
60 Assert("de", dt.DisplayExpression == "");
61 Assert("ep", dt.ExtendedProperties != null);
62 Assert("he", dt.HasErrors == false);
63 Assert("lc", dt.Locale != null);
64 Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
65 Assert("ns", dt.Namespace == "");
66 //Assert(dt.ParentRelations != null);
67 Assert("pf", dt.Prefix == "");
68 Assert("pk", dt.PrimaryKey != null);
69 Assert("rows", dt.Rows != null);
70 Assert("Site", dt.Site == null);
71 Assert("tname", dt.TableName == "");
78 DataSet Set = new DataSet ();
79 DataTable Mom = new DataTable ("Mom");
80 DataTable Child = new DataTable ("Child");
82 Set.Tables.Add (Child);
84 DataColumn Col = new DataColumn ("Name");
85 DataColumn Col2 = new DataColumn ("ChildName");
86 Mom.Columns.Add (Col);
87 Mom.Columns.Add (Col2);
89 DataColumn Col3 = new DataColumn ("Name");
90 DataColumn Col4 = new DataColumn ("Age");
91 Col4.DataType = Type.GetType ("System.Int16");
92 Child.Columns.Add (Col3);
93 Child.Columns.Add (Col4);
95 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
96 Set.Relations.Add (Relation);
98 DataRow Row = Mom.NewRow ();
124 Row [0] = "'Jhon O'' Collenal'";
128 Row = Child.NewRow ();
131 Child.Rows.Add (Row);
133 Row = Child.NewRow ();
136 Child.Rows.Add (Row);
138 Row = Child.NewRow ();
141 Child.Rows.Add (Row);
143 Row = Child.NewRow ();
146 Child.Rows.Add (Row);
148 Row = Child.NewRow ();
151 Child.Rows.Add (Row);
153 Row = Child.NewRow ();
156 Child.Rows.Add (Row);
158 Row = Child.NewRow ();
161 Child.Rows.Add (Row);
163 DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
164 AssertEquals ("test#01", 2, Rows.Length);
166 // test with apos escaped
167 Rows = Mom.Select ("Name = '''Jhon O'''' Collenal'''");
168 AssertEquals ("test#01.1", 1, Rows.Length);
170 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
171 AssertEquals ("test#02", 0, Rows.Length);
173 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
174 AssertEquals ("test#03", 1, Rows.Length);
176 Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
177 AssertEquals ("test#04", "Mack", Rows [0] [1]);
179 Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
180 AssertEquals ("test#05", 6, Rows.Length);
182 Rows = Child.Select ("age = 20 - 1");
183 AssertEquals ("test#06", 1, Rows.Length);
185 Rows = Child.Select ("age <= 20");
186 AssertEquals ("test#07", 3, Rows.Length);
188 Rows = Child.Select ("age >= 20");
189 AssertEquals ("test#08", 4, Rows.Length);
191 Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
192 AssertEquals ("test#09", 2, Rows.Length);
194 Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
195 AssertEquals ("test#10", 1, Rows.Length);
196 AssertEquals ("test#11", "Mack", Rows [0] [0]);
198 Rows = Child.Select ("not (Name = 'Jack')");
199 AssertEquals ("test#12", 6, Rows.Length);
203 public void Select2 ()
205 DataSet Set = new DataSet ();
206 DataTable Child = new DataTable ("Child");
208 Set.Tables.Add (Child);
210 DataColumn Col3 = new DataColumn ("Name");
211 DataColumn Col4 = new DataColumn ("Age");
212 Col4.DataType = Type.GetType ("System.Int16");
213 Child.Columns.Add (Col3);
214 Child.Columns.Add (Col4);
216 DataRow Row = Child.NewRow ();
219 Child.Rows.Add (Row);
221 Row = Child.NewRow ();
224 Child.Rows.Add (Row);
226 Row = Child.NewRow ();
229 Child.Rows.Add (Row);
231 Row = Child.NewRow ();
234 Child.Rows.Add (Row);
236 Row = Child.NewRow ();
239 Child.Rows.Add (Row);
241 Row = Child.NewRow ();
244 Child.Rows.Add (Row);
246 DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
247 AssertEquals ("test#01", 3, Rows.Length);
248 AssertEquals ("test#02", "Mack", Rows [0] [0]);
249 AssertEquals ("test#03", "Mick", Rows [1] [0]);
250 AssertEquals ("test#04", "Dick", Rows [2] [0]);
252 Rows = Child.Select ("age >= 20", "age asc");
253 AssertEquals ("test#05", 3, Rows.Length);
254 AssertEquals ("test#06", "Dick", Rows [0] [0]);
255 AssertEquals ("test#07", "Mick", Rows [1] [0]);
256 AssertEquals ("test#08", "Mack", Rows [2] [0]);
258 Rows = Child.Select ("age >= 20", "name asc");
259 AssertEquals ("test#09", 3, Rows.Length);
260 AssertEquals ("test#10", "Dick", Rows [0] [0]);
261 AssertEquals ("test#11", "Mack", Rows [1] [0]);
262 AssertEquals ("test#12", "Mick", Rows [2] [0]);
264 Rows = Child.Select ("age >= 20", "name desc");
265 AssertEquals ("test#09", 3, Rows.Length);
266 AssertEquals ("test#10", "Mick", Rows [0] [0]);
267 AssertEquals ("test#11", "Mack", Rows [1] [0]);
268 AssertEquals ("test#12", "Dick", Rows [2] [0]);
273 public void SelectParsing ()
275 DataTable T = new DataTable ("test");
276 DataColumn C = new DataColumn ("name");
278 C = new DataColumn ("age");
279 C.DataType = typeof (int);
281 C = new DataColumn ("id");
284 DataSet Set = new DataSet ("TestSet");
288 for (int i = 0; i < 100; i++) {
290 Row [0] = "human" + i;
302 AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
304 AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
307 T.Select ("name = 1human ");
309 } catch (Exception e) {
311 // missing operand after 'human' operand
312 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
316 T.Select ("name = 1");
318 } catch (Exception e) {
320 // Cannot perform '=' operation between string and Int32
321 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
324 AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
329 public void SelectEscaping () {
330 DataTable dt = new DataTable ();
331 dt.Columns.Add ("SomeCol");
332 dt.Rows.Add (new object [] {"\t"});
333 dt.Rows.Add (new object [] {"\\"});
335 AssertEquals ("test#01", 1, dt.Select (@"SomeCol='\t'").Length);
336 AssertEquals ("test#02", 1, dt.Select (@"SomeCol='\\'").Length);
339 dt.Select (@"SomeCol='\x'");
341 } catch (SyntaxErrorException) {}
345 public void SelectOperators ()
347 DataTable T = new DataTable ("test");
348 DataColumn C = new DataColumn ("name");
350 C = new DataColumn ("age");
351 C.DataType = typeof (int);
353 C = new DataColumn ("id");
356 DataSet Set = new DataSet ("TestSet");
360 for (int i = 0; i < 100; i++) {
362 Row [0] = "human" + i;
374 AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
375 AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);
376 AssertEquals ("test#03", 12, T.Select ("age< =10").Length);
377 AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
378 AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);
379 AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
380 AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
381 AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
382 // FIXME: Somebody explain how this can be possible.
383 // it seems that it is no matter between 10 - 30. The
384 // result is allways 25 :-P
385 //AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
390 public void SelectExceptions ()
392 DataTable T = new DataTable ("test");
393 DataColumn C = new DataColumn ("name");
395 C = new DataColumn ("age");
396 C.DataType = typeof (int);
398 C = new DataColumn ("id");
401 for (int i = 0; i < 100; i++) {
402 DataRow Row = T.NewRow ();
403 Row [0] = "human" + i;
410 T.Select ("name = human1");
412 } catch (Exception e) {
414 // column name human not found
415 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
418 AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
419 AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
422 T.Select ("id = 1k3");
424 } catch (Exception e) {
426 // no operands after k3 operator
427 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
432 public void SelectStringOperators ()
434 DataTable T = new DataTable ("test");
435 DataColumn C = new DataColumn ("name");
437 C = new DataColumn ("age");
438 C.DataType = typeof (int);
440 C = new DataColumn ("id");
443 DataSet Set = new DataSet ("TestSet");
447 for (int i = 0; i < 100; i++) {
449 Row [0] = "human" + i;
460 AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
462 AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);
463 AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
464 AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);
465 AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
466 AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
468 AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
470 Set.CaseSensitive = true;
471 AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
473 T.CaseSensitive = false;
474 AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
476 T.CaseSensitive = true;
477 AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
479 Set.CaseSensitive = false;
480 AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
482 T.CaseSensitive = false;
483 AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
485 AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
486 AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
487 AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
490 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
492 } catch (Exception e) {
494 // 'h*an1' is invalid
495 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
499 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
501 } catch (Exception e) {
503 // 'h%an1' is invalid
504 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
507 AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
508 AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
513 public void SelectAggregates ()
515 DataTable T = new DataTable ("test");
516 DataColumn C = new DataColumn ("name");
518 C = new DataColumn ("age");
519 C.DataType = typeof (int);
521 C = new DataColumn ("id");
525 for (int i = 0; i < 1000; i++) {
527 Row [0] = "human" + i;
533 AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
534 AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
535 AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
536 AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
537 AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
538 AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
539 AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
543 public void SelectFunctions ()
545 DataTable T = new DataTable ("test");
546 DataColumn C = new DataColumn ("name");
548 C = new DataColumn ("age");
549 C.DataType = typeof (int);
551 C = new DataColumn ("id");
555 for (int i = 0; i < 1000; i++) {
557 Row [0] = "human" + i;
564 Row [0] = "human" + "test";
565 Row [1] = DBNull.Value;
566 Row [2] = DBNull.Value;
569 //TODO: How to test Convert-function
570 AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);
571 AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
572 AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
573 AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);
574 AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);
575 AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
576 AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
577 AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
578 AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
579 AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
584 public void SelectRelations ()
586 DataSet Set = new DataSet ();
587 DataTable Mom = new DataTable ("Mom");
588 DataTable Child = new DataTable ("Child");
590 Set.Tables.Add (Mom);
591 Set.Tables.Add (Child);
593 DataColumn Col = new DataColumn ("Name");
594 DataColumn Col2 = new DataColumn ("ChildName");
595 Mom.Columns.Add (Col);
596 Mom.Columns.Add (Col2);
598 DataColumn Col3 = new DataColumn ("Name");
599 DataColumn Col4 = new DataColumn ("Age");
600 Col4.DataType = Type.GetType ("System.Int16");
601 Child.Columns.Add (Col3);
602 Child.Columns.Add (Col4);
604 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
605 Set.Relations.Add (Relation);
607 DataRow Row = Mom.NewRow ();
632 Row = Child.NewRow ();
635 Child.Rows.Add (Row);
637 Row = Child.NewRow ();
640 Child.Rows.Add (Row);
642 Row = Child.NewRow ();
645 Child.Rows.Add (Row);
647 Row = Child.NewRow ();
650 Child.Rows.Add (Row);
652 Row = Child.NewRow ();
655 Child.Rows.Add (Row);
657 Row = Child.NewRow ();
660 Child.Rows.Add (Row);
662 DataRow [] Rows = Child.Select ("name = Parent.Childname");
663 AssertEquals ("test#01", 6, Rows.Length);
664 Rows = Child.Select ("Parent.childname = 'Jack'");
665 AssertEquals ("test#02", 1, Rows.Length);
669 // FIXME: LAMESPEC: Why the exception is thrown why... why...
670 Mom.Select ("Child.Name = 'Jack'");
672 } catch (Exception e) {
673 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
674 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
678 Rows = Child.Select ("Parent.name = 'Laura'");
679 AssertEquals ("test#06", 3, Rows.Length);
681 DataTable Parent2 = new DataTable ("Parent2");
682 Col = new DataColumn ("Name");
683 Col2 = new DataColumn ("ChildName");
685 Parent2.Columns.Add (Col);
686 Parent2.Columns.Add (Col2);
687 Set.Tables.Add (Parent2);
689 Row = Parent2.NewRow ();
692 Parent2.Rows.Add (Row);
694 Row = Parent2.NewRow ();
697 Parent2.Rows.Add (Row);
699 Row = Parent2.NewRow ();
702 Parent2.Rows.Add (Row);
704 Row = Parent2.NewRow ();
707 Parent2.Rows.Add (Row);
709 Row = Parent2.NewRow ();
712 Parent2.Rows.Add (Row);
714 Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
715 Set.Relations.Add (Relation);
718 Rows = Child.Select ("Parent.ChildName = 'Jack'");
720 } catch (Exception e) {
721 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
722 //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);
725 Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
726 AssertEquals ("test#10", 1, Rows.Length);
728 Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
729 AssertEquals ("test#10", 1, Rows.Length);
732 Mom.Select ("Parent.name = 'John'");
733 } catch (Exception e) {
734 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
735 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
741 public void SelectRowState()
743 DataTable d = new DataTable();
744 d.Columns.Add (new DataColumn ("aaa"));
745 DataRow [] rows = d.Select (null, null, DataViewRowState.Deleted);
746 AssertEquals(0, rows.Length);
747 d.Rows.Add (new object [] {"bbb"});
748 d.Rows.Add (new object [] {"bbb"});
749 rows = d.Select (null, null, DataViewRowState.Deleted);
750 AssertEquals(0, rows.Length);
754 public void ToStringTest()
756 DataTable dt = new DataTable();
757 dt.Columns.Add("Col1",typeof(int));
759 dt.TableName = "Mytable";
760 dt.DisplayExpression = "Col1";
763 string cmpr = dt.TableName + " + " + dt.DisplayExpression;
764 AssertEquals(cmpr,dt.ToString());
768 public void PrimaryKey ()
770 DataTable dt = new DataTable ();
771 DataColumn Col = new DataColumn ();
772 Col.AllowDBNull = false;
773 Col.DataType = typeof (int);
774 dt.Columns.Add (Col);
779 AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
781 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
782 AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
783 AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
785 dt.PrimaryKey = null;
786 AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
788 Col = new DataColumn ("failed");
791 dt.PrimaryKey = new DataColumn [] {Col};
793 } catch (Exception e) {
794 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
795 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
798 DataTable dt2 = new DataTable ();
802 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
804 } catch (Exception e) {
805 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
806 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
810 AssertEquals ("test#11", 0, dt.Constraints.Count);
812 dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
813 AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
814 AssertEquals ("test#13", 1, dt.Constraints.Count);
815 AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
816 AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
817 AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);
822 public void PropertyExceptions ()
824 DataSet set = new DataSet ();
825 DataTable table = new DataTable ();
826 DataTable table1 = new DataTable ();
827 set.Tables.Add (table);
828 set.Tables.Add (table1);
830 DataColumn col = new DataColumn ();
831 col.ColumnName = "Id";
832 col.DataType = Type.GetType ("System.Int32");
833 table.Columns.Add (col);
834 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
835 table.Constraints.Add (uc);
836 table.CaseSensitive = false;
838 col = new DataColumn ();
839 col.ColumnName = "Name";
840 col.DataType = Type.GetType ("System.String");
841 table.Columns.Add (col);
843 col = new DataColumn ();
844 col.ColumnName = "Id";
845 col.DataType = Type.GetType ("System.Int32");
846 table1.Columns.Add (col);
847 col = new DataColumn ();
848 col.ColumnName = "Name";
849 col.DataType = Type.GetType ("System.String");
850 table1.Columns.Add (col);
852 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
853 set.Relations.Add (dr);
856 table.CaseSensitive = true;
857 table1.CaseSensitive = true;
860 catch (Exception e) {
861 if (e.GetType () != typeof (AssertionException))
862 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);
864 Console.WriteLine (e);
867 CultureInfo cultureInfo = new CultureInfo ("en-gb");
868 table.Locale = cultureInfo;
869 table1.Locale = cultureInfo;
872 catch (Exception e) {
873 if (e.GetType () != typeof (AssertionException))
874 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);
876 Console.WriteLine (e);
879 table.Prefix = "Prefix#1";
883 if (e.GetType () != typeof (AssertionException))
884 AssertEquals ("#A06", "Prefix 'Prefix#1' is not valid, because it contains special characters.",e.Message);
886 Console.WriteLine (e);
892 public void GetErrors ()
894 DataTable table = new DataTable ();
896 DataColumn col = new DataColumn ();
897 col.ColumnName = "Id";
898 col.DataType = Type.GetType ("System.Int32");
899 table.Columns.Add (col);
901 col = new DataColumn ();
902 col.ColumnName = "Name";
903 col.DataType = Type.GetType ("System.String");
904 table.Columns.Add (col);
906 DataRow row = table.NewRow ();
908 row ["name"] = "Abc";
909 row.RowError = "Error#1";
910 table.Rows.Add (row);
912 AssertEquals ("#A01", 1, table.GetErrors ().Length);
913 AssertEquals ("#A02", "Error#1", (table.GetErrors ())[0].RowError);
916 public void CloneCopyTest ()
918 DataTable table = new DataTable ();
919 table.TableName = "Table#1";
920 DataTable table1 = new DataTable ();
921 table1.TableName = "Table#2";
923 table.AcceptChanges ();
925 DataSet set = new DataSet ("Data Set#1");
926 set.DataSetName = "Dataset#1";
927 set.Tables.Add (table);
928 set.Tables.Add (table1);
930 DataColumn col = new DataColumn ();
931 col.ColumnName = "Id";
932 col.DataType = Type.GetType ("System.Int32");
933 table.Columns.Add (col);
934 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
935 table.Constraints.Add (uc);
937 col = new DataColumn ();
938 col.ColumnName = "Id";
939 col.DataType = Type.GetType ("System.Int32");
940 table1.Columns.Add (col);
942 col = new DataColumn ();
943 col.ColumnName = "Name";
944 col.DataType = Type.GetType ("System.String");
945 table.Columns.Add (col);
947 col = new DataColumn ();
948 col.ColumnName = "Name";
949 col.DataType = Type.GetType ("System.String");
950 table1.Columns.Add (col);
951 DataRow row = table.NewRow ();
953 row ["name"] = "Abc";
954 row.RowError = "Error#1";
955 table.Rows.Add (row);
957 row = table.NewRow ();
959 row ["name"] = "Efg";
960 table.Rows.Add (row);
961 table.AcceptChanges ();
963 table.CaseSensitive = true;
964 table1.CaseSensitive = true;
965 table.MinimumCapacity = 100;
966 table.Prefix = "PrefixNo:1";
967 table.Namespace = "Namespace#1";
968 table.DisplayExpression = "Id / Name + (Id * Id)";
969 DataColumn[] colArray = {table.Columns[0]};
970 table.PrimaryKey = colArray;
971 table.ExtendedProperties.Add ("TimeStamp", DateTime.Now);
972 #if NET_1_1 // This prevents further tests after .NET 1.1.
974 CultureInfo cultureInfo = new CultureInfo ("en-gb");
975 table.Locale = cultureInfo;
978 row = table1.NewRow ();
979 row ["Name"] = "Abc";
981 table1.Rows.Add (row);
983 row = table1.NewRow ();
985 row ["Name"] = "Efg";
986 table1.Rows.Add (row);
988 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
989 set.Relations.Add (dr);
991 //Testing properties of clone
992 DataTable cloneTable = table.Clone ();
993 AssertEquals ("#A01",true ,cloneTable.CaseSensitive);
994 AssertEquals ("#A02", 0 , cloneTable.ChildRelations.Count);
995 AssertEquals ("#A03", 0 , cloneTable.ParentRelations.Count);
996 AssertEquals ("#A04", 2, cloneTable.Columns.Count);
997 AssertEquals ("#A05", 1, cloneTable.Constraints.Count);
998 AssertEquals ("#A06", "Id / Name + (Id * Id)", cloneTable.DisplayExpression);
999 AssertEquals ("#A07", 1 ,cloneTable.ExtendedProperties.Count);
1000 AssertEquals ("#A08", false ,cloneTable.HasErrors);
1003 AssertEquals ("#A09", 2057, cloneTable.Locale.LCID);
1005 AssertEquals ("#A10", 100, cloneTable.MinimumCapacity);
1006 AssertEquals ("#A11","Namespace#1", cloneTable.Namespace);
1007 AssertEquals ("#A12", "PrefixNo:1",cloneTable.Prefix);
1008 AssertEquals ("#A13", "Id", cloneTable.PrimaryKey[0].ColumnName);
1009 AssertEquals ("#A14",0 , cloneTable.Rows.Count );
1010 AssertEquals ("#A15", "Table#1", cloneTable.TableName);
1012 //Testing properties of copy
1013 DataTable copyTable = table.Copy ();
1014 AssertEquals ("#A16",true ,copyTable.CaseSensitive);
1015 AssertEquals ("#A17", 0 , copyTable.ChildRelations.Count);
1016 AssertEquals ("#A18", 0 , copyTable.ParentRelations.Count);
1017 AssertEquals ("#A19", 2, copyTable.Columns.Count);
1018 AssertEquals ("#A20", 1, copyTable.Constraints.Count);
1019 AssertEquals ("#A21", "Id / Name + (Id * Id)", copyTable.DisplayExpression);
1020 AssertEquals ("#A22", 1 ,copyTable.ExtendedProperties.Count);
1021 AssertEquals ("#A23", true ,copyTable.HasErrors);
1024 AssertEquals ("#A24", 2057, copyTable.Locale.LCID);
1026 AssertEquals ("#A25", 100, copyTable.MinimumCapacity);
1027 AssertEquals ("#A26","Namespace#1", copyTable.Namespace);
1028 AssertEquals ("#A27", "PrefixNo:1",copyTable.Prefix);
1029 AssertEquals ("#A28", "Id", copyTable.PrimaryKey[0].ColumnName);
1030 AssertEquals ("#A29", 2 , copyTable.Rows.Count );
1031 AssertEquals ("#A30", "Table#1", copyTable.TableName);
1035 public void LoadDataException ()
1037 DataTable table = new DataTable ();
1038 DataColumn col = new DataColumn ();
1039 col.ColumnName = "Id";
1040 col.DataType = Type.GetType ("System.Int32");
1041 col.DefaultValue = 47;
1042 table.Columns.Add (col);
1043 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1044 table.Constraints.Add (uc);
1046 col = new DataColumn ();
1047 col.ColumnName = "Name";
1048 col.DataType = Type.GetType ("System.String");
1049 col.DefaultValue = "Hello";
1050 table.Columns.Add (col);
1052 table.BeginLoadData();
1053 object[] row = {147, "Abc"};
1054 DataRow newRow = table.LoadDataRow (row, true);
1056 object[] row1 = {147, "Efg"};
1057 DataRow newRow1 = table.LoadDataRow (row1, true);
1059 object[] row2 = {143, "Hij"};
1060 DataRow newRow2 = table.LoadDataRow (row2, true);
1063 table.EndLoadData ();
1066 catch (ConstraintException) {
1070 public void Changes () //To test GetChanges and RejectChanges
1072 DataTable table = new DataTable ();
1074 DataColumn col = new DataColumn ();
1075 col.ColumnName = "Id";
1076 col.DataType = Type.GetType ("System.Int32");
1077 table.Columns.Add (col);
1078 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1079 table.Constraints.Add (uc);
1081 col = new DataColumn ();
1082 col.ColumnName = "Name";
1083 col.DataType = Type.GetType ("System.String");
1084 table.Columns.Add (col);
1086 DataRow row = table.NewRow ();
1088 row ["name"] = "Abc";
1089 table.Rows.Add (row);
1090 table.AcceptChanges ();
1092 row = table.NewRow ();
1094 row ["name"] = "Efg";
1095 table.Rows.Add (row);
1097 //Testing GetChanges
1098 DataTable changesTable = table.GetChanges ();
1099 AssertEquals ("#A01", 1 ,changesTable.Rows.Count);
1100 AssertEquals ("#A02","Efg" ,changesTable.Rows[0]["Name"]);
1101 table.AcceptChanges ();
1102 changesTable = table.GetChanges ();
1104 int cnt = changesTable.Rows.Count;
1106 catch(Exception e) {
1107 if (e.GetType () != typeof (AssertionException))
1108 AssertEquals ("#A03",typeof(NullReferenceException) ,e.GetType ());
1110 Console.WriteLine (e);
1113 //Testing RejectChanges
1114 row = table.NewRow ();
1116 row ["name"] = "Hij";
1117 table.Rows.Add (row);
1119 (table.Rows [0])["Name"] = "AaBbCc";
1120 table.RejectChanges ();
1121 AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]);
1122 AssertEquals ("#A04", 2, table.Rows.Count);
1126 public void ImportRowTest ()
1128 // build source table
1129 DataTable src = new DataTable ();
1130 src.Columns.Add ("id", typeof (int));
1131 src.Columns.Add ("name", typeof (string));
1133 src.PrimaryKey = new DataColumn [] {src.Columns [0]} ;
1135 src.Rows.Add (new object [] { 1, "mono 1" });
1136 src.Rows.Add (new object [] { 2, "mono 2" });
1137 src.Rows.Add (new object [] { 3, "mono 3" });
1138 src.AcceptChanges ();
1140 src.Rows [0] [1] = "mono changed 1"; // modify 1st row
1141 src.Rows [1].Delete (); // delete 2nd row
1142 // 3rd row is unchanged
1143 src.Rows.Add (new object [] { 4, "mono 4" }); // add 4th row
1145 // build target table
1146 DataTable target = new DataTable ();
1147 target.Columns.Add ("id", typeof (int));
1148 target.Columns.Add ("name", typeof (string));
1150 target.PrimaryKey = new DataColumn [] {target.Columns [0]} ;
1153 target.ImportRow (src.Rows [0]); // import 1st row
1154 target.ImportRow (src.Rows [1]); // import 2nd row
1155 target.ImportRow (src.Rows [2]); // import 3rd row
1156 target.ImportRow (src.Rows [3]); // import 4th row
1159 target.ImportRow (src.Rows [2]); // import 3rd row again
1160 Fail ("#AA1 Should have thrown exception violativ PK");
1161 } catch (ConstraintException e) {}
1164 AssertEquals ("#A1", src.Rows [0].RowState, target.Rows [0].RowState);
1165 AssertEquals ("#A2", src.Rows [1].RowState, target.Rows [1].RowState);
1166 AssertEquals ("#A3", src.Rows [2].RowState, target.Rows [2].RowState);
1167 AssertEquals ("#A4", src.Rows [3].RowState, target.Rows [3].RowState);
1169 // check for modified row (1st row)
1170 AssertEquals ("#B1", (string) src.Rows [0] [1], (string) target.Rows [0] [1]);
1171 AssertEquals ("#B2", (string) src.Rows [0] [1, DataRowVersion.Default], (string) target.Rows [0] [1, DataRowVersion.Default]);
1172 AssertEquals ("#B3", (string) src.Rows [0] [1, DataRowVersion.Original], (string) target.Rows [0] [1, DataRowVersion.Original]);
1173 AssertEquals ("#B4", (string) src.Rows [0] [1, DataRowVersion.Current], (string) target.Rows [0] [1, DataRowVersion.Current]);
1174 AssertEquals ("#B5", false, target.Rows [0].HasVersion(DataRowVersion.Proposed));
1176 // check for deleted row (2nd row)
1177 AssertEquals ("#C1", (string) src.Rows [1] [1, DataRowVersion.Original], (string) target.Rows [1] [1, DataRowVersion.Original]);
1179 // check for unchanged row (3rd row)
1180 AssertEquals ("#D1", (string) src.Rows [2] [1], (string) target.Rows [2] [1]);
1181 AssertEquals ("#D2", (string) src.Rows [2] [1, DataRowVersion.Default], (string) target.Rows [2] [1, DataRowVersion.Default]);
1182 AssertEquals ("#D3", (string) src.Rows [2] [1, DataRowVersion.Original], (string) target.Rows [2] [1, DataRowVersion.Original]);
1183 AssertEquals ("#D4", (string) src.Rows [2] [1, DataRowVersion.Current], (string) target.Rows [2] [1, DataRowVersion.Current]);
1185 // check for newly added row (4th row)
1186 AssertEquals ("#E1", (string) src.Rows [3] [1], (string) target.Rows [3] [1]);
1187 AssertEquals ("#E2", (string) src.Rows [3] [1, DataRowVersion.Default], (string) target.Rows [3] [1, DataRowVersion.Default]);
1188 AssertEquals ("#E3", (string) src.Rows [3] [1, DataRowVersion.Current], (string) target.Rows [3] [1, DataRowVersion.Current]);
1192 public void ImportRowDetachedTest ()
1194 DataTable table = new DataTable ();
1195 DataColumn col = new DataColumn ();
1196 col.ColumnName = "Id";
1197 col.DataType = Type.GetType ("System.Int32");
1198 table.Columns.Add (col);
1200 table.PrimaryKey = new DataColumn [] {col};
1202 col = new DataColumn ();
1203 col.ColumnName = "Name";
1204 col.DataType = Type.GetType ("System.String");
1205 table.Columns.Add (col);
1207 DataRow row = table.NewRow ();
1209 row ["name"] = "Abc";
1211 // keep silent as ms.net ;-), though this is not useful.
1212 table.ImportRow (row);
1214 //if RowState is detached, then dont import the row.
1215 AssertEquals ("#1", 0, table.Rows.Count);
1219 public void ImportRowDeletedTest ()
1221 DataTable table = new DataTable ();
1222 table.Columns.Add ("col", typeof (int));
1223 table.Columns.Add ("col1", typeof (int));
1225 DataRow row = table.Rows.Add (new object[] {1,2});
1226 table.PrimaryKey = new DataColumn[] {table.Columns[0]};
1227 table.AcceptChanges ();
1229 // If row is in Deleted state, then ImportRow loads the
1232 table.ImportRow (row);
1233 AssertEquals ("#1", 2, table.Rows.Count);
1235 // Both the deleted rows shud be now gone
1236 table.AcceptChanges ();
1237 AssertEquals ("#2", 0, table.Rows.Count);
1239 //just add another row
1240 row = table.Rows.Add (new object[] {1,2});
1241 // no exception shud be thrown
1242 table.AcceptChanges ();
1244 // If row is in Deleted state, then ImportRow loads the
1245 // row and validate only on RejectChanges
1247 table.ImportRow (row);
1248 AssertEquals ("#3", 2, table.Rows.Count);
1249 AssertEquals ("#4", DataRowState.Deleted, table.Rows[1].RowState);
1252 table.RejectChanges ();
1254 } catch (ConstraintException e) {
1259 public void ClearReset () //To test Clear and Reset methods
1261 DataTable table = new DataTable ("table");
1262 DataTable table1 = new DataTable ("table1");
1264 DataSet set = new DataSet ();
1265 set.Tables.Add (table);
1266 set.Tables.Add (table1);
1268 table.Columns.Add ("Id", typeof (int));
1269 table.Columns.Add ("Name", typeof (string));
1270 table.Constraints.Add (new UniqueConstraint ("UK1", table.Columns [0]));
1271 table.CaseSensitive = false;
1273 table1.Columns.Add ("Id", typeof (int));
1274 table1.Columns.Add ("Name", typeof (string));
1276 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
1277 set.Relations.Add (dr);
1279 DataRow row = table.NewRow ();
1281 row ["name"] = "Roopa";
1282 table.Rows.Add (row);
1284 row = table.NewRow ();
1286 row ["Name"] = "roopa";
1287 table.Rows.Add (row);
1289 AssertEquals (2, table.Rows.Count);
1290 AssertEquals (1, table.ChildRelations.Count);
1293 Fail ("#A01, should have thrown ArgumentException");
1295 catch (ArgumentException) {
1297 AssertEquals ("#CT01", 0, table.Rows.Count);
1298 AssertEquals ("#CT02", 0, table.ChildRelations.Count);
1299 AssertEquals ("#CT03", 0, table.ParentRelations.Count);
1300 AssertEquals ("#CT04", 0, table.Constraints.Count);
1303 AssertEquals ("#A05", 0, table1.Rows.Count);
1304 AssertEquals ("#A06", 0, table1.Constraints.Count);
1305 AssertEquals ("#A07", 0, table1.ParentRelations.Count);
1309 AssertEquals ("#A08", 0, table.Rows.Count);
1311 AssertEquals ("#A09", 0, table.Constraints.Count);
1313 AssertEquals ("#A09", 1, table.Constraints.Count);
1315 AssertEquals ("#A10", 0, table.ChildRelations.Count);
1320 public void ClearTest ()
1322 DataTable table = new DataTable ("test");
1323 table.Columns.Add ("id", typeof (int));
1324 table.Columns.Add ("name", typeof (string));
1326 table.PrimaryKey = new DataColumn [] { table.Columns [0] } ;
1328 table.Rows.Add (new object [] { 1, "mono 1" });
1329 table.Rows.Add (new object [] { 2, "mono 2" });
1330 table.Rows.Add (new object [] { 3, "mono 3" });
1331 table.Rows.Add (new object [] { 4, "mono 4" });
1333 table.AcceptChanges ();
1335 _tableClearedEventFired = false;
1336 table.TableCleared += new DataTableClearEventHandler (OnTableCleared);
1341 AssertEquals ("#0 should have fired cleared event", true, _tableClearedEventFired);
1344 DataRow r = table.Rows.Find (1);
1345 AssertEquals ("#1 should have cleared", true, r == null);
1347 // try adding new row. indexes should have cleared
1348 table.Rows.Add (new object [] { 2, "mono 2" });
1349 AssertEquals ("#2 should add row", 1, table.Rows.Count);
1352 private bool _tableClearedEventFired = false;
1353 private void OnTableCleared (object src, DataTableClearEventArgs args)
1355 _tableClearedEventFired = true;
1361 public void Serialize ()
1363 MemoryStream fs = new MemoryStream ();
1365 // Construct a BinaryFormatter and use it
1366 // to serialize the data to the stream.
1367 BinaryFormatter formatter = new BinaryFormatter();
1369 // Create an array with multiple elements refering to
1370 // the one Singleton object.
1371 DataTable dt = new DataTable();
1374 dt.Columns.Add(new DataColumn("Id", typeof(string)));
1375 dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
1376 dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
1377 dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
1378 dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
1381 loRowToAdd = dt.NewRow();
1382 loRowToAdd[0] = "a";
1383 loRowToAdd[1] = "b";
1384 loRowToAdd[2] = "c";
1385 loRowToAdd[3] = "d";
1386 loRowToAdd[4] = "e";
1388 dt.Rows.Add(loRowToAdd);
1390 DataTable[] dtarr = new DataTable[] {dt};
1392 // Serialize the array elements.
1393 formatter.Serialize(fs, dtarr);
1395 // Deserialize the array elements.
1397 DataTable[] a2 = (DataTable[]) formatter.Deserialize(fs);
1399 DataSet ds = new DataSet();
1400 ds.Tables.Add(a2[0]);
1402 StringWriter sw = new StringWriter ();
1404 XmlDocument doc = new XmlDocument ();
1405 doc.LoadXml (sw.ToString ());
1406 AssertEquals (5, doc.DocumentElement.FirstChild.ChildNodes.Count);
1410 [ExpectedException (typeof (DataException))]
1411 public void SetPrimaryKeyAssertsNonNull ()
1413 DataTable dt = new DataTable ("table");
1414 dt.Columns.Add ("col1");
1415 dt.Columns.Add ("col2");
1416 dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1417 dt.Rows.Add (new object [] {1, 3});
1418 dt.Rows.Add (new object [] {DBNull.Value, 3});
1420 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1424 [ExpectedException (typeof (NoNullAllowedException))]
1425 public void PrimaryKeyColumnChecksNonNull ()
1427 DataTable dt = new DataTable ("table");
1428 dt.Columns.Add ("col1");
1429 dt.Columns.Add ("col2");
1430 dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1431 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1432 dt.Rows.Add (new object [] {1, 3});
1433 dt.Rows.Add (new object [] {DBNull.Value, 3});
1437 public void PrimaryKey_CheckSetsAllowDBNull ()
1439 DataTable table = new DataTable ();
1440 DataColumn col1 = table.Columns.Add ("col1", typeof (int));
1441 DataColumn col2 = table.Columns.Add ("col2", typeof (int));
1443 AssertEquals ("#1" , true, col1.AllowDBNull);
1444 AssertEquals ("#2" , true, col2.AllowDBNull);
1445 AssertEquals ("#3" , false, col2.Unique);
1446 AssertEquals ("#4" , false, col2.Unique);
1448 table.PrimaryKey = new DataColumn[] {col1,col2};
1449 AssertEquals ("#5" , false, col1.AllowDBNull);
1450 AssertEquals ("#6" , false, col2.AllowDBNull);
1451 // LAMESPEC or bug ??
1452 AssertEquals ("#7" , false, col1.Unique);
1453 AssertEquals ("#8" , false, col2.Unique);
1456 void RowChanging (object o, DataRowChangeEventArgs e)
1458 AssertEquals ("changing.Action", rowChangingExpectedAction, e.Action);
1459 rowChangingRowChanging = true;
1462 void RowChanged (object o, DataRowChangeEventArgs e)
1464 AssertEquals ("changed.Action", rowChangingExpectedAction, e.Action);
1465 rowChangingRowChanged = true;
1468 bool rowChangingRowChanging, rowChangingRowChanged;
1469 DataRowAction rowChangingExpectedAction;
1472 public void RowChanging ()
1474 DataTable dt = new DataTable ("table");
1475 dt.Columns.Add ("col1");
1476 dt.Columns.Add ("col2");
1477 dt.RowChanging += new DataRowChangeEventHandler (RowChanging);
1478 dt.RowChanged += new DataRowChangeEventHandler (RowChanged);
1479 rowChangingExpectedAction = DataRowAction.Add;
1480 dt.Rows.Add (new object [] {1, 2});
1481 Assert ("changing,Added", rowChangingRowChanging);
1482 Assert ("changed,Added", rowChangingRowChanged);
1483 rowChangingExpectedAction = DataRowAction.Change;
1484 dt.Rows [0] [0] = 2;
1485 Assert ("changing,Changed", rowChangingRowChanging);
1486 Assert ("changed,Changed", rowChangingRowChanged);
1490 public void CloneSubClassTest()
1492 MyDataTable dt1 = new MyDataTable();
1493 MyDataTable dt = (MyDataTable)(dt1.Clone());
1494 AssertEquals("A#01",2,MyDataTable.count);
1497 DataRowAction rowActionChanging = DataRowAction.Nothing;
1498 DataRowAction rowActionChanged = DataRowAction.Nothing;
1500 public void AcceptChangesTest ()
1502 DataTable dt = new DataTable ("test");
1503 dt.Columns.Add ("id", typeof (int));
1504 dt.Columns.Add ("name", typeof (string));
1506 dt.Rows.Add (new object [] { 1, "mono 1" });
1508 dt.RowChanged += new DataRowChangeEventHandler (OnRowChanged);
1509 dt.RowChanging += new DataRowChangeEventHandler (OnRowChanging);
1512 rowActionChanged = rowActionChanging = DataRowAction.Nothing;
1513 dt.AcceptChanges ();
1515 AssertEquals ("#1 should have fired event and set action to commit",
1516 DataRowAction.Commit, rowActionChanging);
1517 AssertEquals ("#2 should have fired event and set action to commit",
1518 DataRowAction.Commit, rowActionChanged);
1521 dt.RowChanged -= new DataRowChangeEventHandler (OnRowChanged);
1522 dt.RowChanging -= new DataRowChangeEventHandler (OnRowChanging);
1528 public void ColumnObjectTypeTest() {
1529 DataTable dt = new DataTable();
1530 dt.Columns.Add("Series Label", typeof(SqlInt32));
1531 dt.Rows.Add(new object[] {"sss"});
1532 AssertEquals(1, dt.Rows.Count);
1535 public void OnRowChanging (object src, DataRowChangeEventArgs args)
1537 rowActionChanging = args.Action;
1540 public void OnRowChanged (object src, DataRowChangeEventArgs args)
1542 rowActionChanged = args.Action;
1547 #region DataTable.CreateDataReader Tests and DataTable.Load Tests
1549 private DataTable dt;
1551 private void localSetup () {
1552 dt = new DataTable ("test");
1553 dt.Columns.Add ("id", typeof (int));
1554 dt.Columns.Add ("name", typeof (string));
1555 dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
1557 dt.Rows.Add (new object[] { 1, "mono 1" });
1558 dt.Rows.Add (new object[] { 2, "mono 2" });
1559 dt.Rows.Add (new object[] { 3, "mono 3" });
1561 dt.AcceptChanges ();
1565 public void CreateDataReader1 () {
1567 DataTableReader dtr = dt.CreateDataReader ();
1568 Assert ("HasRows", dtr.HasRows);
1569 AssertEquals ("CountCols", dt.Columns.Count, dtr.FieldCount);
1571 while (dtr.Read ()) {
1572 for (int i = 0; i < dtr.FieldCount; i++) {
1573 AssertEquals ("RowData-" + ri + "-" + i, dt.Rows[ri][i],
1581 public void CreateDataReader2 () {
1583 DataTableReader dtr = dt.CreateDataReader ();
1584 Assert ("HasRows", dtr.HasRows);
1585 AssertEquals ("CountCols", dt.Columns.Count, dtr.FieldCount);
1587 AssertEquals ("RowData0-0", 1, dtr[0]);
1588 AssertEquals ("RowData0-1", "mono 1", dtr[1]);
1590 AssertEquals ("RowData1-0", 2, dtr[0]);
1591 AssertEquals ("RowData1-1", "mono 2", dtr[1]);
1593 AssertEquals ("RowData2-0", 3, dtr[0]);
1594 AssertEquals ("RowData2-1", "mono 3", dtr[1]);
1598 public void Load_NoSchema () {
1600 DataTable dtLoad = new DataTable ("LoadNoSchema");
1601 DataTableReader dtr = dt.CreateDataReader ();
1603 TableAssert (dtLoad);
1607 [ExpectedException (typeof (ArgumentException))]
1608 public void Load_Incompatible () {
1610 DataTable dtLoad = new DataTable ("LoadIncompatible");
1611 dtLoad.Columns.Add ("name", typeof (double));
1612 DataTableReader dtr = dt.CreateDataReader ();
1617 public void Load_ExtraColsEqualVal () {
1619 DataTable dtLoad = new DataTable ("LoadExtraCols");
1620 dtLoad.Columns.Add ("id", typeof (int));
1621 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1622 dtLoad.Rows.Add (new object[] { 1 });
1623 dtLoad.Rows.Add (new object[] { 2 });
1624 dtLoad.Rows.Add (new object[] { 3 });
1625 dtLoad.AcceptChanges ();
1626 DataTableReader dtr = dt.CreateDataReader ();
1628 TableAssert (dtLoad);
1632 public void Load_ExtraColsNonEqualVal () {
1634 DataTable dtLoad = new DataTable ("LoadExtraCols");
1635 dtLoad.Columns.Add ("id", typeof (int));
1636 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1637 dtLoad.Rows.Add (new object[] { 4 });
1638 dtLoad.Rows.Add (new object[] { 5 });
1639 dtLoad.Rows.Add (new object[] { 6 });
1640 dtLoad.AcceptChanges ();
1641 DataTableReader dtr = dt.CreateDataReader ();
1643 AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1644 AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1645 AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1646 AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1647 AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1648 AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1649 AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1650 AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1651 AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1652 AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1653 AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1657 [ExpectedException (typeof (ConstraintException))]
1658 public void Load_MissingColsNonNullable () {
1660 DataTable dtLoad = new DataTable ("LoadMissingCols");
1661 dtLoad.Columns.Add ("id", typeof (int));
1662 dtLoad.Columns.Add ("name", typeof (string));
1663 dtLoad.Columns.Add ("missing", typeof (string));
1664 dtLoad.Columns["missing"].AllowDBNull = false;
1665 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1666 dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1667 dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1668 dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1669 dtLoad.AcceptChanges ();
1670 DataTableReader dtr = dt.CreateDataReader ();
1675 public void Load_MissingColsDefault () {
1677 DataTable dtLoad = new DataTable ("LoadMissingCols");
1678 dtLoad.Columns.Add ("id", typeof (int));
1679 dtLoad.Columns.Add ("name", typeof (string));
1680 dtLoad.Columns.Add ("missing", typeof (string));
1681 dtLoad.Columns["missing"].AllowDBNull = false;
1682 dtLoad.Columns["missing"].DefaultValue = "DefaultValue";
1683 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1684 dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1685 dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1686 dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1687 dtLoad.AcceptChanges ();
1688 DataTableReader dtr = dt.CreateDataReader ();
1690 AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1691 AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1692 AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1693 AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1694 AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1695 AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1696 AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1697 AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1698 AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1699 AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1700 AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1701 AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1702 AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1703 AssertEquals ("RowData3-2", "DefaultValue", dtLoad.Rows[3][2]);
1704 AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1705 AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1706 AssertEquals ("RowData4-2", "DefaultValue", dtLoad.Rows[4][2]);
1707 AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1708 AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1709 AssertEquals ("RowData5-2", "DefaultValue", dtLoad.Rows[5][2]);
1713 public void Load_MissingColsNullable () {
1715 DataTable dtLoad = new DataTable ("LoadMissingCols");
1716 dtLoad.Columns.Add ("id", typeof (int));
1717 dtLoad.Columns.Add ("name", typeof (string));
1718 dtLoad.Columns.Add ("missing", typeof (string));
1719 dtLoad.Columns["missing"].AllowDBNull = true;
1720 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1721 dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1722 dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1723 dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1724 dtLoad.AcceptChanges ();
1725 DataTableReader dtr = dt.CreateDataReader ();
1727 AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1728 AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1729 AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1730 AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1731 AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1732 AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1733 AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1734 AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1735 AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1736 AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1737 AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1738 AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1739 AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1740 //AssertEquals ("RowData3-2", null, dtLoad.Rows[3][2]);
1741 AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1742 AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1743 //AssertEquals ("RowData4-2", null, dtLoad.Rows[4][2]);
1744 AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1745 AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1746 //AssertEquals ("RowData5-2", null, dtLoad.Rows[5][2]);
1750 [Category ("NotWorking")]
1751 public void Load_RowStateChanges () {
1753 dt.Rows.Add (new object[] { 4, "mono 4" });
1754 dt.AcceptChanges ();
1755 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
1756 dtLoad.Columns.Add ("id", typeof (int));
1757 dtLoad.Columns.Add ("name", typeof (string));
1758 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1759 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
1760 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
1761 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
1762 dtLoad.AcceptChanges ();
1763 // Update Table with following changes:
1764 // Row0 unmodified, Row1 modified, Row2 deleted, Row3 added.
1765 dtLoad.Rows[1]["name"] = "Modify 2";
1766 dtLoad.Rows[2].Delete ();
1767 DataRow row = dtLoad.NewRow ();
1769 row["name"] = "Add 4";
1770 dtLoad.Rows.Add (row);
1771 DataTableReader dtr = dt.CreateDataReader ();
1773 // asserting Unchanged Row0
1774 AssertEquals ("RowData0-C", "mono 1",
1775 dtLoad.Rows[0][1,DataRowVersion.Current]);
1776 AssertEquals ("RowData0-O", "mono 1",
1777 dtLoad.Rows[0][1,DataRowVersion.Original]);
1778 AssertEquals ("RowState0", DataRowState.Unchanged,
1779 dtLoad.Rows[0].RowState);
1780 // asserting Modified Row1
1781 AssertEquals ("RowData1-C", "Modify 2",
1782 dtLoad.Rows[1][1, DataRowVersion.Current]);
1783 AssertEquals ("RowData1-O", "mono 2",
1784 dtLoad.Rows[1][1, DataRowVersion.Original]);
1785 AssertEquals ("RowState1", DataRowState.Modified,
1786 dtLoad.Rows[1].RowState);
1787 // asserting Deleted Row2
1788 AssertEquals ("RowData1-O", "mono 3",
1789 dtLoad.Rows[2][1, DataRowVersion.Original]);
1790 AssertEquals ("RowState2", DataRowState.Deleted,
1791 dtLoad.Rows[2].RowState);
1792 // asserting Added Row3
1793 AssertEquals ("RowData3-C", "Add 4",
1794 dtLoad.Rows[3][1, DataRowVersion.Current]);
1795 AssertEquals ("RowData3-O", "mono 4",
1796 dtLoad.Rows[3][1, DataRowVersion.Original]);
1797 AssertEquals ("RowState3", DataRowState.Modified,
1798 dtLoad.Rows[3].RowState);
1802 [ExpectedException (typeof (VersionNotFoundException))]
1803 [Category ("NotWorking")]
1804 public void Load_RowStateChangeDelete () {
1806 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
1807 dtLoad.Columns.Add ("id", typeof (int));
1808 dtLoad.Columns.Add ("name", typeof (string));
1809 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1810 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
1811 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
1812 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
1813 dtLoad.AcceptChanges ();
1814 dtLoad.Rows[2].Delete ();
1815 DataTableReader dtr = dt.CreateDataReader ();
1817 AssertEquals ("RowData1-O", " ",
1818 dtLoad.Rows[2][1, DataRowVersion.Current]);
1821 private void TableAssert (DataTable dtAssert) {
1822 AssertEquals ("NColumns", 2, dtAssert.Columns.Count);
1823 AssertEquals ("NRows", 3, dtAssert.Rows.Count);
1824 AssertEquals ("RowData0-0", 1, dtAssert.Rows[0][0]);
1825 AssertEquals ("RowData0-1", "mono 1", dtAssert.Rows[0][1]);
1826 AssertEquals ("RowData1-0", 2, dtAssert.Rows[1][0]);
1827 AssertEquals ("RowData1-1", "mono 2", dtAssert.Rows[1][1]);
1828 AssertEquals ("RowData2-0", 3, dtAssert.Rows[2][0]);
1829 AssertEquals ("RowData2-1", "mono 3", dtAssert.Rows[2][1]);
1832 #endregion // DataTable.CreateDataReader Tests and DataTable.Load Tests
1838 public class MyDataTable:DataTable {
1840 public static int count = 0;
1842 public MyDataTable() {
1851 public class AppDomainsAndFormatInfo
1853 public void Remote ()
1855 int n = (int) Convert.ChangeType ("5", typeof (int));
1856 Assertion.AssertEquals ("n", 5, n);
1860 public void NFIFromBug55978 ()
1862 AppDomain domain = AppDomain.CreateDomain ("testdomain");
1863 AppDomainsAndFormatInfo test = new AppDomainsAndFormatInfo ();
1865 domain.DoCallBack (new CrossAppDomainDelegate (test.Remote));
1866 AppDomain.Unload (domain);
1871 public void Bug55978 ()
1873 DataTable dt = new DataTable ();
1874 dt.Columns.Add ("StartDate", typeof (DateTime));
1877 DateTime date = DateTime.Now;
1879 for (int i = 0; i < 10; i++) {
1881 dr ["StartDate"] = date.AddDays (i);
1885 DataView dv = dt.DefaultView;
1886 dv.RowFilter = "StartDate >= '" + DateTime.Now.AddDays (2) + "' and StartDate <= '" + DateTime.Now.AddDays (4) + "'";
1887 Assertion.AssertEquals ("Table", 10, dt.Rows.Count);
1888 Assertion.AssertEquals ("View", 2, dv.Count);