1 // DataTableTest.cs - NUnit Test Cases for testing the DataTable
4 // Franklin Wise (gracenote@earthlink.net)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 // Hagit Yidov (hagity@mainsoft.com)
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using NUnit.Framework;
38 using System.Data.SqlTypes;
39 using System.Globalization;
41 using System.Runtime.Serialization.Formatters.Binary;
43 using MonoTests.System.Data.Utils;
44 using System.Collections;
46 namespace MonoTests.System.Data
49 public class DataTableTest : DataSetAssertion
51 string EOL = Environment.NewLine;
56 DataTable dt = new DataTable();
58 AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
59 Assert("Col",dt.Columns != null);
60 //Assert(dt.ChildRelations != null);
61 Assert("Const", dt.Constraints != null);
62 Assert("ds", dt.DataSet == null);
63 Assert("dv", dt.DefaultView != null);
64 Assert("de", dt.DisplayExpression == "");
65 Assert("ep", dt.ExtendedProperties != null);
66 Assert("he", dt.HasErrors == false);
67 Assert("lc", dt.Locale != null);
68 Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
69 Assert("ns", dt.Namespace == "");
70 //Assert(dt.ParentRelations != null);
71 Assert("pf", dt.Prefix == "");
72 Assert("pk", dt.PrimaryKey != null);
73 Assert("rows", dt.Rows != null);
74 Assert("Site", dt.Site == null);
75 Assert("tname", dt.TableName == "");
82 DataSet Set = new DataSet ();
83 DataTable Mom = new DataTable ("Mom");
84 DataTable Child = new DataTable ("Child");
86 Set.Tables.Add (Child);
88 DataColumn Col = new DataColumn ("Name");
89 DataColumn Col2 = new DataColumn ("ChildName");
90 Mom.Columns.Add (Col);
91 Mom.Columns.Add (Col2);
93 DataColumn Col3 = new DataColumn ("Name");
94 DataColumn Col4 = new DataColumn ("Age");
95 Col4.DataType = Type.GetType ("System.Int16");
96 Child.Columns.Add (Col3);
97 Child.Columns.Add (Col4);
99 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
100 Set.Relations.Add (Relation);
102 DataRow Row = Mom.NewRow ();
128 Row [0] = "'Jhon O'' Collenal'";
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 Row = Child.NewRow ();
165 Child.Rows.Add (Row);
167 DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
168 AssertEquals ("test#01", 2, Rows.Length);
170 // test with apos escaped
171 Rows = Mom.Select ("Name = '''Jhon O'''' Collenal'''");
172 AssertEquals ("test#01.1", 1, Rows.Length);
174 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
175 AssertEquals ("test#02", 0, Rows.Length);
177 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
178 AssertEquals ("test#03", 1, Rows.Length);
180 Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
181 AssertEquals ("test#04", "Mack", Rows [0] [1]);
183 Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
184 AssertEquals ("test#05", 6, Rows.Length);
186 Rows = Child.Select ("age = 20 - 1");
187 AssertEquals ("test#06", 1, Rows.Length);
189 Rows = Child.Select ("age <= 20");
190 AssertEquals ("test#07", 3, Rows.Length);
192 Rows = Child.Select ("age >= 20");
193 AssertEquals ("test#08", 4, Rows.Length);
195 Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
196 AssertEquals ("test#09", 2, Rows.Length);
198 Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
199 AssertEquals ("test#10", 1, Rows.Length);
200 AssertEquals ("test#11", "Mack", Rows [0] [0]);
202 Rows = Child.Select ("not (Name = 'Jack')");
203 AssertEquals ("test#12", 6, Rows.Length);
207 public void Select2 ()
209 DataSet Set = new DataSet ();
210 DataTable Child = new DataTable ("Child");
212 Set.Tables.Add (Child);
214 DataColumn Col3 = new DataColumn ("Name");
215 DataColumn Col4 = new DataColumn ("Age");
216 Col4.DataType = Type.GetType ("System.Int16");
217 Child.Columns.Add (Col3);
218 Child.Columns.Add (Col4);
220 DataRow 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 Row = Child.NewRow ();
248 Child.Rows.Add (Row);
250 DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
251 AssertEquals ("test#01", 3, Rows.Length);
252 AssertEquals ("test#02", "Mack", Rows [0] [0]);
253 AssertEquals ("test#03", "Mick", Rows [1] [0]);
254 AssertEquals ("test#04", "Dick", Rows [2] [0]);
256 Rows = Child.Select ("age >= 20", "age asc");
257 AssertEquals ("test#05", 3, Rows.Length);
258 AssertEquals ("test#06", "Dick", Rows [0] [0]);
259 AssertEquals ("test#07", "Mick", Rows [1] [0]);
260 AssertEquals ("test#08", "Mack", Rows [2] [0]);
262 Rows = Child.Select ("age >= 20", "name asc");
263 AssertEquals ("test#09", 3, Rows.Length);
264 AssertEquals ("test#10", "Dick", Rows [0] [0]);
265 AssertEquals ("test#11", "Mack", Rows [1] [0]);
266 AssertEquals ("test#12", "Mick", Rows [2] [0]);
268 Rows = Child.Select ("age >= 20", "name desc");
269 AssertEquals ("test#09", 3, Rows.Length);
270 AssertEquals ("test#10", "Mick", Rows [0] [0]);
271 AssertEquals ("test#11", "Mack", Rows [1] [0]);
272 AssertEquals ("test#12", "Dick", Rows [2] [0]);
277 public void SelectParsing ()
279 DataTable T = new DataTable ("test");
280 DataColumn C = new DataColumn ("name");
282 C = new DataColumn ("age");
283 C.DataType = typeof (int);
285 C = new DataColumn ("id");
288 DataSet Set = new DataSet ("TestSet");
292 for (int i = 0; i < 100; i++) {
294 Row [0] = "human" + i;
306 AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
308 AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
311 T.Select ("name = 1human ");
313 } catch (Exception e) {
315 // missing operand after 'human' operand
316 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
320 T.Select ("name = 1");
322 } catch (Exception e) {
324 // Cannot perform '=' operation between string and Int32
325 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
328 AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
333 public void SelectEscaping () {
334 DataTable dt = new DataTable ();
335 dt.Columns.Add ("SomeCol");
336 dt.Rows.Add (new object [] {"\t"});
337 dt.Rows.Add (new object [] {"\\"});
339 AssertEquals ("test#01", 1, dt.Select (@"SomeCol='\t'").Length);
340 AssertEquals ("test#02", 1, dt.Select (@"SomeCol='\\'").Length);
343 dt.Select (@"SomeCol='\x'");
345 } catch (SyntaxErrorException) {}
349 public void SelectOperators ()
351 DataTable T = new DataTable ("test");
352 DataColumn C = new DataColumn ("name");
354 C = new DataColumn ("age");
355 C.DataType = typeof (int);
357 C = new DataColumn ("id");
360 DataSet Set = new DataSet ("TestSet");
364 for (int i = 0; i < 100; i++) {
366 Row [0] = "human" + i;
378 AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
379 AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);
380 AssertEquals ("test#03", 12, T.Select ("age< =10").Length);
381 AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
382 AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);
383 AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
384 AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
385 AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
386 // FIXME: Somebody explain how this can be possible.
387 // it seems that it is no matter between 10 - 30. The
388 // result is allways 25 :-P
389 //AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
394 public void SelectExceptions ()
396 DataTable T = new DataTable ("test");
397 DataColumn C = new DataColumn ("name");
399 C = new DataColumn ("age");
400 C.DataType = typeof (int);
402 C = new DataColumn ("id");
405 for (int i = 0; i < 100; i++) {
406 DataRow Row = T.NewRow ();
407 Row [0] = "human" + i;
414 T.Select ("name = human1");
416 } catch (Exception e) {
418 // column name human not found
419 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
422 AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
423 AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
426 T.Select ("id = 1k3");
428 } catch (Exception e) {
430 // no operands after k3 operator
431 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
436 public void SelectStringOperators ()
438 DataTable T = new DataTable ("test");
439 DataColumn C = new DataColumn ("name");
441 C = new DataColumn ("age");
442 C.DataType = typeof (int);
444 C = new DataColumn ("id");
447 DataSet Set = new DataSet ("TestSet");
451 for (int i = 0; i < 100; i++) {
453 Row [0] = "human" + i;
464 AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
466 AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);
467 AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
468 AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);
469 AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
470 AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
472 AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
474 Set.CaseSensitive = true;
475 AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
477 T.CaseSensitive = false;
478 AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
480 T.CaseSensitive = true;
481 AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
483 Set.CaseSensitive = false;
484 AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
486 T.CaseSensitive = false;
487 AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
489 AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
490 AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
491 AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
494 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
496 } catch (Exception e) {
498 // 'h*an1' is invalid
499 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
503 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
505 } catch (Exception e) {
507 // 'h%an1' is invalid
508 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
511 AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
512 AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
517 public void SelectAggregates ()
519 DataTable T = new DataTable ("test");
520 DataColumn C = new DataColumn ("name");
522 C = new DataColumn ("age");
523 C.DataType = typeof (int);
525 C = new DataColumn ("id");
529 for (int i = 0; i < 1000; i++) {
531 Row [0] = "human" + i;
537 AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
538 AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
539 AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
540 AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
541 AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
542 AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
543 AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
547 public void SelectFunctions ()
549 DataTable T = new DataTable ("test");
550 DataColumn C = new DataColumn ("name");
552 C = new DataColumn ("age");
553 C.DataType = typeof (int);
555 C = new DataColumn ("id");
559 for (int i = 0; i < 1000; i++) {
561 Row [0] = "human" + i;
568 Row [0] = "human" + "test";
569 Row [1] = DBNull.Value;
570 Row [2] = DBNull.Value;
573 //TODO: How to test Convert-function
574 AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);
575 AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
576 AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
577 AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);
578 AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);
579 AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
580 AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
581 AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
582 AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
583 AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
588 public void SelectRelations ()
590 DataSet Set = new DataSet ();
591 DataTable Mom = new DataTable ("Mom");
592 DataTable Child = new DataTable ("Child");
594 Set.Tables.Add (Mom);
595 Set.Tables.Add (Child);
597 DataColumn Col = new DataColumn ("Name");
598 DataColumn Col2 = new DataColumn ("ChildName");
599 Mom.Columns.Add (Col);
600 Mom.Columns.Add (Col2);
602 DataColumn Col3 = new DataColumn ("Name");
603 DataColumn Col4 = new DataColumn ("Age");
604 Col4.DataType = Type.GetType ("System.Int16");
605 Child.Columns.Add (Col3);
606 Child.Columns.Add (Col4);
608 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
609 Set.Relations.Add (Relation);
611 DataRow Row = Mom.NewRow ();
636 Row = Child.NewRow ();
639 Child.Rows.Add (Row);
641 Row = Child.NewRow ();
644 Child.Rows.Add (Row);
646 Row = Child.NewRow ();
649 Child.Rows.Add (Row);
651 Row = Child.NewRow ();
654 Child.Rows.Add (Row);
656 Row = Child.NewRow ();
659 Child.Rows.Add (Row);
661 Row = Child.NewRow ();
664 Child.Rows.Add (Row);
666 DataRow [] Rows = Child.Select ("name = Parent.Childname");
667 AssertEquals ("test#01", 6, Rows.Length);
668 Rows = Child.Select ("Parent.childname = 'Jack'");
669 AssertEquals ("test#02", 1, Rows.Length);
673 // FIXME: LAMESPEC: Why the exception is thrown why... why...
674 Mom.Select ("Child.Name = 'Jack'");
676 } catch (Exception e) {
677 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
678 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
682 Rows = Child.Select ("Parent.name = 'Laura'");
683 AssertEquals ("test#06", 3, Rows.Length);
685 DataTable Parent2 = new DataTable ("Parent2");
686 Col = new DataColumn ("Name");
687 Col2 = new DataColumn ("ChildName");
689 Parent2.Columns.Add (Col);
690 Parent2.Columns.Add (Col2);
691 Set.Tables.Add (Parent2);
693 Row = Parent2.NewRow ();
696 Parent2.Rows.Add (Row);
698 Row = Parent2.NewRow ();
701 Parent2.Rows.Add (Row);
703 Row = Parent2.NewRow ();
706 Parent2.Rows.Add (Row);
708 Row = Parent2.NewRow ();
711 Parent2.Rows.Add (Row);
713 Row = Parent2.NewRow ();
716 Parent2.Rows.Add (Row);
718 Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
719 Set.Relations.Add (Relation);
722 Rows = Child.Select ("Parent.ChildName = 'Jack'");
724 } catch (Exception e) {
725 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
726 //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);
729 Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
730 AssertEquals ("test#10", 1, Rows.Length);
732 Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
733 AssertEquals ("test#10", 1, Rows.Length);
736 Mom.Select ("Parent.name = 'John'");
737 } catch (Exception e) {
738 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
739 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
745 public void SelectRowState()
747 DataTable d = new DataTable();
748 d.Columns.Add (new DataColumn ("aaa"));
749 DataRow [] rows = d.Select (null, null, DataViewRowState.Deleted);
750 AssertEquals(0, rows.Length);
751 d.Rows.Add (new object [] {"bbb"});
752 d.Rows.Add (new object [] {"bbb"});
753 rows = d.Select (null, null, DataViewRowState.Deleted);
754 AssertEquals(0, rows.Length);
758 public void ToStringTest()
760 DataTable dt = new DataTable();
761 dt.Columns.Add("Col1",typeof(int));
763 dt.TableName = "Mytable";
764 dt.DisplayExpression = "Col1";
767 string cmpr = dt.TableName + " + " + dt.DisplayExpression;
768 AssertEquals(cmpr,dt.ToString());
772 public void PrimaryKey ()
774 DataTable dt = new DataTable ();
775 DataColumn Col = new DataColumn ();
776 Col.AllowDBNull = false;
777 Col.DataType = typeof (int);
778 dt.Columns.Add (Col);
783 AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
785 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
786 AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
787 AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
789 dt.PrimaryKey = null;
790 AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
792 Col = new DataColumn ("failed");
795 dt.PrimaryKey = new DataColumn [] {Col};
797 } catch (Exception e) {
798 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
799 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
802 DataTable dt2 = new DataTable ();
806 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
808 } catch (Exception e) {
809 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
810 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
814 AssertEquals ("test#11", 0, dt.Constraints.Count);
816 dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
817 AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
818 AssertEquals ("test#13", 1, dt.Constraints.Count);
819 AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
820 AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
821 AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);
826 public void PropertyExceptions ()
828 DataSet set = new DataSet ();
829 DataTable table = new DataTable ();
830 DataTable table1 = new DataTable ();
831 set.Tables.Add (table);
832 set.Tables.Add (table1);
834 DataColumn col = new DataColumn ();
835 col.ColumnName = "Id";
836 col.DataType = Type.GetType ("System.Int32");
837 table.Columns.Add (col);
838 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
839 table.Constraints.Add (uc);
840 table.CaseSensitive = false;
842 col = new DataColumn ();
843 col.ColumnName = "Name";
844 col.DataType = Type.GetType ("System.String");
845 table.Columns.Add (col);
847 col = new DataColumn ();
848 col.ColumnName = "Id";
849 col.DataType = Type.GetType ("System.Int32");
850 table1.Columns.Add (col);
851 col = new DataColumn ();
852 col.ColumnName = "Name";
853 col.DataType = Type.GetType ("System.String");
854 table1.Columns.Add (col);
856 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
857 set.Relations.Add (dr);
860 table.CaseSensitive = true;
861 table1.CaseSensitive = true;
864 catch (Exception e) {
865 if (e.GetType () != typeof (AssertionException))
866 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);
868 Console.WriteLine (e);
871 CultureInfo cultureInfo = new CultureInfo ("en-gb");
872 table.Locale = cultureInfo;
873 table1.Locale = cultureInfo;
876 catch (Exception e) {
877 if (e.GetType () != typeof (AssertionException))
878 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);
880 Console.WriteLine (e);
883 table.Prefix = "Prefix#1";
887 if (e.GetType () != typeof (AssertionException))
888 AssertEquals ("#A06", "Prefix 'Prefix#1' is not valid, because it contains special characters.",e.Message);
890 Console.WriteLine (e);
896 public void GetErrors ()
898 DataTable table = new DataTable ();
900 DataColumn col = new DataColumn ();
901 col.ColumnName = "Id";
902 col.DataType = Type.GetType ("System.Int32");
903 table.Columns.Add (col);
905 col = new DataColumn ();
906 col.ColumnName = "Name";
907 col.DataType = Type.GetType ("System.String");
908 table.Columns.Add (col);
910 DataRow row = table.NewRow ();
912 row ["name"] = "Abc";
913 row.RowError = "Error#1";
914 table.Rows.Add (row);
916 AssertEquals ("#A01", 1, table.GetErrors ().Length);
917 AssertEquals ("#A02", "Error#1", (table.GetErrors ())[0].RowError);
920 public void CloneCopyTest ()
922 DataTable table = new DataTable ();
923 table.TableName = "Table#1";
924 DataTable table1 = new DataTable ();
925 table1.TableName = "Table#2";
927 table.AcceptChanges ();
929 DataSet set = new DataSet ("Data Set#1");
930 set.DataSetName = "Dataset#1";
931 set.Tables.Add (table);
932 set.Tables.Add (table1);
934 DataColumn col = new DataColumn ();
935 col.ColumnName = "Id";
936 col.DataType = Type.GetType ("System.Int32");
937 table.Columns.Add (col);
938 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
939 table.Constraints.Add (uc);
941 col = new DataColumn ();
942 col.ColumnName = "Id";
943 col.DataType = Type.GetType ("System.Int32");
944 table1.Columns.Add (col);
946 col = new DataColumn ();
947 col.ColumnName = "Name";
948 col.DataType = Type.GetType ("System.String");
949 table.Columns.Add (col);
951 col = new DataColumn ();
952 col.ColumnName = "Name";
953 col.DataType = Type.GetType ("System.String");
954 table1.Columns.Add (col);
955 DataRow row = table.NewRow ();
957 row ["name"] = "Abc";
958 row.RowError = "Error#1";
959 table.Rows.Add (row);
961 row = table.NewRow ();
963 row ["name"] = "Efg";
964 table.Rows.Add (row);
965 table.AcceptChanges ();
967 table.CaseSensitive = true;
968 table1.CaseSensitive = true;
969 table.MinimumCapacity = 100;
970 table.Prefix = "PrefixNo:1";
971 table.Namespace = "Namespace#1";
972 table.DisplayExpression = "Id / Name + (Id * Id)";
973 DataColumn[] colArray = {table.Columns[0]};
974 table.PrimaryKey = colArray;
975 table.ExtendedProperties.Add ("TimeStamp", DateTime.Now);
976 #if NET_1_1 // This prevents further tests after .NET 1.1.
978 CultureInfo cultureInfo = new CultureInfo ("en-gb");
979 table.Locale = cultureInfo;
982 row = table1.NewRow ();
983 row ["Name"] = "Abc";
985 table1.Rows.Add (row);
987 row = table1.NewRow ();
989 row ["Name"] = "Efg";
990 table1.Rows.Add (row);
992 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
993 set.Relations.Add (dr);
995 //Testing properties of clone
996 DataTable cloneTable = table.Clone ();
997 AssertEquals ("#A01",true ,cloneTable.CaseSensitive);
998 AssertEquals ("#A02", 0 , cloneTable.ChildRelations.Count);
999 AssertEquals ("#A03", 0 , cloneTable.ParentRelations.Count);
1000 AssertEquals ("#A04", 2, cloneTable.Columns.Count);
1001 AssertEquals ("#A05", 1, cloneTable.Constraints.Count);
1002 AssertEquals ("#A06", "Id / Name + (Id * Id)", cloneTable.DisplayExpression);
1003 AssertEquals ("#A07", 1 ,cloneTable.ExtendedProperties.Count);
1004 AssertEquals ("#A08", false ,cloneTable.HasErrors);
1007 AssertEquals ("#A09", 2057, cloneTable.Locale.LCID);
1009 AssertEquals ("#A10", 100, cloneTable.MinimumCapacity);
1010 AssertEquals ("#A11","Namespace#1", cloneTable.Namespace);
1011 AssertEquals ("#A12", "PrefixNo:1",cloneTable.Prefix);
1012 AssertEquals ("#A13", "Id", cloneTable.PrimaryKey[0].ColumnName);
1013 AssertEquals ("#A14",0 , cloneTable.Rows.Count );
1014 AssertEquals ("#A15", "Table#1", cloneTable.TableName);
1016 //Testing properties of copy
1017 DataTable copyTable = table.Copy ();
1018 AssertEquals ("#A16",true ,copyTable.CaseSensitive);
1019 AssertEquals ("#A17", 0 , copyTable.ChildRelations.Count);
1020 AssertEquals ("#A18", 0 , copyTable.ParentRelations.Count);
1021 AssertEquals ("#A19", 2, copyTable.Columns.Count);
1022 AssertEquals ("#A20", 1, copyTable.Constraints.Count);
1023 AssertEquals ("#A21", "Id / Name + (Id * Id)", copyTable.DisplayExpression);
1024 AssertEquals ("#A22", 1 ,copyTable.ExtendedProperties.Count);
1025 AssertEquals ("#A23", true ,copyTable.HasErrors);
1028 AssertEquals ("#A24", 2057, copyTable.Locale.LCID);
1030 AssertEquals ("#A25", 100, copyTable.MinimumCapacity);
1031 AssertEquals ("#A26","Namespace#1", copyTable.Namespace);
1032 AssertEquals ("#A27", "PrefixNo:1",copyTable.Prefix);
1033 AssertEquals ("#A28", "Id", copyTable.PrimaryKey[0].ColumnName);
1034 AssertEquals ("#A29", 2 , copyTable.Rows.Count );
1035 AssertEquals ("#A30", "Table#1", copyTable.TableName);
1039 public void LoadDataException ()
1041 DataTable table = new DataTable ();
1042 DataColumn col = new DataColumn ();
1043 col.ColumnName = "Id";
1044 col.DataType = Type.GetType ("System.Int32");
1045 col.DefaultValue = 47;
1046 table.Columns.Add (col);
1047 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1048 table.Constraints.Add (uc);
1050 col = new DataColumn ();
1051 col.ColumnName = "Name";
1052 col.DataType = Type.GetType ("System.String");
1053 col.DefaultValue = "Hello";
1054 table.Columns.Add (col);
1056 table.BeginLoadData();
1057 object[] row = {147, "Abc"};
1058 DataRow newRow = table.LoadDataRow (row, true);
1060 object[] row1 = {147, "Efg"};
1061 DataRow newRow1 = table.LoadDataRow (row1, true);
1063 object[] row2 = {143, "Hij"};
1064 DataRow newRow2 = table.LoadDataRow (row2, true);
1067 table.EndLoadData ();
1070 catch (ConstraintException) {
1074 public void Changes () //To test GetChanges and RejectChanges
1076 DataTable table = new DataTable ();
1078 DataColumn col = new DataColumn ();
1079 col.ColumnName = "Id";
1080 col.DataType = Type.GetType ("System.Int32");
1081 table.Columns.Add (col);
1082 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1083 table.Constraints.Add (uc);
1085 col = new DataColumn ();
1086 col.ColumnName = "Name";
1087 col.DataType = Type.GetType ("System.String");
1088 table.Columns.Add (col);
1090 DataRow row = table.NewRow ();
1092 row ["name"] = "Abc";
1093 table.Rows.Add (row);
1094 table.AcceptChanges ();
1096 row = table.NewRow ();
1098 row ["name"] = "Efg";
1099 table.Rows.Add (row);
1101 //Testing GetChanges
1102 DataTable changesTable = table.GetChanges ();
1103 AssertEquals ("#A01", 1 ,changesTable.Rows.Count);
1104 AssertEquals ("#A02","Efg" ,changesTable.Rows[0]["Name"]);
1105 table.AcceptChanges ();
1106 changesTable = table.GetChanges ();
1108 int cnt = changesTable.Rows.Count;
1110 catch(Exception e) {
1111 if (e.GetType () != typeof (AssertionException))
1112 AssertEquals ("#A03",typeof(NullReferenceException) ,e.GetType ());
1114 Console.WriteLine (e);
1117 //Testing RejectChanges
1118 row = table.NewRow ();
1120 row ["name"] = "Hij";
1121 table.Rows.Add (row);
1123 (table.Rows [0])["Name"] = "AaBbCc";
1124 table.RejectChanges ();
1125 AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]);
1126 AssertEquals ("#A04", 2, table.Rows.Count);
1130 public void ImportRowTest ()
1132 // build source table
1133 DataTable src = new DataTable ();
1134 src.Columns.Add ("id", typeof (int));
1135 src.Columns.Add ("name", typeof (string));
1137 src.PrimaryKey = new DataColumn [] {src.Columns [0]} ;
1139 src.Rows.Add (new object [] { 1, "mono 1" });
1140 src.Rows.Add (new object [] { 2, "mono 2" });
1141 src.Rows.Add (new object [] { 3, "mono 3" });
1142 src.AcceptChanges ();
1144 src.Rows [0] [1] = "mono changed 1"; // modify 1st row
1145 src.Rows [1].Delete (); // delete 2nd row
1146 // 3rd row is unchanged
1147 src.Rows.Add (new object [] { 4, "mono 4" }); // add 4th row
1149 // build target table
1150 DataTable target = new DataTable ();
1151 target.Columns.Add ("id", typeof (int));
1152 target.Columns.Add ("name", typeof (string));
1154 target.PrimaryKey = new DataColumn [] {target.Columns [0]} ;
1157 target.ImportRow (src.Rows [0]); // import 1st row
1158 target.ImportRow (src.Rows [1]); // import 2nd row
1159 target.ImportRow (src.Rows [2]); // import 3rd row
1160 target.ImportRow (src.Rows [3]); // import 4th row
1163 target.ImportRow (src.Rows [2]); // import 3rd row again
1164 Fail ("#AA1 Should have thrown exception violativ PK");
1165 } catch (ConstraintException e) {}
1168 AssertEquals ("#A1", src.Rows [0].RowState, target.Rows [0].RowState);
1169 AssertEquals ("#A2", src.Rows [1].RowState, target.Rows [1].RowState);
1170 AssertEquals ("#A3", src.Rows [2].RowState, target.Rows [2].RowState);
1171 AssertEquals ("#A4", src.Rows [3].RowState, target.Rows [3].RowState);
1173 // check for modified row (1st row)
1174 AssertEquals ("#B1", (string) src.Rows [0] [1], (string) target.Rows [0] [1]);
1175 AssertEquals ("#B2", (string) src.Rows [0] [1, DataRowVersion.Default], (string) target.Rows [0] [1, DataRowVersion.Default]);
1176 AssertEquals ("#B3", (string) src.Rows [0] [1, DataRowVersion.Original], (string) target.Rows [0] [1, DataRowVersion.Original]);
1177 AssertEquals ("#B4", (string) src.Rows [0] [1, DataRowVersion.Current], (string) target.Rows [0] [1, DataRowVersion.Current]);
1178 AssertEquals ("#B5", false, target.Rows [0].HasVersion(DataRowVersion.Proposed));
1180 // check for deleted row (2nd row)
1181 AssertEquals ("#C1", (string) src.Rows [1] [1, DataRowVersion.Original], (string) target.Rows [1] [1, DataRowVersion.Original]);
1183 // check for unchanged row (3rd row)
1184 AssertEquals ("#D1", (string) src.Rows [2] [1], (string) target.Rows [2] [1]);
1185 AssertEquals ("#D2", (string) src.Rows [2] [1, DataRowVersion.Default], (string) target.Rows [2] [1, DataRowVersion.Default]);
1186 AssertEquals ("#D3", (string) src.Rows [2] [1, DataRowVersion.Original], (string) target.Rows [2] [1, DataRowVersion.Original]);
1187 AssertEquals ("#D4", (string) src.Rows [2] [1, DataRowVersion.Current], (string) target.Rows [2] [1, DataRowVersion.Current]);
1189 // check for newly added row (4th row)
1190 AssertEquals ("#E1", (string) src.Rows [3] [1], (string) target.Rows [3] [1]);
1191 AssertEquals ("#E2", (string) src.Rows [3] [1, DataRowVersion.Default], (string) target.Rows [3] [1, DataRowVersion.Default]);
1192 AssertEquals ("#E3", (string) src.Rows [3] [1, DataRowVersion.Current], (string) target.Rows [3] [1, DataRowVersion.Current]);
1196 public void ImportRowDetachedTest ()
1198 DataTable table = new DataTable ();
1199 DataColumn col = new DataColumn ();
1200 col.ColumnName = "Id";
1201 col.DataType = Type.GetType ("System.Int32");
1202 table.Columns.Add (col);
1204 table.PrimaryKey = new DataColumn [] {col};
1206 col = new DataColumn ();
1207 col.ColumnName = "Name";
1208 col.DataType = Type.GetType ("System.String");
1209 table.Columns.Add (col);
1211 DataRow row = table.NewRow ();
1213 row ["name"] = "Abc";
1215 // keep silent as ms.net ;-), though this is not useful.
1216 table.ImportRow (row);
1218 //if RowState is detached, then dont import the row.
1219 AssertEquals ("#1", 0, table.Rows.Count);
1223 public void ImportRowDeletedTest ()
1225 DataTable table = new DataTable ();
1226 table.Columns.Add ("col", typeof (int));
1227 table.Columns.Add ("col1", typeof (int));
1229 DataRow row = table.Rows.Add (new object[] {1,2});
1230 table.PrimaryKey = new DataColumn[] {table.Columns[0]};
1231 table.AcceptChanges ();
1233 // If row is in Deleted state, then ImportRow loads the
1236 table.ImportRow (row);
1237 AssertEquals ("#1", 2, table.Rows.Count);
1239 // Both the deleted rows shud be now gone
1240 table.AcceptChanges ();
1241 AssertEquals ("#2", 0, table.Rows.Count);
1243 //just add another row
1244 row = table.Rows.Add (new object[] {1,2});
1245 // no exception shud be thrown
1246 table.AcceptChanges ();
1248 // If row is in Deleted state, then ImportRow loads the
1249 // row and validate only on RejectChanges
1251 table.ImportRow (row);
1252 AssertEquals ("#3", 2, table.Rows.Count);
1253 AssertEquals ("#4", DataRowState.Deleted, table.Rows[1].RowState);
1256 table.RejectChanges ();
1258 } catch (ConstraintException e) {
1263 public void ClearReset () //To test Clear and Reset methods
1265 DataTable table = new DataTable ("table");
1266 DataTable table1 = new DataTable ("table1");
1268 DataSet set = new DataSet ();
1269 set.Tables.Add (table);
1270 set.Tables.Add (table1);
1272 table.Columns.Add ("Id", typeof (int));
1273 table.Columns.Add ("Name", typeof (string));
1274 table.Constraints.Add (new UniqueConstraint ("UK1", table.Columns [0]));
1275 table.CaseSensitive = false;
1277 table1.Columns.Add ("Id", typeof (int));
1278 table1.Columns.Add ("Name", typeof (string));
1280 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
1281 set.Relations.Add (dr);
1283 DataRow row = table.NewRow ();
1285 row ["name"] = "Roopa";
1286 table.Rows.Add (row);
1288 row = table.NewRow ();
1290 row ["Name"] = "roopa";
1291 table.Rows.Add (row);
1293 AssertEquals (2, table.Rows.Count);
1294 AssertEquals (1, table.ChildRelations.Count);
1297 Fail ("#A01, should have thrown ArgumentException");
1299 catch (ArgumentException) {
1301 AssertEquals ("#CT01", 0, table.Rows.Count);
1302 AssertEquals ("#CT02", 0, table.ChildRelations.Count);
1303 AssertEquals ("#CT03", 0, table.ParentRelations.Count);
1304 AssertEquals ("#CT04", 0, table.Constraints.Count);
1307 AssertEquals ("#A05", 0, table1.Rows.Count);
1308 AssertEquals ("#A06", 0, table1.Constraints.Count);
1309 AssertEquals ("#A07", 0, table1.ParentRelations.Count);
1313 AssertEquals ("#A08", 0, table.Rows.Count);
1315 AssertEquals ("#A09", 0, table.Constraints.Count);
1317 AssertEquals ("#A09", 1, table.Constraints.Count);
1319 AssertEquals ("#A10", 0, table.ChildRelations.Count);
1324 public void ClearTest ()
1326 DataTable table = new DataTable ("test");
1327 table.Columns.Add ("id", typeof (int));
1328 table.Columns.Add ("name", typeof (string));
1330 table.PrimaryKey = new DataColumn [] { table.Columns [0] } ;
1332 table.Rows.Add (new object [] { 1, "mono 1" });
1333 table.Rows.Add (new object [] { 2, "mono 2" });
1334 table.Rows.Add (new object [] { 3, "mono 3" });
1335 table.Rows.Add (new object [] { 4, "mono 4" });
1337 table.AcceptChanges ();
1339 _tableClearedEventFired = false;
1340 table.TableCleared += new DataTableClearEventHandler (OnTableCleared);
1345 AssertEquals ("#0 should have fired cleared event", true, _tableClearedEventFired);
1348 DataRow r = table.Rows.Find (1);
1349 AssertEquals ("#1 should have cleared", true, r == null);
1351 // try adding new row. indexes should have cleared
1352 table.Rows.Add (new object [] { 2, "mono 2" });
1353 AssertEquals ("#2 should add row", 1, table.Rows.Count);
1356 private bool _tableClearedEventFired = false;
1357 private void OnTableCleared (object src, DataTableClearEventArgs args)
1359 _tableClearedEventFired = true;
1365 public void Serialize ()
1367 MemoryStream fs = new MemoryStream ();
1369 // Construct a BinaryFormatter and use it
1370 // to serialize the data to the stream.
1371 BinaryFormatter formatter = new BinaryFormatter();
1373 // Create an array with multiple elements refering to
1374 // the one Singleton object.
1375 DataTable dt = new DataTable();
1378 dt.Columns.Add(new DataColumn("Id", typeof(string)));
1379 dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
1380 dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
1381 dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
1382 dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
1385 loRowToAdd = dt.NewRow();
1386 loRowToAdd[0] = "a";
1387 loRowToAdd[1] = "b";
1388 loRowToAdd[2] = "c";
1389 loRowToAdd[3] = "d";
1390 loRowToAdd[4] = "e";
1392 dt.Rows.Add(loRowToAdd);
1394 DataTable[] dtarr = new DataTable[] {dt};
1396 // Serialize the array elements.
1397 formatter.Serialize(fs, dtarr);
1399 // Deserialize the array elements.
1401 DataTable[] a2 = (DataTable[]) formatter.Deserialize(fs);
1403 DataSet ds = new DataSet();
1404 ds.Tables.Add(a2[0]);
1406 StringWriter sw = new StringWriter ();
1408 XmlDocument doc = new XmlDocument ();
1409 doc.LoadXml (sw.ToString ());
1410 AssertEquals (5, doc.DocumentElement.FirstChild.ChildNodes.Count);
1414 [ExpectedException (typeof (DataException))]
1415 public void SetPrimaryKeyAssertsNonNull ()
1417 DataTable dt = new DataTable ("table");
1418 dt.Columns.Add ("col1");
1419 dt.Columns.Add ("col2");
1420 dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1421 dt.Rows.Add (new object [] {1, 3});
1422 dt.Rows.Add (new object [] {DBNull.Value, 3});
1424 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1428 [ExpectedException (typeof (NoNullAllowedException))]
1429 public void PrimaryKeyColumnChecksNonNull ()
1431 DataTable dt = new DataTable ("table");
1432 dt.Columns.Add ("col1");
1433 dt.Columns.Add ("col2");
1434 dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1435 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1436 dt.Rows.Add (new object [] {1, 3});
1437 dt.Rows.Add (new object [] {DBNull.Value, 3});
1441 public void PrimaryKey_CheckSetsAllowDBNull ()
1443 DataTable table = new DataTable ();
1444 DataColumn col1 = table.Columns.Add ("col1", typeof (int));
1445 DataColumn col2 = table.Columns.Add ("col2", typeof (int));
1447 AssertEquals ("#1" , true, col1.AllowDBNull);
1448 AssertEquals ("#2" , true, col2.AllowDBNull);
1449 AssertEquals ("#3" , false, col2.Unique);
1450 AssertEquals ("#4" , false, col2.Unique);
1452 table.PrimaryKey = new DataColumn[] {col1,col2};
1453 AssertEquals ("#5" , false, col1.AllowDBNull);
1454 AssertEquals ("#6" , false, col2.AllowDBNull);
1455 // LAMESPEC or bug ??
1456 AssertEquals ("#7" , false, col1.Unique);
1457 AssertEquals ("#8" , false, col2.Unique);
1460 void RowChanging (object o, DataRowChangeEventArgs e)
1462 AssertEquals ("changing.Action", rowChangingExpectedAction, e.Action);
1463 rowChangingRowChanging = true;
1466 void RowChanged (object o, DataRowChangeEventArgs e)
1468 AssertEquals ("changed.Action", rowChangingExpectedAction, e.Action);
1469 rowChangingRowChanged = true;
1472 bool rowChangingRowChanging, rowChangingRowChanged;
1473 DataRowAction rowChangingExpectedAction;
1476 public void RowChanging ()
1478 DataTable dt = new DataTable ("table");
1479 dt.Columns.Add ("col1");
1480 dt.Columns.Add ("col2");
1481 dt.RowChanging += new DataRowChangeEventHandler (RowChanging);
1482 dt.RowChanged += new DataRowChangeEventHandler (RowChanged);
1483 rowChangingExpectedAction = DataRowAction.Add;
1484 dt.Rows.Add (new object [] {1, 2});
1485 Assert ("changing,Added", rowChangingRowChanging);
1486 Assert ("changed,Added", rowChangingRowChanged);
1487 rowChangingExpectedAction = DataRowAction.Change;
1488 dt.Rows [0] [0] = 2;
1489 Assert ("changing,Changed", rowChangingRowChanging);
1490 Assert ("changed,Changed", rowChangingRowChanged);
1494 public void CloneSubClassTest()
1496 MyDataTable dt1 = new MyDataTable();
1497 MyDataTable dt = (MyDataTable)(dt1.Clone());
1498 AssertEquals("A#01",2,MyDataTable.count);
1501 DataRowAction rowActionChanging = DataRowAction.Nothing;
1502 DataRowAction rowActionChanged = DataRowAction.Nothing;
1504 public void AcceptChangesTest ()
1506 DataTable dt = new DataTable ("test");
1507 dt.Columns.Add ("id", typeof (int));
1508 dt.Columns.Add ("name", typeof (string));
1510 dt.Rows.Add (new object [] { 1, "mono 1" });
1512 dt.RowChanged += new DataRowChangeEventHandler (OnRowChanged);
1513 dt.RowChanging += new DataRowChangeEventHandler (OnRowChanging);
1516 rowActionChanged = rowActionChanging = DataRowAction.Nothing;
1517 dt.AcceptChanges ();
1519 AssertEquals ("#1 should have fired event and set action to commit",
1520 DataRowAction.Commit, rowActionChanging);
1521 AssertEquals ("#2 should have fired event and set action to commit",
1522 DataRowAction.Commit, rowActionChanged);
1525 dt.RowChanged -= new DataRowChangeEventHandler (OnRowChanged);
1526 dt.RowChanging -= new DataRowChangeEventHandler (OnRowChanging);
1532 public void ColumnObjectTypeTest() {
1533 DataTable dt = new DataTable();
1534 dt.Columns.Add("Series Label", typeof(SqlInt32));
1535 dt.Rows.Add(new object[] {"sss"});
1536 AssertEquals(1, dt.Rows.Count);
1539 public void OnRowChanging (object src, DataRowChangeEventArgs args)
1541 rowActionChanging = args.Action;
1544 public void OnRowChanged (object src, DataRowChangeEventArgs args)
1546 rowActionChanged = args.Action;
1551 private DataTable dt;
1552 private void localSetup () {
1553 dt = new DataTable ("test");
1554 dt.Columns.Add ("id", typeof (int));
1555 dt.Columns.Add ("name", typeof (string));
1556 dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
1558 dt.Rows.Add (new object[] { 1, "mono 1" });
1559 dt.Rows.Add (new object[] { 2, "mono 2" });
1560 dt.Rows.Add (new object[] { 3, "mono 3" });
1562 dt.AcceptChanges ();
1565 #region DataTable.CreateDataReader Tests
1568 public void CreateDataReader1 () {
1570 DataTableReader dtr = dt.CreateDataReader ();
1571 Assert ("HasRows", dtr.HasRows);
1572 AssertEquals ("CountCols", dt.Columns.Count, dtr.FieldCount);
1574 while (dtr.Read ()) {
1575 for (int i = 0; i < dtr.FieldCount; i++) {
1576 AssertEquals ("RowData-" + ri + "-" + i, dt.Rows[ri][i],
1584 public void CreateDataReader2 () {
1586 DataTableReader dtr = dt.CreateDataReader ();
1587 Assert ("HasRows", dtr.HasRows);
1588 AssertEquals ("CountCols", dt.Columns.Count, dtr.FieldCount);
1590 AssertEquals ("RowData0-0", 1, dtr[0]);
1591 AssertEquals ("RowData0-1", "mono 1", dtr[1]);
1593 AssertEquals ("RowData1-0", 2, dtr[0]);
1594 AssertEquals ("RowData1-1", "mono 2", dtr[1]);
1596 AssertEquals ("RowData2-0", 3, dtr[0]);
1597 AssertEquals ("RowData2-1", "mono 3", dtr[1]);
1600 #endregion // DataTable.CreateDataReader Tests
1602 #region DataTable.Load Tests
1605 public void Load_Basic () {
1607 DataTable dtLoad = new DataTable ("LoadBasic");
1608 dtLoad.Columns.Add ("id", typeof (int));
1609 dtLoad.Columns.Add ("name", typeof (string));
1610 dtLoad.Columns["id"].ReadOnly = true;
1611 dtLoad.Columns["name"].ReadOnly = true;
1612 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1613 dtLoad.Rows.Add (new object[] { 1, "load 1" });
1614 dtLoad.Rows.Add (new object[] { 2, "load 2" });
1615 dtLoad.Rows.Add (new object[] { 3, "load 3" });
1616 dtLoad.AcceptChanges ();
1617 DataTableReader dtr = dt.CreateDataReader ();
1619 AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1620 AssertEquals ("NRows", 3, dtLoad.Rows.Count);
1621 AssertEquals ("RowData0-0", 1, dtLoad.Rows[0][0]);
1622 AssertEquals ("RowData0-1", "mono 1", dtLoad.Rows[0][1]);
1623 AssertEquals ("RowData1-0", 2, dtLoad.Rows[1][0]);
1624 AssertEquals ("RowData1-1", "mono 2", dtLoad.Rows[1][1]);
1625 AssertEquals ("RowData2-0", 3, dtLoad.Rows[2][0]);
1626 AssertEquals ("RowData2-1", "mono 3", dtLoad.Rows[2][1]);
1630 public void Load_NoSchema () {
1632 DataTable dtLoad = new DataTable ("LoadNoSchema");
1633 DataTableReader dtr = dt.CreateDataReader ();
1635 AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1636 AssertEquals ("NRows", 3, dtLoad.Rows.Count);
1637 AssertEquals ("RowData0-0", 1, dtLoad.Rows[0][0]);
1638 AssertEquals ("RowData0-1", "mono 1", dtLoad.Rows[0][1]);
1639 AssertEquals ("RowData1-0", 2, dtLoad.Rows[1][0]);
1640 AssertEquals ("RowData1-1", "mono 2", dtLoad.Rows[1][1]);
1641 AssertEquals ("RowData2-0", 3, dtLoad.Rows[2][0]);
1642 AssertEquals ("RowData2-1", "mono 3", dtLoad.Rows[2][1]);
1645 internal struct fillErrorStruct {
1646 internal string error;
1647 internal string tableName;
1648 internal int rowKey;
1649 internal bool contFlag;
1650 internal void init (string tbl, int row, bool cont, string err) {
1657 private fillErrorStruct[] fillErr = new fillErrorStruct[3];
1658 private int fillErrCounter;
1659 private void fillErrorHandler (object sender, FillErrorEventArgs e) {
1660 e.Continue = fillErr[fillErrCounter].contFlag;
1661 AssertEquals ("fillErr-T", fillErr[fillErrCounter].tableName, e.DataTable.TableName);
1662 AssertEquals ("fillErr-R", fillErr[fillErrCounter].rowKey, e.Values[0]);
1663 AssertEquals ("fillErr-C", fillErr[fillErrCounter].contFlag, e.Continue);
1664 AssertEquals ("fillErr-E", fillErr[fillErrCounter].error, e.Errors.Message);
1669 [ExpectedException (typeof (ArgumentException))]
1670 public void Load_Incompatible () {
1672 DataTable dtLoad = new DataTable ("LoadIncompatible");
1673 dtLoad.Columns.Add ("name", typeof (double));
1674 DataTableReader dtr = dt.CreateDataReader ();
1678 // Load doesn't have a third overload in System.Data
1679 // and is commented-out below
1680 public void Load_IncompatibleEHandlerT () {
1682 fillErr[0].init ("LoadIncompatible", 1, true,
1683 "Input string was not in a correct format.Couldn't store <mono 1> in name Column. Expected type is Double.");
1684 fillErr[1].init ("LoadIncompatible", 2, true,
1685 "Input string was not in a correct format.Couldn't store <mono 2> in name Column. Expected type is Double.");
1686 fillErr[2].init ("LoadIncompatible", 3, true,
1687 "Input string was not in a correct format.Couldn't store <mono 3> in name Column. Expected type is Double.");
1689 DataTable dtLoad = new DataTable ("LoadIncompatible");
1690 dtLoad.Columns.Add ("name", typeof (double));
1691 DataTableReader dtr = dt.CreateDataReader ();
1692 //dtLoad.Load (dtr,LoadOption.PreserveChanges,fillErrorHandler);
1695 [Category ("NotWorking")]
1696 // Load doesn't have a third overload in System.Data
1697 // and is commented-out below
1698 [ExpectedException (typeof (ArgumentException))]
1699 public void Load_IncompatibleEHandlerF () {
1701 fillErr[0].init ("LoadIncompatible", 1, false,
1702 "Input string was not in a correct format.Couldn't store <mono 1> in name Column. Expected type is Double.");
1704 DataTable dtLoad = new DataTable ("LoadIncompatible");
1705 dtLoad.Columns.Add ("name", typeof (double));
1706 DataTableReader dtr = dt.CreateDataReader ();
1707 //dtLoad.Load (dtr, LoadOption.PreserveChanges, fillErrorHandler);
1711 public void Load_ExtraColsEqualVal () {
1713 DataTable dtLoad = new DataTable ("LoadExtraCols");
1714 dtLoad.Columns.Add ("id", typeof (int));
1715 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1716 dtLoad.Rows.Add (new object[] { 1 });
1717 dtLoad.Rows.Add (new object[] { 2 });
1718 dtLoad.Rows.Add (new object[] { 3 });
1719 dtLoad.AcceptChanges ();
1720 DataTableReader dtr = dt.CreateDataReader ();
1722 AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1723 AssertEquals ("NRows", 3, dtLoad.Rows.Count);
1724 AssertEquals ("RowData0-0", 1, dtLoad.Rows[0][0]);
1725 AssertEquals ("RowData0-1", "mono 1", dtLoad.Rows[0][1]);
1726 AssertEquals ("RowData1-0", 2, dtLoad.Rows[1][0]);
1727 AssertEquals ("RowData1-1", "mono 2", dtLoad.Rows[1][1]);
1728 AssertEquals ("RowData2-0", 3, dtLoad.Rows[2][0]);
1729 AssertEquals ("RowData2-1", "mono 3", dtLoad.Rows[2][1]);
1733 public void Load_ExtraColsNonEqualVal () {
1735 DataTable dtLoad = new DataTable ("LoadExtraCols");
1736 dtLoad.Columns.Add ("id", typeof (int));
1737 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1738 dtLoad.Rows.Add (new object[] { 4 });
1739 dtLoad.Rows.Add (new object[] { 5 });
1740 dtLoad.Rows.Add (new object[] { 6 });
1741 dtLoad.AcceptChanges ();
1742 DataTableReader dtr = dt.CreateDataReader ();
1744 AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1745 AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1746 AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1747 AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1748 AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1749 AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1750 AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1751 AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1752 AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1753 AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1754 AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1758 [ExpectedException (typeof (ConstraintException))]
1759 public void Load_MissingColsNonNullable () {
1761 DataTable dtLoad = new DataTable ("LoadMissingCols");
1762 dtLoad.Columns.Add ("id", typeof (int));
1763 dtLoad.Columns.Add ("name", typeof (string));
1764 dtLoad.Columns.Add ("missing", typeof (string));
1765 dtLoad.Columns["missing"].AllowDBNull = false;
1766 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1767 dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1768 dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1769 dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1770 dtLoad.AcceptChanges ();
1771 DataTableReader dtr = dt.CreateDataReader ();
1776 public void Load_MissingColsDefault () {
1778 DataTable dtLoad = new DataTable ("LoadMissingCols");
1779 dtLoad.Columns.Add ("id", typeof (int));
1780 dtLoad.Columns.Add ("name", typeof (string));
1781 dtLoad.Columns.Add ("missing", typeof (string));
1782 dtLoad.Columns["missing"].AllowDBNull = false;
1783 dtLoad.Columns["missing"].DefaultValue = "DefaultValue";
1784 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1785 dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1786 dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1787 dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1788 dtLoad.AcceptChanges ();
1789 DataTableReader dtr = dt.CreateDataReader ();
1791 AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1792 AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1793 AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1794 AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1795 AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1796 AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1797 AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1798 AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1799 AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1800 AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1801 AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1802 AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1803 AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1804 AssertEquals ("RowData3-2", "DefaultValue", dtLoad.Rows[3][2]);
1805 AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1806 AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1807 AssertEquals ("RowData4-2", "DefaultValue", dtLoad.Rows[4][2]);
1808 AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1809 AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1810 AssertEquals ("RowData5-2", "DefaultValue", dtLoad.Rows[5][2]);
1814 public void Load_MissingColsNullable () {
1816 DataTable dtLoad = new DataTable ("LoadMissingCols");
1817 dtLoad.Columns.Add ("id", typeof (int));
1818 dtLoad.Columns.Add ("name", typeof (string));
1819 dtLoad.Columns.Add ("missing", typeof (string));
1820 dtLoad.Columns["missing"].AllowDBNull = true;
1821 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1822 dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1823 dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1824 dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1825 dtLoad.AcceptChanges ();
1826 DataTableReader dtr = dt.CreateDataReader ();
1828 AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1829 AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1830 AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1831 AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1832 AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1833 AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1834 AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1835 AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1836 AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1837 AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1838 AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1839 AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1840 AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1841 //AssertEquals ("RowData3-2", null, dtLoad.Rows[3][2]);
1842 AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1843 AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1844 //AssertEquals ("RowData4-2", null, dtLoad.Rows[4][2]);
1845 AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1846 AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1847 //AssertEquals ("RowData5-2", null, dtLoad.Rows[5][2]);
1850 private DataTable setupRowState () {
1851 DataTable tbl = new DataTable ("LoadRowStateChanges");
1852 tbl.RowChanged += new DataRowChangeEventHandler (dtLoad_RowChanged);
1853 tbl.RowChanging += new DataRowChangeEventHandler (dtLoad_RowChanging);
1854 tbl.Columns.Add ("id", typeof (int));
1855 tbl.Columns.Add ("name", typeof (string));
1856 tbl.PrimaryKey = new DataColumn[] { tbl.Columns["id"] };
1857 tbl.Rows.Add (new object[] { 1, "RowState 1" });
1858 tbl.Rows.Add (new object[] { 2, "RowState 2" });
1859 tbl.Rows.Add (new object[] { 3, "RowState 3" });
1860 tbl.AcceptChanges ();
1861 // Update Table with following changes: Row0 unmodified,
1862 // Row1 modified, Row2 deleted, Row3 added, Row4 not-present.
1863 tbl.Rows[1]["name"] = "Modify 2";
1864 tbl.Rows[2].Delete ();
1865 DataRow row = tbl.NewRow ();
1867 row["name"] = "Add 4";
1872 private DataRowAction[] rowChangeAction = new DataRowAction[5];
1873 private bool checkAction = false;
1874 private int rowChagedCounter, rowChangingCounter;
1875 private void rowActionInit (DataRowAction[] act) {
1877 rowChagedCounter = 0;
1878 rowChangingCounter = 0;
1879 for (int i = 0; i < 5; i++)
1880 rowChangeAction[i] = act[i];
1882 private void rowActionEnd () {
1883 checkAction = false;
1885 private void dtLoad_RowChanged (object sender, DataRowChangeEventArgs e) {
1887 AssertEquals ("RowChanged" + rowChagedCounter,
1888 rowChangeAction[rowChagedCounter], e.Action);
1892 private void dtLoad_RowChanging (object sender, DataRowChangeEventArgs e) {
1894 AssertEquals ("RowChanging" + rowChangingCounter,
1895 rowChangeAction[rowChangingCounter], e.Action);
1896 rowChangingCounter++;
1901 public void Load_RowStateChangesDefault () {
1903 dt.Rows.Add (new object[] { 4, "mono 4" });
1904 dt.Rows.Add (new object[] { 5, "mono 5" });
1905 dt.AcceptChanges ();
1906 DataTableReader dtr = dt.CreateDataReader ();
1907 DataTable dtLoad = setupRowState ();
1908 DataRowAction[] dra = new DataRowAction[] {
1909 DataRowAction.ChangeCurrentAndOriginal,
1910 DataRowAction.ChangeOriginal,
1911 DataRowAction.ChangeOriginal,
1912 DataRowAction.ChangeOriginal,
1913 DataRowAction.ChangeCurrentAndOriginal};
1914 rowActionInit (dra);
1917 // asserting Unchanged Row0
1918 AssertEquals ("RowData0-C", "mono 1",
1919 dtLoad.Rows[0][1,DataRowVersion.Current]);
1920 AssertEquals ("RowData0-O", "mono 1",
1921 dtLoad.Rows[0][1,DataRowVersion.Original]);
1922 AssertEquals ("RowState0", DataRowState.Unchanged,
1923 dtLoad.Rows[0].RowState);
1924 // asserting Modified Row1
1925 AssertEquals ("RowData1-C", "Modify 2",
1926 dtLoad.Rows[1][1, DataRowVersion.Current]);
1927 AssertEquals ("RowData1-O", "mono 2",
1928 dtLoad.Rows[1][1, DataRowVersion.Original]);
1929 AssertEquals ("RowState1", DataRowState.Modified,
1930 dtLoad.Rows[1].RowState);
1931 // asserting Deleted Row2
1932 AssertEquals ("RowData1-O", "mono 3",
1933 dtLoad.Rows[2][1, DataRowVersion.Original]);
1934 AssertEquals ("RowState2", DataRowState.Deleted,
1935 dtLoad.Rows[2].RowState);
1936 // asserting Added Row3
1937 AssertEquals ("RowData3-C", "Add 4",
1938 dtLoad.Rows[3][1, DataRowVersion.Current]);
1939 AssertEquals ("RowData3-O", "mono 4",
1940 dtLoad.Rows[3][1, DataRowVersion.Original]);
1941 AssertEquals ("RowState3", DataRowState.Modified,
1942 dtLoad.Rows[3].RowState);
1943 // asserting Unpresent Row4
1944 AssertEquals ("RowData4-C", "mono 5",
1945 dtLoad.Rows[4][1, DataRowVersion.Current]);
1946 AssertEquals ("RowData4-O", "mono 5",
1947 dtLoad.Rows[4][1, DataRowVersion.Original]);
1948 AssertEquals ("RowState4", DataRowState.Unchanged,
1949 dtLoad.Rows[4].RowState);
1953 [ExpectedException (typeof (VersionNotFoundException))]
1954 public void Load_RowStateChangesDefaultDelete () {
1956 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
1957 dtLoad.Columns.Add ("id", typeof (int));
1958 dtLoad.Columns.Add ("name", typeof (string));
1959 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1960 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
1961 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
1962 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
1963 dtLoad.AcceptChanges ();
1964 dtLoad.Rows[2].Delete ();
1965 DataTableReader dtr = dt.CreateDataReader ();
1967 AssertEquals ("RowData2-C", " ",
1968 dtLoad.Rows[2][1, DataRowVersion.Current]);
1972 public void Load_RowStatePreserveChanges () {
1974 dt.Rows.Add (new object[] { 4, "mono 4" });
1975 dt.Rows.Add (new object[] { 5, "mono 5" });
1976 dt.AcceptChanges ();
1977 DataTableReader dtr = dt.CreateDataReader ();
1978 DataTable dtLoad = setupRowState ();
1979 DataRowAction[] dra = new DataRowAction[] {
1980 DataRowAction.ChangeCurrentAndOriginal,
1981 DataRowAction.ChangeOriginal,
1982 DataRowAction.ChangeOriginal,
1983 DataRowAction.ChangeOriginal,
1984 DataRowAction.ChangeCurrentAndOriginal};
1985 rowActionInit (dra);
1986 dtLoad.Load (dtr, LoadOption.PreserveChanges);
1988 // asserting Unchanged Row0
1989 AssertEquals ("RowData0-C", "mono 1",
1990 dtLoad.Rows[0][1, DataRowVersion.Current]);
1991 AssertEquals ("RowData0-O", "mono 1",
1992 dtLoad.Rows[0][1, DataRowVersion.Original]);
1993 AssertEquals ("RowState0", DataRowState.Unchanged,
1994 dtLoad.Rows[0].RowState);
1995 // asserting Modified Row1
1996 AssertEquals ("RowData1-C", "Modify 2",
1997 dtLoad.Rows[1][1, DataRowVersion.Current]);
1998 AssertEquals ("RowData1-O", "mono 2",
1999 dtLoad.Rows[1][1, DataRowVersion.Original]);
2000 AssertEquals ("RowState1", DataRowState.Modified,
2001 dtLoad.Rows[1].RowState);
2002 // asserting Deleted Row2
2003 AssertEquals ("RowData1-O", "mono 3",
2004 dtLoad.Rows[2][1, DataRowVersion.Original]);
2005 AssertEquals ("RowState2", DataRowState.Deleted,
2006 dtLoad.Rows[2].RowState);
2007 // asserting Added Row3
2008 AssertEquals ("RowData3-C", "Add 4",
2009 dtLoad.Rows[3][1, DataRowVersion.Current]);
2010 AssertEquals ("RowData3-O", "mono 4",
2011 dtLoad.Rows[3][1, DataRowVersion.Original]);
2012 AssertEquals ("RowState3", DataRowState.Modified,
2013 dtLoad.Rows[3].RowState);
2014 // asserting Unpresent Row4
2015 AssertEquals ("RowData4-C", "mono 5",
2016 dtLoad.Rows[4][1, DataRowVersion.Current]);
2017 AssertEquals ("RowData4-O", "mono 5",
2018 dtLoad.Rows[4][1, DataRowVersion.Original]);
2019 AssertEquals ("RowState4", DataRowState.Unchanged,
2020 dtLoad.Rows[4].RowState);
2024 [ExpectedException (typeof (VersionNotFoundException))]
2025 public void Load_RowStatePreserveChangesDelete () {
2027 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2028 dtLoad.Columns.Add ("id", typeof (int));
2029 dtLoad.Columns.Add ("name", typeof (string));
2030 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2031 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2032 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2033 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2034 dtLoad.AcceptChanges ();
2035 dtLoad.Rows[2].Delete ();
2036 DataTableReader dtr = dt.CreateDataReader ();
2037 dtLoad.Load (dtr,LoadOption.PreserveChanges);
2038 AssertEquals ("RowData2-C", " ",
2039 dtLoad.Rows[2][1, DataRowVersion.Current]);
2043 public void Load_RowStateOverwriteChanges () {
2045 dt.Rows.Add (new object[] { 4, "mono 4" });
2046 dt.Rows.Add (new object[] { 5, "mono 5" });
2047 dt.AcceptChanges ();
2048 DataTableReader dtr = dt.CreateDataReader ();
2049 DataTable dtLoad = setupRowState ();
2050 DataRowAction[] dra = new DataRowAction[] {
2051 DataRowAction.ChangeCurrentAndOriginal,
2052 DataRowAction.ChangeCurrentAndOriginal,
2053 DataRowAction.ChangeCurrentAndOriginal,
2054 DataRowAction.ChangeCurrentAndOriginal,
2055 DataRowAction.ChangeCurrentAndOriginal};
2056 rowActionInit (dra);
2057 dtLoad.Load (dtr, LoadOption.OverwriteChanges);
2059 // asserting Unchanged Row0
2060 AssertEquals ("RowData0-C", "mono 1",
2061 dtLoad.Rows[0][1, DataRowVersion.Current]);
2062 AssertEquals ("RowData0-O", "mono 1",
2063 dtLoad.Rows[0][1, DataRowVersion.Original]);
2064 AssertEquals ("RowState0", DataRowState.Unchanged,
2065 dtLoad.Rows[0].RowState);
2066 // asserting Modified Row1
2067 AssertEquals ("RowData1-C", "mono 2",
2068 dtLoad.Rows[1][1, DataRowVersion.Current]);
2069 AssertEquals ("RowData1-O", "mono 2",
2070 dtLoad.Rows[1][1, DataRowVersion.Original]);
2071 AssertEquals ("RowState1", DataRowState.Unchanged,
2072 dtLoad.Rows[1].RowState);
2073 // asserting Deleted Row2
2074 AssertEquals ("RowData1-C", "mono 3",
2075 dtLoad.Rows[2][1, DataRowVersion.Current]);
2076 AssertEquals ("RowData1-O", "mono 3",
2077 dtLoad.Rows[2][1, DataRowVersion.Original]);
2078 AssertEquals ("RowState2", DataRowState.Unchanged,
2079 dtLoad.Rows[2].RowState);
2080 // asserting Added Row3
2081 AssertEquals ("RowData3-C", "mono 4",
2082 dtLoad.Rows[3][1, DataRowVersion.Current]);
2083 AssertEquals ("RowData3-O", "mono 4",
2084 dtLoad.Rows[3][1, DataRowVersion.Original]);
2085 AssertEquals ("RowState3", DataRowState.Unchanged,
2086 dtLoad.Rows[3].RowState);
2087 // asserting Unpresent Row4
2088 AssertEquals ("RowData4-C", "mono 5",
2089 dtLoad.Rows[4][1, DataRowVersion.Current]);
2090 AssertEquals ("RowData4-O", "mono 5",
2091 dtLoad.Rows[4][1, DataRowVersion.Original]);
2092 AssertEquals ("RowState4", DataRowState.Unchanged,
2093 dtLoad.Rows[4].RowState);
2097 public void Load_RowStateUpsert () {
2099 dt.Rows.Add (new object[] { 4, "mono 4" });
2100 dt.Rows.Add (new object[] { 5, "mono 5" });
2101 dt.AcceptChanges ();
2102 DataTableReader dtr = dt.CreateDataReader ();
2103 DataTable dtLoad = setupRowState ();
2104 // Notice rowChange-Actions only occur 5 times, as number
2105 // of actual rows, ignoring row duplication of the deleted row.
2106 DataRowAction[] dra = new DataRowAction[] {
2107 DataRowAction.Change,
2108 DataRowAction.Change,
2110 DataRowAction.Change,
2112 rowActionInit (dra);
2113 dtLoad.Load (dtr, LoadOption.Upsert);
2115 // asserting Unchanged Row0
2116 AssertEquals ("RowData0-C", "mono 1",
2117 dtLoad.Rows[0][1, DataRowVersion.Current]);
2118 AssertEquals ("RowData0-O", "RowState 1",
2119 dtLoad.Rows[0][1, DataRowVersion.Original]);
2120 AssertEquals ("RowState0", DataRowState.Modified,
2121 dtLoad.Rows[0].RowState);
2122 // asserting Modified Row1
2123 AssertEquals ("RowData1-C", "mono 2",
2124 dtLoad.Rows[1][1, DataRowVersion.Current]);
2125 AssertEquals ("RowData1-O", "RowState 2",
2126 dtLoad.Rows[1][1, DataRowVersion.Original]);
2127 AssertEquals ("RowState1", DataRowState.Modified,
2128 dtLoad.Rows[1].RowState);
2129 // asserting Deleted Row2 and "Deleted-Added" Row4
2130 AssertEquals ("RowData2-O", "RowState 3",
2131 dtLoad.Rows[2][1, DataRowVersion.Original]);
2132 AssertEquals ("RowState2", DataRowState.Deleted,
2133 dtLoad.Rows[2].RowState);
2134 AssertEquals ("RowData4-C", "mono 3",
2135 dtLoad.Rows[4][1, DataRowVersion.Current]);
2136 AssertEquals ("RowState4", DataRowState.Added,
2137 dtLoad.Rows[4].RowState);
2138 // asserting Added Row3
2139 AssertEquals ("RowData3-C", "mono 4",
2140 dtLoad.Rows[3][1, DataRowVersion.Current]);
2141 AssertEquals ("RowState3", DataRowState.Added,
2142 dtLoad.Rows[3].RowState);
2143 // asserting Unpresent Row5
2144 // Notice row4 is used for added row of deleted row2 and so
2145 // unpresent row4 moves to row5
2146 AssertEquals ("RowData5-C", "mono 5",
2147 dtLoad.Rows[5][1, DataRowVersion.Current]);
2148 AssertEquals ("RowState5", DataRowState.Added,
2149 dtLoad.Rows[5].RowState);
2153 public void Load_RowStateUpsertDuplicateKey1 () {
2155 dt.Rows.Add (new object[] { 4, "mono 4" });
2156 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2157 dtLoad.Columns.Add ("id", typeof (int));
2158 dtLoad.Columns.Add ("name", typeof (string));
2159 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2160 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2161 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2162 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2163 dtLoad.AcceptChanges ();
2164 dtLoad.Rows[2].Delete ();
2165 DataTableReader dtr = dt.CreateDataReader ();
2166 dtLoad.Load (dtr, LoadOption.Upsert);
2167 dtLoad.Rows[3][1] = "NEWVAL";
2168 AssertEquals ("A-RowState2", DataRowState.Deleted,
2169 dtLoad.Rows[2].RowState);
2170 AssertEquals ("A-RowData2-id", 3,
2171 dtLoad.Rows[2][0, DataRowVersion.Original]);
2172 AssertEquals ("A-RowData2-name", "RowState 3",
2173 dtLoad.Rows[2][1, DataRowVersion.Original]);
2174 AssertEquals ("A-RowState3", DataRowState.Added,
2175 dtLoad.Rows[3].RowState);
2176 AssertEquals ("A-RowData3-id", 3,
2177 dtLoad.Rows[3][0, DataRowVersion.Current]);
2178 AssertEquals ("A-RowData3-name", "NEWVAL",
2179 dtLoad.Rows[3][1, DataRowVersion.Current]);
2180 AssertEquals ("A-RowState4", DataRowState.Added,
2181 dtLoad.Rows[4].RowState);
2182 AssertEquals ("A-RowData4-id", 4,
2183 dtLoad.Rows[4][0, DataRowVersion.Current]);
2184 AssertEquals ("A-RowData4-name", "mono 4",
2185 dtLoad.Rows[4][1, DataRowVersion.Current]);
2187 dtLoad.AcceptChanges ();
2189 AssertEquals ("B-RowState2", DataRowState.Unchanged,
2190 dtLoad.Rows[2].RowState);
2191 AssertEquals ("B-RowData2-id", 3,
2192 dtLoad.Rows[2][0, DataRowVersion.Current]);
2193 AssertEquals ("B-RowData2-name", "NEWVAL",
2194 dtLoad.Rows[2][1, DataRowVersion.Current]);
2195 AssertEquals ("B-RowState3", DataRowState.Unchanged,
2196 dtLoad.Rows[3].RowState);
2197 AssertEquals ("B-RowData3-id", 4,
2198 dtLoad.Rows[3][0, DataRowVersion.Current]);
2199 AssertEquals ("B-RowData3-name", "mono 4",
2200 dtLoad.Rows[3][1, DataRowVersion.Current]);
2204 [ExpectedException (typeof (IndexOutOfRangeException))]
2205 public void Load_RowStateUpsertDuplicateKey2 () {
2207 dt.Rows.Add (new object[] { 4, "mono 4" });
2208 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2209 dtLoad.Columns.Add ("id", typeof (int));
2210 dtLoad.Columns.Add ("name", typeof (string));
2211 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2212 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2213 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2214 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2215 dtLoad.AcceptChanges ();
2216 dtLoad.Rows[2].Delete ();
2217 DataTableReader dtr = dt.CreateDataReader ();
2218 dtLoad.Load (dtr, LoadOption.Upsert);
2219 dtLoad.AcceptChanges ();
2220 AssertEquals ("RowData4", " ", dtLoad.Rows[4][1]);
2224 [ExpectedException (typeof (VersionNotFoundException))]
2225 public void Load_RowStateUpsertDelete1 () {
2227 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2228 dtLoad.Columns.Add ("id", typeof (int));
2229 dtLoad.Columns.Add ("name", typeof (string));
2230 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2231 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2232 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2233 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2234 dtLoad.AcceptChanges ();
2235 dtLoad.Rows[2].Delete ();
2236 DataTableReader dtr = dt.CreateDataReader ();
2237 dtLoad.Load (dtr, LoadOption.Upsert);
2238 AssertEquals ("RowData2-C", " ",
2239 dtLoad.Rows[2][1, DataRowVersion.Current]);
2243 [ExpectedException (typeof (VersionNotFoundException))]
2244 public void Load_RowStateUpsertDelete2 () {
2246 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2247 dtLoad.Columns.Add ("id", typeof (int));
2248 dtLoad.Columns.Add ("name", typeof (string));
2249 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2250 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2251 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2252 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2253 dtLoad.AcceptChanges ();
2254 dtLoad.Rows[2].Delete ();
2255 DataTableReader dtr = dt.CreateDataReader ();
2256 dtLoad.Load (dtr, LoadOption.Upsert);
2257 AssertEquals ("RowData3-O", " ",
2258 dtLoad.Rows[3][1, DataRowVersion.Original]);
2262 [ExpectedException (typeof (VersionNotFoundException))]
2263 public void Load_RowStateUpsertAdd () {
2265 dt.Rows.Add (new object[] { 4, "mono 4" });
2266 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2267 dtLoad.Columns.Add ("id", typeof (int));
2268 dtLoad.Columns.Add ("name", typeof (string));
2269 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2270 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2271 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2272 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2273 dtLoad.AcceptChanges ();
2274 DataRow row = dtLoad.NewRow ();
2276 row["name"] = "Add 4";
2277 dtLoad.Rows.Add (row);
2278 DataTableReader dtr = dt.CreateDataReader ();
2279 dtLoad.Load (dtr, LoadOption.Upsert);
2280 AssertEquals ("RowData3-O", " ",
2281 dtLoad.Rows[3][1, DataRowVersion.Original]);
2285 [ExpectedException (typeof (VersionNotFoundException))]
2286 public void Load_RowStateUpsertUnpresent () {
2288 dt.Rows.Add (new object[] { 4, "mono 4" });
2289 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2290 dtLoad.Columns.Add ("id", typeof (int));
2291 dtLoad.Columns.Add ("name", typeof (string));
2292 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2293 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2294 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2295 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2296 dtLoad.AcceptChanges ();
2297 DataTableReader dtr = dt.CreateDataReader ();
2298 dtLoad.Load (dtr, LoadOption.Upsert);
2299 AssertEquals ("RowData3-O", " ",
2300 dtLoad.Rows[3][1, DataRowVersion.Original]);
2304 public void Load_RowStateUpsertUnchangedEqualVal () {
2306 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2307 dtLoad.Columns.Add ("id", typeof (int));
2308 dtLoad.Columns.Add ("name", typeof (string));
2309 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2310 dtLoad.Rows.Add (new object[] { 1, "mono 1" });
2311 dtLoad.AcceptChanges ();
2312 DataTableReader dtr = dt.CreateDataReader ();
2313 DataRowAction[] dra = new DataRowAction[] {
2314 DataRowAction.Nothing,// REAL action
2315 DataRowAction.Nothing,// dummy
2316 DataRowAction.Nothing,// dummy
2317 DataRowAction.Nothing,// dummy
2318 DataRowAction.Nothing};// dummy
2319 rowActionInit (dra);
2320 dtLoad.Load (dtr, LoadOption.Upsert);
2322 AssertEquals ("RowData0-C", "mono 1",
2323 dtLoad.Rows[0][1, DataRowVersion.Current]);
2324 AssertEquals ("RowData0-O", "mono 1",
2325 dtLoad.Rows[0][1, DataRowVersion.Original]);
2326 AssertEquals ("RowState0", DataRowState.Unchanged,
2327 dtLoad.Rows[0].RowState);
2331 public void LoadDataRow_LoadOptions () {
2332 // LoadDataRow is covered in detail (without LoadOptions) in DataTableTest2
2333 // LoadOption tests are covered in detail in DataTable.Load().
2334 // Therefore only minimal tests of LoadDataRow with LoadOptions are covered here.
2337 dt = CreateDataTableExample ();
2338 dt.PrimaryKey = new DataColumn[] { dt.Columns[0] }; //add ParentId as Primary Key
2339 dt.Columns["String1"].DefaultValue = "Default";
2341 dr = dt.Select ("ParentId=1")[0];
2343 //Update existing row with LoadOptions = OverwriteChanges
2344 dt.BeginLoadData ();
2345 dt.LoadDataRow (new object[] { 1, null, "Changed" },
2346 LoadOption.OverwriteChanges);
2349 // LoadDataRow(update1) - check column String2
2350 AssertEquals ("DT72-C", "Changed",
2351 dr["String2", DataRowVersion.Current]);
2352 AssertEquals ("DT72-O", "Changed",
2353 dr["String2", DataRowVersion.Original]);
2355 // LoadDataRow(update1) - check row state
2356 AssertEquals ("DT73-LO", DataRowState.Unchanged, dr.RowState);
2358 //Add New row with LoadOptions = Upsert
2359 dt.BeginLoadData ();
2360 dt.LoadDataRow (new object[] { 99, null, "Changed" },
2364 // LoadDataRow(insert1) - check column String2
2365 dr = dt.Select ("ParentId=99")[0];
2366 AssertEquals ("DT75-C", "Changed",
2367 dr["String2", DataRowVersion.Current]);
2369 // LoadDataRow(insert1) - check row state
2370 AssertEquals ("DT76-LO", DataRowState.Added, dr.RowState);
2373 public static DataTable CreateDataTableExample () {
2374 DataTable dtParent = new DataTable ("Parent");
2376 dtParent.Columns.Add ("ParentId", typeof (int));
2377 dtParent.Columns.Add ("String1", typeof (string));
2378 dtParent.Columns.Add ("String2", typeof (string));
2380 dtParent.Columns.Add ("ParentDateTime", typeof (DateTime));
2381 dtParent.Columns.Add ("ParentDouble", typeof (double));
2382 dtParent.Columns.Add ("ParentBool", typeof (bool));
2384 dtParent.Rows.Add (new object[] { 1, "1-String1", "1-String2", new DateTime (2005, 1, 1, 0, 0, 0, 0), 1.534, true });
2385 dtParent.Rows.Add (new object[] { 2, "2-String1", "2-String2", new DateTime (2004, 1, 1, 0, 0, 0, 1), -1.534, true });
2386 dtParent.Rows.Add (new object[] { 3, "3-String1", "3-String2", new DateTime (2003, 1, 1, 0, 0, 1, 0), double.MinValue * 10000, false });
2387 dtParent.Rows.Add (new object[] { 4, "4-String1", "4-String2", new DateTime (2002, 1, 1, 0, 1, 0, 0), double.MaxValue / 10000, true });
2388 dtParent.Rows.Add (new object[] { 5, "5-String1", "5-String2", new DateTime (2001, 1, 1, 1, 0, 0, 0), 0.755, true });
2389 dtParent.Rows.Add (new object[] { 6, "6-String1", "6-String2", new DateTime (2000, 1, 1, 0, 0, 0, 0), 0.001, false });
2390 dtParent.AcceptChanges ();
2394 #endregion // DataTable.Load Tests
2396 #region Read/Write XML Tests
2399 [Category ("NotWorking")]
2400 public void ReadXmlSchema () {
2401 DataTable Table = new DataTable ();
2402 Table.ReadXmlSchema ("Test/System.Data/own_schema1.xsd");
2404 AssertEquals ("test#02", "test_table", Table.TableName);
2405 AssertEquals ("test#03", "", Table.Namespace);
2406 AssertEquals ("test#04", 2, Table.Columns.Count);
2407 AssertEquals ("test#05", 0, Table.Rows.Count);
2408 AssertEquals ("test#06", false, Table.CaseSensitive);
2409 AssertEquals ("test#07", 1, Table.Constraints.Count);
2410 AssertEquals ("test#08", "", Table.Prefix);
2412 Constraint cons = Table.Constraints[0];
2413 AssertEquals ("test#09", "Constraint1", cons.ConstraintName.ToString ());
2414 AssertEquals ("test#10", "Constraint1", cons.ToString ());
2416 DataColumn column = Table.Columns[0];
2417 AssertEquals ("test#11", true, column.AllowDBNull);
2418 AssertEquals ("test#12", false, column.AutoIncrement);
2419 AssertEquals ("test#13", 0L, column.AutoIncrementSeed);
2420 AssertEquals ("test#14", 1L, column.AutoIncrementStep);
2421 AssertEquals ("test#15", "test", column.Caption);
2422 AssertEquals ("test#16", "Element", column.ColumnMapping.ToString ());
2423 AssertEquals ("test#17", "first", column.ColumnName);
2424 AssertEquals ("test#18", "System.String", column.DataType.ToString ());
2425 AssertEquals ("test#19", "test_default_value", column.DefaultValue.ToString ());
2426 AssertEquals ("test#20", false, column.DesignMode);
2427 AssertEquals ("test#21", "", column.Expression);
2428 AssertEquals ("test#22", 100, column.MaxLength);
2429 AssertEquals ("test#23", "", column.Namespace);
2430 AssertEquals ("test#24", 0, column.Ordinal);
2431 AssertEquals ("test#25", "", column.Prefix);
2432 AssertEquals ("test#26", false, column.ReadOnly);
2433 AssertEquals ("test#27", true, column.Unique);
2435 DataColumn column2 = Table.Columns[1];
2436 AssertEquals ("test#28", true, column2.AllowDBNull);
2437 AssertEquals ("test#29", false, column2.AutoIncrement);
2438 AssertEquals ("test#30", 0L, column2.AutoIncrementSeed);
2439 AssertEquals ("test#31", 1L, column2.AutoIncrementStep);
2440 AssertEquals ("test#32", "second", column2.Caption);
2441 AssertEquals ("test#33", "Element", column2.ColumnMapping.ToString ());
2442 AssertEquals ("test#34", "second", column2.ColumnName);
2443 AssertEquals ("test#35", "System.Data.SqlTypes.SqlGuid", column2.DataType.ToString ());
2444 AssertEquals ("test#36", "Null", column2.DefaultValue.ToString ());
2445 AssertEquals ("test#37", false, column2.DesignMode);
2446 AssertEquals ("test#38", "", column2.Expression);
2447 AssertEquals ("test#39", -1, column2.MaxLength);
2448 AssertEquals ("test#40", "", column2.Namespace);
2449 AssertEquals ("test#41", 1, column2.Ordinal);
2450 AssertEquals ("test#42", "", column2.Prefix);
2451 AssertEquals ("test#43", false, column2.ReadOnly);
2452 AssertEquals ("test#44", false, column2.Unique);
2454 DataTable Table2 = new DataTable ();
2455 Table2.ReadXmlSchema ("Test/System.Data/own_schema2.xsd");
2457 AssertEquals ("test#45", "second_test_table", Table2.TableName);
2458 AssertEquals ("test#46", "", Table2.Namespace);
2459 AssertEquals ("test#47", 1, Table2.Columns.Count);
2460 AssertEquals ("test#48", 0, Table2.Rows.Count);
2461 AssertEquals ("test#49", false, Table2.CaseSensitive);
2462 AssertEquals ("test#50", 1, Table2.Constraints.Count);
2463 AssertEquals ("test#51", "", Table2.Prefix);
2465 DataColumn column3 = Table2.Columns[0];
2466 AssertEquals ("test#52", true, column3.AllowDBNull);
2467 AssertEquals ("test#53", false, column3.AutoIncrement);
2468 AssertEquals ("test#54", 0L, column3.AutoIncrementSeed);
2469 AssertEquals ("test#55", 1L, column3.AutoIncrementStep);
2470 AssertEquals ("test#56", "second_first", column3.Caption);
2471 AssertEquals ("test#57", "Element", column3.ColumnMapping.ToString ());
2472 AssertEquals ("test#58", "second_first", column3.ColumnName);
2473 AssertEquals ("test#59", "System.String", column3.DataType.ToString ());
2474 AssertEquals ("test#60", "default_value", column3.DefaultValue.ToString ());
2475 AssertEquals ("test#61", false, column3.DesignMode);
2476 AssertEquals ("test#62", "", column3.Expression);
2477 AssertEquals ("test#63", 100, column3.MaxLength);
2478 AssertEquals ("test#64", "", column3.Namespace);
2479 AssertEquals ("test#65", 0, column3.Ordinal);
2480 AssertEquals ("test#66", "", column3.Prefix);
2481 AssertEquals ("test#67", false, column3.ReadOnly);
2482 AssertEquals ("test#68", true, column3.Unique);
2486 public void ReadXmlSchema_2 () {
2487 DataTable dt = new DataTable ();
2488 string xmlData = string.Empty;
2489 xmlData += "<?xml version=\"1.0\"?>";
2490 xmlData += "<xs:schema id=\"SiteConfiguration\" targetNamespace=\"http://tempuri.org/PortalCfg.xsd\" xmlns:mstns=\"http://tempuri.org/PortalCfg.xsd\" xmlns=\"http://tempuri.org/PortalCfg.xsd\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" attributeFormDefault=\"qualified\" elementFormDefault=\"qualified\">";
2491 xmlData += "<xs:element name=\"SiteConfiguration\" msdata:IsDataSet=\"true\" msdata:EnforceConstraints=\"False\">";
2492 xmlData += "<xs:complexType>";
2493 xmlData += "<xs:choice maxOccurs=\"unbounded\">";
2494 xmlData += "<xs:element name=\"Tab\">";
2495 xmlData += "<xs:complexType>";
2496 xmlData += "<xs:sequence>";
2497 xmlData += "<xs:element name=\"Module\" minOccurs=\"0\" maxOccurs=\"unbounded\">";
2498 xmlData += "<xs:complexType>";
2499 xmlData += "<xs:attribute name=\"ModuleId\" form=\"unqualified\" type=\"xs:int\" />";
2500 xmlData += "</xs:complexType>";
2501 xmlData += "</xs:element>";
2502 xmlData += "</xs:sequence>";
2503 xmlData += "<xs:attribute name=\"TabId\" form=\"unqualified\" type=\"xs:int\" />";
2504 xmlData += "</xs:complexType>";
2505 xmlData += "</xs:element>";
2506 xmlData += "</xs:choice>";
2507 xmlData += "</xs:complexType>";
2508 xmlData += "<xs:key name=\"TabKey\" msdata:PrimaryKey=\"true\">";
2509 xmlData += "<xs:selector xpath=\".//mstns:Tab\" />";
2510 xmlData += "<xs:field xpath=\"@TabId\" />";
2511 xmlData += "</xs:key>";
2512 xmlData += "<xs:key name=\"ModuleKey\" msdata:PrimaryKey=\"true\">";
2513 xmlData += "<xs:selector xpath=\".//mstns:Module\" />";
2514 xmlData += "<xs:field xpath=\"@ModuleID\" />";
2515 xmlData += "</xs:key>";
2516 xmlData += "</xs:element>";
2517 xmlData += "</xs:schema>";
2518 dt.ReadXmlSchema (new StringReader (xmlData));
2522 public void ReadXmlSchema_ByStream () {
2523 DataSet ds1 = new DataSet ();
2524 ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2525 ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2527 MemoryStream ms1 = new MemoryStream ();
2528 MemoryStream ms2 = new MemoryStream ();
2529 //write xml schema only
2530 //ds1.WriteXmlSchema (ms);
2531 ds1.Tables[0].WriteXmlSchema (ms1);
2532 ds1.Tables[1].WriteXmlSchema (ms2);
2534 MemoryStream ms11 = new MemoryStream (ms1.GetBuffer ());
2535 MemoryStream ms22 = new MemoryStream (ms2.GetBuffer ());
2537 //DataSet ds2 = new DataSet ();
2538 DataTable dt1 = new DataTable ();
2539 DataTable dt2 = new DataTable ();
2541 //ds2.ReadXmlSchema (ms1);
2542 dt1.ReadXmlSchema (ms11);
2543 dt2.ReadXmlSchema (ms22);
2546 // ReadXmlSchema - Tables count
2547 //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS269");
2549 // ReadXmlSchema - Tables 0 Col count
2550 AssertEquals ("DS270", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2552 // ReadXmlSchema - Tables 1 Col count
2553 AssertEquals ("DS271", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2555 //check some colummns types
2556 // ReadXmlSchema - Tables 0 Col type
2557 AssertEquals ("DS272", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2559 // ReadXmlSchema - Tables 1 Col type
2560 AssertEquals ("DS273", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2562 //check that no data exists
2563 // ReadXmlSchema - Table 1 row count
2564 AssertEquals ("DS274",0, dt1.Rows.Count);
2566 // ReadXmlSchema - Table 2 row count
2567 AssertEquals ("DS275",0, dt2.Rows.Count);
2571 [Category ("NotWorking")]
2572 public void ReadWriteXmlSchema_ByFileName () {
2573 string sTempFileName1 = "tmpDataSet_ReadWriteXml_43899-1.xml";
2574 string sTempFileName2 = "tmpDataSet_ReadWriteXml_43899-2.xml";
2576 DataSet ds1 = new DataSet ();
2577 ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2578 ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2580 ds1.Tables[0].WriteXmlSchema (sTempFileName1);
2581 ds1.Tables[1].WriteXmlSchema (sTempFileName2);
2583 DataTable dt1 = new DataTable ();
2584 DataTable dt2 = new DataTable ();
2586 dt1.ReadXmlSchema (sTempFileName1);
2587 dt2.ReadXmlSchema (sTempFileName2);
2589 AssertEquals ("DS277", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2590 AssertEquals ("DS278", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2591 AssertEquals ("DS279", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2592 AssertEquals ("DS280", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2593 AssertEquals ("DS281", 0, dt1.Rows.Count);
2594 AssertEquals ("DS282", 0, dt2.Rows.Count);
2596 File.Delete (sTempFileName1);
2597 File.Delete (sTempFileName2);
2601 public void ReadXmlSchema_ByTextReader () {
2602 DataSet ds1 = new DataSet ();
2603 ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2604 ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2606 StringWriter sw1 = new StringWriter ();
2607 StringWriter sw2 = new StringWriter ();
2608 //write xml file, schema only
2609 //ds1.WriteXmlSchema (sw);
2610 ds1.Tables[0].WriteXmlSchema (sw1);
2611 ds1.Tables[1].WriteXmlSchema (sw2);
2613 StringReader sr1 = new StringReader (sw1.GetStringBuilder ().ToString ());
2614 StringReader sr2 = new StringReader (sw2.GetStringBuilder ().ToString ());
2615 //copy both data and schema
2616 //DataSet ds2 = new DataSet ();
2617 DataTable dt1 = new DataTable ();
2618 DataTable dt2 = new DataTable ();
2620 //ds2.ReadXmlSchema (sr);
2621 dt1.ReadXmlSchema (sr1);
2622 dt2.ReadXmlSchema (sr2);
2625 // ReadXmlSchema - Tables count
2626 //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS283");
2628 // ReadXmlSchema - Tables 0 Col count
2629 AssertEquals ("DS284", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2631 // ReadXmlSchema - Tables 1 Col count
2632 AssertEquals ("DS285", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2634 //check some colummns types
2635 // ReadXmlSchema - Tables 0 Col type
2636 AssertEquals ("DS286", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2638 // ReadXmlSchema - Tables 1 Col type
2639 AssertEquals ("DS287", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2641 //check that no data exists
2642 // ReadXmlSchema - Table 1 row count
2643 AssertEquals ("DS288", 0, dt1.Rows.Count);
2645 // ReadXmlSchema - Table 2 row count
2646 AssertEquals ("DS289", 0, dt2.Rows.Count);
2650 public void ReadXmlSchema_ByXmlReader () {
2651 DataSet ds1 = new DataSet ();
2652 ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2653 ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2655 StringWriter sw1 = new StringWriter ();
2656 XmlTextWriter xmlTW1 = new XmlTextWriter (sw1);
2657 StringWriter sw2 = new StringWriter ();
2658 XmlTextWriter xmlTW2 = new XmlTextWriter (sw2);
2660 //write xml file, schema only
2661 ds1.Tables[0].WriteXmlSchema (xmlTW1);
2663 ds1.Tables[1].WriteXmlSchema (xmlTW2);
2666 StringReader sr1 = new StringReader (sw1.ToString ());
2667 XmlTextReader xmlTR1 = new XmlTextReader (sr1);
2668 StringReader sr2 = new StringReader (sw2.ToString ());
2669 XmlTextReader xmlTR2 = new XmlTextReader (sr2);
2671 //copy both data and schema
2672 //DataSet ds2 = new DataSet ();
2673 DataTable dt1 = new DataTable ();
2674 DataTable dt2 = new DataTable ();
2676 //ds2.ReadXmlSchema (xmlTR);
2677 dt1.ReadXmlSchema (xmlTR1);
2678 dt2.ReadXmlSchema (xmlTR2);
2681 // ReadXmlSchema - Tables count
2682 //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS290");
2684 // ReadXmlSchema - Tables 0 Col count
2685 AssertEquals ("DS291", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2687 // ReadXmlSchema - Tables 1 Col count
2688 AssertEquals ("DS292", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2690 //check some colummns types
2691 // ReadXmlSchema - Tables 0 Col type
2692 AssertEquals ("DS293", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2694 // ReadXmlSchema - Tables 1 Col type
2695 AssertEquals ("DS294", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2697 //check that no data exists
2698 // ReadXmlSchema - Table 1 row count
2699 AssertEquals ("DS295", 0, dt1.Rows.Count);
2701 // ReadXmlSchema - Table 2 row count
2702 AssertEquals ("DS296", 0, dt2.Rows.Count);
2706 [Category ("NotWorking")]
2707 public void WriteXmlSchema () {
2708 DataSet ds = new DataSet ();
2709 ds.ReadXml ("Test/System.Data/region.xml");
2710 TextWriter writer = new StringWriter ();
2711 ds.Tables[0].WriteXmlSchema (writer);
2714 string TextString = GetNormalizedSchema (writer.ToString ());
2715 //string TextString = writer.ToString ();
2717 string substring = TextString.Substring (0, TextString.IndexOf (EOL));
2718 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2719 AssertEquals ("test#01", "<?xml version=\"1.0\" encoding=\"utf-16\"?>", substring);
2721 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2722 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2723 AssertEquals ("test#02", "<xs:schema id=\"Root\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">", substring);
2725 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2726 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2727 AssertEquals ("test#03", " <xs:element msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Region\" msdata:UseCurrentLocale=\"true\" name=\"Root\">", substring);
2729 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2730 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2731 AssertEquals ("test#04", " <xs:complexType>", substring);
2733 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2734 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2735 AssertEquals ("test#05", " <xs:choice maxOccurs=\"unbounded\" minOccurs=\"0\">", substring);
2737 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2738 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2739 AssertEquals ("test#06", " <xs:element name=\"Region\">", substring);
2741 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2742 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2743 AssertEquals ("test#07", " <xs:complexType>", substring);
2745 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2746 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2747 AssertEquals ("test#08", " <xs:sequence>", substring);
2749 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2750 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2751 AssertEquals ("test#09", " <xs:element minOccurs=\"0\" name=\"RegionID\" type=\"xs:string\" />", substring);
2753 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2754 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2755 AssertEquals ("test#10", " <xs:element minOccurs=\"0\" name=\"RegionDescription\" type=\"xs:string\" />", substring);
2757 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2758 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2759 AssertEquals ("test#11", " </xs:sequence>", substring);
2761 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2762 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2763 AssertEquals ("test#12", " </xs:complexType>", substring);
2765 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2766 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2767 AssertEquals ("test#13", " </xs:element>", substring);
2769 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2770 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2771 AssertEquals ("test#14", " </xs:choice>", substring);
2773 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2774 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2775 AssertEquals ("test#15", " </xs:complexType>", substring);
2777 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2778 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2779 AssertEquals ("test#16", " </xs:element>", substring);
2781 AssertEquals ("test#17", "</xs:schema>", TextString);
2785 [Category ("NotWorking")]
2786 public void WriteXmlSchema2 () {
2787 string xml = @"<myDataSet xmlns='NetFrameWork'><myTable><id>0</id><item>item 0</item></myTable><myTable><id>1</id><item>item 1</item></myTable><myTable><id>2</id><item>item 2</item></myTable><myTable><id>3</id><item>item 3</item></myTable><myTable><id>4</id><item>item 4</item></myTable><myTable><id>5</id><item>item 5</item></myTable><myTable><id>6</id><item>item 6</item></myTable><myTable><id>7</id><item>item 7</item></myTable><myTable><id>8</id><item>item 8</item></myTable><myTable><id>9</id><item>item 9</item></myTable></myDataSet>";
2788 string schema = @"<?xml version='1.0' encoding='utf-16'?>
2789 <xs:schema id='myDataSet' targetNamespace='NetFrameWork' xmlns:mstns='NetFrameWork' xmlns='NetFrameWork' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified'>
2790 <xs:element name='myDataSet' msdata:IsDataSet='true' msdata:MainDataTable='NetFrameWork_x003A_myTable' msdata:UseCurrentLocale='true'>
2792 <xs:choice minOccurs='0' maxOccurs='unbounded'>
2793 <xs:element name='myTable'>
2796 <xs:element name='id' msdata:AutoIncrement='true' type='xs:int' minOccurs='0' />
2797 <xs:element name='item' type='xs:string' minOccurs='0' />
2805 DataSet OriginalDataSet = new DataSet ("myDataSet");
2806 OriginalDataSet.Namespace = "NetFrameWork";
2807 DataTable myTable = new DataTable ("myTable");
2808 DataColumn c1 = new DataColumn ("id", typeof (int));
2809 c1.AutoIncrement = true;
2810 DataColumn c2 = new DataColumn ("item");
2811 myTable.Columns.Add (c1);
2812 myTable.Columns.Add (c2);
2813 OriginalDataSet.Tables.Add (myTable);
2816 for (int i = 0; i < 10; i++) {
2817 newRow = myTable.NewRow ();
2818 newRow["item"] = "item " + i;
2819 myTable.Rows.Add (newRow);
2821 OriginalDataSet.AcceptChanges ();
2823 StringWriter sw = new StringWriter ();
2824 XmlTextWriter xtw = new XmlTextWriter (sw);
2825 xtw.QuoteChar = '\'';
2826 OriginalDataSet.WriteXml (xtw);
2827 string result = sw.ToString ();
2829 AssertEquals (xml, result);
2831 sw = new StringWriter ();
2832 xtw = new XmlTextWriter (sw);
2833 xtw.Formatting = Formatting.Indented;
2834 OriginalDataSet.Tables[0].WriteXmlSchema (xtw);
2835 result = sw.ToString ();
2837 result = result.Replace ("\r\n", "\n").Replace ('"', '\'');
2838 AssertEquals (schema.Replace ("\r\n", "\n"), result);
2842 [Category ("NotWorking")]
2843 public void WriteXmlSchema3 () {
2844 string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
2845 <xs:schema id=""ExampleDataSet"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2846 <xs:element name=""ExampleDataSet"" msdata:IsDataSet=""true"" msdata:MainDataTable=""ExampleDataTable"" msdata:UseCurrentLocale=""true"">
2848 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2849 <xs:element name=""ExampleDataTable"">
2851 <xs:attribute name=""PrimaryKeyColumn"" type=""xs:int"" use=""required"" />
2856 <xs:unique name=""PK_ExampleDataTable"" msdata:PrimaryKey=""true"">
2857 <xs:selector xpath="".//ExampleDataTable"" />
2858 <xs:field xpath=""@PrimaryKeyColumn"" />
2862 DataSet ds = new DataSet ("ExampleDataSet");
2864 ds.Tables.Add (new DataTable ("ExampleDataTable"));
2865 ds.Tables["ExampleDataTable"].Columns.Add (
2866 new DataColumn ("PrimaryKeyColumn", typeof (int), "", MappingType.Attribute));
2867 ds.Tables["ExampleDataTable"].Columns["PrimaryKeyColumn"].AllowDBNull = false;
2869 ds.Tables["ExampleDataTable"].Constraints.Add (
2870 "PK_ExampleDataTable",
2871 ds.Tables["ExampleDataTable"].Columns["PrimaryKeyColumn"],
2874 ds.AcceptChanges ();
2875 StringWriter sw = new StringWriter ();
2876 ds.Tables[0].WriteXmlSchema (sw);
2878 string result = sw.ToString ();
2880 AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
2881 //AssertEquals (xmlschema, result.Replace ("\r\n", "\n"));
2885 [Category ("NotWorking")]
2886 public void WriteXmlSchema4 () {
2887 string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
2888 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2889 <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""MyType"" msdata:UseCurrentLocale=""true"">
2891 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2892 <xs:element name=""MyType"">
2894 <xs:attribute name=""ID"" type=""xs:int"" use=""required"" />
2895 <xs:attribute name=""Desc"" type=""xs:string"" />
2902 DataSet ds = new DataSet ("Example");
2904 // Add MyType DataTable
2905 DataTable dt = new DataTable ("MyType");
2908 dt.Columns.Add (new DataColumn ("ID", typeof (int), "",
2909 MappingType.Attribute));
2910 dt.Columns["ID"].AllowDBNull = false;
2912 dt.Columns.Add (new DataColumn ("Desc", typeof
2913 (string), "", MappingType.Attribute));
2915 ds.AcceptChanges ();
2917 StringWriter sw = new StringWriter ();
2918 ds.Tables[0].WriteXmlSchema (sw);
2920 string result = sw.ToString ();
2922 AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
2926 [Category ("NotWorking")]
2927 public void WriteXmlSchema5 () {
2928 string xmlschema1 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2929 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2930 <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""StandAlone"" msdata:UseCurrentLocale=""true"">
2932 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2933 <xs:element name=""StandAlone"">
2935 <xs:attribute name=""ID"" type=""xs:int"" use=""required"" />
2936 <xs:attribute name=""Desc"" type=""xs:string"" use=""required"" />
2943 string xmlschema2 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2944 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2945 <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""Dimension"" msdata:UseCurrentLocale=""true"">
2947 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2948 <xs:element name=""Dimension"">
2950 <xs:attribute name=""Number"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2951 <xs:attribute name=""Title"" type=""xs:string"" use=""required"" />
2956 <xs:unique name=""PK_Dimension"" msdata:PrimaryKey=""true"">
2957 <xs:selector xpath="".//Dimension"" />
2958 <xs:field xpath=""@Number"" />
2962 string xmlschema3 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2963 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2964 <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""Element"" msdata:UseCurrentLocale=""true"">
2966 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2967 <xs:element name=""Element"">
2969 <xs:attribute name=""Dimension"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2970 <xs:attribute name=""Number"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2971 <xs:attribute name=""Title"" type=""xs:string"" use=""required"" />
2976 <xs:unique name=""PK_Element"" msdata:PrimaryKey=""true"">
2977 <xs:selector xpath="".//Element"" />
2978 <xs:field xpath=""@Dimension"" />
2979 <xs:field xpath=""@Number"" />
2983 DataSet ds = new DataSet ("Example");
2985 // Add a DataTable with no ReadOnly columns
2986 DataTable dt1 = new DataTable ("StandAlone");
2987 ds.Tables.Add (dt1);
2989 // Add a ReadOnly column
2990 dt1.Columns.Add (new DataColumn ("ID", typeof (int), "",
2991 MappingType.Attribute));
2992 dt1.Columns["ID"].AllowDBNull = false;
2994 dt1.Columns.Add (new DataColumn ("Desc", typeof
2995 (string), "", MappingType.Attribute));
2996 dt1.Columns["Desc"].AllowDBNull = false;
2998 // Add related DataTables with ReadOnly columns
2999 DataTable dt2 = new DataTable ("Dimension");
3000 ds.Tables.Add (dt2);
3001 dt2.Columns.Add (new DataColumn ("Number", typeof
3002 (int), "", MappingType.Attribute));
3003 dt2.Columns["Number"].AllowDBNull = false;
3004 dt2.Columns["Number"].ReadOnly = true;
3006 dt2.Columns.Add (new DataColumn ("Title", typeof
3007 (string), "", MappingType.Attribute));
3008 dt2.Columns["Title"].AllowDBNull = false;
3010 dt2.Constraints.Add ("PK_Dimension", dt2.Columns["Number"], true);
3012 DataTable dt3 = new DataTable ("Element");
3013 ds.Tables.Add (dt3);
3015 dt3.Columns.Add (new DataColumn ("Dimension", typeof
3016 (int), "", MappingType.Attribute));
3017 dt3.Columns["Dimension"].AllowDBNull = false;
3018 dt3.Columns["Dimension"].ReadOnly = true;
3020 dt3.Columns.Add (new DataColumn ("Number", typeof
3021 (int), "", MappingType.Attribute));
3022 dt3.Columns["Number"].AllowDBNull = false;
3023 dt3.Columns["Number"].ReadOnly = true;
3025 dt3.Columns.Add (new DataColumn ("Title", typeof
3026 (string), "", MappingType.Attribute));
3027 dt3.Columns["Title"].AllowDBNull = false;
3029 dt3.Constraints.Add ("PK_Element", new DataColumn[] {
3030 dt3.Columns ["Dimension"],
3031 dt3.Columns ["Number"] }, true);
3033 ds.AcceptChanges ();
3035 StringWriter sw1 = new StringWriter ();
3036 ds.Tables[0].WriteXmlSchema (sw1);
3037 string result1 = sw1.ToString ();
3038 AssertEquals (xmlschema1.Replace ("\r\n", "\n"), result1.Replace ("\r\n", "\n"));
3040 StringWriter sw2 = new StringWriter ();
3041 ds.Tables[1].WriteXmlSchema (sw2);
3042 string result2 = sw2.ToString ();
3043 AssertEquals (xmlschema2.Replace ("\r\n", "\n"), result2.Replace ("\r\n", "\n"));
3045 StringWriter sw3 = new StringWriter ();
3046 ds.Tables[2].WriteXmlSchema (sw3);
3047 string result3 = sw3.ToString ();
3048 AssertEquals (xmlschema3.Replace ("\r\n", "\n"), result3.Replace ("\r\n", "\n"));
3052 [Category ("NotWorking")]
3053 public void WriteXmlSchema6 () {
3054 string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
3055 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
3056 <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""MyType"" msdata:UseCurrentLocale=""true"">
3058 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
3059 <xs:element name=""MyType"">
3061 <xs:attribute name=""Desc"">
3063 <xs:restriction base=""xs:string"">
3064 <xs:maxLength value=""32"" />
3074 DataSet ds = new DataSet ("Example");
3076 // Add MyType DataTable
3077 ds.Tables.Add ("MyType");
3079 ds.Tables["MyType"].Columns.Add (new DataColumn (
3080 "Desc", typeof (string), "", MappingType.Attribute));
3081 ds.Tables["MyType"].Columns["Desc"].MaxLength = 32;
3083 ds.AcceptChanges ();
3085 StringWriter sw = new StringWriter ();
3086 ds.Tables[0].WriteXmlSchema (sw);
3088 string result = sw.ToString ();
3090 AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
3094 public void WriteXmlSchema7 () {
3095 DataSet ds = new DataSet ();
3096 DataTable dt = new DataTable ("table");
3097 dt.Columns.Add ("col1");
3098 dt.Columns.Add ("col2");
3100 dt.Rows.Add (new object[] { "foo", "bar" });
3101 StringWriter sw = new StringWriter ();
3102 ds.Tables[0].WriteXmlSchema (sw);
3103 Assert (sw.ToString ().IndexOf ("xmlns=\"\"") > 0);
3107 public void WriteXmlSchema_ConstraintNameWithSpaces () {
3108 DataSet ds = new DataSet ();
3109 DataTable table1 = ds.Tables.Add ("table1");
3110 DataTable table2 = ds.Tables.Add ("table2");
3112 table1.Columns.Add ("col1", typeof (int));
3113 table2.Columns.Add ("col1", typeof (int));
3115 table1.Constraints.Add ("uc 1", table1.Columns[0], false);
3116 table2.Constraints.Add ("fc 1", table1.Columns[0], table2.Columns[0]);
3118 StringWriter sw1 = new StringWriter ();
3119 StringWriter sw2 = new StringWriter ();
3121 //should not throw an exception
3122 ds.Tables[0].WriteXmlSchema (sw1);
3123 ds.Tables[1].WriteXmlSchema (sw2);
3127 public void WriteXmlSchema_ForignKeyConstraint () {
3128 DataSet ds1 = new DataSet ();
3130 DataTable table1 = ds1.Tables.Add ();
3131 DataTable table2 = ds1.Tables.Add ();
3133 DataColumn col1_1 = table1.Columns.Add ("col1", typeof (int));
3134 DataColumn col2_1 = table2.Columns.Add ("col1", typeof (int));
3136 table2.Constraints.Add ("fk", col1_1, col2_1);
3138 StringWriter sw1 = new StringWriter ();
3139 ds1.Tables[0].WriteXmlSchema (sw1);
3140 String xml1 = sw1.ToString ();
3141 Assert ("#1", xml1.IndexOf (@"<xs:unique name=""Constraint1"">") != -1);
3143 StringWriter sw2 = new StringWriter ();
3144 ds1.Tables[1].WriteXmlSchema (sw2);
3145 String xml2 = sw2.ToString ();
3146 Assert ("#2", xml2.IndexOf (@"<xs:unique name=""Constraint1"">") == -1);
3150 [Category ("NotWorking")]
3151 public void WriteXmlSchema_Relations_ForeignKeys () {
3152 MemoryStream ms1 = null;
3153 MemoryStream ms2 = null;
3154 MemoryStream msA = null;
3155 MemoryStream msB = null;
3157 DataSet ds1 = new DataSet ();
3159 DataTable table1 = ds1.Tables.Add ("Table 1");
3160 DataTable table2 = ds1.Tables.Add ("Table 2");
3162 DataColumn col1_1 = table1.Columns.Add ("col 1", typeof (int));
3163 DataColumn col1_2 = table1.Columns.Add ("col 2", typeof (int));
3164 DataColumn col1_3 = table1.Columns.Add ("col 3", typeof (int));
3165 DataColumn col1_4 = table1.Columns.Add ("col 4", typeof (int));
3166 DataColumn col1_5 = table1.Columns.Add ("col 5", typeof (int));
3167 DataColumn col1_6 = table1.Columns.Add ("col 6", typeof (int));
3168 DataColumn col1_7 = table1.Columns.Add ("col 7", typeof (int));
3170 DataColumn col2_1 = table2.Columns.Add ("col 1", typeof (int));
3171 DataColumn col2_2 = table2.Columns.Add ("col 2", typeof (int));
3172 DataColumn col2_3 = table2.Columns.Add ("col 3", typeof (int));
3173 DataColumn col2_4 = table2.Columns.Add ("col 4", typeof (int));
3174 DataColumn col2_5 = table2.Columns.Add ("col 5", typeof (int));
3175 DataColumn col2_6 = table2.Columns.Add ("col 6", typeof (int));
3176 DataColumn col2_7 = table2.Columns.Add ("col 7", typeof (int));
3178 ds1.Relations.Add ("rel 1",
3179 new DataColumn[] { col1_1, col1_2 },
3180 new DataColumn[] { col2_1, col2_2 },
3182 ds1.Relations.Add ("rel 2",
3183 new DataColumn[] { col1_3, col1_4 },
3184 new DataColumn[] { col2_3, col2_4 },
3186 table2.Constraints.Add ("fk 1",
3187 new DataColumn[] { col1_5, col1_6 },
3188 new DataColumn[] { col2_5, col2_6 });
3189 table1.Constraints.Add ("fk 2",
3190 new DataColumn[] { col2_5, col2_6 },
3191 new DataColumn[] { col1_5, col1_6 });
3193 table1.Constraints.Add ("pk 1", col1_7, true);
3194 table2.Constraints.Add ("pk 2", col2_7, true);
3196 ms1 = new MemoryStream ();
3197 ds1.Tables[0].WriteXmlSchema (ms1);
3198 ms2 = new MemoryStream ();
3199 ds1.Tables[1].WriteXmlSchema (ms2);
3201 msA = new MemoryStream (ms1.GetBuffer ());
3202 DataTable dtA = new DataTable ();
3203 dtA.ReadXmlSchema (msA);
3205 msB = new MemoryStream (ms2.GetBuffer ());
3206 DataTable dtB = new DataTable ();
3207 dtB.ReadXmlSchema (msB);
3209 AssertEquals ("#2", 3, dtA.Constraints.Count);
3210 AssertEquals ("#3", 2, dtB.Constraints.Count);
3212 Assert ("#5", dtA.Constraints.Contains ("pk 1"));
3213 Assert ("#6", dtA.Constraints.Contains ("Constraint1"));
3214 Assert ("#7", dtA.Constraints.Contains ("Constraint2"));
3215 Assert ("#9", dtB.Constraints.Contains ("pk 2"));
3216 Assert ("#10", dtB.Constraints.Contains ("Constraint1"));
3220 [Category ("NotWorking")]
3221 public void WriteXmlSchema_DifferentNamespace () {
3222 string schema = @"<xs:schema id='NewDataSet' targetNamespace='urn:bar' xmlns:mstns='urn:bar' xmlns='urn:bar' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified' xmlns:app1='urn:baz' xmlns:app2='urn:foo' msdata:schemafragmentcount='3'>
3223 <xs:import namespace='urn:foo' />
3224 <xs:import namespace='urn:baz' />
3225 <xs:element name='NewDataSet' msdata:IsDataSet='true' msdata:MainDataTable='urn_x003A_foo_x003A_NS1Table' msdata:UseCurrentLocale='true'>
3227 <xs:choice minOccurs='0' maxOccurs='unbounded'>
3228 <xs:element ref='app2:NS1Table' />
3233 <xs:schema targetNamespace='urn:baz' xmlns:mstns='urn:bar' xmlns='urn:baz' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified' xmlns:app1='urn:baz' xmlns:app2='urn:foo'>
3234 <xs:import namespace='urn:foo' />
3235 <xs:import namespace='urn:bar' />
3236 <xs:element name='column2' type='xs:string' />
3238 <xs:schema targetNamespace='urn:foo' xmlns:mstns='urn:bar' xmlns='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified' xmlns:app2='urn:foo' xmlns:app1='urn:baz'>
3239 <xs:import namespace='urn:bar' />
3240 <xs:import namespace='urn:baz' />
3241 <xs:element name='NS1Table'>
3244 <xs:element name='column1' type='xs:string' minOccurs='0' />
3245 <xs:element ref='app1:column2' minOccurs='0' />
3250 DataSet ds = new DataSet ();
3251 DataTable dt = new DataTable ();
3252 dt.TableName = "NS1Table";
3253 dt.Namespace = "urn:foo";
3254 dt.Columns.Add ("column1");
3255 dt.Columns.Add ("column2");
3256 dt.Columns[1].Namespace = "urn:baz";
3258 DataTable dt2 = new DataTable ();
3259 dt2.TableName = "NS2Table";
3260 dt2.Namespace = "urn:bar";
3261 ds.Tables.Add (dt2);
3262 ds.Namespace = "urn:bar";
3264 StringWriter sw1 = new StringWriter ();
3265 XmlTextWriter xw1 = new XmlTextWriter (sw1);
3266 xw1.Formatting = Formatting.Indented;
3267 xw1.QuoteChar = '\'';
3268 ds.Tables[0].WriteXmlSchema (xw1);
3269 string result1 = sw1.ToString ();
3270 AssertEquals ("#1", schema, result1);
3272 StringWriter sw2 = new StringWriter ();
3273 XmlTextWriter xw2 = new XmlTextWriter (sw2);
3274 xw2.Formatting = Formatting.Indented;
3275 xw2.QuoteChar = '\'';
3276 ds.Tables[0].WriteXmlSchema (xw2);
3277 string result2 = sw2.ToString ();
3278 AssertEquals ("#2", schema, result2);
3282 [Category ("NotWorking")]
3283 // WriteXmlSchema doesn't have overload wityh 2 parameters in System.Data
3284 // and is commented-out TWICE below
3285 public void WriteXmlSchema_Hierarchy () {
3286 DataSet ds = new DataSet ();
3287 DataTable table1 = new DataTable ();
3288 DataColumn idColumn = table1.Columns.Add ("ID", typeof (Int32));
3289 table1.Columns.Add ("Name", typeof (String));
3290 table1.PrimaryKey = new DataColumn[] { idColumn };
3291 DataTable table2 = new DataTable ();
3292 table2.Columns.Add (new DataColumn ("OrderID", typeof (Int32)));
3293 table2.Columns.Add (new DataColumn ("CustomerID", typeof (Int32)));
3294 table2.Columns.Add (new DataColumn ("OrderDate", typeof (DateTime)));
3295 table2.PrimaryKey = new DataColumn[] { table2.Columns[0] };
3296 ds.Tables.Add (table1);
3297 ds.Tables.Add (table2);
3298 ds.Relations.Add ("CustomerOrder",
3299 new DataColumn[] { table1.Columns[0] },
3300 new DataColumn[] { table2.Columns[1] }, true);
3302 StringWriter writer1 = new StringWriter ();
3303 //table1.WriteXmlSchema (writer1, false);
3304 string expected1 = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<xs:schema id=\"NewDataSet\" xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">\r\n <xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Table1\" msdata:UseCurrentLocale=\"true\">\r\n <xs:complexType>\r\n <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\">\r\n <xs:element name=\"Table1\">\r\n <xs:complexType>\r\n <xs:sequence>\r\n <xs:element name=\"ID\" type=\"xs:int\" />\r\n <xs:element name=\"Name\" type=\"xs:string\" minOccurs=\"0\" />\r\n </xs:sequence>\r\n </xs:complexType>\r\n </xs:element>\r\n </xs:choice>\r\n </xs:complexType>\r\n <xs:unique name=\"Constraint1\" msdata:PrimaryKey=\"true\">\r\n <xs:selector xpath=\".//Table1\" />\r\n <xs:field xpath=\"ID\" />\r\n </xs:unique>\r\n </xs:element>\r\n</xs:schema>";
3305 AssertEquals ("#1", expected1, writer1.ToString());
3307 StringWriter writer2 = new StringWriter ();
3308 //table1.WriteXmlSchema (writer2, true);
3309 string expected2 = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<xs:schema id=\"NewDataSet\" xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">\r\n <xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Table1\" msdata:UseCurrentLocale=\"true\">\r\n <xs:complexType>\r\n <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\">\r\n <xs:element name=\"Table1\">\r\n <xs:complexType>\r\n <xs:sequence>\r\n <xs:element name=\"ID\" type=\"xs:int\" />\r\n <xs:element name=\"Name\" type=\"xs:string\" minOccurs=\"0\" />\r\n </xs:sequence>\r\n </xs:complexType>\r\n </xs:element>\r\n <xs:element name=\"Table2\">\r\n <xs:complexType>\r\n <xs:sequence>\r\n <xs:element name=\"OrderID\" type=\"xs:int\" />\r\n <xs:element name=\"CustomerID\" type=\"xs:int\" minOccurs=\"0\" />\r\n <xs:element name=\"OrderDate\" type=\"xs:dateTime\" minOccurs=\"0\" />\r\n </xs:sequence>\r\n </xs:complexType>\r\n </xs:element>\r\n </xs:choice>\r\n </xs:complexType>\r\n <xs:unique name=\"Constraint1\" msdata:PrimaryKey=\"true\">\r\n <xs:selector xpath=\".//Table1\" />\r\n <xs:field xpath=\"ID\" />\r\n </xs:unique>\r\n <xs:unique name=\"Table2_Constraint1\" msdata:ConstraintName=\"Constraint1\" msdata:PrimaryKey=\"true\">\r\n <xs:selector xpath=\".//Table2\" />\r\n <xs:field xpath=\"OrderID\" />\r\n </xs:unique>\r\n <xs:keyref name=\"CustomerOrder\" refer=\"Constraint1\">\r\n <xs:selector xpath=\".//Table2\" />\r\n <xs:field xpath=\"CustomerID\" />\r\n </xs:keyref>\r\n </xs:element>\r\n</xs:schema>";
3310 AssertEquals ("#2", expected2, writer2.ToString ());
3314 [Category ("NotWorking")]
3315 // WriteXmlSchema doesn't have overload wityh 2 parameters in System.Data
3316 // and is commented-out TWICE below
3317 public void ReadWriteXmlSchema () {
3318 DataSet ds = new DataSet ();
3319 ds.ReadXmlSchema ("Test/System.Data/store.xsd");
3320 // check dataset properties before testing write
3321 AssertDataSet ("ds", ds, "NewDataSet", 3, 2);
3322 AssertDataTable ("tab1", ds.Tables[0], "bookstore", 1, 0, 0, 1, 1, 1);
3323 AssertDataTable ("tab2", ds.Tables[1], "book", 5, 0, 1, 1, 2, 1);
3324 AssertDataTable ("tab3", ds.Tables[2], "author", 3, 0, 1, 0, 1, 0);
3325 // FIXME: currently order is not compatible. Use name as index
3326 AssertDataRelation ("rel1", ds.Relations["book_author"], "book_author", true, new string[] { "book_Id" }, new string[] { "book_Id" }, true, true);
3327 AssertDataRelation ("rel2", ds.Relations["bookstore_book"], "bookstore_book", true, new string[] { "bookstore_Id" }, new string[] { "bookstore_Id" }, true, true);
3329 ds.ReadXml ("Test/System.Data/region.xml", XmlReadMode.InferSchema);
3330 ds.Relations.Clear (); // because can not call WriteXmlSchema with nested relations.
3332 TextWriter writer1 = new StringWriter ();
3333 ds.Tables[0].WriteXmlSchema (writer1);
3334 string TextString1 = GetNormalizedSchema (writer1.ToString ());
3335 string expected1 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3336 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3337 @"<xs:complexType name=""bookstoreType"">" +
3338 @"</xs:complexType>" +
3339 @"<xs:element name=""bookstore"" type=""bookstoreType"" />" +
3340 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""bookstore"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3341 @"<xs:complexType>" +
3342 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3343 @"<xs:element ref=""bookstore"" />" +
3345 @"</xs:complexType>" +
3348 AssertEquals ("#1", expected1, TextString1.Replace("\r\n","").Replace(" ",""));
3350 TextWriter writer2 = new StringWriter ();
3351 //ds.Tables[1].WriteXmlSchema (writer2,false);
3352 string TextString2 = GetNormalizedSchema (writer2.ToString ());
3353 string expected2 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3354 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3355 @"<xs:complexType name=""bookType"">" +
3357 @"<xs:element msdata:Ordinal=""1"" name=""title"" type=""xs:string"" />" +
3358 @"<xs:element msdata:Ordinal=""2"" name=""price"" type=""xs:decimal"" />" +
3360 @"<xs:attribute name=""genre"" type=""xs:string"" />" +
3361 @"<xs:attribute name=""bookstore_Id"" type=""xs:int"" use=""prohibited"" />" +
3362 @"</xs:complexType>" +
3363 @"<xs:element name=""book"" type=""bookType"" />" +
3364 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""book"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3365 @"<xs:complexType>" +
3366 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3367 @"<xs:element ref=""book"" />" +
3369 @"</xs:complexType>" +
3372 AssertEquals ("#2", expected2, TextString2.Replace ("\r\n", "").Replace (" ", ""));
3374 TextWriter writer3 = new StringWriter ();
3375 ds.Tables[2].WriteXmlSchema (writer3);
3376 string TextString3 = GetNormalizedSchema (writer3.ToString ());
3377 string expected3 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3378 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3379 @"<xs:complexType name=""authorName"">" +
3381 @"<xs:element msdata:Ordinal=""0"" name=""first-name"" type=""xs:string"" />" +
3382 @"<xs:element msdata:Ordinal=""1"" name=""last-name"" type=""xs:string"" />" +
3384 @"<xs:attribute name=""book_Id"" type=""xs:int"" use=""prohibited"" />" +
3385 @"</xs:complexType>" +
3386 @"<xs:element name=""author"" type=""authorName"" />" +
3387 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""author"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3388 @"<xs:complexType>" +
3389 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3390 @"<xs:element ref=""author"" />" +
3392 @"</xs:complexType>" +
3395 AssertEquals ("#3", expected3, TextString3.Replace ("\r\n", "").Replace (" ", ""));
3397 TextWriter writer4 = new StringWriter ();
3398 ds.Tables[3].WriteXmlSchema (writer4);
3399 string TextString4 = GetNormalizedSchema (writer4.ToString ());
3400 string expected4 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3401 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3402 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""Region"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3403 @"<xs:complexType>" +
3404 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3405 @"<xs:element name=""Region"">" +
3406 @"<xs:complexType>" +
3408 @"<xs:element minOccurs=""0"" name=""RegionID"" type=""xs:string"" />" +
3409 @"<xs:element minOccurs=""0"" name=""RegionDescription"" type=""xs:string"" />" +
3411 @"</xs:complexType>" +
3414 @"</xs:complexType>" +
3417 AssertEquals ("#4", expected4, TextString4.Replace ("\r\n", "").Replace (" ", ""));
3421 [Category ("NotWorking")]
3422 public void ReadWriteXmlSchema_IgnoreSchema () {
3423 DataSet ds = new DataSet ();
3424 ds.ReadXmlSchema ("Test/System.Data/store.xsd");
3425 // check dataset properties before testing write
3426 AssertDataSet ("ds", ds, "NewDataSet", 3, 2);
3427 AssertDataTable ("tab1", ds.Tables[0], "bookstore", 1, 0, 0, 1, 1, 1);
3428 AssertDataTable ("tab2", ds.Tables[1], "book", 5, 0, 1, 1, 2, 1);
3429 AssertDataTable ("tab3", ds.Tables[2], "author", 3, 0, 1, 0, 1, 0);
3430 // FIXME: currently order is not compatible. Use name as index
3431 AssertDataRelation ("rel1", ds.Relations["book_author"], "book_author", true, new string[] { "book_Id" }, new string[] { "book_Id" }, true, true);
3432 AssertDataRelation ("rel2", ds.Relations["bookstore_book"], "bookstore_book", true, new string[] { "bookstore_Id" }, new string[] { "bookstore_Id" }, true, true);
3434 ds.ReadXml ("Test/System.Data/region.xml", XmlReadMode.IgnoreSchema);
3435 ds.Relations.Clear (); // because can not call WriteXmlSchema with nested relations.
3437 TextWriter writer1 = new StringWriter ();
3438 ds.Tables[0].WriteXmlSchema (writer1);
3439 string TextString1 = GetNormalizedSchema (writer1.ToString ());
3440 string expected1 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3441 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3442 @"<xs:complexType name=""bookstoreType"">" +
3443 @"</xs:complexType>" +
3444 @"<xs:element name=""bookstore"" type=""bookstoreType"" />" +
3445 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""bookstore"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3446 @"<xs:complexType>" +
3447 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3448 @"<xs:element ref=""bookstore"" />" +
3450 @"</xs:complexType>" +
3453 AssertEquals ("#1", expected1, TextString1.Replace ("\r\n", "").Replace (" ", ""));
3455 TextWriter writer2 = new StringWriter ();
3456 //ds.Tables[1].WriteXmlSchema (writer2, false);
3457 string TextString2 = GetNormalizedSchema (writer2.ToString ());
3458 string expected2 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3459 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3460 @"<xs:complexType name=""bookType"">" +
3462 @"<xs:element msdata:Ordinal=""1"" name=""title"" type=""xs:string"" />" +
3463 @"<xs:element msdata:Ordinal=""2"" name=""price"" type=""xs:decimal"" />" +
3465 @"<xs:attribute name=""genre"" type=""xs:string"" />" +
3466 @"<xs:attribute name=""bookstore_Id"" type=""xs:int"" use=""prohibited"" />" +
3467 @"</xs:complexType>" +
3468 @"<xs:element name=""book"" type=""bookType"" />" +
3469 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""book"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3470 @"<xs:complexType>" +
3471 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3472 @"<xs:element ref=""book"" />" +
3474 @"</xs:complexType>" +
3477 AssertEquals ("#2", expected2, TextString2.Replace ("\r\n", "").Replace (" ", ""));
3479 TextWriter writer3 = new StringWriter ();
3480 ds.Tables[2].WriteXmlSchema (writer3);
3481 string TextString3 = GetNormalizedSchema (writer3.ToString ());
3482 string expected3 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3483 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3484 @"<xs:complexType name=""authorName"">" +
3486 @"<xs:element msdata:Ordinal=""0"" name=""first-name"" type=""xs:string"" />" +
3487 @"<xs:element msdata:Ordinal=""1"" name=""last-name"" type=""xs:string"" />" +
3489 @"<xs:attribute name=""book_Id"" type=""xs:int"" use=""prohibited"" />" +
3490 @"</xs:complexType>" +
3491 @"<xs:element name=""author"" type=""authorName"" />" +
3492 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""author"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3493 @"<xs:complexType>" +
3494 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3495 @"<xs:element ref=""author"" />" +
3497 @"</xs:complexType>" +
3500 AssertEquals ("#3", expected3, TextString3.Replace ("\r\n", "").Replace (" ", ""));
3502 TextWriter writer4 = new StringWriter ();
3505 ds.Tables[3].WriteXmlSchema (writer4);
3507 catch (Exception ex) {
3508 expStr = ex.Message;
3510 AssertEquals ("#4", "Cannot find table 3.", expStr);
3514 [Category ("NotWorking")]
3515 public void ReadWriteXmlSchema_2 () {
3516 DataSet ds = new DataSet ("dataset");
3517 ds.Tables.Add ("table1");
3518 ds.Tables.Add ("table2");
3519 ds.Tables[0].Columns.Add ("col");
3520 ds.Tables[1].Columns.Add ("col");
3521 ds.Relations.Add ("rel", ds.Tables[0].Columns[0], ds.Tables[1].Columns[0], true);
3523 MemoryStream ms1 = new MemoryStream ();
3524 ds.Tables[0].WriteXmlSchema (ms1);
3525 MemoryStream ms2 = new MemoryStream ();
3526 ds.Tables[1].WriteXmlSchema (ms2);
3528 DataSet ds1 = new DataSet ();
3531 ds1.Tables[0].ReadXmlSchema (new MemoryStream (ms1.GetBuffer ()));
3532 ds1.Tables[1].ReadXmlSchema (new MemoryStream (ms2.GetBuffer ()));
3534 AssertEquals ("#1", 0, ds1.Relations.Count);
3535 AssertEquals ("#2", 1, ds1.Tables[0].Columns.Count);
3536 AssertEquals ("#3", 1, ds1.Tables[1].Columns.Count);
3540 [ExpectedException (typeof (XmlException))]
3541 public void ReadWriteXmlSchemaExp_NoRootElmnt () {
3542 MemoryStream ms = new MemoryStream ();
3543 DataTable dtr = new DataTable ();
3544 dtr.ReadXmlSchema (ms);
3548 [Category ("NotWorking")]
3549 [ExpectedException (typeof (InvalidOperationException))]
3550 public void ReadWriteXmlSchemaExp_NoTableName () {
3551 DataTable dtw = new DataTable ();
3552 MemoryStream ms = new MemoryStream ();
3553 dtw.WriteXmlSchema (ms);
3557 [ExpectedException (typeof (ArgumentException))]
3558 public void ReadWriteXmlSchemaExp_TableNameConflict () {
3559 DataTable dtw = new DataTable ("Table1");
3560 StringWriter writer1 = new StringWriter ();
3561 dtw.WriteXmlSchema (writer1);
3562 DataTable dtr = new DataTable ("Table2");
3563 StringReader reader1 = new StringReader (writer1.ToString());
3564 dtr.ReadXmlSchema (reader1);
3567 #endregion // Read/Write XML Tests
3574 public class MyDataTable:DataTable {
3576 public static int count = 0;
3578 public MyDataTable() {
3587 public class AppDomainsAndFormatInfo
3589 public void Remote ()
3591 int n = (int) Convert.ChangeType ("5", typeof (int));
3592 Assertion.AssertEquals ("n", 5, n);
3596 public void NFIFromBug55978 ()
3598 AppDomain domain = AppDomain.CreateDomain ("testdomain");
3599 AppDomainsAndFormatInfo test = new AppDomainsAndFormatInfo ();
3601 domain.DoCallBack (new CrossAppDomainDelegate (test.Remote));
3602 AppDomain.Unload (domain);
3607 public void Bug55978 ()
3609 DataTable dt = new DataTable ();
3610 dt.Columns.Add ("StartDate", typeof (DateTime));
3613 DateTime date = DateTime.Now;
3615 for (int i = 0; i < 10; i++) {
3617 dr ["StartDate"] = date.AddDays (i);
3621 DataView dv = dt.DefaultView;
3622 dv.RowFilter = "StartDate >= '" + DateTime.Now.AddDays (2) + "' and StartDate <= '" + DateTime.Now.AddDays (4) + "'";
3623 Assertion.AssertEquals ("Table", 10, dt.Rows.Count);
3624 Assertion.AssertEquals ("View", 2, dv.Count);