2 // Rafael Mizrahi <rafim@mainsoft.com>
3 // Erez Lotan <erezl@mainsoft.com>
4 // Oren Gurfinkel <oreng@mainsoft.com>
7 // Copyright (c) 2004 Mainsoft Co.
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using NUnit.Framework;
34 using MonoTests.System.Data.Utils;
37 namespace MonoTests_System.Data
39 [TestFixture] public class DataSetTest2
41 private DataSet m_ds = null;
42 private bool EventRaised = false;
44 [Test] public void AcceptChanges()
46 DataSet ds = new DataSet();
47 DataTable dtP = DataProvider.CreateParentDataTable();
48 DataTable dtC = DataProvider.CreateChildDataTable();
51 ds.Relations.Add(new DataRelation("myRelation",dtP.Columns[0],dtC.Columns[0]));
54 dtP.Rows[0][0] = "70";
56 dtP.Rows.Add(new object[] {9,"string1","string2"});
60 Assert.AreEqual(null, dtP.GetChanges(), "DS1");
63 dtP.Columns[0].ReadOnly = true;
64 // check ReadOnlyException
68 Assert.Fail("DS2: Indexer Failed to throw ReadOnlyException");
70 catch (ReadOnlyException) {}
71 catch (AssertionException exc) {throw exc;}
74 Assert.Fail("DS3: Indexer. Wrong exception type. Got:" + exc);
77 // check invoke AcceptChanges
81 [Test] public void CaseSensitive()
83 DataSet ds = new DataSet();
84 DataTable dt = new DataTable();
86 // CaseSensitive - default value (false)
87 Assert.AreEqual(false , ds.CaseSensitive , "DS4");
89 ds.CaseSensitive = true;
91 // CaseSensitive - get
92 Assert.AreEqual(true , ds.CaseSensitive , "DS5");
94 //add a datatable to a dataset
96 // DataTable CaseSensitive from DataSet - true
97 Assert.AreEqual(true , dt.CaseSensitive , "DS6");
100 ds.CaseSensitive = false;
101 dt = new DataTable();
104 // DataTable CaseSensitive from DataSet - false
105 Assert.AreEqual(false, dt.CaseSensitive , "DS7");
107 //change DataSet CaseSensitive and check DataTables in it
109 ds.CaseSensitive = false;
110 dt = new DataTable();
113 // Change DataSet CaseSensitive - check Table - true
114 ds.CaseSensitive = true;
115 Assert.AreEqual(true, dt.CaseSensitive , "DS8");
117 // Change DataSet CaseSensitive - check Table - false
118 ds.CaseSensitive = false;
119 Assert.AreEqual(false, dt.CaseSensitive , "DS9");
121 //Add new table to DataSet with CaseSensitive,check the table case after adding it to DataSet
123 ds.CaseSensitive = true;
124 dt = new DataTable();
125 dt.CaseSensitive = false;
128 // DataTable get case sensitive from DataSet - false
129 Assert.AreEqual(false, dt.CaseSensitive , "DS10");
132 ds.CaseSensitive = false;
133 dt = new DataTable();
134 dt.CaseSensitive = true;
137 // DataTable get case sensitive from DataSet - true
138 Assert.AreEqual(true, dt.CaseSensitive , "DS11");
140 //Add new table to DataSet and change the DataTable CaseSensitive
142 ds.CaseSensitive = true;
143 dt = new DataTable();
146 // Add new table to DataSet and change the DataTable CaseSensitive - false
147 dt.CaseSensitive = false;
148 Assert.AreEqual(false, dt.CaseSensitive , "DS12");
151 ds.CaseSensitive = false;
152 dt = new DataTable();
155 // Add new table to DataSet and change the DataTable CaseSensitive - true
156 dt.CaseSensitive = true;
157 Assert.AreEqual(true, dt.CaseSensitive , "DS13");
159 //Add DataTable to Dataset, Change DataSet CaseSensitive, check DataTable
161 ds.CaseSensitive = true;
162 dt = new DataTable();
163 dt.CaseSensitive = true;
166 // Add DataTable to Dataset, Change DataSet CaseSensitive, check DataTable - true
167 ds.CaseSensitive = false;
168 Assert.AreEqual(true, dt.CaseSensitive , "DS14");
171 [Test] public void Clear()
173 DataSet ds = new DataSet();
174 ds.Tables.Add(DataProvider.CreateParentDataTable());
175 ds.Tables[0].Rows.Add(new object[] {9,"",""});
179 Assert.AreEqual(0, ds.Tables[0].Rows.Count , "DS15");
182 [Test] public void Clear_WithNoDataWithConstraint()
184 // Test dataset with no data and with constraint
185 DataSet ds = new DataSet();
186 ds.Tables.Add(DataProvider.CreateParentDataTable());
187 ds.Tables.Add(DataProvider.CreateChildDataTable());
188 ds.Tables[0].Rows.Clear();
189 ds.Tables[1].Rows.Clear();
191 ds.Tables[0].Constraints.Add("test",ds.Tables[1].Columns[0],ds.Tables[0].Columns[0]);
195 [Test] public void Clone()
197 DataSet ds = new DataSet(), dsTarget = null;
198 ds.Tables.Add(DataProvider.CreateParentDataTable());
199 ds.Tables.Add(DataProvider.CreateChildDataTable());
200 ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
201 ds.Tables[0].Rows.Add(new object[] {9,"",""});
202 ds.Tables[1].Columns[2].ReadOnly = true;
203 ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
205 //copy schema only, no data
208 dsTarget = ds.Clone();
209 //Assert.AreEqual(ds.GetXmlSchema(), dsTarget.GetXmlSchema() , "DS16");
210 //use my function because GetXmlSchema not implemented in java
211 Assert.AreEqual(DataProvider.GetDSSchema(ds), DataProvider.GetDSSchema(dsTarget), "DS17");
214 Assert.AreEqual(false, dsTarget.GetXml() == ds.GetXml(), "DS18");
217 [Test] public void Copy()
219 DataSet ds = new DataSet(), dsTarget = null;
220 ds.Tables.Add(DataProvider.CreateParentDataTable());
221 ds.Tables.Add(DataProvider.CreateChildDataTable());
222 ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
223 ds.Tables[0].Rows.Add(new object[] {9,"",""});
224 ds.Tables[1].Columns[2].ReadOnly = true;
225 ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
227 //copy data and schema
230 dsTarget = ds.Copy();
231 //Assert.AreEqual(ds.GetXmlSchema(), dsTarget.GetXmlSchema() , "DS19");
232 //using my function because GetXmlSchema in not implemented in java
233 Assert.AreEqual(DataProvider.GetDSSchema(ds), DataProvider.GetDSSchema (dsTarget) , "DS20");
236 Assert.AreEqual(true, dsTarget.GetXml() == ds.GetXml(), "DS21");
239 [Test] public void DataSetName()
241 DataSet ds = new DataSet();
243 // DataSetName - default value
244 Assert.AreEqual("NewDataSet" , ds.DataSetName , "DS22");
246 ds.DataSetName = "NewName";
249 Assert.AreEqual("NewName" , ds.DataSetName , "DS23");
252 [Test] public void EnforceConstraints()
254 DataSet ds = new DataSet();
256 // EnforceConstraints - default value (true)
257 Assert.AreEqual(true, ds.EnforceConstraints , "DS24");
259 ds.EnforceConstraints = false;
261 // EnforceConstraints - get
262 Assert.AreEqual(false, ds.EnforceConstraints , "DS25");
265 [Test] public void GetChanges()
267 DataSet ds = new DataSet();
268 ds.Tables.Add(DataProvider.CreateParentDataTable());
271 Assert.AreEqual(null , ds.GetChanges(), "DS26");
273 DataRow dr = ds.Tables[0].NewRow();
275 ds.Tables[0].Rows.Add(dr);
278 Assert.AreEqual(true , ds.GetChanges()!=null, "DS27");
281 Assert.AreEqual(dr.ItemArray, ds.GetChanges().Tables[0].Rows[0].ItemArray , "DS28");
284 [Test] public void GetChanges_ByDataRowState()
286 DataSet ds = new DataSet();
287 object[] arrAdded,arrDeleted,arrModified,arrUnchanged;
288 //object[] arrDetached;
291 ds.Tables.Add(DataProvider.CreateParentDataTable());
294 Assert.AreEqual(null , ds.GetChanges(), "DS29");
298 // can't check detached
299 // dr = ds.Tables[0].Rows[0];
300 // arrDetached = dr.ItemArray;
302 // ds.Tables[0].AcceptChanges();
304 dr= ds.Tables[0].Rows[1];
305 arrDeleted = dr.ItemArray;
308 dr = ds.Tables[0].Rows[2];
310 arrModified = dr.ItemArray;
312 dr = ds.Tables[0].Select("","",DataViewRowState.Unchanged)[0];
313 arrUnchanged = dr.ItemArray;
315 dr = ds.Tables[0].NewRow();
317 ds.Tables[0].Rows.Add(dr);
318 arrAdded = dr.ItemArray;
321 Assert.AreEqual(arrAdded, ds.GetChanges(DataRowState.Added).Tables[0].Rows[0].ItemArray , "DS30");
323 // GetChanges Deleted
324 dr = ds.GetChanges(DataRowState.Deleted).Tables[0].Rows[0];
325 object[] tmp = new object[] {dr[0,DataRowVersion.Original],dr[1,DataRowVersion.Original],dr[2,DataRowVersion.Original],dr[3,DataRowVersion.Original],dr[4,DataRowVersion.Original],dr[5,DataRowVersion.Original]};
326 Assert.AreEqual(arrDeleted, tmp, "DS31");
329 // // GetChanges Detached
330 // dr = ds.GetChanges(DataRowState.Detached).Tables[0].Rows[0];
331 // object[] tmp = new object[] {dr[0,DataRowVersion.Original],dr[1,DataRowVersion.Original],dr[2,DataRowVersion.Original]};
332 // Assert.AreEqual(arrDetached, tmp, "DS32");
334 // GetChanges Modified
335 Assert.AreEqual(arrModified, ds.GetChanges(DataRowState.Modified).Tables[0].Rows[0].ItemArray , "DS33");
337 // GetChanges Unchanged
338 Assert.AreEqual(arrUnchanged, ds.GetChanges(DataRowState.Unchanged).Tables[0].Rows[0].ItemArray , "DS34");
341 [Test] public void GetXml()
343 DataSet ds = new DataSet();
344 ds.Namespace = "namespace"; //if we don't add namespace the test will fail because GH (by design) always add namespace
345 DataTable dt = DataProvider.CreateParentDataTable();
347 dt.Rows.Add(new object[] {1,"Value1","Value2"});
348 dt.Rows.Add(new object[] {2,"Value3","Value4"});
349 dt.Rows.Add(new object[] {3,"Value5","Value5"});
351 System.Text.StringBuilder resultXML = new System.Text.StringBuilder();
353 resultXML.Append("<" + ds.DataSetName + "xmlns=\"namespace\">");
355 resultXML.Append("<Parent>");
356 resultXML.Append("<ParentId>1</ParentId>");
357 resultXML.Append("<String1>Value1</String1>");
358 resultXML.Append("<String2>Value2</String2>");
359 resultXML.Append("</Parent>");
361 resultXML.Append("<Parent>");
362 resultXML.Append("<ParentId>2</ParentId>");
363 resultXML.Append("<String1>Value3</String1>");
364 resultXML.Append("<String2>Value4</String2>");
365 resultXML.Append("</Parent>");
367 resultXML.Append("<Parent>");
368 resultXML.Append("<ParentId>3</ParentId>");
369 resultXML.Append("<String1>Value5</String1>");
370 resultXML.Append("<String2>Value5</String2>");
371 resultXML.Append("</Parent>");
373 resultXML.Append("</" + ds.DataSetName + ">");
376 string strXML = ds.GetXml();
377 strXML = strXML.Replace(" ","");
378 strXML = strXML.Replace("\t","");
379 strXML = strXML.Replace("\n","");
380 strXML = strXML.Replace("\r","");
383 Assert.AreEqual(resultXML.ToString() , strXML , "DS35");
386 [Test] public void HasChanges()
388 DataSet ds = new DataSet();
389 ds.Tables.Add(DataProvider.CreateParentDataTable());
392 Assert.AreEqual(false , ds.HasChanges(), "DS36");
394 DataRow dr = ds.Tables[0].NewRow();
396 ds.Tables[0].Rows.Add(dr);
399 Assert.AreEqual(true , ds.HasChanges(), "DS37");
402 [Test] public void HasChanges_ByDataRowState()
404 DataSet ds = new DataSet();
407 ds.Tables.Add(DataProvider.CreateParentDataTable());
410 Assert.AreEqual(false , ds.HasChanges(), "DS38");
414 dr= ds.Tables[0].Rows[1];
417 dr = ds.Tables[0].Rows[2];
420 dr = ds.Tables[0].Select("","",DataViewRowState.Unchanged)[0];
422 dr = ds.Tables[0].NewRow();
424 ds.Tables[0].Rows.Add(dr);
427 Assert.AreEqual(true , ds.HasChanges(DataRowState.Added), "DS39");
429 // HasChanges Deleted
430 Assert.AreEqual(true , ds.HasChanges(DataRowState.Deleted) , "DS40");
432 // HasChanges Modified
433 Assert.AreEqual(true, ds.HasChanges(DataRowState.Modified), "DS41");
435 // HasChanges Unchanged
436 Assert.AreEqual(true, ds.HasChanges(DataRowState.Unchanged), "DS42");
439 [Test] public void HasErrors()
441 DataSet ds = new DataSet();
442 ds.Tables.Add(DataProvider.CreateParentDataTable());
444 // HasErrors - default
445 Assert.AreEqual(false , ds.HasErrors , "DS43");
447 ds.Tables[0].Rows[0].RowError = "ErrDesc";
450 Assert.AreEqual(true , ds.HasErrors , "DS44");
453 #region test namespaces
455 [Test] public void InferXmlSchema_BasicXml()
457 StringBuilder sb = new StringBuilder();
458 sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
459 sb.Append("<Categories>");
460 sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
461 sb.Append("<CategoryName od:maxLength='15' od:adotype='130'>Beverages</CategoryName>");
462 sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
463 sb.Append("</Categories>");
464 sb.Append("<Products>");
465 sb.Append("<ProductID od:adotype='20'>1</ProductID>");
466 sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
467 sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
468 sb.Append("</Products>");
469 sb.Append("</NewDataSet>");
471 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
473 DataSet ds = new DataSet();
474 // ds.ReadXml(myStream);
475 ds.InferXmlSchema(myStream, new string[] {"urn:schemas-microsoft-com:officedata"});
476 Assert.AreEqual(2, ds.Tables.Count, "DS45");
477 Assert.AreEqual("CategoryID", ds.Tables[0].Columns[0].ColumnName, "DS46");
478 Assert.AreEqual("CategoryName", ds.Tables[0].Columns[1].ColumnName, "DS47");
479 Assert.AreEqual("Description", ds.Tables[0].Columns[2].ColumnName, "DS48");
481 Assert.AreEqual("ProductID", ds.Tables[1].Columns[0].ColumnName, "DS49");
482 Assert.AreEqual("ReorderLevel", ds.Tables[1].Columns[1].ColumnName, "DS50");
483 Assert.AreEqual("Discontinued", ds.Tables[1].Columns[2].ColumnName, "DS51");
486 [Test] public void InferXmlSchema_WithoutIgnoreNameSpaces()
488 StringBuilder sb = new StringBuilder();
489 sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
490 sb.Append("<Categories>");
491 sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
492 sb.Append("<CategoryName od:maxLength='15' od:adotype='130'>Beverages</CategoryName>");
493 sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
494 sb.Append("</Categories>");
495 sb.Append("<Products>");
496 sb.Append("<ProductID od:adotype='20'>1</ProductID>");
497 sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
498 sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
499 sb.Append("</Products>");
500 sb.Append("</NewDataSet>");
502 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
504 DataSet ds = new DataSet();
505 //ds.ReadXml(myStream);
506 ds.InferXmlSchema(myStream,new string[] {"urn:schemas-microsoft-com:officedata1"});
507 Assert.AreEqual(8, ds.Tables.Count, "DS52");
510 [Test] public void InferXmlSchema_IgnoreNameSpace()
512 StringBuilder sb = new StringBuilder();
513 sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
514 sb.Append("<Categories>");
515 sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
516 sb.Append("<CategoryName od:maxLength='15' adotype='130'>Beverages</CategoryName>");
517 sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
518 sb.Append("</Categories>");
519 sb.Append("<Products>");
520 sb.Append("<ProductID od:adotype='20'>1</ProductID>");
521 sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
522 sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
523 sb.Append("</Products>");
524 sb.Append("</NewDataSet>");
526 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
528 DataSet ds = new DataSet();
529 // ds.ReadXml(myStream);
530 ds.InferXmlSchema(myStream, new string[] {"urn:schemas-microsoft-com:officedata"});
531 Assert.AreEqual(3, ds.Tables.Count, "DS53");
533 Assert.AreEqual(3, ds.Tables[0].Columns.Count, "DS54");
534 Assert.AreEqual("CategoryID", ds.Tables[0].Columns["CategoryID"].ColumnName, "DS55");
535 Assert.AreEqual("Categories_Id", ds.Tables[0].Columns["Categories_Id"].ColumnName, "DS56");//Hidden
536 Assert.AreEqual("Description", ds.Tables[0].Columns["Description"].ColumnName, "DS57");
538 Assert.AreEqual(3, ds.Tables[1].Columns.Count, "DS58");
539 Assert.AreEqual("adotype", ds.Tables[1].Columns["adotype"].ColumnName, "DS59");
540 Assert.AreEqual("CategoryName_Text", ds.Tables[1].Columns["CategoryName_Text"].ColumnName, "DS60");
541 Assert.AreEqual("Categories_Id", ds.Tables[1].Columns["Categories_Id"].ColumnName, "DS61");//Hidden
543 Assert.AreEqual(3, ds.Tables[2].Columns.Count, "DS62");
544 Assert.AreEqual("ProductID", ds.Tables[2].Columns["ProductID"].ColumnName, "DS63");
545 Assert.AreEqual("ReorderLevel", ds.Tables[2].Columns["ReorderLevel"].ColumnName, "DS64");
546 Assert.AreEqual("Discontinued", ds.Tables[2].Columns["Discontinued"].ColumnName, "DS65");
549 [Test] public void InferXmlSchema_IgnoreNameSpaces() //Ignoring 2 namespaces
551 StringBuilder sb = new StringBuilder();
552 sb.Append("<h:html xmlns:xdc='http://www.xml.com/books' xmlns:h='http://www.w3.org/HTML/1998/html4'>");
553 sb.Append("<h:head><h:title>Book Review</h:title></h:head>");
554 sb.Append("<h:body>");
555 sb.Append("<xdc:bookreview>");
556 sb.Append("<xdc:title h:attrib1='1' xdc:attrib2='2' >XML: A Primer</xdc:title>");
557 sb.Append("</xdc:bookreview>");
558 sb.Append("</h:body>");
559 sb.Append("</h:html>");
561 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
562 DataSet tempDs = new DataSet();
563 tempDs.ReadXml(myStream);
564 myStream.Seek(0,SeekOrigin.Begin);
565 DataSet ds = new DataSet();
566 ds.InferXmlSchema(myStream, new string[] {"http://www.xml.com/books","http://www.w3.org/HTML/1998/html4"});
567 //Assert.AreEqual(8, ds.Tables.Count, "DS66");
569 // string str1 = tempDs.GetXmlSchema(); //DataProvider.GetDSSchema(tempDs);
570 // string str2 = ds.GetXmlSchema(); //DataProvider.GetDSSchema(ds);
571 Assert.AreEqual(3, ds.Tables.Count, "DS67");
572 Assert.AreEqual("bookreview", ds.Tables[2].TableName, "DS68");
573 Assert.AreEqual(2, ds.Tables[2].Columns.Count, "DS69");
577 #region inferingTables
578 [Test] public void InferXmlSchema_inferingTables1()
580 //Acroding to the msdn documantaion :
581 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
582 //Elements that have attributes specified in them will result in inferred tables
585 StringBuilder sb = new StringBuilder();
587 sb.Append("<DocumentElement>");
588 sb.Append("<Element1 attr1='value1'/>");
589 sb.Append("<Element1 attr1='value2'>Text1</Element1>");
590 sb.Append("</DocumentElement>");
591 DataSet ds = new DataSet();
592 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
593 ds.InferXmlSchema(myStream,null);
594 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS70");
595 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS71");
596 Assert.AreEqual(1, ds.Tables.Count, "DS72");
597 Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS73");
598 Assert.AreEqual("Element1_Text", ds.Tables[0].Columns["Element1_Text"].ColumnName, "DS74");
601 [Test] public void InferXmlSchema_inferingTables2()
603 //Acroding to the msdn documantaion :
604 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
605 //Elements that have child elements will result in inferred tables
608 StringBuilder sb = new StringBuilder();
610 sb.Append("<DocumentElement>");
611 sb.Append("<Element1>");
612 sb.Append("<ChildElement1>Text1</ChildElement1>");
613 sb.Append("</Element1>");
614 sb.Append("</DocumentElement>");
615 DataSet ds = new DataSet();
616 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
617 ds.InferXmlSchema(myStream,null);
618 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS75");
619 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS76");
620 Assert.AreEqual(1, ds.Tables.Count, "DS77");
621 Assert.AreEqual("ChildElement1", ds.Tables[0].Columns["ChildElement1"].ColumnName, "DS78");
624 [Test] public void InferXmlSchema_inferingTables3()
626 //Acroding to the msdn documantaion :
627 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
628 //The document, or root, element will result in an inferred table if it has attributes
629 //or child elements that will be inferred as columns.
630 //If the document element has no attributes and no child elements that would be inferred as columns, the element will be inferred as a DataSet
633 StringBuilder sb = new StringBuilder();
635 sb.Append("<DocumentElement>");
636 sb.Append("<Element1>Text1</Element1>");
637 sb.Append("<Element2>Text2</Element2>");
638 sb.Append("</DocumentElement>");
639 DataSet ds = new DataSet();
640 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
641 ds.InferXmlSchema(myStream,null);
642 Assert.AreEqual("NewDataSet", ds.DataSetName, "DS79");
643 Assert.AreEqual("DocumentElement", ds.Tables[0].TableName, "DS80");
644 Assert.AreEqual(1, ds.Tables.Count, "DS81");
645 Assert.AreEqual("Element1", ds.Tables[0].Columns["Element1"].ColumnName, "DS82");
646 Assert.AreEqual("Element2", ds.Tables[0].Columns["Element2"].ColumnName, "DS83");
649 [Test] public void InferXmlSchema_inferingTables4()
651 //Acroding to the msdn documantaion :
652 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
653 //The document, or root, element will result in an inferred table if it has attributes
654 //or child elements that will be inferred as columns.
655 //If the document element has no attributes and no child elements that would be inferred as columns, the element will be inferred as a DataSet
658 StringBuilder sb = new StringBuilder();
660 sb.Append("<DocumentElement>");
661 sb.Append("<Element1 attr1='value1' attr2='value2'/>");
662 sb.Append("</DocumentElement>");
663 DataSet ds = new DataSet();
664 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
665 ds.InferXmlSchema(myStream,null);
666 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS84");
667 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS85");
668 Assert.AreEqual(1, ds.Tables.Count, "DS86");
669 Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS87");
670 Assert.AreEqual("attr2", ds.Tables[0].Columns["attr2"].ColumnName, "DS88");
674 [Category ("NotWorking")]
675 public void InferXmlSchema_inferingTables5()
677 //Acroding to the msdn documantaion :
678 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
679 //Elements that repeat will result in a single inferred table
682 StringBuilder sb = new StringBuilder();
684 sb.Append("<DocumentElement>");
685 sb.Append("<Element1>Text1</Element1>");
686 sb.Append("<Element1>Text2</Element1>");
687 sb.Append("</DocumentElement>");
688 DataSet ds = new DataSet();
689 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
690 ds.InferXmlSchema(myStream,null);
691 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS89");
692 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS90");
693 Assert.AreEqual(1, ds.Tables.Count, "DS91");
694 Assert.AreEqual("Element1_Text", ds.Tables[0].Columns["Element1_Text"].ColumnName, "DS92");
698 #region inferringColumns
699 [Test] public void InferXmlSchema_inferringColumns1()
701 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm
703 StringBuilder sb = new StringBuilder();
705 sb.Append("<DocumentElement>");
706 sb.Append("<Element1 attr1='value1' attr2='value2'/>");
707 sb.Append("</DocumentElement>");
708 DataSet ds = new DataSet();
709 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
710 ds.InferXmlSchema(myStream,null);
711 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS93");
712 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS94");
713 Assert.AreEqual(1, ds.Tables.Count, "DS95");
714 Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS96");
715 Assert.AreEqual("attr2", ds.Tables[0].Columns["attr2"].ColumnName, "DS97");
716 Assert.AreEqual(MappingType.Attribute, ds.Tables[0].Columns["attr1"].ColumnMapping , "DS98");
717 Assert.AreEqual(MappingType.Attribute, ds.Tables[0].Columns["attr2"].ColumnMapping , "DS99");
718 Assert.AreEqual(typeof(string), ds.Tables[0].Columns["attr1"].DataType , "DS100");
719 Assert.AreEqual(typeof(string), ds.Tables[0].Columns["attr2"].DataType , "DS101");
722 [Test] public void InferXmlSchema_inferringColumns2()
724 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm
725 //If an element has no child elements or attributes, it will be inferred as a column.
726 //The ColumnMapping property of the column will be set to MappingType.Element.
727 //The text for child elements is stored in a row in the table
730 StringBuilder sb = new StringBuilder();
732 sb.Append("<DocumentElement>");
733 sb.Append("<Element1>");
734 sb.Append("<ChildElement1>Text1</ChildElement1>");
735 sb.Append("<ChildElement2>Text2</ChildElement2>");
736 sb.Append("</Element1>");
737 sb.Append("</DocumentElement>");
738 DataSet ds = new DataSet();
739 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
740 ds.InferXmlSchema(myStream,null);
741 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS102");
742 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS103");
743 Assert.AreEqual(1, ds.Tables.Count, "DS104");
744 Assert.AreEqual("ChildElement1", ds.Tables[0].Columns["ChildElement1"].ColumnName, "DS105");
745 Assert.AreEqual("ChildElement2", ds.Tables[0].Columns["ChildElement2"].ColumnName, "DS106");
746 Assert.AreEqual(MappingType.Element, ds.Tables[0].Columns["ChildElement1"].ColumnMapping , "DS107");
747 Assert.AreEqual(MappingType.Element, ds.Tables[0].Columns["ChildElement2"].ColumnMapping , "DS108");
748 Assert.AreEqual(typeof(string), ds.Tables[0].Columns["ChildElement1"].DataType , "DS109");
749 Assert.AreEqual(typeof(string), ds.Tables[0].Columns["ChildElement2"].DataType , "DS110");
754 #region Inferring Relationships
756 [Test] public void InferXmlSchema_inferringRelationships1()
758 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringrelationships.htm
760 // inferringRelationships1
761 StringBuilder sb = new StringBuilder();
763 sb.Append("<DocumentElement>");
764 sb.Append("<Element1>");
765 sb.Append("<ChildElement1 attr1='value1' attr2='value2'/>");
766 sb.Append("<ChildElement2>Text2</ChildElement2>");
767 sb.Append("</Element1>");
768 sb.Append("</DocumentElement>");
769 DataSet ds = new DataSet();
770 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
771 ds.InferXmlSchema(myStream,null);
772 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS111");
773 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS112");
774 Assert.AreEqual("ChildElement1", ds.Tables[1].TableName, "DS113");
775 Assert.AreEqual(2, ds.Tables.Count, "DS114");
777 Assert.AreEqual("Element1_Id", ds.Tables["Element1"].Columns["Element1_Id"].ColumnName, "DS115");
778 Assert.AreEqual(MappingType.Hidden, ds.Tables["Element1"].Columns["Element1_Id"].ColumnMapping , "DS116");
779 Assert.AreEqual(typeof(Int32), ds.Tables["Element1"].Columns["Element1_Id"].DataType , "DS117");
781 Assert.AreEqual("ChildElement2", ds.Tables["Element1"].Columns["ChildElement2"].ColumnName, "DS118");
782 Assert.AreEqual(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement2"].ColumnMapping , "DS119");
783 Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["ChildElement2"].DataType , "DS120");
785 Assert.AreEqual("attr1", ds.Tables["ChildElement1"].Columns["attr1"].ColumnName, "DS121");
786 Assert.AreEqual(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr1"].ColumnMapping , "DS122");
787 Assert.AreEqual(typeof(string), ds.Tables["ChildElement1"].Columns["attr1"].DataType , "DS123");
789 Assert.AreEqual("attr2", ds.Tables["ChildElement1"].Columns["attr2"].ColumnName, "DS124");
790 Assert.AreEqual(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr2"].ColumnMapping , "DS125");
791 Assert.AreEqual(typeof(string), ds.Tables["ChildElement1"].Columns["attr2"].DataType , "DS126");
793 Assert.AreEqual("Element1_Id", ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnName, "DS127");
794 Assert.AreEqual(MappingType.Hidden, ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnMapping , "DS128");
795 Assert.AreEqual(typeof(Int32), ds.Tables["ChildElement1"].Columns["Element1_Id"].DataType , "DS129");
797 //Checking dataRelation :
798 Assert.AreEqual("Element1", ds.Relations["Element1_ChildElement1"].ParentTable.TableName, "DS130");
799 Assert.AreEqual("Element1_Id", ds.Relations["Element1_ChildElement1"].ParentColumns[0].ColumnName, "DS131");
800 Assert.AreEqual("ChildElement1", ds.Relations["Element1_ChildElement1"].ChildTable.TableName, "DS132");
801 Assert.AreEqual("Element1_Id", ds.Relations["Element1_ChildElement1"].ChildColumns[0].ColumnName, "DS133");
802 Assert.AreEqual(true, ds.Relations["Element1_ChildElement1"].Nested, "DS134");
804 //Checking ForeignKeyConstraint
806 ForeignKeyConstraint con = (ForeignKeyConstraint)ds.Tables["ChildElement1"].Constraints["Element1_ChildElement1"];
808 Assert.AreEqual("Element1_Id", con.Columns[0].ColumnName, "DS135");
809 Assert.AreEqual(Rule.Cascade, con.DeleteRule, "DS136");
810 Assert.AreEqual(AcceptRejectRule.None, con.AcceptRejectRule, "DS137");
811 Assert.AreEqual("Element1", con.RelatedTable.TableName, "DS138");
812 Assert.AreEqual("ChildElement1", con.Table.TableName, "DS139");
817 #region Inferring Element Text
819 [Test] public void InferXmlSchema_elementText1()
821 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringelementtext.htm
824 StringBuilder sb = new StringBuilder();
826 sb.Append("<DocumentElement>");
827 sb.Append("<Element1 attr1='value1'>Text1</Element1>");
828 sb.Append("</DocumentElement>");
829 DataSet ds = new DataSet();
830 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
831 ds.InferXmlSchema(myStream,null);
833 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS140");
834 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS141");
835 Assert.AreEqual(1, ds.Tables.Count, "DS142");
837 Assert.AreEqual("attr1", ds.Tables["Element1"].Columns["attr1"].ColumnName, "DS143");
838 Assert.AreEqual(MappingType.Attribute, ds.Tables["Element1"].Columns["attr1"].ColumnMapping , "DS144");
839 Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["attr1"].DataType , "DS145");
841 Assert.AreEqual("Element1_Text", ds.Tables["Element1"].Columns["Element1_Text"].ColumnName, "DS146");
842 Assert.AreEqual(MappingType.SimpleContent, ds.Tables["Element1"].Columns["Element1_Text"].ColumnMapping , "DS147");
843 Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["Element1_Text"].DataType , "DS148");
846 [Test] public void InferXmlSchema_elementText2()
848 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringelementtext.htm
851 StringBuilder sb = new StringBuilder();
853 sb.Append("<DocumentElement>");
854 sb.Append("<Element1>");
856 sb.Append("<ChildElement1>Text2</ChildElement1>");
858 sb.Append("</Element1>");
859 sb.Append("</DocumentElement>");
860 DataSet ds = new DataSet();
861 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
862 ds.InferXmlSchema(myStream,null);
864 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS149");
865 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS150");
866 Assert.AreEqual(1, ds.Tables.Count, "DS151");
868 Assert.AreEqual("ChildElement1", ds.Tables["Element1"].Columns["ChildElement1"].ColumnName, "DS152");
869 Assert.AreEqual(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement1"].ColumnMapping , "DS153");
870 Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["ChildElement1"].DataType , "DS154");
871 Assert.AreEqual(1, ds.Tables["Element1"].Columns.Count, "DS155");
876 [Test] public void Locale()
878 DataSet ds = new DataSet("MyDataSet");
879 System.Globalization.CultureInfo culInfo = System.Globalization.CultureInfo.CurrentCulture ;
881 // Checking Locale default from system
882 Assert.AreEqual(culInfo, ds.Locale , "DS156");
884 // Checking Locale get/set
885 culInfo = new System.Globalization.CultureInfo("fr"); // = french
886 ds.Locale = culInfo ;
887 Assert.AreEqual(culInfo , ds.Locale , "DS157");
890 [Test] public void MergeFailed()
895 ds1.Tables.Add(DataProvider.CreateParentDataTable());
896 //add primary key to the FIRST column
897 ds1.Tables[0].PrimaryKey = new DataColumn[] {ds1.Tables[0].Columns[0]};
899 //create target dataset which is a copy of the source
903 //add primary key to the SECOND columnn
904 ds2.Tables[0].PrimaryKey = new DataColumn[] {ds2.Tables[0].Columns[1]};
905 //add a new row that already exists in the source dataset
906 //ds2.Tables[0].Rows.Add(ds1.Tables[0].Rows[0].ItemArray);
908 //enforce constraints
909 ds2.EnforceConstraints = true;
910 ds1.EnforceConstraints = true;
912 // Add MergeFailed event handler for the table.
913 ds2.MergeFailed += new MergeFailedEventHandler( Merge_Failed );
915 ds2.Merge(ds1); //will raise MergeFailed event
918 Assert.AreEqual(true , EventRaised , "DS158");
920 private void Merge_Failed( object sender, MergeFailedEventArgs e )
925 [Test] public void Merge_ByDataRowsNoPreserveIgnoreMissingSchema()
927 DataTable dt = DataProvider.CreateParentDataTable();
928 dt.TableName = "Table1";
929 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
931 //create target dataset (copy of source dataset)
932 DataSet dsTarget = new DataSet();
933 dsTarget.Tables.Add(dt.Copy());
935 DataRow[] drArr = new DataRow[3];
937 string OldValue = dt.Select("ParentId=1")[0][1].ToString();
938 drArr[0] = dt.Select("ParentId=1")[0];
939 drArr[0][1] = "NewValue";
941 drArr[1] = dt.Select("ParentId=2")[0];
944 drArr[2] = dt.NewRow();
945 drArr[2].ItemArray = new object[] {99 ,"NewRowValue1", "NewRowValue2"};
946 dt.Rows.Add(drArr[2]);
948 dsTarget.Merge(drArr,false,MissingSchemaAction.Ignore );
951 Assert.AreEqual("NewValue", dsTarget.Tables["Table1"].Select("ParentId=1")[0][1] , "DS159");
954 Assert.AreEqual(1, dsTarget.Tables["Table1"].Select("ParentId=99").Length , "DS160");
957 Assert.AreEqual(0, dsTarget.Tables["Table1"].Select("ParentId=2").Length , "DS161");
960 [Test] public void Merge_ByDataRowsPreserveMissingSchema()
962 DataTable dt = DataProvider.CreateParentDataTable();
963 dt.TableName = "Table1";
964 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
966 //create target dataset (copy of source dataset)
967 DataSet dsTarget = new DataSet();
968 dsTarget.Tables.Add(dt.Copy());
970 //add new column (for checking MissingSchemaAction)
971 DataColumn dc = new DataColumn("NewColumn",typeof(float));
974 DataRow[] drArr = new DataRow[3];
977 string OldValue = dt.Select("ParentId=1")[0][1].ToString();
978 drArr[0] = dt.Select("ParentId=1")[0];
979 drArr[0][1] = "NewValue";
981 drArr[1] = dt.Select("ParentId=2")[0];
984 drArr[2] = dt.NewRow();
985 drArr[2].ItemArray = new object[] {99 ,"NewRowValue1", "NewRowValue2",null};
986 dt.Rows.Add(drArr[2]);
988 DataSet dsTarget1 = null;
990 #region "Merge(drArr,true,MissingSchemaAction.Ignore )"
991 dsTarget1 = dsTarget.Copy();
992 dsTarget1.Merge(drArr,true,MissingSchemaAction.Ignore );
993 // Merge true,Ignore - Column
994 Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS162");
996 // Merge true,Ignore - changed values
997 Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS163");
999 // Merge true,Ignore - added values
1000 Assert.AreEqual(1 , dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS164");
1002 // Merge true,Ignore - deleted row
1003 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS165");
1006 #region "Merge(drArr,false,MissingSchemaAction.Ignore )"
1007 dsTarget1 = dsTarget.Copy();
1008 dsTarget1.Merge(drArr,false,MissingSchemaAction.Ignore );
1009 // Merge true,Ignore - Column
1010 Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS166");
1012 // Merge true,Ignore - changed values
1013 Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS167");
1015 // Merge true,Ignore - added values
1016 Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS168");
1018 // Merge true,Ignore - deleted row
1019 Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS169");
1022 #region "Merge(drArr,true,MissingSchemaAction.Add )"
1023 dsTarget1 = dsTarget.Copy();
1024 dsTarget1.Merge(drArr,true,MissingSchemaAction.Add );
1025 // Merge true,Ignore - Column
1026 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS170");
1028 // Merge true,Ignore - changed values
1029 Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS171");
1031 // Merge true,Ignore - added values
1032 Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS172");
1034 // Merge true,Ignore - deleted row
1035 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length >0, "DS173");
1038 #region "Merge(drArr,false,MissingSchemaAction.Add )"
1039 dsTarget1 = dsTarget.Copy();
1040 dsTarget1.Merge(drArr,false,MissingSchemaAction.Add );
1041 // Merge true,Ignore - Column
1042 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS174");
1044 // Merge true,Ignore - changed values
1045 Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS175");
1047 // Merge true,Ignore - added values
1048 Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS176");
1050 // Merge true,Ignore - deleted row
1051 Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS177");
1054 #region "Merge(drArr,false/true,MissingSchemaAction.Error )"
1055 // dsTarget1 = dsTarget.Copy();
1056 // // Merge true,Error - Column
1058 // dsTarget1.Merge(drArr,true,MissingSchemaAction.Error );
1059 // Assert.Fail("DS178: Merge Failed to throw InvalidOperationException");
1061 // catch (InvalidOperationException) {}
1062 // catch (AssertionException exc) {throw exc;}
1063 // catch (Exception exc)
1065 // Assert.Fail("DS179: Merge. Wrong exception type. Got:" + exc);
1068 // // Merge false,Error - Column
1070 // dsTarget1.Merge(drArr,false,MissingSchemaAction.Error );
1071 // Assert.Fail("DS180: Merge Failed to throw InvalidOperationException");
1073 // catch (InvalidOperationException) {}
1074 // catch (AssertionException exc) {throw exc;}
1075 // catch (Exception exc)
1077 // Assert.Fail("DS181: Merge. Wrong exception type. Got:" + exc);
1082 [Test] public void Merge_ByDataSet()
1084 //create source dataset
1085 DataSet ds = new DataSet();
1086 DataTable dt = DataProvider.CreateParentDataTable();
1087 dt.TableName = "Table1";
1088 ds.Tables.Add(dt.Copy());
1089 dt.TableName = "Table2";
1091 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1092 ds.Tables.Add(dt.Copy());
1094 //create target dataset (copy of source dataset)
1095 DataSet dsTarget = ds.Copy();
1096 int iTable1RowsCount = dsTarget.Tables["Table1"].Rows.Count;
1098 //Source - add another table, don't exists on the target dataset
1099 ds.Tables.Add(new DataTable("SomeTable"));
1100 ds.Tables["SomeTable"].Columns.Add("Id");
1101 ds.Tables["SomeTable"].Rows.Add(new object[] {777});
1103 //Target - add another table, don't exists on the source dataset
1104 dsTarget.Tables.Add(new DataTable("SmallTable"));
1105 dsTarget.Tables["SmallTable"].Columns.Add("Id");
1106 dsTarget.Tables["SmallTable"].Rows.Add(new object[] {777});
1108 //update existing row
1109 ds.Tables["Table2"].Select("ParentId=1")[0][1] = "OldValue1";
1111 object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
1112 ds.Tables["Table2"].Rows.Add(arrAddedRow);
1113 //delete existing rows
1114 foreach (DataRow dr in ds.Tables["Table2"].Select("ParentId=2"))
1119 // Merge - changed values
1121 Assert.AreEqual("OldValue1", dsTarget.Tables["Table2"].Select("ParentId=1")[0][1] , "DS182");
1123 // Merge - added values
1124 Assert.AreEqual(arrAddedRow, dsTarget.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS183");
1126 // Merge - deleted row
1127 Assert.AreEqual(0, dsTarget.Tables["Table2"].Select("ParentId=2").Length , "DS184");
1129 //Table1 rows count should be double (no primary key)
1130 // Merge - Unchanged table 1
1131 Assert.AreEqual(iTable1RowsCount * 2, dsTarget.Tables["Table1"].Rows.Count , "DS185");
1133 //SmallTable rows count should be the same
1134 // Merge - Unchanged table 2
1135 Assert.AreEqual(1, dsTarget.Tables["SmallTable"].Rows.Count , "DS186");
1137 //SomeTable - new table
1138 // Merge - new table
1139 Assert.AreEqual(true, dsTarget.Tables["SomeTable"] != null, "DS187");
1142 [Test] public void Merge_ByDataSetPreserve()
1144 //create source dataset
1145 DataSet ds = new DataSet();
1146 DataTable dt = DataProvider.CreateParentDataTable();
1147 dt.TableName = "Table1";
1148 ds.Tables.Add(dt.Copy());
1149 dt.TableName = "Table2";
1151 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1152 ds.Tables.Add(dt.Copy());
1154 //create target dataset (copy of source dataset)
1155 DataSet dsTarget1 = ds.Copy();
1156 DataSet dsTarget2 = ds.Copy();
1157 int iTable1RowsCount = dsTarget1.Tables["Table1"].Rows.Count;
1159 //update existing row
1160 string oldValue = ds.Tables["Table2"].Select("ParentId=1")[0][1].ToString();
1161 ds.Tables["Table2"].Select("ParentId=1")[0][1] = "NewValue";
1163 object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
1164 ds.Tables["Table2"].Rows.Add(arrAddedRow);
1165 //delete existing rows
1166 int iDeleteLength = dsTarget1.Tables["Table2"].Select("ParentId=2").Length;
1167 foreach (DataRow dr in ds.Tables["Table2"].Select("ParentId=2"))
1172 #region "Merge(ds,true)"
1173 //only new added rows are merged (preserveChanges = true)
1174 dsTarget1.Merge(ds,true);
1175 // Merge - changed values
1176 Assert.AreEqual(oldValue, dsTarget1.Tables["Table2"].Select("ParentId=1")[0][1] , "DS188");
1178 // Merge - added values
1179 Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS189");
1181 // Merge - deleted row
1182 Assert.AreEqual(iDeleteLength, dsTarget1.Tables["Table2"].Select("ParentId=2").Length , "DS190");
1185 #region "Merge(ds,false)"
1186 //all changes are merged (preserveChanges = false)
1187 dsTarget2.Merge(ds,false);
1188 // Merge - changed values
1189 Assert.AreEqual("NewValue", dsTarget2.Tables["Table2"].Select("ParentId=1")[0][1] , "DS191");
1191 // Merge - added values
1192 Assert.AreEqual(arrAddedRow, dsTarget2.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS192");
1194 // Merge - deleted row
1195 Assert.AreEqual(0, dsTarget2.Tables["Table2"].Select("ParentId=2").Length , "DS193");
1199 [Test] public void Merge_ByDataSetPreserveMissingSchemaAction()
1201 //create source dataset
1202 DataSet ds = new DataSet();
1204 DataTable dt = DataProvider.CreateParentDataTable();
1205 dt.TableName = "Table1";
1207 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1209 //add table to dataset
1210 ds.Tables.Add(dt.Copy());
1214 //create target dataset (copy of source dataset)
1215 DataSet dsTarget = ds.Copy();
1217 //add new column (for checking MissingSchemaAction)
1218 DataColumn dc = new DataColumn("NewColumn",typeof(float));
1219 //make the column to be primary key
1222 //add new table (for checking MissingSchemaAction)
1223 ds.Tables.Add(new DataTable("NewTable"));
1224 ds.Tables["NewTable"].Columns.Add("NewColumn1",typeof(int));
1225 ds.Tables["NewTable"].Columns.Add("NewColumn2",typeof(long));
1226 ds.Tables["NewTable"].Rows.Add(new object[] {1,2});
1227 ds.Tables["NewTable"].Rows.Add(new object[] {3,4});
1228 ds.Tables["NewTable"].PrimaryKey = new DataColumn[] {ds.Tables["NewTable"].Columns["NewColumn1"]};
1230 #region "ds,false,MissingSchemaAction.Add)"
1231 DataSet dsTarget1 = dsTarget.Copy();
1232 dsTarget1.Merge(ds,false,MissingSchemaAction.Add);
1233 // Merge MissingSchemaAction.Add - Column
1234 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS194");
1236 // Merge MissingSchemaAction.Add - Table
1237 Assert.AreEqual(true, dsTarget1.Tables.Contains("NewTable"), "DS195");
1239 //failed, should be success by MSDN Library documentation
1240 // // Merge MissingSchemaAction.Add - PrimaryKey
1241 // Assert.AreEqual(0, dsTarget1.Tables["NewTable"].PrimaryKey.Length, "DS196");
1244 #region "ds,false,MissingSchemaAction.AddWithKey)"
1245 //MissingSchemaAction.Add,MissingSchemaAction.AddWithKey - behave the same, checked only Add
1247 // DataSet dsTarget2 = dsTarget.Copy();
1248 // dsTarget2.Merge(ds,false,MissingSchemaAction.AddWithKey);
1249 // // Merge MissingSchemaAction.AddWithKey - Column
1250 // Assert.AreEqual(true, dsTarget2.Tables["Table1"].Columns.Contains("NewColumn"), "DS197");
1252 // // Merge MissingSchemaAction.AddWithKey - Table
1253 // Assert.AreEqual(true, dsTarget2.Tables.Contains("NewTable"), "DS198");
1255 // // Merge MissingSchemaAction.AddWithKey - PrimaryKey
1256 // Assert.AreEqual(dsTarget2.Tables["NewTable"].Columns["NewColumn1"], dsTarget2.Tables["NewTable"].PrimaryKey[0], "DS199");
1259 #region "ds,false,MissingSchemaAction.Error)"
1260 //Error - throw System.Data.DataException, should throw InvalidOperationException
1261 // DataSet dsTarget3 ;
1262 // // Merge MissingSchemaAction.Error
1263 // dsTarget3 = dsTarget.Copy();
1265 // dsTarget3.Merge(ds,false,MissingSchemaAction.Error);
1266 // Assert.Fail("DS200: Merge Failed to throw InvalidOperationException");
1268 // catch (InvalidOperationException) {}
1269 // catch (AssertionException exc) {throw exc;}
1270 // catch (Exception exc)
1272 // Assert.Fail("DS201: Merge. Wrong exception type. Got:" + exc);
1276 #region "ds,false,MissingSchemaAction.Ignore )"
1277 DataSet dsTarget4 = dsTarget.Copy();
1278 dsTarget4.Merge(ds,false,MissingSchemaAction.Ignore );
1279 // Merge MissingSchemaAction.Ignore - Column
1280 Assert.AreEqual(false, dsTarget4.Tables["Table1"].Columns.Contains("NewColumn"), "DS202");
1282 // Merge MissingSchemaAction.Ignore - Table
1283 Assert.AreEqual(false, dsTarget4.Tables.Contains("NewTable"), "DS203");
1287 [Test] public void Merge_ByComplexDataSet()
1289 //create source dataset
1290 DataSet ds = new DataSet();
1292 ds.Tables.Add(DataProvider.CreateParentDataTable());
1293 ds.Tables.Add(DataProvider.CreateChildDataTable());
1294 ds.Tables["Child"].TableName = "Child2";
1295 ds.Tables.Add(DataProvider.CreateChildDataTable());
1297 // Console.WriteLine(ds.Tables[0].TableName + ds.Tables[1].TableName + ds.Tables[2].TableName);
1298 // Console.WriteLine(ds.Tables[2].Rows.Count.ToString());
1300 //craete a target dataset to the merge operation
1301 DataSet dsTarget = ds.Copy();
1303 //craete a second target dataset to the merge operation
1304 DataSet dsTarget1 = ds.Copy();
1306 //------------------ make some changes in the second target dataset schema --------------------
1308 dsTarget1.Tables["Parent"].PrimaryKey = new DataColumn[] {dsTarget1.Tables["Parent"].Columns["ParentId"]};
1309 dsTarget1.Tables["Child"].PrimaryKey = new DataColumn[] {dsTarget1.Tables["Child"].Columns["ParentId"],dsTarget1.Tables["Child"].Columns["ChildId"]};
1311 //add Foreign Key (different name)
1312 dsTarget1.Tables["Child2"].Constraints.Add("Child2_FK_2",dsTarget1.Tables["Parent"].Columns["ParentId"],dsTarget1.Tables["Child2"].Columns["ParentId"]);
1314 //add relation (different name)
1315 //dsTarget1.Relations.Add("Parent_Child_1",dsTarget1.Tables["Parent"].Columns["ParentId"],dsTarget1.Tables["Child"].Columns["ParentId"]);
1317 //------------------ make some changes in the source dataset schema --------------------
1319 ds.Tables["Parent"].PrimaryKey = new DataColumn[] {ds.Tables["Parent"].Columns["ParentId"]};
1320 ds.Tables["Child"].PrimaryKey = new DataColumn[] {ds.Tables["Child"].Columns["ParentId"],ds.Tables["Child"].Columns["ChildId"]};
1323 ds.Tables["Parent"].Columns["String2"].Unique = true; //will not be merged
1326 ds.Tables["Child2"].Constraints.Add("Child2_FK",ds.Tables["Parent"].Columns["ParentId"],ds.Tables["Child2"].Columns["ParentId"]);
1329 ds.Relations.Add("Parent_Child",ds.Tables["Parent"].Columns["ParentId"],ds.Tables["Child"].Columns["ParentId"]);
1331 //add allow null constraint
1332 ds.Tables["Parent"].Columns["ParentBool"].AllowDBNull = false; //will not be merged
1334 //add Indentity column
1335 ds.Tables["Parent"].Columns.Add("Indentity",typeof(int));
1336 ds.Tables["Parent"].Columns["Indentity"].AutoIncrement = true;
1337 ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep = 2;
1339 //modify default value
1340 ds.Tables["Child"].Columns["String1"].DefaultValue = "Default"; //will not be merged
1343 ds.Tables["Child"].Columns.Remove("String2"); //will not be merged
1345 //-------------------- begin to check ----------------------------------------------
1346 // merge 1 - make sure the merge method invoked without exceptions
1348 Assert.AreEqual("Success", "Success", "DS204");
1350 CompareResults_1("merge 1",ds,dsTarget);
1353 // merge 2 - make sure the merge method invoked without exceptions
1355 Assert.AreEqual("Success", "Success", "DS205");
1357 CompareResults_1("merge 2",ds,dsTarget);
1359 // merge second dataset - make sure the merge method invoked without exceptions
1360 dsTarget1.Merge(ds);
1361 Assert.AreEqual("Success", "Success", "DS206");
1363 CompareResults_2("merge 3",ds,dsTarget1);
1366 void CompareResults_1(string Msg,DataSet ds, DataSet dsTarget)
1368 // check Parent Primary key length
1369 Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey.Length , ds.Tables["Parent"].PrimaryKey.Length , "DS207");
1371 // check Child Primary key length
1372 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey.Length , ds.Tables["Child"].PrimaryKey.Length , "DS208");
1374 // check Parent Primary key columns
1375 Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey[0].ColumnName, ds.Tables["Parent"].PrimaryKey[0].ColumnName , "DS209");
1377 // check Child Primary key columns[0]
1378 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[0].ColumnName, ds.Tables["Child"].PrimaryKey[0].ColumnName , "DS210");
1380 // check Child Primary key columns[1]
1381 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[1].ColumnName, ds.Tables["Child"].PrimaryKey[1].ColumnName , "DS211");
1383 // check Parent Unique columns
1384 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["String2"].Unique, ds.Tables["Parent"].Columns["String2"].Unique , "DS212");
1386 // check Child2 Foreign Key name
1387 Assert.AreEqual(dsTarget.Tables["Child2"].Constraints[0].ConstraintName , ds.Tables["Child2"].Constraints[0].ConstraintName , "DS213");
1389 // check dataset relation count
1390 Assert.AreEqual(dsTarget.Relations.Count , ds.Relations.Count , "DS214");
1392 // check dataset relation - Parent column
1393 Assert.AreEqual(dsTarget.Relations[0].ParentColumns[0].ColumnName , ds.Relations[0].ParentColumns[0].ColumnName , "DS215");
1395 // check dataset relation - Child column
1396 Assert.AreEqual(dsTarget.Relations[0].ChildColumns[0].ColumnName , ds.Relations[0].ChildColumns[0].ColumnName , "DS216");
1398 // check allow null constraint
1399 Assert.AreEqual(true, dsTarget.Tables["Parent"].Columns["ParentBool"].AllowDBNull, "DS217");
1401 // check Indentity column
1402 Assert.AreEqual(dsTarget.Tables["Parent"].Columns.Contains("Indentity"), ds.Tables["Parent"].Columns.Contains("Indentity"), "DS218");
1404 // check Indentity column - AutoIncrementStep
1405 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, "DS219");
1407 // check Indentity column - AutoIncrement
1408 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrement, ds.Tables["Parent"].Columns["Indentity"].AutoIncrement, "DS220");
1410 // check Indentity column - DefaultValue
1411 Assert.AreEqual(true, dsTarget.Tables["Child"].Columns["String1"].DefaultValue == DBNull.Value , "DS221");
1413 // check remove colum
1414 Assert.AreEqual(true, dsTarget.Tables["Child"].Columns.Contains("String2"), "DS222");
1417 void CompareResults_2(string Msg,DataSet ds, DataSet dsTarget)
1419 // check Parent Primary key length
1420 Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey.Length , ds.Tables["Parent"].PrimaryKey.Length , "DS223");
1422 // check Child Primary key length
1423 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey.Length , ds.Tables["Child"].PrimaryKey.Length , "DS224");
1425 // check Parent Primary key columns
1426 Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey[0].ColumnName, ds.Tables["Parent"].PrimaryKey[0].ColumnName , "DS225");
1428 // check Child Primary key columns[0]
1429 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[0].ColumnName, ds.Tables["Child"].PrimaryKey[0].ColumnName , "DS226");
1431 // check Child Primary key columns[1]
1432 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[1].ColumnName, ds.Tables["Child"].PrimaryKey[1].ColumnName , "DS227");
1434 // check Parent Unique columns
1435 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["String2"].Unique, ds.Tables["Parent"].Columns["String2"].Unique , "DS228");
1437 // check Child2 Foreign Key name
1438 Assert.AreEqual("Child2_FK_2" , dsTarget.Tables["Child2"].Constraints[0].ConstraintName, "DS229");
1440 // check dataset relation count
1441 Assert.AreEqual(dsTarget.Relations.Count , ds.Relations.Count , "DS230");
1443 // check dataset relation - Parent column
1444 Assert.AreEqual(dsTarget.Relations[0].ParentColumns[0].ColumnName , ds.Relations[0].ParentColumns[0].ColumnName , "DS231");
1446 // check dataset relation - Child column
1447 Assert.AreEqual(dsTarget.Relations[0].ChildColumns[0].ColumnName , ds.Relations[0].ChildColumns[0].ColumnName , "DS232");
1449 // check allow null constraint
1450 Assert.AreEqual(true, dsTarget.Tables["Parent"].Columns["ParentBool"].AllowDBNull, "DS233");
1452 // check Indentity column
1453 Assert.AreEqual(dsTarget.Tables["Parent"].Columns.Contains("Indentity"), ds.Tables["Parent"].Columns.Contains("Indentity"), "DS234");
1455 // check Indentity column - AutoIncrementStep
1456 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, "DS235");
1458 // check Indentity column - AutoIncrement
1459 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrement, ds.Tables["Parent"].Columns["Indentity"].AutoIncrement, "DS236");
1461 // check Indentity column - DefaultValue
1462 Assert.AreEqual(true, dsTarget.Tables["Child"].Columns["String1"].DefaultValue == DBNull.Value , "DS237");
1464 // check remove colum
1465 Assert.AreEqual(true, dsTarget.Tables["Child"].Columns.Contains("String2"), "DS238");
1466 //TestCase for bug #3168
1467 // Check Relation.Nested value, TestCase for bug #3168
1468 DataSet orig = new DataSet();
1470 DataTable parent = orig.Tables.Add("Parent");
1471 parent.Columns.Add("Id", typeof(int));
1472 parent.Columns.Add("col1", typeof(string));
1473 parent.Rows.Add(new object[] {0, "aaa"});
1475 DataTable child = orig.Tables.Add("Child");
1476 child.Columns.Add("ParentId", typeof(int));
1477 child.Columns.Add("col1", typeof(string));
1478 child.Rows.Add(new object[] {0, "bbb"});
1480 orig.Relations.Add("Parent_Child", parent.Columns["Id"], child.Columns["ParentId"]);
1481 orig.Relations["Parent_Child"].Nested = true;
1483 DataSet merged = new DataSet();
1485 Assert.AreEqual(orig.Relations["Parent_Child"].Nested, merged.Relations["Parent_Child"].Nested, "DS239");
1488 [Test] public void Merge_ByDataTable()
1490 //create source dataset
1491 DataSet ds = new DataSet();
1493 DataTable dt = DataProvider.CreateParentDataTable();
1494 dt.TableName = "Table1";
1495 //add a copy of the datatable to the dataset
1496 ds.Tables.Add(dt.Copy());
1498 dt.TableName = "Table2";
1500 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1501 ds.Tables.Add(dt.Copy());
1502 //now the dataset hase two tables
1504 //create target dataset (copy of source dataset)
1505 DataSet dsTarget = ds.Copy();
1507 dt = ds.Tables["Table2"];
1508 //update existing row
1509 dt.Select("ParentId=1")[0][1] = "OldValue1";
1511 object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
1512 dt.Rows.Add(arrAddedRow);
1513 //delete existing rows
1514 foreach (DataRow dr in dt.Select("ParentId=2"))
1519 // Merge - changed values
1521 Assert.AreEqual("OldValue1", dsTarget.Tables["Table2"].Select("ParentId=1")[0][1] , "DS240");
1523 // Merge - added values
1524 Assert.AreEqual(arrAddedRow, dsTarget.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS241");
1526 // Merge - deleted row
1527 Assert.AreEqual(0, dsTarget.Tables["Table2"].Select("ParentId=2").Length , "DS242");
1529 //test case added due to a reported bug from infogate
1530 //when merging a DataTable with TableName=null, GH throw null reference exception.
1532 dt = new DataTable();
1533 dt.Columns.Add("Col1");
1534 dt.Rows.Add(new object[] {1});
1536 // Merge - add a table with no name
1538 Assert.AreEqual(1, ds.Tables.Count, "DS243");
1540 // Merge - add a table with no name - check Rows.Count
1541 Assert.AreEqual(dt.Rows.Count , ds.Tables[0].Rows.Count , "DS244");
1544 [Test] public void Merge_ByDataTablePreserveMissingSchemaAction()
1546 DataTable dt = DataProvider.CreateParentDataTable();
1547 dt.TableName = "Table1";
1548 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1550 //create target dataset (copy of source dataset)
1551 DataSet dsTarget = new DataSet();
1552 dsTarget.Tables.Add(dt.Copy());
1554 //add new column (for checking MissingSchemaAction)
1555 DataColumn dc = new DataColumn("NewColumn",typeof(float));
1559 string OldValue = dt.Select("ParentId=1")[0][1].ToString();
1560 dt.Select("ParentId=1")[0][1] = "NewValue";
1562 dt.Select("ParentId=2")[0].Delete();
1564 object[] arrAddedRow = new object[] {99,"NewRowValue1","NewRowValue2",new DateTime(0),0.5,true};
1565 dt.Rows.Add(arrAddedRow);
1567 #region "Merge(dt,true,MissingSchemaAction.Ignore )"
1568 DataSet dsTarget1 = dsTarget.Copy();
1569 dsTarget1.Merge(dt,true,MissingSchemaAction.Ignore );
1570 // Merge true,Ignore - Column
1571 Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS245");
1573 // Merge true,Ignore - changed values
1574 Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS246");
1576 // Merge true,Ignore - added values
1577 Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table1"].Select("ParentId=99")[0].ItemArray , "DS247");
1579 // Merge true,Ignore - deleted row
1580 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS248");
1583 #region "Merge(dt,false,MissingSchemaAction.Ignore )"
1585 dsTarget1 = dsTarget.Copy();
1586 dsTarget1.Merge(dt,false,MissingSchemaAction.Ignore );
1587 // Merge true,Ignore - Column
1588 Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS249");
1590 // Merge true,Ignore - changed values
1591 Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS250");
1593 // Merge true,Ignore - added values
1594 Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table1"].Select("ParentId=99")[0].ItemArray , "DS251");
1596 // Merge true,Ignore - deleted row
1597 Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS252");
1600 #region "Merge(dt,true,MissingSchemaAction.Add )"
1601 dsTarget1 = dsTarget.Copy();
1602 dsTarget1.Merge(dt,true,MissingSchemaAction.Add );
1603 // Merge true,Add - Column
1604 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS253");
1606 // Merge true,Add - changed values
1607 Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS254");
1609 // Merge true,Add - added values
1610 Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS255");
1612 // Merge true,Add - deleted row
1613 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS256");
1616 #region "Merge(dt,false,MissingSchemaAction.Add )"
1617 dsTarget1 = dsTarget.Copy();
1618 dsTarget1.Merge(dt,false,MissingSchemaAction.Add );
1619 // Merge true,Add - Column
1620 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS257");
1622 // Merge true,Add - changed values
1623 Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS258");
1625 // Merge true,Add - added values
1626 Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS259");
1628 // Merge true,Add - deleted row
1629 Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS260");
1632 #region "Merge(dt,false/true,MissingSchemaAction.Error )"
1633 // dsTarget1 = dsTarget.Copy();
1634 // // Merge true,Error - Column
1636 // dsTarget1.Merge(dt,true,MissingSchemaAction.Error );
1637 // Assert.Fail("DS261: Merge Failed to throw InvalidOperationException");
1639 // catch (InvalidOperationException) {}
1640 // catch (AssertionException exc) {throw exc;}
1641 // catch (Exception exc)
1643 // Assert.Fail("DS262: Merge. Wrong exception type. Got:" + exc);
1646 // // Merge false,Error - Column
1648 // dsTarget1.Merge(dt,false,MissingSchemaAction.Error );
1649 // Assert.Fail("DS263: Merge Failed to throw InvalidOperationException");
1651 // catch (InvalidOperationException) {}
1652 // catch (AssertionException exc) {throw exc;}
1653 // catch (Exception exc)
1655 // Assert.Fail("DS264: Merge. Wrong exception type. Got:" + exc);
1660 [Test] public void Namespace()
1662 DataSet ds = new DataSet();
1664 // Checking Namespace default
1665 Assert.AreEqual(String.Empty, ds.Namespace, "DS265");
1667 // Checking Namespace set/get
1668 String s = "MyNamespace";
1670 Assert.AreEqual(s, ds.Namespace, "DS266");
1673 [Test] public void Prefix()
1675 DataSet ds = new DataSet();
1677 // Checking Prefix default
1678 Assert.AreEqual(String.Empty, ds.Prefix , "DS267");
1680 // Checking Prefix set/get
1681 String s = "MyPrefix";
1683 Assert.AreEqual(s, ds.Prefix, "DS268");
1686 [Test] public void ReadXmlSchema_ByStream()
1688 DataSet ds1 = new DataSet();
1689 ds1.Tables.Add(DataProvider.CreateParentDataTable());
1690 ds1.Tables.Add(DataProvider.CreateChildDataTable());
1692 System.IO.MemoryStream ms = new System.IO.MemoryStream();
1693 //write xml schema only
1694 ds1.WriteXmlSchema(ms);
1696 System.IO.MemoryStream ms1 = new System.IO.MemoryStream(ms.GetBuffer());
1698 DataSet ds2 = new DataSet();
1699 ds2.ReadXmlSchema(ms1);
1702 // ReadXmlSchema - Tables count
1703 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS269");
1705 // ReadXmlSchema - Tables 0 Col count
1706 Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS270");
1708 // ReadXmlSchema - Tables 1 Col count
1709 Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS271");
1711 //check some colummns types
1712 // ReadXmlSchema - Tables 0 Col type
1713 Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS272");
1715 // ReadXmlSchema - Tables 1 Col type
1716 Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS273");
1718 //check that no data exists
1719 // ReadXmlSchema - Table 1 row count
1720 Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS274");
1722 // ReadXmlSchema - Table 2 row count
1723 Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS275");
1726 [Test] public void ReadXmlSchema_ByFileName()
1728 string sTempFileName = "tmpDataSet_ReadWriteXml_43899.xml" ;
1730 DataSet ds1 = new DataSet();
1731 ds1.Tables.Add(DataProvider.CreateParentDataTable());
1732 ds1.Tables.Add(DataProvider.CreateChildDataTable());
1734 //write xml file, schema only
1735 ds1.WriteXmlSchema(sTempFileName);
1737 //copy both data and schema
1738 DataSet ds2 = new DataSet();
1740 ds2.ReadXmlSchema(sTempFileName);
1743 // ReadXmlSchema - Tables count
1744 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS276");
1746 // ReadXmlSchema - Tables 0 Col count
1747 Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS277");
1749 // ReadXmlSchema - Tables 1 Col count
1750 Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS278");
1752 //check some colummns types
1753 // ReadXmlSchema - Tables 0 Col type
1754 Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS279");
1756 // ReadXmlSchema - Tables 1 Col type
1757 Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS280");
1759 //check that no data exists
1760 // ReadXmlSchema - Table 1 row count
1761 Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS281");
1763 // ReadXmlSchema - Table 2 row count
1764 Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS282");
1766 //try to delete the file
1767 System.IO.File.Delete(sTempFileName);
1770 [Test] public void ReadXmlSchema_ByTextReader()
1772 DataSet ds1 = new DataSet();
1773 ds1.Tables.Add(DataProvider.CreateParentDataTable());
1774 ds1.Tables.Add(DataProvider.CreateChildDataTable());
1776 System.IO.StringWriter sw = new System.IO.StringWriter();
1777 //write xml file, schema only
1778 ds1.WriteXmlSchema(sw);
1780 System.IO.StringReader sr = new System.IO.StringReader(sw.GetStringBuilder().ToString());
1781 //copy both data and schema
1782 DataSet ds2 = new DataSet();
1783 ds2.ReadXmlSchema(sr);
1786 // ReadXmlSchema - Tables count
1787 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS283");
1789 // ReadXmlSchema - Tables 0 Col count
1790 Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS284");
1792 // ReadXmlSchema - Tables 1 Col count
1793 Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS285");
1795 //check some colummns types
1796 // ReadXmlSchema - Tables 0 Col type
1797 Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS286");
1799 // ReadXmlSchema - Tables 1 Col type
1800 Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS287");
1802 //check that no data exists
1803 // ReadXmlSchema - Table 1 row count
1804 Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS288");
1806 // ReadXmlSchema - Table 2 row count
1807 Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS289");
1810 [Test] public void ReadXmlSchema_ByXmlReader()
1812 DataSet ds1 = new DataSet();
1813 ds1.Tables.Add(DataProvider.CreateParentDataTable());
1814 ds1.Tables.Add(DataProvider.CreateChildDataTable());
1816 System.IO.StringWriter sw = new System.IO.StringWriter();
1817 System.Xml.XmlTextWriter xmlTW = new System.Xml.XmlTextWriter(sw);
1818 //write xml file, schema only
1819 ds1.WriteXmlSchema(xmlTW);
1822 System.IO.StringReader sr = new System.IO.StringReader(sw.ToString());
1823 System.Xml.XmlTextReader xmlTR = new System.Xml.XmlTextReader(sr);
1825 //copy both data and schema
1826 DataSet ds2 = new DataSet();
1827 ds2.ReadXmlSchema(xmlTR);
1830 // ReadXmlSchema - Tables count
1831 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS290");
1833 // ReadXmlSchema - Tables 0 Col count
1834 Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS291");
1836 // ReadXmlSchema - Tables 1 Col count
1837 Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS292");
1839 //check some colummns types
1840 // ReadXmlSchema - Tables 0 Col type
1841 Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS293");
1843 // ReadXmlSchema - Tables 1 Col type
1844 Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS294");
1846 //check that no data exists
1847 // ReadXmlSchema - Table 1 row count
1848 Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS295");
1850 // ReadXmlSchema - Table 2 row count
1851 Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS296");
1855 [Category ("NotWorking")]
1856 public void ReadXml_Strg()
1858 string sTempFileName = "tmpDataSet_ReadWriteXml_43894.xml" ;
1860 DataSet ds1 = new DataSet();
1861 ds1.Tables.Add(DataProvider.CreateParentDataTable());
1862 ds1.Tables.Add(DataProvider.CreateChildDataTable());
1864 //add data to check GH bug of DataSet.ReadXml of empty strings
1865 ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
1866 ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
1867 ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
1869 //write xml file, data only
1870 ds1.WriteXml(sTempFileName);
1872 //copy both data and schema
1873 DataSet ds2 = ds1.Copy();
1877 ds2.ReadXml(sTempFileName);
1880 // ReadXml - Tables count
1881 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS297");
1883 // ReadXml - Table 1 row count
1884 Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS298");
1886 // ReadXml - Table 2 row count
1887 Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS299");
1889 //try to delete the file
1890 System.IO.File.Delete(sTempFileName);
1894 [Category ("NotWorking")]
1895 public void ReadXml_Strm()
1897 DataSet ds1 = new DataSet();
1898 ds1.Tables.Add(DataProvider.CreateParentDataTable());
1899 ds1.Tables.Add(DataProvider.CreateChildDataTable());
1901 //add data to check GH bug of DataSet.ReadXml of empty strings
1902 ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
1903 ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
1904 ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
1906 System.IO.MemoryStream ms = new System.IO.MemoryStream();
1907 //write xml file, data only
1910 //copy both data and schema
1911 DataSet ds2 = ds1.Copy();
1919 // ReadXml - Tables count
1920 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS300");
1922 // ReadXml - Table 1 row count
1923 Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS301");
1925 // ReadXml - Table 2 row count
1926 Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS302");
1931 [Test] public void ReadXml_Strm2()
1933 string input = string.Empty;
1935 System.IO.StringReader sr;
1936 DataSet ds = new DataSet();
1938 input += "<?xml version=\"1.0\"?>";
1939 input += "<Stock name=\"MSFT\">";
1940 input += " <Company name=\"Microsoft Corp.\"/>";
1941 input += " <Price type=\"high\">";
1942 input += " <Value>10.0</Value>";
1943 input += " <Date>01/20/2000</Date>";
1944 input += " </Price>";
1945 input += " <Price type=\"low\">";
1946 input += " <Value>1.0</Value>";
1947 input += " <Date>03/21/2002</Date>";
1948 input += " </Price>";
1949 input += " <Price type=\"current\">";
1950 input += " <Value>3.0</Value>";
1951 input += " <Date>TODAY</Date>";
1952 input += " </Price>";
1953 input += "</Stock>";
1955 sr = new System.IO.StringReader(input);
1960 Assert.AreEqual(2 , ds.Relations.Count , "DS303");
1963 Assert.AreEqual("Stock_Company" , ds.Relations[0].RelationName , "DS304");
1966 Assert.AreEqual("Stock_Price" , ds.Relations[1].RelationName , "DS305");
1969 Assert.AreEqual(3, ds.Tables.Count , "DS306");
1971 // Tables[0] ChildRelations count
1972 Assert.AreEqual(2, ds.Tables[0].ChildRelations.Count , "DS307");
1974 // Tables[0] ChildRelations[0] name
1975 Assert.AreEqual("Stock_Company", ds.Tables[0].ChildRelations[0].RelationName , "DS308");
1977 // Tables[0] ChildRelations[1] name
1978 Assert.AreEqual("Stock_Price", ds.Tables[0].ChildRelations[1].RelationName , "DS309");
1980 // Tables[1] ChildRelations count
1981 Assert.AreEqual(0, ds.Tables[1].ChildRelations.Count , "DS310");
1983 // Tables[2] ChildRelations count
1984 Assert.AreEqual(0, ds.Tables[2].ChildRelations.Count , "DS311");
1986 // Tables[0] ParentRelations count
1987 Assert.AreEqual(0, ds.Tables[0].ParentRelations.Count , "DS312");
1989 // Tables[1] ParentRelations count
1990 Assert.AreEqual(1, ds.Tables[1].ParentRelations.Count , "DS313");
1992 // Tables[1] ParentRelations[0] name
1993 Assert.AreEqual("Stock_Company", ds.Tables[1].ParentRelations[0].RelationName , "DS314");
1995 // Tables[2] ParentRelations count
1996 Assert.AreEqual(1, ds.Tables[2].ParentRelations.Count , "DS315");
1998 // Tables[2] ParentRelations[0] name
1999 Assert.AreEqual("Stock_Price", ds.Tables[2].ParentRelations[0].RelationName , "DS316");
2001 [Test] public void ReadXml_Strm3()
2003 DataSet ds = new DataSet("TestDataSet");
2004 string input = string.Empty;
2005 System.IO.StringReader sr;
2007 input += "<?xml version=\"1.0\" standalone=\"yes\"?>";
2008 input += "<Stocks><Stock name=\"MSFT\"><Company name=\"Microsoft Corp.\" /><Price type=\"high\"><Value>10.0</Value>";
2009 input += "<Date>01/20/2000</Date></Price><Price type=\"low\"><Value>10</Value><Date>03/21/2002</Date></Price>";
2010 input += "<Price type=\"current\"><Value>3.0</Value><Date>TODAY</Date></Price></Stock><Stock name=\"GE\">";
2011 input += "<Company name=\"General Electric\" /><Price type=\"high\"><Value>22.23</Value><Date>02/12/2001</Date></Price>";
2012 input += "<Price type=\"low\"><Value>1.97</Value><Date>04/20/2003</Date></Price><Price type=\"current\"><Value>3.0</Value>";
2013 input += "<Date>TODAY</Date></Price></Stock></Stocks>";
2014 sr = new System.IO.StringReader(input);
2015 ds.EnforceConstraints = false;
2018 //Test that all added columns have "Hidden" mapping type.
2019 // StockTable.Stock_IdCol.ColumnMapping
2020 Assert.AreEqual(MappingType.Hidden, ds.Tables["Stock"].Columns["Stock_Id"].ColumnMapping, "DS317");
2022 // CompanyTable.Stock_IdCol.ColumnMapping
2023 Assert.AreEqual(MappingType.Hidden, ds.Tables["Company"].Columns["Stock_Id"].ColumnMapping, "DS318");
2025 // PriceTable.Stock_IdCol.ColumnMapping
2026 Assert.AreEqual(MappingType.Hidden, ds.Tables["Price"].Columns["Stock_Id"].ColumnMapping, "DS319");
2029 [Test] public void ReadXml_Strm4()
2031 m_ds = new DataSet("Stocks");
2032 string input = string.Empty;
2033 System.IO.StringReader sr;
2035 input += "<?xml version=\"1.0\"?>";
2036 input += "<Stocks>";
2037 input += " <Stock name=\"MSFT\">";
2038 input += " <Company name=\"Microsoft Corp.\" />";
2039 input += " <Company name=\"General Electric\"/>";
2040 input += " <Price type=\"high\">";
2041 input += " <Value>10.0</Value>";
2042 input += " <Date>01/20/2000</Date>";
2043 input += " </Price>";
2044 input += " <Price type=\"low\">";
2045 input += " <Value>1.0</Value>";
2046 input += " <Date>03/21/2002</Date>";
2047 input += " </Price>";
2048 input += " <Price type=\"current\">";
2049 input += " <Value>3.0</Value>";
2050 input += " <Date>TODAY</Date>";
2051 input += " </Price>";
2052 input += " </Stock>";
2053 input += " <Stock name=\"GE\">";
2054 input += " <Company name=\"GE company\"/>";
2055 input += " <Price type=\"high\">";
2056 input += " <Value>22.23</Value>";
2057 input += " <Date>02/12/2001</Date>";
2058 input += " </Price>";
2059 input += " <Price type=\"low\">";
2060 input += " <Value>1.97</Value>";
2061 input += " <Date>04/20/2003</Date>";
2062 input += " </Price>";
2063 input += " <Price type=\"current\">";
2064 input += " <Value>3.0</Value>";
2065 input += " <Date>TODAY</Date>";
2066 input += " </Price>";
2067 input += " </Stock>";
2068 input += " <Stock name=\"Intel\">";
2069 input += " <Company name=\"Intel Corp.\"/>";
2070 input += " <Company name=\"Test1\" />";
2071 input += " <Company name=\"Test2\"/>";
2072 input += " <Price type=\"high\">";
2073 input += " <Value>15.0</Value>";
2074 input += " <Date>01/25/2000</Date>";
2075 input += " </Price>";
2076 input += " <Price type=\"low\">";
2077 input += " <Value>1.0</Value>";
2078 input += " <Date>03/23/2002</Date>";
2079 input += " </Price>";
2080 input += " <Price type=\"current\">";
2081 input += " <Value>3.0</Value>";
2082 input += " <Date>TODAY</Date>";
2083 input += " </Price>";
2084 input += " </Stock>";
2085 input += " <Stock name=\"Mainsoft\">";
2086 input += " <Company name=\"Mainsoft Corp.\"/>";
2087 input += " <Price type=\"high\">";
2088 input += " <Value>30.0</Value>";
2089 input += " <Date>01/26/2000</Date>";
2090 input += " </Price>";
2091 input += " <Price type=\"low\">";
2092 input += " <Value>1.0</Value>";
2093 input += " <Date>03/26/2002</Date>";
2094 input += " </Price>";
2095 input += " <Price type=\"current\">";
2096 input += " <Value>27.0</Value>";
2097 input += " <Date>TODAY</Date>";
2098 input += " </Price>";
2099 input += " </Stock>";
2100 input += "</Stocks>";
2102 sr = new System.IO.StringReader(input);
2103 m_ds.EnforceConstraints = true;
2105 this.privateTestCase("TestCase 1", "Company", "name='Microsoft Corp.'", "Stock", "name='MSFT'", "DS320");
2106 this.privateTestCase("TestCase 2", "Company", "name='General Electric'", "Stock", "name='MSFT'", "DS321");
2107 this.privateTestCase("TestCase 3", "Price", "Date='01/20/2000'", "Stock", "name='MSFT'", "DS322");
2108 this.privateTestCase("TestCase 4", "Price", "Date='03/21/2002'", "Stock", "name='MSFT'", "DS323");
2109 this.privateTestCase("TestCase 5", "Company", "name='GE company'", "Stock", "name='GE'", "DS324");
2110 this.privateTestCase("TestCase 6", "Price", "Date='02/12/2001'", "Stock", "name='GE'", "DS325");
2111 this.privateTestCase("TestCase 7", "Price", "Date='04/20/2003'", "Stock", "name='GE'", "DS326");
2112 this.privateTestCase("TestCase 8", "Company", "name='Intel Corp.'", "Stock", "name='Intel'", "DS327");
2113 this.privateTestCase("TestCase 9", "Company", "name='Test1'", "Stock", "name='Intel'", "DS328");
2114 this.privateTestCase("TestCase 10", "Company", "name='Test2'", "Stock", "name='Intel'", "DS329");
2115 this.privateTestCase("TestCase 11", "Price", "Date='01/25/2000'", "Stock", "name='Intel'", "DS330");
2116 this.privateTestCase("TestCase 12", "Price", "Date='03/23/2002'", "Stock", "name='Intel'", "DS331");
2117 this.privateTestCase("TestCase 13", "Company", "name='Mainsoft Corp.'", "Stock", "name='Mainsoft'", "DS332");
2118 this.privateTestCase("TestCase 12", "Price", "Date='01/26/2000'", "Stock", "name='Mainsoft'", "DS333");
2119 this.privateTestCase("TestCase 12", "Price", "Date='03/26/2002'", "Stock", "name='Mainsoft'", "DS334");
2122 private void privateTestCase(string name, string toTable, string toTestSelect, string toCompareTable, string toCompareSelect, string AssertTag)
2124 DataRow drToTest = m_ds.Tables[toTable].Select(toTestSelect)[0];
2125 DataRow drToCompare = m_ds.Tables[toCompareTable].Select(toCompareSelect)[0];
2126 Assert.AreEqual(m_ds.Tables[toTable].Select(toTestSelect)[0]["Stock_Id"], m_ds.Tables[toCompareTable].Select(toCompareSelect)[0]["Stock_Id"], AssertTag);
2130 [Category ("NotWorking")]
2131 public void ReadXml_Strm5()
2136 #region "TestCase 1 - Empty string"
2138 DataSet ds = new DataSet();
2139 System.IO.StringReader sr = new System.IO.StringReader (string.Empty);
2140 System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr);
2143 ds.ReadXml (xReader);
2144 Assert.Fail("DS335: ReadXml Failed to throw XmlException");
2146 catch (System.Xml.XmlException) {}
2147 catch (AssertionException exc) {throw exc;}
2148 catch (Exception exc)
2150 Assert.Fail("DS336: ReadXml. Wrong exception type. Got:" + exc);
2153 #region "TestCase 2 - Single element"
2154 name = "Single element";
2155 expected = "DataSet Name=a Tables count=0";
2156 xmlData = "<a>1</a>";
2157 PrivateTestCase(name, expected, xmlData);
2159 #region "TestCase 3 - Nesting one level single element."
2160 name = "Nesting one level single element.";
2161 expected = "DataSet Name=NewDataSet Tables count=1 Table Name=a Rows count=1 Items count=1 1";
2162 xmlData = "<a><b>1</b></a>";
2163 PrivateTestCase(name, expected, xmlData);
2165 #region "TestCase 4 - Nesting one level multiple elements."
2166 name = "Nesting one level multiple elements.";
2167 expected = "DataSet Name=NewDataSet Tables count=1 Table Name=a Rows count=1 Items count=3 bb cc dd";
2168 xmlData = "<a><b>bb</b><c>cc</c><d>dd</d></a>";
2169 PrivateTestCase(name, expected, xmlData);
2171 #region "TestCase 5 - Nesting two levels single elements."
2172 name = "Nesting two levels single elements.";
2173 expected = "DataSet Name=a Tables count=1 Table Name=b Rows count=1 Items count=1 cc";
2174 xmlData = "<a><b><c>cc</c></b></a>";
2175 PrivateTestCase(name, expected, xmlData);
2177 #region "TestCase 6 - Nesting two levels multiple elements."
2178 name = "Nesting two levels multiple elements.";
2179 expected = "DataSet Name=a Tables count=1 Table Name=b Rows count=1 Items count=2 cc dd";
2180 xmlData = string.Empty;
2183 xmlData += "<c>cc</c>";
2184 xmlData += "<d>dd</d>";
2187 PrivateTestCase(name, expected, xmlData);
2189 #region "TestCase 7 - Nesting two levels multiple elements."
2190 name = "Nesting two levels multiple elements.";
2191 expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=2 cc dd Table Name=e Rows count=1 Items count=2 cc dd";
2192 xmlData = string.Empty;
2195 xmlData += "<c>cc</c>";
2196 xmlData += "<d>dd</d>";
2199 xmlData += "<c>cc</c>";
2200 xmlData += "<d>dd</d>";
2203 PrivateTestCase(name, expected, xmlData);
2205 #region "TestCase 8 - Nesting three levels single element."
2206 name = "Nesting three levels single element.";
2207 xmlData = string.Empty;
2211 xmlData += "<d>dd</d>";
2215 expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=1 0 Table Name=c Rows count=1 Items count=2 0 dd";
2216 PrivateTestCase(name, expected, xmlData);
2218 #region "TestCase 9 - Nesting three levels multiple elements."
2219 name = "Nesting three levels multiple elements.";
2220 xmlData = string.Empty;
2224 xmlData += "<d>dd</d>";
2225 xmlData += "<e>ee</e>";
2229 expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=1 0 Table Name=c Rows count=1 Items count=3 0 dd ee";
2230 PrivateTestCase(name, expected, xmlData);
2232 #region "TestCase 10 - Nesting three levels multiple elements."
2233 name = "Nesting three levels multiple elements.";
2234 xmlData = string.Empty;
2238 xmlData += "<d>dd</d>";
2239 xmlData += "<e>ee</e>";
2241 xmlData += "<f>ff</f>";
2244 expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=2 0 ff Table Name=c Rows count=1 Items count=3 0 dd ee";
2245 PrivateTestCase(name, expected, xmlData);
2247 #region "TestCase 11 - Nesting three levels multiple elements."
2248 name = "Nesting three levels multiple elements.";
2249 xmlData = string.Empty;
2253 xmlData += "<d>dd</d>";
2254 xmlData += "<e>ee</e>";
2256 xmlData += "<f>ff</f>";
2258 xmlData += "<h>hh</h>";
2259 xmlData += "<i>ii</i>";
2261 xmlData += "<j>jj</j>";
2264 expected = "DataSet Name=a Tables count=3 Table Name=b Rows count=1 Items count=3 0 ff jj Table Name=c Rows count=1 Items count=3 0 dd ee Table Name=g Rows count=1 Items count=3 0 hh ii";
2265 PrivateTestCase(name, expected, xmlData);
2267 #region "TestCase 12 - Nesting three levels multiple elements."
2268 name = "Nesting three levels multiple elements.";
2269 xmlData = string.Empty;
2273 xmlData += "<d>dd</d>";
2274 xmlData += "<e>ee</e>";
2276 xmlData += "<f>ff</f>";
2280 xmlData += "<i>ii</i>";
2281 xmlData += "<j>jj</j>";
2283 xmlData += "<f>ff</f>";
2286 expected = "DataSet Name=a Tables count=4 Table Name=b Rows count=1 Items count=2 0 ff Table Name=c Rows count=1 Items count=3 0 dd ee Table Name=g Rows count=1 Items count=2 ff 0 Table Name=h Rows count=1 Items count=3 0 ii jj";
2287 PrivateTestCase(name, expected, xmlData);
2289 #region "TestCase 13 - Nesting three levels multiple elements."
2290 name = "Nesting three levels multiple elements.";
2291 xmlData = string.Empty;
2295 xmlData += "<d>dd</d>";
2296 xmlData += "<e>ee</e>";
2298 xmlData += "<f>ff</f>";
2300 xmlData += "<l>ll</l>";
2301 xmlData += "<m>mm</m>";
2303 xmlData += "<n>nn</n>";
2307 xmlData += "<i>ii</i>";
2308 xmlData += "<j>jj</j>";
2310 xmlData += "<o>oo</o>";
2313 expected = "DataSet Name=a Tables count=5 Table Name=b Rows count=1 Items count=3 0 ff nn Table Name=c Rows count=1 Items count=3 0 dd ee Table Name=k Rows count=1 Items count=3 0 ll mm Table Name=g Rows count=1 Items count=2 0 oo Table Name=h Rows count=1 Items count=3 0 ii jj";
2314 PrivateTestCase(name, expected, xmlData);
2317 #region "TestCase 14 - for Bug 2387 (System.Data.DataSet.ReadXml(..) - ArgumentException while reading specific XML)"
2319 name = "Specific XML - for Bug 2387";
2320 expected = "DataSet Name=PKRoot Tables count=2 Table Name=Content Rows count=4 Items count=2 0 Items count=2 1 103 Items count=2 2 123 Items count=2 3 252 Table Name=Cont Rows count=3 Items count=3 1 103 0 Items count=3 2 123 0 Items count=3 3 252 -4";
2321 xmlData = "<PKRoot><Content /><Content><ContentId>103</ContentId><Cont><ContentId>103</ContentId><ContentStatusId>0</ContentStatusId></Cont></Content><Content><ContentId>123</ContentId><Cont><ContentId>123</ContentId><ContentStatusId>0</ContentStatusId></Cont></Content><Content><ContentId>252</ContentId><Cont><ContentId>252</ContentId><ContentStatusId>-4</ContentStatusId></Cont></Content></PKRoot>";
2322 PrivateTestCase(name, expected, xmlData);
2327 private void PrivateTestCase(string a_name, string a_expected, string a_xmlData)
2329 DataSet ds = new DataSet();
2330 System.IO.StringReader sr = new System.IO.StringReader(a_xmlData) ;
2331 System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
2332 ds.ReadXml (xReader);
2333 ds.WriteXml(a_name + System.DateTime.Now.Ticks + ".xml");
2334 Assert.AreEqual(a_expected, this.dataSetDescription(ds), "DS337");
2337 private string dataSetDescription(DataSet ds)
2339 string desc = string.Empty;
2340 desc += "DataSet Name=" + ds.DataSetName;
2341 desc += " Tables count=" + ds.Tables.Count;
2342 foreach (DataTable dt in ds.Tables)
2344 desc += " Table Name=" + dt.TableName;
2345 desc += " Rows count=" + dt.Rows.Count;
2347 string[] colNames = new string[dt.Columns.Count];
2348 for(int i = 0; i < dt.Columns.Count; i++)
2349 colNames[i] = dt.Columns[i].ColumnName;
2351 Array.Sort(colNames);
2353 foreach (DataRow dr in dt.Rows)
2355 desc += " Items count=" + dr.ItemArray.Length;
2356 foreach (string name in colNames)
2358 desc += " " + dr[name].ToString();
2365 [Test] public void ReadXml_Strm6()
2368 DataSet ds = new DataSet();
2369 string xmlData = string.Empty;
2372 xmlData += "<c>1</c>";
2373 xmlData += "<c>2</c>";
2374 xmlData += "<c>3</c>";
2377 System.IO.StringReader sr = new System.IO.StringReader(xmlData) ;
2378 System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
2379 ds.ReadXml (xReader);
2380 Assert.AreEqual(3, ds.Tables["c"].Rows.Count, "DS338");
2384 public void ReadXmlSchema_2()
2386 DataSet ds = new DataSet();
2387 string xmlData = string.Empty;
2388 xmlData += "<?xml version=\"1.0\"?>";
2389 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\">";
2390 xmlData += "<xs:element name=\"SiteConfiguration\" msdata:IsDataSet=\"true\" msdata:EnforceConstraints=\"False\">";
2391 xmlData += "<xs:complexType>";
2392 xmlData += "<xs:choice maxOccurs=\"unbounded\">";
2393 xmlData += "<xs:element name=\"Tab\">";
2394 xmlData += "<xs:complexType>";
2395 xmlData += "<xs:sequence>";
2396 xmlData += "<xs:element name=\"Module\" minOccurs=\"0\" maxOccurs=\"unbounded\">";
2397 xmlData += "<xs:complexType>";
2398 xmlData += "<xs:attribute name=\"ModuleId\" form=\"unqualified\" type=\"xs:int\" />";
2399 xmlData += "</xs:complexType>";
2400 xmlData += "</xs:element>";
2401 xmlData += "</xs:sequence>";
2402 xmlData += "<xs:attribute name=\"TabId\" form=\"unqualified\" type=\"xs:int\" />";
2403 xmlData += "</xs:complexType>";
2404 xmlData += "</xs:element>";
2405 xmlData += "</xs:choice>";
2406 xmlData += "</xs:complexType>";
2407 xmlData += "<xs:key name=\"TabKey\" msdata:PrimaryKey=\"true\">";
2408 xmlData += "<xs:selector xpath=\".//mstns:Tab\" />";
2409 xmlData += "<xs:field xpath=\"@TabId\" />";
2410 xmlData += "</xs:key>";
2411 xmlData += "<xs:key name=\"ModuleKey\" msdata:PrimaryKey=\"true\">";
2412 xmlData += "<xs:selector xpath=\".//mstns:Module\" />";
2413 xmlData += "<xs:field xpath=\"@ModuleID\" />";
2414 xmlData += "</xs:key>";
2415 xmlData += "</xs:element>";
2416 xmlData += "</xs:schema>";
2418 ds.ReadXmlSchema(new StringReader(xmlData));
2422 [Category ("NotWorking")]
2423 public void ReadXml_ByTextReader()
2425 DataSet ds1 = new DataSet();
2426 ds1.Tables.Add(DataProvider.CreateParentDataTable());
2427 ds1.Tables.Add(DataProvider.CreateChildDataTable());
2429 //add data to check GH bug of DataSet.ReadXml of empty strings
2430 ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
2431 ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
2432 ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
2434 System.IO.StringWriter sw = new System.IO.StringWriter();
2435 //write xml file, data only
2438 //copy both data and schema
2439 DataSet ds2 = ds1.Copy();
2443 System.IO.StringReader sr = new System.IO.StringReader(sw.GetStringBuilder().ToString());
2447 // ReadXml - Tables count
2448 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS339");
2450 // ReadXml - Table 1 row count
2451 Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS340");
2453 // ReadXml - Table 2 row count
2454 Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS341");
2461 [Category ("NotWorking")]
2462 public void ReadXml_ByXmlReader()
2464 DataSet ds1 = new DataSet();
2465 ds1.Tables.Add(DataProvider.CreateParentDataTable());
2466 ds1.Tables.Add(DataProvider.CreateChildDataTable());
2468 //add data to check GH bug of DataSet.ReadXml of empty strings
2469 ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
2470 ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
2471 ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
2473 System.IO.StringWriter sw = new System.IO.StringWriter();
2474 System.Xml.XmlTextWriter xmlTW = new System.Xml.XmlTextWriter(sw);
2476 //write xml file, data only
2477 ds1.WriteXml(xmlTW);
2478 //ds1.WriteXml("C:\\Temp\\q.xml");
2480 //copy both data and schema
2481 DataSet ds2 = ds1.Copy();
2484 System.IO.StringReader sr = new System.IO.StringReader(sw.ToString());
2485 System.Xml.XmlTextReader xmlTR = new System.Xml.XmlTextReader(sr);
2489 // ReadXml - Tables count
2490 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS342");
2492 // ReadXml - Table 1 row count
2493 Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS343");
2495 // ReadXml - Table 2 row count
2496 Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS344");
2499 [Test] public void RejectChanges()
2501 DataSet ds1,ds2 = new DataSet();
2502 ds2.Tables.Add(DataProvider.CreateParentDataTable());
2506 ds2.Tables[0].Rows[0][0] = "70";
2507 ds2.Tables[0].Rows[1].Delete();
2508 ds2.Tables[0].Rows.Add(new object[] {9,"string1","string2"});
2511 ds2.RejectChanges();
2512 Assert.AreEqual(ds2.GetXml(), ds1.GetXml(), "DS345");
2515 [Test] public void Relations()
2517 DataTable dtChild1,dtChild2,dtParent;
2518 DataSet ds = new DataSet();
2520 dtChild1 = DataProvider.CreateChildDataTable();
2521 dtChild1.TableName = "Child";
2522 dtChild2 = DataProvider.CreateChildDataTable();
2523 dtChild2.TableName = "CHILD";
2524 dtParent= DataProvider.CreateParentDataTable();
2525 //Add tables to dataset
2526 ds.Tables.Add(dtChild1);
2527 ds.Tables.Add(dtChild2);
2529 ds.Tables.Add(dtParent);
2531 DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild1.Columns["ParentId"]);
2532 DataRelation drl1 = new DataRelation("Parent-CHILD",dtParent.Columns["ParentId"],dtChild2.Columns["ParentId"]);
2534 // Checking Relations - default value
2536 Assert.AreEqual(0, ds.Relations.Count , "DS346");
2538 ds.Relations.Add(drl);
2540 // Checking Relations Count
2541 Assert.AreEqual(1, ds.Relations.Count , "DS347");
2543 // Checking Relations Value
2544 Assert.AreEqual(drl, ds.Relations[0] , "DS348");
2546 // Checking Relations - get by name
2547 Assert.AreEqual(drl, ds.Relations["Parent-Child"] , "DS349");
2549 // Checking Relations - get by name case sensetive
2550 Assert.AreEqual(drl, ds.Relations["PARENT-CHILD"] , "DS350");
2552 // Checking Relations Count 2
2553 ds.Relations.Add(drl1);
2554 Assert.AreEqual(2, ds.Relations.Count , "DS351");
2556 // Checking Relations - get by name case sensetive,ArgumentException
2559 DataRelation tmp = ds.Relations["PARENT-CHILD"];
2560 Assert.Fail("DS352: Relations Failed to throw ArgumentException");
2562 catch (ArgumentException) {}
2563 catch (AssertionException exc) {throw exc;}
2564 catch (Exception exc)
2566 Assert.Fail("DS353: Relations. Wrong exception type. Got:" + exc);
2570 [Test] public void Reset()
2572 DataTable dt1 = DataProvider.CreateParentDataTable();
2573 DataTable dt2 = DataProvider.CreateChildDataTable();
2574 dt1.PrimaryKey = new DataColumn[] {dt1.Columns[0]};
2575 dt2.PrimaryKey = new DataColumn[] {dt2.Columns[0],dt2.Columns[1]};
2576 DataRelation rel = new DataRelation("Rel",dt1.Columns["ParentId"],dt2.Columns["ParentId"]);
2577 DataSet ds = new DataSet();
2578 ds.Tables.AddRange(new DataTable[] {dt1,dt2});
2579 ds.Relations.Add(rel);
2583 // Reset - Relations
2584 Assert.AreEqual(0 , ds.Relations.Count , "DS354");
2586 Assert.AreEqual(0 , ds.Tables.Count , "DS355");
2589 [Test] public void ShouldSerializeRelations()
2591 // DataSet ShouldSerializeRelations
2592 newDataSet ds = new newDataSet();
2594 Assert.AreEqual(true, ds.testMethod(), "DS356");
2597 class newDataSet:DataSet
2599 public bool testMethod()
2601 return ShouldSerializeRelations();
2604 [Test] public void ShouldSerializeTables()
2606 // DataSet ShouldSerializeTables
2607 newDataSet1 ds = new newDataSet1();
2609 Assert.AreEqual(true, ds.testMethod(), "DS357");
2612 class newDataSet1:DataSet
2614 public bool testMethod()
2616 return ShouldSerializeTables();
2619 [Test] public void Tables()
2621 //References by name to tables and relations in a DataSet are case-sensitive. Two or more tables or relations can exist in a DataSet that have the same name, but that differ in case. For example you can have Table1 and table1. In this situation, a reference to one of the tables by name must match the case of the table name exactly, otherwise an exception is thrown. For example, if the DataSet myDS contains tables Table1 and table1, you would reference Table1 by name as myDS.Tables["Table1"], and table1 as myDS.Tables ["table1"]. Attempting to reference either of the tables as myDS.Tables ["TABLE1"] would generate an exception.
2622 //The case-sensitivity rule does not apply if only one table or relation exists with a particular name. That is, if no other table or relation object in the DataSet matches the name of that particular table or relation object, even by a difference in case, you can reference the object by name using any case and no exception is thrown. For example, if the DataSet has only Table1, you can reference it using myDS.Tables["TABLE1"].
2623 //The CaseSensitive property of the DataSet does not affect this behavior. The CaseSensitive property
2625 DataSet ds = new DataSet();
2627 DataTable dt1 = new DataTable();
2628 DataTable dt2 = new DataTable();
2629 DataTable dt3 = new DataTable();
2630 dt3.TableName = "Table3";
2631 DataTable dt4 = new DataTable(dt3.TableName.ToUpper());
2633 // Checking Tables - default value
2635 Assert.AreEqual(0, ds.Tables.Count , "DS358");
2641 // Checking Tables Count
2642 Assert.AreEqual(3, ds.Tables.Count , "DS359");
2644 // Checking Tables Value 1
2645 Assert.AreEqual(dt1, ds.Tables[0] , "DS360");
2647 // Checking Tables Value 2
2648 Assert.AreEqual(dt2, ds.Tables[1] , "DS361");
2650 // Checking Tables Value 3
2651 Assert.AreEqual(dt3, ds.Tables[2] , "DS362");
2653 // Checking get table by name.ToUpper
2654 Assert.AreEqual(dt3, ds.Tables[dt3.TableName.ToUpper()] , "DS363");
2656 // Checking get table by name.ToLower
2657 Assert.AreEqual(dt3, ds.Tables[dt3.TableName.ToLower()] , "DS364");
2659 // Checking Tables add with name case insensetive
2660 ds.Tables.Add(dt4); //same name as Table3, but different case
2661 Assert.AreEqual(4, ds.Tables.Count , "DS365");
2663 // Checking get table by name
2664 Assert.AreEqual(dt4, ds.Tables[dt4.TableName] , "DS366");
2666 // Checking get table by name with diferent case, ArgumentException
2669 DataTable tmp = ds.Tables[dt4.TableName.ToLower()];
2670 Assert.Fail("DS367: Tables Failed to throw ArgumentException");
2672 catch (ArgumentException) {}
2673 catch (AssertionException exc) {throw exc;}
2674 catch (Exception exc)
2676 Assert.Fail("DS368: Tables. Wrong exception type. Got:" + exc);
2680 [Test] public void WriteXml_ByTextWriterXmlWriteMode()
2682 System.IO.StringReader sr = null;
2683 System.IO.StringWriter sw = null;
2687 // ReadXml - DataSetOut
2689 DataSet oDataset = new DataSet("DataSetOut");
2690 sw = new System.IO.StringWriter();
2691 oDataset.WriteXml(sw,System.Data.XmlWriteMode.WriteSchema);
2693 sr = new System.IO.StringReader(sw.GetStringBuilder().ToString());
2694 oDataset = new DataSet("DataSetOut");
2696 oDataset.ReadXml(sr);
2697 Assert.AreEqual(0, oDataset.Tables.Count , "DS369");
2705 [Test] public void ctor()
2711 Assert.AreEqual(true, ds != null , "DS370");
2714 [Test] public void ctor_ByDataSetName()
2719 ds = new DataSet("NewDataSet");
2720 Assert.AreEqual(true, ds != null , "DS371");
2723 Assert.AreEqual("NewDataSet" , ds.DataSetName , "DS372");
2726 [Test] public void extendedProperties()
2728 DataSet ds = new DataSet();
2729 PropertyCollection pc;
2731 pc = ds.ExtendedProperties ;
2733 // Checking ExtendedProperties default
2734 Assert.AreEqual(true, pc != null, "DS373");
2736 // Checking ExtendedProperties count
2737 Assert.AreEqual(0, pc.Count , "DS374");
2741 // Test for bug #76517
2742 [Test] public void SchemaSerializationModeTest ()
2744 DataSet ds = new DataSet ();
2745 Assert.AreEqual (SchemaSerializationMode.IncludeSchema,
2746 ds.SchemaSerializationMode, "#1");
2748 ds.SchemaSerializationMode = SchemaSerializationMode.ExcludeSchema;
2749 Assert.Fail ("#2 InvalidOperationException must be thrown");
2750 }catch (InvalidOperationException e) {
2756 ///<?xml version="1.0" encoding="utf-16"?>
2757 ///<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
2758 /// <xs:element name="NewDataSet" msdata:IsDataSet="true">
2759 /// <xs:complexType>
2760 /// <xs:choice maxOccurs="unbounded">
2761 /// <xs:element name="Parent">
2762 /// <xs:complexType>
2764 /// <xs:element name="ParentId" type="xs:int" minOccurs="0"/>
2765 /// <xs:element name="String1" type="xs:string" minOccurs="0"/>
2766 /// <xs:element name="String2" type="xs:string" minOccurs="0"/>
2767 /// <xs:element name="ParentDateTime" type="xs:dateTime" minOccurs="0"/>
2768 /// <xs:element name="ParentDouble" type="xs:double" minOccurs="0"/>
2769 /// <xs:element name="ParentBool" type="xs:boolean" minOccurs="0"/>
2771 /// </xs:complexType>
2774 /// </xs:complexType>
2779 public void ParentDataTableSchema()
2781 XmlDocument testedSchema;
2782 XmlNamespaceManager testedSchemaNamepaces;
2783 InitParentDataTableSchema(out testedSchema, out testedSchemaNamepaces);
2785 TestNode("DataSet name", "/xs:schema/xs:element[@name='NewDataSet']", 1, testedSchema, testedSchemaNamepaces);
2787 TestNode("Parent datatable name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element[@name='Parent']", 1, testedSchema, testedSchemaNamepaces);
2789 TestNode("ParentId column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentId']", 1, testedSchema, testedSchemaNamepaces);
2791 TestNode("String1 column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='String1']", 1, testedSchema, testedSchemaNamepaces);
2793 TestNode("String2 column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='String1']", 1, testedSchema, testedSchemaNamepaces);
2795 TestNode("ParentDateTime column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentDateTime']", 1, testedSchema, testedSchemaNamepaces);
2797 TestNode("ParentDouble column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentDouble']", 1, testedSchema, testedSchemaNamepaces);
2799 TestNode("ParentBool column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentBool']", 1, testedSchema, testedSchemaNamepaces);
2801 TestNode("Int columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:int']", 1, testedSchema, testedSchemaNamepaces);
2803 TestNode("string columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:string']", 2, testedSchema, testedSchemaNamepaces);
2805 TestNode("dateTime columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:dateTime']", 1, testedSchema, testedSchemaNamepaces);
2807 TestNode("double columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:double']", 1, testedSchema, testedSchemaNamepaces);
2809 TestNode("boolean columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:boolean']", 1, testedSchema, testedSchemaNamepaces);
2811 TestNode("minOccurs columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@minOccurs='0']", 6, testedSchema, testedSchemaNamepaces);
2814 private void InitParentDataTableSchema(out XmlDocument schemaDocInit, out XmlNamespaceManager namespaceManagerToInit)
2816 DataSet ds = new DataSet();
2817 ds.Tables.Add(DataProvider.CreateParentDataTable());
2818 string strXML = ds.GetXmlSchema();
2819 schemaDocInit = new XmlDocument();
2820 schemaDocInit.LoadXml(strXML);
2821 namespaceManagerToInit = new XmlNamespaceManager(schemaDocInit.NameTable);
2822 namespaceManagerToInit.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
2823 namespaceManagerToInit.AddNamespace("msdata", "urn:schemas-microsoft-com:xml-msdata");
2826 private void TestNode(string description, string xPath, int expectedNodesCout, XmlDocument schemaDoc, XmlNamespaceManager nm)
2828 int actualNodeCount = schemaDoc.SelectNodes(xPath, nm).Count;
2829 Assert.AreEqual(expectedNodesCout,actualNodeCount, "DS75" + description);
2833 public void WriteXml_Stream()
2836 DataSet ds = new DataSet();
2837 string input = "<a><b><c>2</c></b></a>";
2838 System.IO.StringReader sr = new System.IO.StringReader(input) ;
2839 System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
2840 ds.ReadXml (xReader);
2842 System.Text.StringBuilder sb = new System.Text.StringBuilder();
2843 System.IO.StringWriter sw = new System.IO.StringWriter(sb);
2844 System.Xml.XmlTextWriter xWriter = new System.Xml.XmlTextWriter(sw);
2845 ds.WriteXml(xWriter);
2846 string output = sb.ToString();
2847 Assert.AreEqual(input,output, "DS76");
2850 DataSet ds = new DataSet();
2851 string input = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><a><b><c>2</c></b></a>";
2852 string expectedOutput = "<a><b><c>2</c></b></a>";
2853 System.IO.StringReader sr = new System.IO.StringReader(input) ;
2854 System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
2855 ds.ReadXml (xReader);
2857 System.Text.StringBuilder sb = new System.Text.StringBuilder();
2858 System.IO.StringWriter sw = new System.IO.StringWriter(sb);
2859 System.Xml.XmlTextWriter xWriter = new System.Xml.XmlTextWriter(sw);
2860 ds.WriteXml(xWriter);
2861 string output = sb.ToString();
2862 Assert.AreEqual(expectedOutput,output, "DS77");
2865 DataSet ds = new DataSet("DSName");
2866 System.IO.StringWriter sr = new System.IO.StringWriter();
2868 Assert.AreEqual("<DSName />",sr.ToString(), "DS78");
2871 DataSet ds = new DataSet();
2874 //Create parent table.
2875 dt = ds.Tables.Add("ParentTable");
2876 dt.Columns.Add("ParentTable_Id", typeof(int));
2877 dt.Columns.Add("ParentTableCol", typeof(int));
2878 dt.Rows.Add(new object[] {0,1});
2880 //Create child table.
2881 dt = ds.Tables.Add("ChildTable");
2882 dt.Columns.Add("ParentTable_Id", typeof(int));
2883 dt.Columns.Add("ChildTableCol", typeof(string));
2884 dt.Rows.Add(new object[] {0,"aa"});
2886 //Add a relation between parent and child table.
2887 ds.Relations.Add("ParentTable_ChildTable", ds.Tables["ParentTable"].Columns["ParentTable_Id"], ds.Tables["ChildTable"].Columns["ParentTable_Id"], true);
2888 ds.Relations["ParentTable_ChildTable"].Nested=true;
2890 //Reomve the Parent_Child relation.
2891 dt = ds.Tables["ChildTable"];
2892 dt.ParentRelations.Remove("ParentTable_ChildTable");
2894 //Remove the constraint created automatically to enforce the "ParentTable_ChildTable" relation.
2895 dt.Constraints.Remove("ParentTable_ChildTable");
2897 //Remove the child table from the dataset.
2898 ds.Tables.Remove("ChildTable");
2900 //Get the xml representation of the dataset.
2901 System.IO.StringWriter sr = new System.IO.StringWriter();
2903 string xml = sr.ToString();
2905 Assert.AreEqual(-1,xml.IndexOf("<ChildTable>"), "DS79");