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
51 DataTable dt = new DataTable();
53 AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
54 Assert("Col",dt.Columns != null);
55 //Assert(dt.ChildRelations != null);
56 Assert("Const", dt.Constraints != null);
57 Assert("ds", dt.DataSet == null);
58 Assert("dv", dt.DefaultView != null);
59 Assert("de", dt.DisplayExpression == "");
60 Assert("ep", dt.ExtendedProperties != null);
61 Assert("he", dt.HasErrors == false);
62 Assert("lc", dt.Locale != null);
63 Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
64 Assert("ns", dt.Namespace == "");
65 //Assert(dt.ParentRelations != null);
66 Assert("pf", dt.Prefix == "");
67 Assert("pk", dt.PrimaryKey != null);
68 Assert("rows", dt.Rows != null);
69 Assert("Site", dt.Site == null);
70 Assert("tname", dt.TableName == "");
77 DataSet Set = new DataSet ();
78 DataTable Mom = new DataTable ("Mom");
79 DataTable Child = new DataTable ("Child");
81 Set.Tables.Add (Child);
83 DataColumn Col = new DataColumn ("Name");
84 DataColumn Col2 = new DataColumn ("ChildName");
85 Mom.Columns.Add (Col);
86 Mom.Columns.Add (Col2);
88 DataColumn Col3 = new DataColumn ("Name");
89 DataColumn Col4 = new DataColumn ("Age");
90 Col4.DataType = Type.GetType ("System.Int16");
91 Child.Columns.Add (Col3);
92 Child.Columns.Add (Col4);
94 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
95 Set.Relations.Add (Relation);
97 DataRow Row = Mom.NewRow ();
123 Row [0] = "'Jhon O'' Collenal'";
127 Row = Child.NewRow ();
130 Child.Rows.Add (Row);
132 Row = Child.NewRow ();
135 Child.Rows.Add (Row);
137 Row = Child.NewRow ();
140 Child.Rows.Add (Row);
142 Row = Child.NewRow ();
145 Child.Rows.Add (Row);
147 Row = Child.NewRow ();
150 Child.Rows.Add (Row);
152 Row = Child.NewRow ();
155 Child.Rows.Add (Row);
157 Row = Child.NewRow ();
160 Child.Rows.Add (Row);
162 DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
163 AssertEquals ("test#01", 2, Rows.Length);
165 // test with apos escaped
166 Rows = Mom.Select ("Name = '''Jhon O'''' Collenal'''");
167 AssertEquals ("test#01.1", 1, Rows.Length);
169 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
170 AssertEquals ("test#02", 0, Rows.Length);
172 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
173 AssertEquals ("test#03", 1, Rows.Length);
175 Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
176 AssertEquals ("test#04", "Mack", Rows [0] [1]);
178 Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
179 AssertEquals ("test#05", 6, Rows.Length);
181 Rows = Child.Select ("age = 20 - 1");
182 AssertEquals ("test#06", 1, Rows.Length);
184 Rows = Child.Select ("age <= 20");
185 AssertEquals ("test#07", 3, Rows.Length);
187 Rows = Child.Select ("age >= 20");
188 AssertEquals ("test#08", 4, Rows.Length);
190 Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
191 AssertEquals ("test#09", 2, Rows.Length);
193 Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
194 AssertEquals ("test#10", 1, Rows.Length);
195 AssertEquals ("test#11", "Mack", Rows [0] [0]);
197 Rows = Child.Select ("not (Name = 'Jack')");
198 AssertEquals ("test#12", 6, Rows.Length);
202 public void Select2 ()
204 DataSet Set = new DataSet ();
205 DataTable Child = new DataTable ("Child");
207 Set.Tables.Add (Child);
209 DataColumn Col3 = new DataColumn ("Name");
210 DataColumn Col4 = new DataColumn ("Age");
211 Col4.DataType = Type.GetType ("System.Int16");
212 Child.Columns.Add (Col3);
213 Child.Columns.Add (Col4);
215 DataRow 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 Row = Child.NewRow ();
233 Child.Rows.Add (Row);
235 Row = Child.NewRow ();
238 Child.Rows.Add (Row);
240 Row = Child.NewRow ();
243 Child.Rows.Add (Row);
245 DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
246 AssertEquals ("test#01", 3, Rows.Length);
247 AssertEquals ("test#02", "Mack", Rows [0] [0]);
248 AssertEquals ("test#03", "Mick", Rows [1] [0]);
249 AssertEquals ("test#04", "Dick", Rows [2] [0]);
251 Rows = Child.Select ("age >= 20", "age asc");
252 AssertEquals ("test#05", 3, Rows.Length);
253 AssertEquals ("test#06", "Dick", Rows [0] [0]);
254 AssertEquals ("test#07", "Mick", Rows [1] [0]);
255 AssertEquals ("test#08", "Mack", Rows [2] [0]);
257 Rows = Child.Select ("age >= 20", "name asc");
258 AssertEquals ("test#09", 3, Rows.Length);
259 AssertEquals ("test#10", "Dick", Rows [0] [0]);
260 AssertEquals ("test#11", "Mack", Rows [1] [0]);
261 AssertEquals ("test#12", "Mick", Rows [2] [0]);
263 Rows = Child.Select ("age >= 20", "name desc");
264 AssertEquals ("test#09", 3, Rows.Length);
265 AssertEquals ("test#10", "Mick", Rows [0] [0]);
266 AssertEquals ("test#11", "Mack", Rows [1] [0]);
267 AssertEquals ("test#12", "Dick", Rows [2] [0]);
272 public void SelectParsing ()
274 DataTable T = new DataTable ("test");
275 DataColumn C = new DataColumn ("name");
277 C = new DataColumn ("age");
278 C.DataType = typeof (int);
280 C = new DataColumn ("id");
283 DataSet Set = new DataSet ("TestSet");
287 for (int i = 0; i < 100; i++) {
289 Row [0] = "human" + i;
301 AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
303 AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
306 T.Select ("name = 1human ");
308 } catch (Exception e) {
310 // missing operand after 'human' operand
311 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
315 T.Select ("name = 1");
317 } catch (Exception e) {
319 // Cannot perform '=' operation between string and Int32
320 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
323 AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
328 public void SelectOperators ()
330 DataTable T = new DataTable ("test");
331 DataColumn C = new DataColumn ("name");
333 C = new DataColumn ("age");
334 C.DataType = typeof (int);
336 C = new DataColumn ("id");
339 DataSet Set = new DataSet ("TestSet");
343 for (int i = 0; i < 100; i++) {
345 Row [0] = "human" + i;
357 AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
358 AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);
359 AssertEquals ("test#03", 12, T.Select ("age< =10").Length);
360 AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
361 AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);
362 AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
363 AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
364 AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
365 // FIXME: Somebody explain how this can be possible.
366 // it seems that it is no matter between 10 - 30. The
367 // result is allways 25 :-P
368 //AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
373 public void SelectExceptions ()
375 DataTable T = new DataTable ("test");
376 DataColumn C = new DataColumn ("name");
378 C = new DataColumn ("age");
379 C.DataType = typeof (int);
381 C = new DataColumn ("id");
384 for (int i = 0; i < 100; i++) {
385 DataRow Row = T.NewRow ();
386 Row [0] = "human" + i;
393 T.Select ("name = human1");
395 } catch (Exception e) {
397 // column name human not found
398 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
401 AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
402 AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
405 T.Select ("id = 1k3");
407 } catch (Exception e) {
409 // no operands after k3 operator
410 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
415 public void SelectStringOperators ()
417 DataTable T = new DataTable ("test");
418 DataColumn C = new DataColumn ("name");
420 C = new DataColumn ("age");
421 C.DataType = typeof (int);
423 C = new DataColumn ("id");
426 DataSet Set = new DataSet ("TestSet");
430 for (int i = 0; i < 100; i++) {
432 Row [0] = "human" + i;
443 AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
445 AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);
446 AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
447 AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);
448 AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
449 AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
451 AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
453 Set.CaseSensitive = true;
454 AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
456 T.CaseSensitive = false;
457 AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
459 T.CaseSensitive = true;
460 AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
462 Set.CaseSensitive = false;
463 AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
465 T.CaseSensitive = false;
466 AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
468 AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
469 AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
470 AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
473 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
475 } catch (Exception e) {
477 // 'h*an1' is invalid
478 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
482 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
484 } catch (Exception e) {
486 // 'h%an1' is invalid
487 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
490 AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
491 AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
496 public void SelectAggregates ()
498 DataTable T = new DataTable ("test");
499 DataColumn C = new DataColumn ("name");
501 C = new DataColumn ("age");
502 C.DataType = typeof (int);
504 C = new DataColumn ("id");
508 for (int i = 0; i < 1000; i++) {
510 Row [0] = "human" + i;
516 AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
517 AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
518 AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
519 AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
520 AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
521 AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
522 AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
526 public void SelectFunctions ()
528 DataTable T = new DataTable ("test");
529 DataColumn C = new DataColumn ("name");
531 C = new DataColumn ("age");
532 C.DataType = typeof (int);
534 C = new DataColumn ("id");
538 for (int i = 0; i < 1000; i++) {
540 Row [0] = "human" + i;
547 Row [0] = "human" + "test";
548 Row [1] = DBNull.Value;
549 Row [2] = DBNull.Value;
552 //TODO: How to test Convert-function
553 AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);
554 AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
555 AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
556 AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);
557 AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);
558 AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
559 AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
560 AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
561 AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
562 AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
567 public void SelectRelations ()
569 DataSet Set = new DataSet ();
570 DataTable Mom = new DataTable ("Mom");
571 DataTable Child = new DataTable ("Child");
573 Set.Tables.Add (Mom);
574 Set.Tables.Add (Child);
576 DataColumn Col = new DataColumn ("Name");
577 DataColumn Col2 = new DataColumn ("ChildName");
578 Mom.Columns.Add (Col);
579 Mom.Columns.Add (Col2);
581 DataColumn Col3 = new DataColumn ("Name");
582 DataColumn Col4 = new DataColumn ("Age");
583 Col4.DataType = Type.GetType ("System.Int16");
584 Child.Columns.Add (Col3);
585 Child.Columns.Add (Col4);
587 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
588 Set.Relations.Add (Relation);
590 DataRow Row = Mom.NewRow ();
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 Row = Child.NewRow ();
633 Child.Rows.Add (Row);
635 Row = Child.NewRow ();
638 Child.Rows.Add (Row);
640 Row = Child.NewRow ();
643 Child.Rows.Add (Row);
645 DataRow [] Rows = Child.Select ("name = Parent.Childname");
646 AssertEquals ("test#01", 6, Rows.Length);
647 Rows = Child.Select ("Parent.childname = 'Jack'");
648 AssertEquals ("test#02", 1, Rows.Length);
652 // FIXME: LAMESPEC: Why the exception is thrown why... why...
653 Mom.Select ("Child.Name = 'Jack'");
655 } catch (Exception e) {
656 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
657 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
661 Rows = Child.Select ("Parent.name = 'Laura'");
662 AssertEquals ("test#06", 3, Rows.Length);
664 DataTable Parent2 = new DataTable ("Parent2");
665 Col = new DataColumn ("Name");
666 Col2 = new DataColumn ("ChildName");
668 Parent2.Columns.Add (Col);
669 Parent2.Columns.Add (Col2);
670 Set.Tables.Add (Parent2);
672 Row = Parent2.NewRow ();
675 Parent2.Rows.Add (Row);
677 Row = Parent2.NewRow ();
680 Parent2.Rows.Add (Row);
682 Row = Parent2.NewRow ();
685 Parent2.Rows.Add (Row);
687 Row = Parent2.NewRow ();
690 Parent2.Rows.Add (Row);
692 Row = Parent2.NewRow ();
695 Parent2.Rows.Add (Row);
697 Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
698 Set.Relations.Add (Relation);
701 Rows = Child.Select ("Parent.ChildName = 'Jack'");
703 } catch (Exception e) {
704 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
705 //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);
708 Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
709 AssertEquals ("test#10", 1, Rows.Length);
711 Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
712 AssertEquals ("test#10", 1, Rows.Length);
715 Mom.Select ("Parent.name = 'John'");
716 } catch (Exception e) {
717 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
718 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
724 public void ToStringTest()
726 DataTable dt = new DataTable();
727 dt.Columns.Add("Col1",typeof(int));
729 dt.TableName = "Mytable";
730 dt.DisplayExpression = "Col1";
733 string cmpr = dt.TableName + " + " + dt.DisplayExpression;
734 AssertEquals(cmpr,dt.ToString());
738 public void PrimaryKey ()
740 DataTable dt = new DataTable ();
741 DataColumn Col = new DataColumn ();
742 Col.AllowDBNull = false;
743 Col.DataType = typeof (int);
744 dt.Columns.Add (Col);
749 AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
751 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
752 AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
753 AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
755 dt.PrimaryKey = null;
756 AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
758 Col = new DataColumn ("failed");
761 dt.PrimaryKey = new DataColumn [] {Col};
763 } catch (Exception e) {
764 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
765 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
768 DataTable dt2 = new DataTable ();
772 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
774 } catch (Exception e) {
775 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
776 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
780 AssertEquals ("test#11", 0, dt.Constraints.Count);
782 dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
783 AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
784 AssertEquals ("test#13", 1, dt.Constraints.Count);
785 AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
786 AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
787 AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);
792 public void PropertyExceptions ()
794 DataSet set = new DataSet ();
795 DataTable table = new DataTable ();
796 DataTable table1 = new DataTable ();
797 set.Tables.Add (table);
798 set.Tables.Add (table1);
800 DataColumn col = new DataColumn ();
801 col.ColumnName = "Id";
802 col.DataType = Type.GetType ("System.Int32");
803 table.Columns.Add (col);
804 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
805 table.Constraints.Add (uc);
806 table.CaseSensitive = false;
808 col = new DataColumn ();
809 col.ColumnName = "Name";
810 col.DataType = Type.GetType ("System.String");
811 table.Columns.Add (col);
813 col = new DataColumn ();
814 col.ColumnName = "Id";
815 col.DataType = Type.GetType ("System.Int32");
816 table1.Columns.Add (col);
817 col = new DataColumn ();
818 col.ColumnName = "Name";
819 col.DataType = Type.GetType ("System.String");
820 table1.Columns.Add (col);
822 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
823 set.Relations.Add (dr);
826 table.CaseSensitive = true;
827 table1.CaseSensitive = true;
830 catch (Exception e) {
831 if (e.GetType () != typeof (AssertionException))
832 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);
834 Console.WriteLine (e);
837 CultureInfo cultureInfo = new CultureInfo ("en-gb");
838 table.Locale = cultureInfo;
839 table1.Locale = cultureInfo;
842 catch (Exception e) {
843 if (e.GetType () != typeof (AssertionException))
844 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);
846 Console.WriteLine (e);
849 table.Prefix = "Prefix#1";
853 if (e.GetType () != typeof (AssertionException))
854 AssertEquals ("#A06", "Prefix 'Prefix#1' is not valid, because it contains special characters.",e.Message);
856 Console.WriteLine (e);
862 public void GetErrors ()
864 DataTable table = new DataTable ();
866 DataColumn col = new DataColumn ();
867 col.ColumnName = "Id";
868 col.DataType = Type.GetType ("System.Int32");
869 table.Columns.Add (col);
871 col = new DataColumn ();
872 col.ColumnName = "Name";
873 col.DataType = Type.GetType ("System.String");
874 table.Columns.Add (col);
876 DataRow row = table.NewRow ();
878 row ["name"] = "Abc";
879 row.RowError = "Error#1";
880 table.Rows.Add (row);
882 AssertEquals ("#A01", 1, table.GetErrors ().Length);
883 AssertEquals ("#A02", "Error#1", (table.GetErrors ())[0].RowError);
886 public void CloneCopyTest ()
888 DataTable table = new DataTable ();
889 table.TableName = "Table#1";
890 DataTable table1 = new DataTable ();
891 table1.TableName = "Table#2";
893 table.AcceptChanges ();
895 DataSet set = new DataSet ("Data Set#1");
896 set.DataSetName = "Dataset#1";
897 set.Tables.Add (table);
898 set.Tables.Add (table1);
900 DataColumn col = new DataColumn ();
901 col.ColumnName = "Id";
902 col.DataType = Type.GetType ("System.Int32");
903 table.Columns.Add (col);
904 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
905 table.Constraints.Add (uc);
907 col = new DataColumn ();
908 col.ColumnName = "Id";
909 col.DataType = Type.GetType ("System.Int32");
910 table1.Columns.Add (col);
912 col = new DataColumn ();
913 col.ColumnName = "Name";
914 col.DataType = Type.GetType ("System.String");
915 table.Columns.Add (col);
917 col = new DataColumn ();
918 col.ColumnName = "Name";
919 col.DataType = Type.GetType ("System.String");
920 table1.Columns.Add (col);
921 DataRow row = table.NewRow ();
923 row ["name"] = "Abc";
924 row.RowError = "Error#1";
925 table.Rows.Add (row);
927 row = table.NewRow ();
929 row ["name"] = "Efg";
930 table.Rows.Add (row);
931 table.AcceptChanges ();
933 table.CaseSensitive = true;
934 table1.CaseSensitive = true;
935 table.MinimumCapacity = 100;
936 table.Prefix = "PrefixNo:1";
937 table.Namespace = "Namespace#1";
938 table.DisplayExpression = "Id / Name + (Id * Id)";
939 DataColumn[] colArray = {table.Columns[0]};
940 table.PrimaryKey = colArray;
941 table.ExtendedProperties.Add ("TimeStamp", DateTime.Now);
942 #if NET_1_1 // This prevents further tests after .NET 1.1.
944 CultureInfo cultureInfo = new CultureInfo ("en-gb");
945 table.Locale = cultureInfo;
948 row = table1.NewRow ();
949 row ["Name"] = "Abc";
951 table1.Rows.Add (row);
953 row = table1.NewRow ();
955 row ["Name"] = "Efg";
956 table1.Rows.Add (row);
958 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
959 set.Relations.Add (dr);
961 //Testing properties of clone
962 DataTable cloneTable = table.Clone ();
963 AssertEquals ("#A01",true ,cloneTable.CaseSensitive);
964 AssertEquals ("#A02", 0 , cloneTable.ChildRelations.Count);
965 AssertEquals ("#A03", 0 , cloneTable.ParentRelations.Count);
966 AssertEquals ("#A04", 2, cloneTable.Columns.Count);
967 AssertEquals ("#A05", 1, cloneTable.Constraints.Count);
968 AssertEquals ("#A06", "Id / Name + (Id * Id)", cloneTable.DisplayExpression);
969 AssertEquals ("#A07", 1 ,cloneTable.ExtendedProperties.Count);
970 AssertEquals ("#A08", false ,cloneTable.HasErrors);
973 AssertEquals ("#A09", 2057, cloneTable.Locale.LCID);
975 AssertEquals ("#A10", 100, cloneTable.MinimumCapacity);
976 AssertEquals ("#A11","Namespace#1", cloneTable.Namespace);
977 AssertEquals ("#A12", "PrefixNo:1",cloneTable.Prefix);
978 AssertEquals ("#A13", "Id", cloneTable.PrimaryKey[0].ColumnName);
979 AssertEquals ("#A14",0 , cloneTable.Rows.Count );
980 AssertEquals ("#A15", "Table#1", cloneTable.TableName);
982 //Testing properties of copy
983 DataTable copyTable = table.Copy ();
984 AssertEquals ("#A16",true ,copyTable.CaseSensitive);
985 AssertEquals ("#A17", 0 , copyTable.ChildRelations.Count);
986 AssertEquals ("#A18", 0 , copyTable.ParentRelations.Count);
987 AssertEquals ("#A19", 2, copyTable.Columns.Count);
988 AssertEquals ("#A20", 1, copyTable.Constraints.Count);
989 AssertEquals ("#A21", "Id / Name + (Id * Id)", copyTable.DisplayExpression);
990 AssertEquals ("#A22", 1 ,copyTable.ExtendedProperties.Count);
991 AssertEquals ("#A23", true ,copyTable.HasErrors);
994 AssertEquals ("#A24", 2057, copyTable.Locale.LCID);
996 AssertEquals ("#A25", 100, copyTable.MinimumCapacity);
997 AssertEquals ("#A26","Namespace#1", copyTable.Namespace);
998 AssertEquals ("#A27", "PrefixNo:1",copyTable.Prefix);
999 AssertEquals ("#A28", "Id", copyTable.PrimaryKey[0].ColumnName);
1000 AssertEquals ("#A29", 2 , copyTable.Rows.Count );
1001 AssertEquals ("#A30", "Table#1", copyTable.TableName);
1005 public void LoadDataException ()
1007 DataTable table = new DataTable ();
1008 DataColumn col = new DataColumn ();
1009 col.ColumnName = "Id";
1010 col.DataType = Type.GetType ("System.Int32");
1011 col.DefaultValue = 47;
1012 table.Columns.Add (col);
1013 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1014 table.Constraints.Add (uc);
1016 col = new DataColumn ();
1017 col.ColumnName = "Name";
1018 col.DataType = Type.GetType ("System.String");
1019 col.DefaultValue = "Hello";
1020 table.Columns.Add (col);
1022 table.BeginLoadData();
1023 object[] row = {147, "Abc"};
1024 DataRow newRow = table.LoadDataRow (row, true);
1026 object[] row1 = {147, "Efg"};
1027 DataRow newRow1 = table.LoadDataRow (row1, true);
1029 object[] row2 = {143, "Hij"};
1030 DataRow newRow2 = table.LoadDataRow (row2, true);
1033 table.EndLoadData ();
1036 catch (ConstraintException) {
1040 public void Changes () //To test GetChanges and RejectChanges
1042 DataTable table = new DataTable ();
1044 DataColumn col = new DataColumn ();
1045 col.ColumnName = "Id";
1046 col.DataType = Type.GetType ("System.Int32");
1047 table.Columns.Add (col);
1048 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1049 table.Constraints.Add (uc);
1051 col = new DataColumn ();
1052 col.ColumnName = "Name";
1053 col.DataType = Type.GetType ("System.String");
1054 table.Columns.Add (col);
1056 DataRow row = table.NewRow ();
1058 row ["name"] = "Abc";
1059 table.Rows.Add (row);
1060 table.AcceptChanges ();
1062 row = table.NewRow ();
1064 row ["name"] = "Efg";
1065 table.Rows.Add (row);
1067 //Testing GetChanges
1068 DataTable changesTable = table.GetChanges ();
1069 AssertEquals ("#A01", 1 ,changesTable.Rows.Count);
1070 AssertEquals ("#A02","Efg" ,changesTable.Rows[0]["Name"]);
1071 table.AcceptChanges ();
1072 changesTable = table.GetChanges ();
1074 int cnt = changesTable.Rows.Count;
1076 catch(Exception e) {
1077 if (e.GetType () != typeof (AssertionException))
1078 AssertEquals ("#A03",typeof(NullReferenceException) ,e.GetType ());
1080 Console.WriteLine (e);
1083 //Testing RejectChanges
1084 row = table.NewRow ();
1086 row ["name"] = "Hij";
1087 table.Rows.Add (row);
1089 (table.Rows [0])["Name"] = "AaBbCc";
1090 table.RejectChanges ();
1091 AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]);
1092 AssertEquals ("#A04", 2, table.Rows.Count);
1096 public void ImportRowTest ()
1098 // build source table
1099 DataTable src = new DataTable ();
1100 src.Columns.Add ("id", typeof (int));
1101 src.Columns.Add ("name", typeof (string));
1103 src.PrimaryKey = new DataColumn [] {src.Columns [0]} ;
1105 src.Rows.Add (new object [] { 1, "mono 1" });
1106 src.Rows.Add (new object [] { 2, "mono 2" });
1107 src.Rows.Add (new object [] { 3, "mono 3" });
1108 src.AcceptChanges ();
1110 src.Rows [0] [1] = "mono changed 1"; // modify 1st row
1111 src.Rows [1].Delete (); // delete 2nd row
1112 // 3rd row is unchanged
1113 src.Rows.Add (new object [] { 4, "mono 4" }); // add 4th row
1115 // build target table
1116 DataTable target = new DataTable ();
1117 target.Columns.Add ("id", typeof (int));
1118 target.Columns.Add ("name", typeof (string));
1120 target.PrimaryKey = new DataColumn [] {target.Columns [0]} ;
1123 target.ImportRow (src.Rows [0]); // import 1st row
1124 target.ImportRow (src.Rows [1]); // import 2nd row
1125 target.ImportRow (src.Rows [2]); // import 3rd row
1126 target.ImportRow (src.Rows [3]); // import 4th row
1129 target.ImportRow (src.Rows [2]); // import 3rd row again
1130 Fail ("#AA1 Should have thrown exception violativ PK");
1131 } catch (ConstraintException e) {}
1134 AssertEquals ("#A1", src.Rows [0].RowState, target.Rows [0].RowState);
1135 AssertEquals ("#A2", src.Rows [1].RowState, target.Rows [1].RowState);
1136 AssertEquals ("#A3", src.Rows [2].RowState, target.Rows [2].RowState);
1137 AssertEquals ("#A4", src.Rows [3].RowState, target.Rows [3].RowState);
1139 // check for modified row (1st row)
1140 AssertEquals ("#B1", (string) src.Rows [0] [1], (string) target.Rows [0] [1]);
1141 AssertEquals ("#B2", (string) src.Rows [0] [1, DataRowVersion.Default], (string) target.Rows [0] [1, DataRowVersion.Default]);
1142 AssertEquals ("#B3", (string) src.Rows [0] [1, DataRowVersion.Original], (string) target.Rows [0] [1, DataRowVersion.Original]);
1143 AssertEquals ("#B4", (string) src.Rows [0] [1, DataRowVersion.Current], (string) target.Rows [0] [1, DataRowVersion.Current]);
1144 AssertEquals ("#B5", false, target.Rows [0].HasVersion(DataRowVersion.Proposed));
1146 // check for deleted row (2nd row)
1147 AssertEquals ("#C1", (string) src.Rows [1] [1, DataRowVersion.Original], (string) target.Rows [1] [1, DataRowVersion.Original]);
1149 // check for unchanged row (3rd row)
1150 AssertEquals ("#D1", (string) src.Rows [2] [1], (string) target.Rows [2] [1]);
1151 AssertEquals ("#D2", (string) src.Rows [2] [1, DataRowVersion.Default], (string) target.Rows [2] [1, DataRowVersion.Default]);
1152 AssertEquals ("#D3", (string) src.Rows [2] [1, DataRowVersion.Original], (string) target.Rows [2] [1, DataRowVersion.Original]);
1153 AssertEquals ("#D4", (string) src.Rows [2] [1, DataRowVersion.Current], (string) target.Rows [2] [1, DataRowVersion.Current]);
1155 // check for newly added row (4th row)
1156 AssertEquals ("#E1", (string) src.Rows [3] [1], (string) target.Rows [3] [1]);
1157 AssertEquals ("#E2", (string) src.Rows [3] [1, DataRowVersion.Default], (string) target.Rows [3] [1, DataRowVersion.Default]);
1158 AssertEquals ("#E3", (string) src.Rows [3] [1, DataRowVersion.Current], (string) target.Rows [3] [1, DataRowVersion.Current]);
1162 public void ImportRowDetachedTest ()
1164 DataTable table = new DataTable ();
1165 DataColumn col = new DataColumn ();
1166 col.ColumnName = "Id";
1167 col.DataType = Type.GetType ("System.Int32");
1168 table.Columns.Add (col);
1170 table.PrimaryKey = new DataColumn [] {col};
1172 col = new DataColumn ();
1173 col.ColumnName = "Name";
1174 col.DataType = Type.GetType ("System.String");
1175 table.Columns.Add (col);
1177 DataRow row = table.NewRow ();
1179 row ["name"] = "Abc";
1181 // keep silent as ms.net ;-), though this is not useful.
1182 table.ImportRow (row);
1186 public void ClearReset () //To test Clear and Reset methods
1188 DataTable table = new DataTable ("table");
1189 DataTable table1 = new DataTable ("table1");
1191 DataSet set = new DataSet ();
1192 set.Tables.Add (table);
1193 set.Tables.Add (table1);
1195 table.Columns.Add ("Id", typeof (int));
1196 table.Columns.Add ("Name", typeof (string));
1197 table.Constraints.Add (new UniqueConstraint ("UK1", table.Columns [0]));
1198 table.CaseSensitive = false;
1200 table1.Columns.Add ("Id", typeof (int));
1201 table1.Columns.Add ("Name", typeof (string));
1203 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
1204 set.Relations.Add (dr);
1206 DataRow row = table.NewRow ();
1208 row ["name"] = "Roopa";
1209 table.Rows.Add (row);
1211 row = table.NewRow ();
1213 row ["Name"] = "roopa";
1214 table.Rows.Add (row);
1216 AssertEquals (2, table.Rows.Count);
1217 AssertEquals (1, table.ChildRelations.Count);
1220 Fail ("#A01, should have thrown ArgumentException");
1222 catch (ArgumentException) {
1224 AssertEquals ("#CT01", 0, table.Rows.Count);
1225 AssertEquals ("#CT02", 0, table.ChildRelations.Count);
1226 AssertEquals ("#CT03", 0, table.ParentRelations.Count);
1227 AssertEquals ("#CT04", 0, table.Constraints.Count);
1230 AssertEquals ("#A05", 0, table1.Rows.Count);
1231 AssertEquals ("#A06", 0, table1.Constraints.Count);
1232 AssertEquals ("#A07", 0, table1.ParentRelations.Count);
1236 AssertEquals ("#A08", 0, table.Rows.Count);
1238 AssertEquals ("#A09", 0, table.Constraints.Count);
1240 AssertEquals ("#A09", 1, table.Constraints.Count);
1242 AssertEquals ("#A10", 0, table.ChildRelations.Count);
1247 public void ClearTest ()
1249 DataTable table = new DataTable ("test");
1250 table.Columns.Add ("id", typeof (int));
1251 table.Columns.Add ("name", typeof (string));
1253 table.PrimaryKey = new DataColumn [] { table.Columns [0] } ;
1255 table.Rows.Add (new object [] { 1, "mono 1" });
1256 table.Rows.Add (new object [] { 2, "mono 2" });
1257 table.Rows.Add (new object [] { 3, "mono 3" });
1258 table.Rows.Add (new object [] { 4, "mono 4" });
1260 table.AcceptChanges ();
1262 _tableClearedEventFired = false;
1263 table.TableCleared += new DataTableClearEventHandler (OnTableCleared);
1268 AssertEquals ("#0 should have fired cleared event", true, _tableClearedEventFired);
1271 DataRow r = table.Rows.Find (1);
1272 AssertEquals ("#1 should have cleared", true, r == null);
1274 // try adding new row. indexes should have cleared
1275 table.Rows.Add (new object [] { 2, "mono 2" });
1276 AssertEquals ("#2 should add row", 1, table.Rows.Count);
1279 private bool _tableClearedEventFired = false;
1280 private void OnTableCleared (object src, DataTableClearEventArgs args)
1282 _tableClearedEventFired = true;
1288 public void Serialize ()
1290 MemoryStream fs = new MemoryStream ();
1292 // Construct a BinaryFormatter and use it
1293 // to serialize the data to the stream.
1294 BinaryFormatter formatter = new BinaryFormatter();
1296 // Create an array with multiple elements refering to
1297 // the one Singleton object.
1298 DataTable dt = new DataTable();
1301 dt.Columns.Add(new DataColumn("Id", typeof(string)));
1302 dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
1303 dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
1304 dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
1305 dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
1308 loRowToAdd = dt.NewRow();
1309 loRowToAdd[0] = "a";
1310 loRowToAdd[1] = "b";
1311 loRowToAdd[2] = "c";
1312 loRowToAdd[3] = "d";
1313 loRowToAdd[4] = "e";
1315 dt.Rows.Add(loRowToAdd);
1317 DataTable[] dtarr = new DataTable[] {dt};
1319 // Serialize the array elements.
1320 formatter.Serialize(fs, dtarr);
1322 // Deserialize the array elements.
1324 DataTable[] a2 = (DataTable[]) formatter.Deserialize(fs);
1326 DataSet ds = new DataSet();
1327 ds.Tables.Add(a2[0]);
1329 StringWriter sw = new StringWriter ();
1331 XmlDocument doc = new XmlDocument ();
1332 doc.LoadXml (sw.ToString ());
1333 AssertEquals (5, doc.DocumentElement.FirstChild.ChildNodes.Count);
1337 [ExpectedException (typeof (DataException))]
1338 [NUnit.Framework.Category ("NotWorking")]
1339 public void SetPrimaryKeyAssertsNonNull ()
1341 DataTable dt = new DataTable ("table");
1342 dt.Columns.Add ("col1");
1343 dt.Columns.Add ("col2");
1344 dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1345 dt.Rows.Add (new object [] {1, 3});
1346 dt.Rows.Add (new object [] {DBNull.Value, 3});
1348 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1352 [ExpectedException (typeof (NoNullAllowedException))]
1353 public void PrimaryKeyColumnChecksNonNull ()
1355 DataTable dt = new DataTable ("table");
1356 dt.Columns.Add ("col1");
1357 dt.Columns.Add ("col2");
1358 dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1359 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1360 dt.Rows.Add (new object [] {1, 3});
1361 dt.Rows.Add (new object [] {DBNull.Value, 3});
1364 void RowChanging (object o, DataRowChangeEventArgs e)
1366 AssertEquals ("changing.Action", rowChangingExpectedAction, e.Action);
1367 rowChangingRowChanging = true;
1370 void RowChanged (object o, DataRowChangeEventArgs e)
1372 AssertEquals ("changed.Action", rowChangingExpectedAction, e.Action);
1373 rowChangingRowChanged = true;
1376 bool rowChangingRowChanging, rowChangingRowChanged;
1377 DataRowAction rowChangingExpectedAction;
1380 public void RowChanging ()
1382 DataTable dt = new DataTable ("table");
1383 dt.Columns.Add ("col1");
1384 dt.Columns.Add ("col2");
1385 dt.RowChanging += new DataRowChangeEventHandler (RowChanging);
1386 dt.RowChanged += new DataRowChangeEventHandler (RowChanged);
1387 rowChangingExpectedAction = DataRowAction.Add;
1388 dt.Rows.Add (new object [] {1, 2});
1389 Assert ("changing,Added", rowChangingRowChanging);
1390 Assert ("changed,Added", rowChangingRowChanged);
1391 rowChangingExpectedAction = DataRowAction.Change;
1392 dt.Rows [0] [0] = 2;
1393 Assert ("changing,Changed", rowChangingRowChanging);
1394 Assert ("changed,Changed", rowChangingRowChanged);
1398 public void CloneSubClassTest()
1400 MyDataTable dt1 = new MyDataTable();
1401 MyDataTable dt = (MyDataTable)(dt1.Clone());
1402 AssertEquals("A#01",2,MyDataTable.count);
1405 DataRowAction rowActionChanging = DataRowAction.Nothing;
1406 DataRowAction rowActionChanged = DataRowAction.Nothing;
1408 public void AcceptChangesTest ()
1410 DataTable dt = new DataTable ("test");
1411 dt.Columns.Add ("id", typeof (int));
1412 dt.Columns.Add ("name", typeof (string));
1414 dt.Rows.Add (new object [] { 1, "mono 1" });
1416 dt.RowChanged += new DataRowChangeEventHandler (OnRowChanged);
1417 dt.RowChanging += new DataRowChangeEventHandler (OnRowChanging);
1420 rowActionChanged = rowActionChanging = DataRowAction.Nothing;
1421 dt.AcceptChanges ();
1423 AssertEquals ("#1 should have fired event and set action to commit",
1424 DataRowAction.Commit, rowActionChanging);
1425 AssertEquals ("#2 should have fired event and set action to commit",
1426 DataRowAction.Commit, rowActionChanged);
1429 dt.RowChanged -= new DataRowChangeEventHandler (OnRowChanged);
1430 dt.RowChanging -= new DataRowChangeEventHandler (OnRowChanging);
1436 public void ColumnObjectTypeTest() {
1437 DataTable dt = new DataTable();
\r
1438 dt.Columns.Add("Series Label", typeof(SqlInt32));
\r
1439 dt.Rows.Add(new object[] {"sss"});
1440 AssertEquals(1, dt.Rows.Count);
1443 public void OnRowChanging (object src, DataRowChangeEventArgs args)
1445 rowActionChanging = args.Action;
1448 public void OnRowChanged (object src, DataRowChangeEventArgs args)
1450 rowActionChanged = args.Action;
1457 public class MyDataTable:DataTable {
1459 public static int count = 0;
1461 public MyDataTable() {
1470 public class AppDomainsAndFormatInfo
1472 public void Remote ()
1474 int n = (int) Convert.ChangeType ("5", typeof (int));
1475 Assertion.AssertEquals ("n", 5, n);
1479 public void NFIFromBug55978 ()
1481 AppDomain domain = AppDomain.CreateDomain ("testdomain");
1482 AppDomainsAndFormatInfo test = new AppDomainsAndFormatInfo ();
1484 domain.DoCallBack (new CrossAppDomainDelegate (test.Remote));
1485 AppDomain.Unload (domain);
1489 public void Bug55978 ()
1491 DataTable dt = new DataTable ();
1492 dt.Columns.Add ("StartDate", typeof (DateTime));
1495 DateTime date = DateTime.Now;
1497 for (int i = 0; i < 10; i++) {
1499 dr ["StartDate"] = date.AddDays (i);
1503 DataView dv = dt.DefaultView;
1504 dv.RowFilter = "StartDate >= '" + DateTime.Now.AddDays (2) + "' and StartDate <= '" + DateTime.Now.AddDays (4) + "'";
1505 Assertion.AssertEquals ("Table", 10, dt.Rows.Count);
1506 Assertion.AssertEquals ("View", 2, dv.Count);