DataSetTest2.cs 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540
  1. // Authors:
  2. // Rafael Mizrahi <[email protected]>
  3. // Erez Lotan <[email protected]>
  4. // Oren Gurfinkel <[email protected]>
  5. // Ofer Borstein
  6. //
  7. // Copyright (c) 2004 Mainsoft Co.
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. using NUnit.Framework;
  29. using System;
  30. using System.Text;
  31. using System.IO;
  32. using System.Data;
  33. using MonoTests.System.Data.Utils;
  34. using System.Xml;
  35. namespace MonoTests_System.Data
  36. {
  37. [TestFixture] public class DataSetTest2
  38. {
  39. private DataSet m_ds = null;
  40. private bool EventRaised = false;
  41. [Test] public void AcceptChanges()
  42. {
  43. DataSet ds = new DataSet();
  44. DataTable dtP = DataProvider.CreateParentDataTable();
  45. DataTable dtC = DataProvider.CreateChildDataTable();
  46. ds.Tables.Add(dtP);
  47. ds.Tables.Add(dtC);
  48. ds.Relations.Add(new DataRelation("myRelation",dtP.Columns[0],dtC.Columns[0]));
  49. //create changes
  50. dtP.Rows[0][0] = "70";
  51. dtP.Rows[1].Delete();
  52. dtP.Rows.Add(new object[] {9,"string1","string2"});
  53. // AcceptChanges
  54. ds.AcceptChanges();
  55. Assert.AreEqual(null, dtP.GetChanges(), "DS1");
  56. //read only exception
  57. dtP.Columns[0].ReadOnly = true;
  58. // check ReadOnlyException
  59. try
  60. {
  61. dtP.Rows[0][0] = 99;
  62. Assert.Fail("DS2: Indexer Failed to throw ReadOnlyException");
  63. }
  64. catch (ReadOnlyException) {}
  65. catch (AssertionException exc) {throw exc;}
  66. catch (Exception exc)
  67. {
  68. Assert.Fail("DS3: Indexer. Wrong exception type. Got:" + exc);
  69. }
  70. // check invoke AcceptChanges
  71. ds.AcceptChanges();
  72. }
  73. [Test] public void CaseSensitive()
  74. {
  75. DataSet ds = new DataSet();
  76. DataTable dt = new DataTable();
  77. // CaseSensitive - default value (false)
  78. Assert.AreEqual(false , ds.CaseSensitive , "DS4");
  79. ds.CaseSensitive = true;
  80. // CaseSensitive - get
  81. Assert.AreEqual(true , ds.CaseSensitive , "DS5");
  82. //add a datatable to a dataset
  83. ds.Tables.Add(dt);
  84. // DataTable CaseSensitive from DataSet - true
  85. Assert.AreEqual(true , dt.CaseSensitive , "DS6");
  86. ds.Tables.Clear();
  87. ds.CaseSensitive = false;
  88. dt = new DataTable();
  89. ds.Tables.Add(dt);
  90. // DataTable CaseSensitive from DataSet - false
  91. Assert.AreEqual(false, dt.CaseSensitive , "DS7");
  92. //change DataSet CaseSensitive and check DataTables in it
  93. ds.Tables.Clear();
  94. ds.CaseSensitive = false;
  95. dt = new DataTable();
  96. ds.Tables.Add(dt);
  97. // Change DataSet CaseSensitive - check Table - true
  98. ds.CaseSensitive = true;
  99. Assert.AreEqual(true, dt.CaseSensitive , "DS8");
  100. // Change DataSet CaseSensitive - check Table - false
  101. ds.CaseSensitive = false;
  102. Assert.AreEqual(false, dt.CaseSensitive , "DS9");
  103. //Add new table to DataSet with CaseSensitive,check the table case after adding it to DataSet
  104. ds.Tables.Clear();
  105. ds.CaseSensitive = true;
  106. dt = new DataTable();
  107. dt.CaseSensitive = false;
  108. ds.Tables.Add(dt);
  109. // DataTable get case sensitive from DataSet - false
  110. Assert.AreEqual(false, dt.CaseSensitive , "DS10");
  111. ds.Tables.Clear();
  112. ds.CaseSensitive = false;
  113. dt = new DataTable();
  114. dt.CaseSensitive = true;
  115. ds.Tables.Add(dt);
  116. // DataTable get case sensitive from DataSet - true
  117. Assert.AreEqual(true, dt.CaseSensitive , "DS11");
  118. //Add new table to DataSet and change the DataTable CaseSensitive
  119. ds.Tables.Clear();
  120. ds.CaseSensitive = true;
  121. dt = new DataTable();
  122. ds.Tables.Add(dt);
  123. // Add new table to DataSet and change the DataTable CaseSensitive - false
  124. dt.CaseSensitive = false;
  125. Assert.AreEqual(false, dt.CaseSensitive , "DS12");
  126. ds.Tables.Clear();
  127. ds.CaseSensitive = false;
  128. dt = new DataTable();
  129. ds.Tables.Add(dt);
  130. // Add new table to DataSet and change the DataTable CaseSensitive - true
  131. dt.CaseSensitive = true;
  132. Assert.AreEqual(true, dt.CaseSensitive , "DS13");
  133. //Add DataTable to Dataset, Change DataSet CaseSensitive, check DataTable
  134. ds.Tables.Clear();
  135. ds.CaseSensitive = true;
  136. dt = new DataTable();
  137. dt.CaseSensitive = true;
  138. ds.Tables.Add(dt);
  139. // Add DataTable to Dataset, Change DataSet CaseSensitive, check DataTable - true
  140. ds.CaseSensitive = false;
  141. Assert.AreEqual(true, dt.CaseSensitive , "DS14");
  142. }
  143. [Test] public void Clear()
  144. {
  145. DataSet ds = new DataSet();
  146. ds.Tables.Add(DataProvider.CreateParentDataTable());
  147. ds.Tables[0].Rows.Add(new object[] {9,"",""});
  148. // Clear
  149. ds.Clear();
  150. Assert.AreEqual(0, ds.Tables[0].Rows.Count , "DS15");
  151. }
  152. [Test] public void Clear_WithNoDataWithConstraint()
  153. {
  154. // Test dataset with no data and with constraint
  155. DataSet ds = new DataSet();
  156. ds.Tables.Add(DataProvider.CreateParentDataTable());
  157. ds.Tables.Add(DataProvider.CreateChildDataTable());
  158. ds.Tables[0].Rows.Clear();
  159. ds.Tables[1].Rows.Clear();
  160. ds.Tables[0].Constraints.Add("test",ds.Tables[1].Columns[0],ds.Tables[0].Columns[0]);
  161. ds.Clear();
  162. }
  163. [Test] public void Clone()
  164. {
  165. DataSet ds = new DataSet(), dsTarget = null;
  166. ds.Tables.Add(DataProvider.CreateParentDataTable());
  167. ds.Tables.Add(DataProvider.CreateChildDataTable());
  168. ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
  169. ds.Tables[0].Rows.Add(new object[] {9,"",""});
  170. ds.Tables[1].Columns[2].ReadOnly = true;
  171. ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
  172. //copy schema only, no data
  173. // Clone 1
  174. dsTarget = ds.Clone();
  175. //Assert.AreEqual(ds.GetXmlSchema(), dsTarget.GetXmlSchema() , "DS16");
  176. //use my function because GetXmlSchema not implemented in java
  177. Assert.AreEqual(DataProvider.GetDSSchema(ds), DataProvider.GetDSSchema(dsTarget), "DS17");
  178. // Clone 2
  179. Assert.AreEqual(false, dsTarget.GetXml() == ds.GetXml(), "DS18");
  180. }
  181. [Test] public void Copy()
  182. {
  183. DataSet ds = new DataSet(), dsTarget = null;
  184. ds.Tables.Add(DataProvider.CreateParentDataTable());
  185. ds.Tables.Add(DataProvider.CreateChildDataTable());
  186. ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
  187. ds.Tables[0].Rows.Add(new object[] {9,"",""});
  188. ds.Tables[1].Columns[2].ReadOnly = true;
  189. ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
  190. //copy data and schema
  191. // Copy 1
  192. dsTarget = ds.Copy();
  193. //Assert.AreEqual(ds.GetXmlSchema(), dsTarget.GetXmlSchema() , "DS19");
  194. //using my function because GetXmlSchema in not implemented in java
  195. Assert.AreEqual(DataProvider.GetDSSchema(ds), DataProvider.GetDSSchema (dsTarget) , "DS20");
  196. // Copy 2
  197. Assert.AreEqual(true, dsTarget.GetXml() == ds.GetXml(), "DS21");
  198. }
  199. [Test] public void DataSetName()
  200. {
  201. DataSet ds = new DataSet();
  202. // DataSetName - default value
  203. Assert.AreEqual("NewDataSet" , ds.DataSetName , "DS22");
  204. ds.DataSetName = "NewName";
  205. // DataSetName - get
  206. Assert.AreEqual("NewName" , ds.DataSetName , "DS23");
  207. }
  208. [Test] public void EnforceConstraints()
  209. {
  210. DataSet ds = new DataSet();
  211. // EnforceConstraints - default value (true)
  212. Assert.AreEqual(true, ds.EnforceConstraints , "DS24");
  213. ds.EnforceConstraints = false;
  214. // EnforceConstraints - get
  215. Assert.AreEqual(false, ds.EnforceConstraints , "DS25");
  216. }
  217. [Test]
  218. public void EnforceConstraints_CheckPrimaryConstraint ()
  219. {
  220. DataSet ds = new DataSet();
  221. ds.Tables.Add ("table");
  222. ds.Tables [0].Columns.Add ("col");
  223. ds.Tables [0].PrimaryKey = new DataColumn[] {ds.Tables [0].Columns [0]};
  224. ds.EnforceConstraints = false;
  225. ds.Tables [0].Rows.Add (new object[] {null});
  226. try {
  227. ds.EnforceConstraints = true;
  228. Assert.Fail ("#1");
  229. } catch (ConstraintException e) {
  230. Assert.AreEqual ("Failed to enable constraints. One or more rows contain values " +
  231. "violating non-null, unique, or foreign-key constraints.", e.Message, "#2");
  232. }
  233. }
  234. [Test]
  235. public void EnforceConstraints_NonNullCols ()
  236. {
  237. DataSet ds = new DataSet();
  238. ds.Tables.Add ("table");
  239. ds.Tables [0].Columns.Add ("col");
  240. ds.Tables [0].Columns [0].AllowDBNull = false;
  241. ds.EnforceConstraints = false;
  242. ds.Tables [0].Rows.Add (new object[] {null});
  243. try {
  244. ds.EnforceConstraints = true;
  245. Assert.Fail ("#1");
  246. } catch (ConstraintException e) {
  247. Assert.AreEqual ("Failed to enable constraints. One or more rows contain values " +
  248. "violating non-null, unique, or foreign-key constraints.", e.Message, "#2");
  249. }
  250. }
  251. [Test] public void GetChanges()
  252. {
  253. DataSet ds = new DataSet();
  254. ds.Tables.Add(DataProvider.CreateParentDataTable());
  255. // GetChanges 1
  256. Assert.AreEqual(null , ds.GetChanges(), "DS26");
  257. DataRow dr = ds.Tables[0].NewRow();
  258. dr[0] = 9;
  259. ds.Tables[0].Rows.Add(dr);
  260. // GetChanges 2
  261. Assert.AreEqual(true , ds.GetChanges()!=null, "DS27");
  262. // GetChanges 3
  263. Assert.AreEqual(dr.ItemArray, ds.GetChanges().Tables[0].Rows[0].ItemArray , "DS28");
  264. }
  265. [Test] public void GetChanges_ByDataRowState()
  266. {
  267. DataSet ds = new DataSet();
  268. object[] arrAdded,arrDeleted,arrModified,arrUnchanged;
  269. //object[] arrDetached;
  270. DataRow dr;
  271. ds.Tables.Add(DataProvider.CreateParentDataTable());
  272. // GetChanges 1
  273. Assert.AreEqual(null , ds.GetChanges(), "DS29");
  274. //make some changes
  275. // can't check detached
  276. // dr = ds.Tables[0].Rows[0];
  277. // arrDetached = dr.ItemArray;
  278. // dr.Delete();
  279. // ds.Tables[0].AcceptChanges();
  280. dr= ds.Tables[0].Rows[1];
  281. arrDeleted = dr.ItemArray;
  282. dr.Delete();
  283. dr = ds.Tables[0].Rows[2];
  284. dr[1] = "NewValue";
  285. arrModified = dr.ItemArray;
  286. dr = ds.Tables[0].Select("","",DataViewRowState.Unchanged)[0];
  287. arrUnchanged = dr.ItemArray;
  288. dr = ds.Tables[0].NewRow();
  289. dr[0] = 1;
  290. ds.Tables[0].Rows.Add(dr);
  291. arrAdded = dr.ItemArray;
  292. // GetChanges Added
  293. Assert.AreEqual(arrAdded, ds.GetChanges(DataRowState.Added).Tables[0].Rows[0].ItemArray , "DS30");
  294. // GetChanges Deleted
  295. dr = ds.GetChanges(DataRowState.Deleted).Tables[0].Rows[0];
  296. 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]};
  297. Assert.AreEqual(arrDeleted, tmp, "DS31");
  298. // can't check it
  299. // // GetChanges Detached
  300. // dr = ds.GetChanges(DataRowState.Detached).Tables[0].Rows[0];
  301. // object[] tmp = new object[] {dr[0,DataRowVersion.Original],dr[1,DataRowVersion.Original],dr[2,DataRowVersion.Original]};
  302. // Assert.AreEqual(arrDetached, tmp, "DS32");
  303. // GetChanges Modified
  304. Assert.AreEqual(arrModified, ds.GetChanges(DataRowState.Modified).Tables[0].Rows[0].ItemArray , "DS33");
  305. // GetChanges Unchanged
  306. Assert.AreEqual(arrUnchanged, ds.GetChanges(DataRowState.Unchanged).Tables[0].Rows[0].ItemArray , "DS34");
  307. }
  308. [Test] public void BeginInitTest ()
  309. {
  310. DataSet ds = new DataSet ();
  311. DataTable table1 = new DataTable ("table1");
  312. DataTable table2 = new DataTable ("table2");
  313. DataColumn col1 = new DataColumn ("col1", typeof (int));
  314. DataColumn col2 = new DataColumn ("col2", typeof (int));
  315. table1.Columns.Add (col1);
  316. table2.Columns.Add (col2);
  317. UniqueConstraint pkey = new UniqueConstraint ("pk", new string[] {"col1"}, true);
  318. ForeignKeyConstraint fkey = new ForeignKeyConstraint ("fk", "table1", new String[] {"col1"},
  319. new String[] {"col2"}, AcceptRejectRule.Cascade,
  320. Rule.Cascade, Rule.Cascade);
  321. DataRelation relation = new DataRelation ("rel", "table1", "table2", new String[] {"col1"},
  322. new String[] {"col2"}, false);
  323. ds.BeginInit ();
  324. table1.BeginInit ();
  325. table2.BeginInit ();
  326. ds.Tables.AddRange (new DataTable[] {table1, table2});
  327. ds.Relations.AddRange (new DataRelation[] {relation});
  328. table1.Constraints.AddRange (new Constraint[] {pkey});
  329. table2.Constraints.AddRange (new Constraint[] {fkey});
  330. // The tables/relations shud not get added to the DataSet yet
  331. Assert.AreEqual (0, ds.Tables.Count, "#1");
  332. Assert.AreEqual (0, ds.Relations.Count, "#2");
  333. Assert.AreEqual (0, table1.Constraints.Count, "#3");
  334. Assert.AreEqual (0, table2.Constraints.Count, "#4");
  335. ds.EndInit ();
  336. Assert.AreEqual (2, ds.Tables.Count, "#5");
  337. Assert.AreEqual (1, ds.Relations.Count, "#6");
  338. Assert.AreEqual (1, ds.Tables [0].Constraints.Count, "#7");
  339. Assert.AreEqual (1, ds.Tables [1].Constraints.Count, "#8");
  340. // Table shud still be in BeginInit ..
  341. DataColumn col3 = new DataColumn ("col2");
  342. UniqueConstraint uc = new UniqueConstraint ("uc", new string[] {"col2"}, false);
  343. table1.Columns.AddRange (new DataColumn[] {col3});
  344. table1.Constraints.AddRange (new Constraint[] {uc});
  345. Assert.AreEqual (1, table1.Columns.Count, "#9");
  346. Assert.AreEqual (1, table1.Constraints.Count, "#10");
  347. table1.EndInit ();
  348. Assert.AreEqual (2, table1.Columns.Count, "#11");
  349. Assert.AreEqual (2, table1.Columns.Count, "#12");
  350. }
  351. [Test] public void GetXml()
  352. {
  353. DataSet ds = new DataSet();
  354. ds.Namespace = "namespace"; //if we don't add namespace the test will fail because GH (by design) always add namespace
  355. DataTable dt = DataProvider.CreateParentDataTable();
  356. dt.Clear();
  357. dt.Rows.Add(new object[] {1,"Value1","Value2"});
  358. dt.Rows.Add(new object[] {2,"Value3","Value4"});
  359. dt.Rows.Add(new object[] {3,"Value5","Value5"});
  360. System.Text.StringBuilder resultXML = new System.Text.StringBuilder();
  361. resultXML.Append("<" + ds.DataSetName + "xmlns=\"namespace\">");
  362. resultXML.Append("<Parent>");
  363. resultXML.Append("<ParentId>1</ParentId>");
  364. resultXML.Append("<String1>Value1</String1>");
  365. resultXML.Append("<String2>Value2</String2>");
  366. resultXML.Append("</Parent>");
  367. resultXML.Append("<Parent>");
  368. resultXML.Append("<ParentId>2</ParentId>");
  369. resultXML.Append("<String1>Value3</String1>");
  370. resultXML.Append("<String2>Value4</String2>");
  371. resultXML.Append("</Parent>");
  372. resultXML.Append("<Parent>");
  373. resultXML.Append("<ParentId>3</ParentId>");
  374. resultXML.Append("<String1>Value5</String1>");
  375. resultXML.Append("<String2>Value5</String2>");
  376. resultXML.Append("</Parent>");
  377. resultXML.Append("</" + ds.DataSetName + ">");
  378. ds.Tables.Add(dt);
  379. string strXML = ds.GetXml();
  380. strXML = strXML.Replace(" ","");
  381. strXML = strXML.Replace("\t","");
  382. strXML = strXML.Replace("\n","");
  383. strXML = strXML.Replace("\r","");
  384. // GetXml
  385. Assert.AreEqual(resultXML.ToString() , strXML , "DS35");
  386. }
  387. [Test] public void HasChanges()
  388. {
  389. DataSet ds = new DataSet();
  390. ds.Tables.Add(DataProvider.CreateParentDataTable());
  391. // HasChanges 1
  392. Assert.AreEqual(false , ds.HasChanges(), "DS36");
  393. DataRow dr = ds.Tables[0].NewRow();
  394. dr[0] = 9;
  395. ds.Tables[0].Rows.Add(dr);
  396. // HasChanges 2
  397. Assert.AreEqual(true , ds.HasChanges(), "DS37");
  398. }
  399. [Test] public void HasChanges_ByDataRowState()
  400. {
  401. DataSet ds = new DataSet();
  402. DataRow dr;
  403. ds.Tables.Add(DataProvider.CreateParentDataTable());
  404. // HasChanges 1
  405. Assert.AreEqual(false , ds.HasChanges(), "DS38");
  406. //make some changes
  407. dr= ds.Tables[0].Rows[1];
  408. dr.Delete();
  409. dr = ds.Tables[0].Rows[2];
  410. dr[1] = "NewValue";
  411. dr = ds.Tables[0].Select("","",DataViewRowState.Unchanged)[0];
  412. dr = ds.Tables[0].NewRow();
  413. dr[0] = 1;
  414. ds.Tables[0].Rows.Add(dr);
  415. // HasChanges Added
  416. Assert.AreEqual(true , ds.HasChanges(DataRowState.Added), "DS39");
  417. // HasChanges Deleted
  418. Assert.AreEqual(true , ds.HasChanges(DataRowState.Deleted) , "DS40");
  419. // HasChanges Modified
  420. Assert.AreEqual(true, ds.HasChanges(DataRowState.Modified), "DS41");
  421. // HasChanges Unchanged
  422. Assert.AreEqual(true, ds.HasChanges(DataRowState.Unchanged), "DS42");
  423. }
  424. [Test] public void HasErrors()
  425. {
  426. DataSet ds = new DataSet();
  427. ds.Tables.Add(DataProvider.CreateParentDataTable());
  428. // HasErrors - default
  429. Assert.AreEqual(false , ds.HasErrors , "DS43");
  430. ds.Tables[0].Rows[0].RowError = "ErrDesc";
  431. // HasErrors
  432. Assert.AreEqual(true , ds.HasErrors , "DS44");
  433. }
  434. #region test namespaces
  435. [Test] public void InferXmlSchema_BasicXml()
  436. {
  437. StringBuilder sb = new StringBuilder();
  438. sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
  439. sb.Append("<Categories>");
  440. sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
  441. sb.Append("<CategoryName od:maxLength='15' od:adotype='130'>Beverages</CategoryName>");
  442. sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
  443. sb.Append("</Categories>");
  444. sb.Append("<Products>");
  445. sb.Append("<ProductID od:adotype='20'>1</ProductID>");
  446. sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
  447. sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
  448. sb.Append("</Products>");
  449. sb.Append("</NewDataSet>");
  450. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  451. DataSet ds = new DataSet();
  452. // ds.ReadXml(myStream);
  453. ds.InferXmlSchema(myStream, new string[] {"urn:schemas-microsoft-com:officedata"});
  454. Assert.AreEqual(2, ds.Tables.Count, "DS45");
  455. Assert.AreEqual("CategoryID", ds.Tables[0].Columns[0].ColumnName, "DS46");
  456. Assert.AreEqual("CategoryName", ds.Tables[0].Columns[1].ColumnName, "DS47");
  457. Assert.AreEqual("Description", ds.Tables[0].Columns[2].ColumnName, "DS48");
  458. Assert.AreEqual("ProductID", ds.Tables[1].Columns[0].ColumnName, "DS49");
  459. Assert.AreEqual("ReorderLevel", ds.Tables[1].Columns[1].ColumnName, "DS50");
  460. Assert.AreEqual("Discontinued", ds.Tables[1].Columns[2].ColumnName, "DS51");
  461. }
  462. [Test] public void InferXmlSchema_WithoutIgnoreNameSpaces()
  463. {
  464. StringBuilder sb = new StringBuilder();
  465. sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
  466. sb.Append("<Categories>");
  467. sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
  468. sb.Append("<CategoryName od:maxLength='15' od:adotype='130'>Beverages</CategoryName>");
  469. sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
  470. sb.Append("</Categories>");
  471. sb.Append("<Products>");
  472. sb.Append("<ProductID od:adotype='20'>1</ProductID>");
  473. sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
  474. sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
  475. sb.Append("</Products>");
  476. sb.Append("</NewDataSet>");
  477. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  478. DataSet ds = new DataSet();
  479. //ds.ReadXml(myStream);
  480. ds.InferXmlSchema(myStream,new string[] {"urn:schemas-microsoft-com:officedata1"});
  481. Assert.AreEqual(8, ds.Tables.Count, "DS52");
  482. }
  483. [Test] public void InferXmlSchema_IgnoreNameSpace()
  484. {
  485. StringBuilder sb = new StringBuilder();
  486. sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
  487. sb.Append("<Categories>");
  488. sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
  489. sb.Append("<CategoryName od:maxLength='15' adotype='130'>Beverages</CategoryName>");
  490. sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
  491. sb.Append("</Categories>");
  492. sb.Append("<Products>");
  493. sb.Append("<ProductID od:adotype='20'>1</ProductID>");
  494. sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
  495. sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
  496. sb.Append("</Products>");
  497. sb.Append("</NewDataSet>");
  498. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  499. DataSet ds = new DataSet();
  500. // ds.ReadXml(myStream);
  501. ds.InferXmlSchema(myStream, new string[] {"urn:schemas-microsoft-com:officedata"});
  502. Assert.AreEqual(3, ds.Tables.Count, "DS53");
  503. Assert.AreEqual(3, ds.Tables[0].Columns.Count, "DS54");
  504. Assert.AreEqual("CategoryID", ds.Tables[0].Columns["CategoryID"].ColumnName, "DS55");
  505. Assert.AreEqual("Categories_Id", ds.Tables[0].Columns["Categories_Id"].ColumnName, "DS56");//Hidden
  506. Assert.AreEqual("Description", ds.Tables[0].Columns["Description"].ColumnName, "DS57");
  507. Assert.AreEqual(3, ds.Tables[1].Columns.Count, "DS58");
  508. Assert.AreEqual("adotype", ds.Tables[1].Columns["adotype"].ColumnName, "DS59");
  509. Assert.AreEqual("CategoryName_Text", ds.Tables[1].Columns["CategoryName_Text"].ColumnName, "DS60");
  510. Assert.AreEqual("Categories_Id", ds.Tables[1].Columns["Categories_Id"].ColumnName, "DS61");//Hidden
  511. Assert.AreEqual(3, ds.Tables[2].Columns.Count, "DS62");
  512. Assert.AreEqual("ProductID", ds.Tables[2].Columns["ProductID"].ColumnName, "DS63");
  513. Assert.AreEqual("ReorderLevel", ds.Tables[2].Columns["ReorderLevel"].ColumnName, "DS64");
  514. Assert.AreEqual("Discontinued", ds.Tables[2].Columns["Discontinued"].ColumnName, "DS65");
  515. }
  516. [Test] public void InferXmlSchema_IgnoreNameSpaces() //Ignoring 2 namespaces
  517. {
  518. StringBuilder sb = new StringBuilder();
  519. sb.Append("<h:html xmlns:xdc='http://www.xml.com/books' xmlns:h='http://www.w3.org/HTML/1998/html4'>");
  520. sb.Append("<h:head><h:title>Book Review</h:title></h:head>");
  521. sb.Append("<h:body>");
  522. sb.Append("<xdc:bookreview>");
  523. sb.Append("<xdc:title h:attrib1='1' xdc:attrib2='2' >XML: A Primer</xdc:title>");
  524. sb.Append("</xdc:bookreview>");
  525. sb.Append("</h:body>");
  526. sb.Append("</h:html>");
  527. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  528. DataSet tempDs = new DataSet();
  529. tempDs.ReadXml(myStream);
  530. myStream.Seek(0,SeekOrigin.Begin);
  531. DataSet ds = new DataSet();
  532. ds.InferXmlSchema(myStream, new string[] {"http://www.xml.com/books","http://www.w3.org/HTML/1998/html4"});
  533. //Assert.AreEqual(8, ds.Tables.Count, "DS66");
  534. // string str1 = tempDs.GetXmlSchema(); //DataProvider.GetDSSchema(tempDs);
  535. // string str2 = ds.GetXmlSchema(); //DataProvider.GetDSSchema(ds);
  536. Assert.AreEqual(3, ds.Tables.Count, "DS67");
  537. Assert.AreEqual("bookreview", ds.Tables[2].TableName, "DS68");
  538. Assert.AreEqual(2, ds.Tables[2].Columns.Count, "DS69");
  539. }
  540. #endregion
  541. #region inferingTables
  542. [Test] public void InferXmlSchema_inferingTables1()
  543. {
  544. //Acroding to the msdn documantaion :
  545. //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
  546. //Elements that have attributes specified in them will result in inferred tables
  547. // inferingTables1
  548. StringBuilder sb = new StringBuilder();
  549. sb.Append("<DocumentElement>");
  550. sb.Append("<Element1 attr1='value1'/>");
  551. sb.Append("<Element1 attr1='value2'>Text1</Element1>");
  552. sb.Append("</DocumentElement>");
  553. DataSet ds = new DataSet();
  554. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  555. ds.InferXmlSchema(myStream,null);
  556. Assert.AreEqual("DocumentElement", ds.DataSetName, "DS70");
  557. Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS71");
  558. Assert.AreEqual(1, ds.Tables.Count, "DS72");
  559. Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS73");
  560. Assert.AreEqual("Element1_Text", ds.Tables[0].Columns["Element1_Text"].ColumnName, "DS74");
  561. }
  562. [Test] public void InferXmlSchema_inferingTables2()
  563. {
  564. //Acroding to the msdn documantaion :
  565. //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
  566. //Elements that have child elements will result in inferred tables
  567. // inferingTables2
  568. StringBuilder sb = new StringBuilder();
  569. sb.Append("<DocumentElement>");
  570. sb.Append("<Element1>");
  571. sb.Append("<ChildElement1>Text1</ChildElement1>");
  572. sb.Append("</Element1>");
  573. sb.Append("</DocumentElement>");
  574. DataSet ds = new DataSet();
  575. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  576. ds.InferXmlSchema(myStream,null);
  577. Assert.AreEqual("DocumentElement", ds.DataSetName, "DS75");
  578. Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS76");
  579. Assert.AreEqual(1, ds.Tables.Count, "DS77");
  580. Assert.AreEqual("ChildElement1", ds.Tables[0].Columns["ChildElement1"].ColumnName, "DS78");
  581. }
  582. [Test] public void InferXmlSchema_inferingTables3()
  583. {
  584. //Acroding to the msdn documantaion :
  585. //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
  586. //The document, or root, element will result in an inferred table if it has attributes
  587. //or child elements that will be inferred as columns.
  588. //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
  589. // inferingTables3
  590. StringBuilder sb = new StringBuilder();
  591. sb.Append("<DocumentElement>");
  592. sb.Append("<Element1>Text1</Element1>");
  593. sb.Append("<Element2>Text2</Element2>");
  594. sb.Append("</DocumentElement>");
  595. DataSet ds = new DataSet();
  596. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  597. ds.InferXmlSchema(myStream,null);
  598. Assert.AreEqual("NewDataSet", ds.DataSetName, "DS79");
  599. Assert.AreEqual("DocumentElement", ds.Tables[0].TableName, "DS80");
  600. Assert.AreEqual(1, ds.Tables.Count, "DS81");
  601. Assert.AreEqual("Element1", ds.Tables[0].Columns["Element1"].ColumnName, "DS82");
  602. Assert.AreEqual("Element2", ds.Tables[0].Columns["Element2"].ColumnName, "DS83");
  603. }
  604. [Test] public void InferXmlSchema_inferingTables4()
  605. {
  606. //Acroding to the msdn documantaion :
  607. //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
  608. //The document, or root, element will result in an inferred table if it has attributes
  609. //or child elements that will be inferred as columns.
  610. //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
  611. // inferingTables4
  612. StringBuilder sb = new StringBuilder();
  613. sb.Append("<DocumentElement>");
  614. sb.Append("<Element1 attr1='value1' attr2='value2'/>");
  615. sb.Append("</DocumentElement>");
  616. DataSet ds = new DataSet();
  617. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  618. ds.InferXmlSchema(myStream,null);
  619. Assert.AreEqual("DocumentElement", ds.DataSetName, "DS84");
  620. Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS85");
  621. Assert.AreEqual(1, ds.Tables.Count, "DS86");
  622. Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS87");
  623. Assert.AreEqual("attr2", ds.Tables[0].Columns["attr2"].ColumnName, "DS88");
  624. }
  625. [Test]
  626. public void InferXmlSchema_inferingTables5()
  627. {
  628. //Acroding to the msdn documantaion :
  629. //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
  630. //Elements that repeat will result in a single inferred table
  631. // inferingTables5
  632. StringBuilder sb = new StringBuilder();
  633. sb.Append("<DocumentElement>");
  634. sb.Append("<Element1>Text1</Element1>");
  635. sb.Append("<Element1>Text2</Element1>");
  636. sb.Append("</DocumentElement>");
  637. DataSet ds = new DataSet();
  638. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  639. ds.InferXmlSchema(myStream,null);
  640. Assert.AreEqual("DocumentElement", ds.DataSetName, "DS89");
  641. Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS90");
  642. Assert.AreEqual(1, ds.Tables.Count, "DS91");
  643. Assert.AreEqual("Element1_Text", ds.Tables[0].Columns["Element1_Text"].ColumnName, "DS92");
  644. }
  645. #endregion
  646. #region inferringColumns
  647. [Test] public void InferXmlSchema_inferringColumns1()
  648. {
  649. //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm
  650. // inferringColumns1
  651. StringBuilder sb = new StringBuilder();
  652. sb.Append("<DocumentElement>");
  653. sb.Append("<Element1 attr1='value1' attr2='value2'/>");
  654. sb.Append("</DocumentElement>");
  655. DataSet ds = new DataSet();
  656. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  657. ds.InferXmlSchema(myStream,null);
  658. Assert.AreEqual("DocumentElement", ds.DataSetName, "DS93");
  659. Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS94");
  660. Assert.AreEqual(1, ds.Tables.Count, "DS95");
  661. Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS96");
  662. Assert.AreEqual("attr2", ds.Tables[0].Columns["attr2"].ColumnName, "DS97");
  663. Assert.AreEqual(MappingType.Attribute, ds.Tables[0].Columns["attr1"].ColumnMapping , "DS98");
  664. Assert.AreEqual(MappingType.Attribute, ds.Tables[0].Columns["attr2"].ColumnMapping , "DS99");
  665. Assert.AreEqual(typeof(string), ds.Tables[0].Columns["attr1"].DataType , "DS100");
  666. Assert.AreEqual(typeof(string), ds.Tables[0].Columns["attr2"].DataType , "DS101");
  667. }
  668. [Test] public void InferXmlSchema_inferringColumns2()
  669. {
  670. //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm
  671. //If an element has no child elements or attributes, it will be inferred as a column.
  672. //The ColumnMapping property of the column will be set to MappingType.Element.
  673. //The text for child elements is stored in a row in the table
  674. // inferringColumns2
  675. StringBuilder sb = new StringBuilder();
  676. sb.Append("<DocumentElement>");
  677. sb.Append("<Element1>");
  678. sb.Append("<ChildElement1>Text1</ChildElement1>");
  679. sb.Append("<ChildElement2>Text2</ChildElement2>");
  680. sb.Append("</Element1>");
  681. sb.Append("</DocumentElement>");
  682. DataSet ds = new DataSet();
  683. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  684. ds.InferXmlSchema(myStream,null);
  685. Assert.AreEqual("DocumentElement", ds.DataSetName, "DS102");
  686. Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS103");
  687. Assert.AreEqual(1, ds.Tables.Count, "DS104");
  688. Assert.AreEqual("ChildElement1", ds.Tables[0].Columns["ChildElement1"].ColumnName, "DS105");
  689. Assert.AreEqual("ChildElement2", ds.Tables[0].Columns["ChildElement2"].ColumnName, "DS106");
  690. Assert.AreEqual(MappingType.Element, ds.Tables[0].Columns["ChildElement1"].ColumnMapping , "DS107");
  691. Assert.AreEqual(MappingType.Element, ds.Tables[0].Columns["ChildElement2"].ColumnMapping , "DS108");
  692. Assert.AreEqual(typeof(string), ds.Tables[0].Columns["ChildElement1"].DataType , "DS109");
  693. Assert.AreEqual(typeof(string), ds.Tables[0].Columns["ChildElement2"].DataType , "DS110");
  694. }
  695. #endregion
  696. #region Inferring Relationships
  697. [Test] public void InferXmlSchema_inferringRelationships1()
  698. {
  699. //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringrelationships.htm
  700. // inferringRelationships1
  701. StringBuilder sb = new StringBuilder();
  702. sb.Append("<DocumentElement>");
  703. sb.Append("<Element1>");
  704. sb.Append("<ChildElement1 attr1='value1' attr2='value2'/>");
  705. sb.Append("<ChildElement2>Text2</ChildElement2>");
  706. sb.Append("</Element1>");
  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, "DS111");
  712. Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS112");
  713. Assert.AreEqual("ChildElement1", ds.Tables[1].TableName, "DS113");
  714. Assert.AreEqual(2, ds.Tables.Count, "DS114");
  715. Assert.AreEqual("Element1_Id", ds.Tables["Element1"].Columns["Element1_Id"].ColumnName, "DS115");
  716. Assert.AreEqual(MappingType.Hidden, ds.Tables["Element1"].Columns["Element1_Id"].ColumnMapping , "DS116");
  717. Assert.AreEqual(typeof(Int32), ds.Tables["Element1"].Columns["Element1_Id"].DataType , "DS117");
  718. Assert.AreEqual("ChildElement2", ds.Tables["Element1"].Columns["ChildElement2"].ColumnName, "DS118");
  719. Assert.AreEqual(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement2"].ColumnMapping , "DS119");
  720. Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["ChildElement2"].DataType , "DS120");
  721. Assert.AreEqual("attr1", ds.Tables["ChildElement1"].Columns["attr1"].ColumnName, "DS121");
  722. Assert.AreEqual(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr1"].ColumnMapping , "DS122");
  723. Assert.AreEqual(typeof(string), ds.Tables["ChildElement1"].Columns["attr1"].DataType , "DS123");
  724. Assert.AreEqual("attr2", ds.Tables["ChildElement1"].Columns["attr2"].ColumnName, "DS124");
  725. Assert.AreEqual(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr2"].ColumnMapping , "DS125");
  726. Assert.AreEqual(typeof(string), ds.Tables["ChildElement1"].Columns["attr2"].DataType , "DS126");
  727. Assert.AreEqual("Element1_Id", ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnName, "DS127");
  728. Assert.AreEqual(MappingType.Hidden, ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnMapping , "DS128");
  729. Assert.AreEqual(typeof(Int32), ds.Tables["ChildElement1"].Columns["Element1_Id"].DataType , "DS129");
  730. //Checking dataRelation :
  731. Assert.AreEqual("Element1", ds.Relations["Element1_ChildElement1"].ParentTable.TableName, "DS130");
  732. Assert.AreEqual("Element1_Id", ds.Relations["Element1_ChildElement1"].ParentColumns[0].ColumnName, "DS131");
  733. Assert.AreEqual("ChildElement1", ds.Relations["Element1_ChildElement1"].ChildTable.TableName, "DS132");
  734. Assert.AreEqual("Element1_Id", ds.Relations["Element1_ChildElement1"].ChildColumns[0].ColumnName, "DS133");
  735. Assert.AreEqual(true, ds.Relations["Element1_ChildElement1"].Nested, "DS134");
  736. //Checking ForeignKeyConstraint
  737. ForeignKeyConstraint con = (ForeignKeyConstraint)ds.Tables["ChildElement1"].Constraints["Element1_ChildElement1"];
  738. Assert.AreEqual("Element1_Id", con.Columns[0].ColumnName, "DS135");
  739. Assert.AreEqual(Rule.Cascade, con.DeleteRule, "DS136");
  740. Assert.AreEqual(AcceptRejectRule.None, con.AcceptRejectRule, "DS137");
  741. Assert.AreEqual("Element1", con.RelatedTable.TableName, "DS138");
  742. Assert.AreEqual("ChildElement1", con.Table.TableName, "DS139");
  743. }
  744. #endregion
  745. #region Inferring Element Text
  746. [Test] public void InferXmlSchema_elementText1()
  747. {
  748. //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringelementtext.htm
  749. // elementText1
  750. StringBuilder sb = new StringBuilder();
  751. sb.Append("<DocumentElement>");
  752. sb.Append("<Element1 attr1='value1'>Text1</Element1>");
  753. sb.Append("</DocumentElement>");
  754. DataSet ds = new DataSet();
  755. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  756. ds.InferXmlSchema(myStream,null);
  757. Assert.AreEqual("DocumentElement", ds.DataSetName, "DS140");
  758. Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS141");
  759. Assert.AreEqual(1, ds.Tables.Count, "DS142");
  760. Assert.AreEqual("attr1", ds.Tables["Element1"].Columns["attr1"].ColumnName, "DS143");
  761. Assert.AreEqual(MappingType.Attribute, ds.Tables["Element1"].Columns["attr1"].ColumnMapping , "DS144");
  762. Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["attr1"].DataType , "DS145");
  763. Assert.AreEqual("Element1_Text", ds.Tables["Element1"].Columns["Element1_Text"].ColumnName, "DS146");
  764. Assert.AreEqual(MappingType.SimpleContent, ds.Tables["Element1"].Columns["Element1_Text"].ColumnMapping , "DS147");
  765. Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["Element1_Text"].DataType , "DS148");
  766. }
  767. [Test] public void InferXmlSchema_elementText2()
  768. {
  769. //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringelementtext.htm
  770. // elementText1
  771. StringBuilder sb = new StringBuilder();
  772. sb.Append("<DocumentElement>");
  773. sb.Append("<Element1>");
  774. sb.Append("Text1");
  775. sb.Append("<ChildElement1>Text2</ChildElement1>");
  776. sb.Append("Text3");
  777. sb.Append("</Element1>");
  778. sb.Append("</DocumentElement>");
  779. DataSet ds = new DataSet();
  780. MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
  781. ds.InferXmlSchema(myStream,null);
  782. Assert.AreEqual("DocumentElement", ds.DataSetName, "DS149");
  783. Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS150");
  784. Assert.AreEqual(1, ds.Tables.Count, "DS151");
  785. Assert.AreEqual("ChildElement1", ds.Tables["Element1"].Columns["ChildElement1"].ColumnName, "DS152");
  786. Assert.AreEqual(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement1"].ColumnMapping , "DS153");
  787. Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["ChildElement1"].DataType , "DS154");
  788. Assert.AreEqual(1, ds.Tables["Element1"].Columns.Count, "DS155");
  789. }
  790. #endregion
  791. [Test] public void Locale()
  792. {
  793. DataSet ds = new DataSet("MyDataSet");
  794. System.Globalization.CultureInfo culInfo = System.Globalization.CultureInfo.CurrentCulture ;
  795. // Checking Locale default from system
  796. Assert.AreEqual(culInfo, ds.Locale , "DS156");
  797. // Checking Locale get/set
  798. culInfo = new System.Globalization.CultureInfo("fr"); // = french
  799. ds.Locale = culInfo ;
  800. Assert.AreEqual(culInfo , ds.Locale , "DS157");
  801. }
  802. [Test] public void MergeFailed()
  803. {
  804. EventRaised = false;
  805. DataSet ds1,ds2;
  806. ds1 = new DataSet();
  807. ds1.Tables.Add(DataProvider.CreateParentDataTable());
  808. //add primary key to the FIRST column
  809. ds1.Tables[0].PrimaryKey = new DataColumn[] {ds1.Tables[0].Columns[0]};
  810. //create target dataset which is a copy of the source
  811. ds2 = ds1.Copy();
  812. //clear the data
  813. ds2.Clear();
  814. //add primary key to the SECOND columnn
  815. ds2.Tables[0].PrimaryKey = new DataColumn[] {ds2.Tables[0].Columns[1]};
  816. //add a new row that already exists in the source dataset
  817. //ds2.Tables[0].Rows.Add(ds1.Tables[0].Rows[0].ItemArray);
  818. //enforce constraints
  819. ds2.EnforceConstraints = true;
  820. ds1.EnforceConstraints = true;
  821. // Add MergeFailed event handler for the table.
  822. ds2.MergeFailed += new MergeFailedEventHandler( Merge_Failed );
  823. ds2.Merge(ds1); //will raise MergeFailed event
  824. // MergeFailed event
  825. Assert.AreEqual(true , EventRaised , "DS158");
  826. }
  827. private void Merge_Failed( object sender, MergeFailedEventArgs e )
  828. {
  829. EventRaised = true;
  830. }
  831. [Test] public void Merge_ByDataRowsNoPreserveIgnoreMissingSchema()
  832. {
  833. DataTable dt = DataProvider.CreateParentDataTable();
  834. dt.TableName = "Table1";
  835. dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
  836. //create target dataset (copy of source dataset)
  837. DataSet dsTarget = new DataSet();
  838. dsTarget.Tables.Add(dt.Copy());
  839. DataRow[] drArr = new DataRow[3];
  840. //Update row
  841. string OldValue = dt.Select("ParentId=1")[0][1].ToString();
  842. drArr[0] = dt.Select("ParentId=1")[0];
  843. drArr[0][1] = "NewValue";
  844. //delete rows
  845. drArr[1] = dt.Select("ParentId=2")[0];
  846. drArr[1].Delete();
  847. //add row
  848. drArr[2] = dt.NewRow();
  849. drArr[2].ItemArray = new object[] {99 ,"NewRowValue1", "NewRowValue2"};
  850. dt.Rows.Add(drArr[2]);
  851. dsTarget.Merge(drArr,false,MissingSchemaAction.Ignore );
  852. // Merge update row
  853. Assert.AreEqual("NewValue", dsTarget.Tables["Table1"].Select("ParentId=1")[0][1] , "DS159");
  854. // Merge added row
  855. Assert.AreEqual(1, dsTarget.Tables["Table1"].Select("ParentId=99").Length , "DS160");
  856. // Merge deleted row
  857. Assert.AreEqual(0, dsTarget.Tables["Table1"].Select("ParentId=2").Length , "DS161");
  858. }
  859. [Test] public void Merge_ByDataRowsPreserveMissingSchema()
  860. {
  861. DataTable dt = DataProvider.CreateParentDataTable();
  862. dt.TableName = "Table1";
  863. dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
  864. //create target dataset (copy of source dataset)
  865. DataSet dsTarget = new DataSet();
  866. dsTarget.Tables.Add(dt.Copy());
  867. //add new column (for checking MissingSchemaAction)
  868. DataColumn dc = new DataColumn("NewColumn",typeof(float));
  869. dt.Columns.Add(dc);
  870. DataRow[] drArr = new DataRow[3];
  871. //Update row
  872. string OldValue = dt.Select("ParentId=1")[0][1].ToString();
  873. drArr[0] = dt.Select("ParentId=1")[0];
  874. drArr[0][1] = "NewValue";
  875. //delete rows
  876. drArr[1] = dt.Select("ParentId=2")[0];
  877. drArr[1].Delete();
  878. //add row
  879. drArr[2] = dt.NewRow();
  880. drArr[2].ItemArray = new object[] {99 ,"NewRowValue1", "NewRowValue2",null};
  881. dt.Rows.Add(drArr[2]);
  882. DataSet dsTarget1 = null;
  883. #region "Merge(drArr,true,MissingSchemaAction.Ignore )"
  884. dsTarget1 = dsTarget.Copy();
  885. dsTarget1.Merge(drArr,true,MissingSchemaAction.Ignore );
  886. // Merge true,Ignore - Column
  887. Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS162");
  888. // Merge true,Ignore - changed values
  889. Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS163");
  890. // Merge true,Ignore - added values
  891. Assert.AreEqual(1 , dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS164");
  892. // Merge true,Ignore - deleted row
  893. Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS165");
  894. #endregion
  895. #region "Merge(drArr,false,MissingSchemaAction.Ignore )"
  896. dsTarget1 = dsTarget.Copy();
  897. dsTarget1.Merge(drArr,false,MissingSchemaAction.Ignore );
  898. // Merge true,Ignore - Column
  899. Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS166");
  900. // Merge true,Ignore - changed values
  901. Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS167");
  902. // Merge true,Ignore - added values
  903. Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS168");
  904. // Merge true,Ignore - deleted row
  905. Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS169");
  906. #endregion
  907. #region "Merge(drArr,true,MissingSchemaAction.Add )"
  908. dsTarget1 = dsTarget.Copy();
  909. dsTarget1.Merge(drArr,true,MissingSchemaAction.Add );
  910. // Merge true,Ignore - Column
  911. Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS170");
  912. // Merge true,Ignore - changed values
  913. Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS171");
  914. // Merge true,Ignore - added values
  915. Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS172");
  916. // Merge true,Ignore - deleted row
  917. Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length >0, "DS173");
  918. #endregion
  919. #region "Merge(drArr,false,MissingSchemaAction.Add )"
  920. dsTarget1 = dsTarget.Copy();
  921. dsTarget1.Merge(drArr,false,MissingSchemaAction.Add );
  922. // Merge true,Ignore - Column
  923. Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS174");
  924. // Merge true,Ignore - changed values
  925. Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS175");
  926. // Merge true,Ignore - added values
  927. Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS176");
  928. // Merge true,Ignore - deleted row
  929. Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS177");
  930. #endregion
  931. #region "Merge(drArr,false/true,MissingSchemaAction.Error )"
  932. // dsTarget1 = dsTarget.Copy();
  933. // // Merge true,Error - Column
  934. // try {
  935. // dsTarget1.Merge(drArr,true,MissingSchemaAction.Error );
  936. // Assert.Fail("DS178: Merge Failed to throw InvalidOperationException");
  937. // }
  938. // catch (InvalidOperationException) {}
  939. // catch (AssertionException exc) {throw exc;}
  940. // catch (Exception exc)
  941. // {
  942. // Assert.Fail("DS179: Merge. Wrong exception type. Got:" + exc);
  943. // }
  944. //
  945. // // Merge false,Error - Column
  946. // try {
  947. // dsTarget1.Merge(drArr,false,MissingSchemaAction.Error );
  948. // Assert.Fail("DS180: Merge Failed to throw InvalidOperationException");
  949. // }
  950. // catch (InvalidOperationException) {}
  951. // catch (AssertionException exc) {throw exc;}
  952. // catch (Exception exc)
  953. // {
  954. // Assert.Fail("DS181: Merge. Wrong exception type. Got:" + exc);
  955. // }
  956. #endregion
  957. }
  958. [Test] public void Merge_ByDataSet()
  959. {
  960. //create source dataset
  961. DataSet ds = new DataSet();
  962. DataTable dt = DataProvider.CreateParentDataTable();
  963. dt.TableName = "Table1";
  964. ds.Tables.Add(dt.Copy());
  965. dt.TableName = "Table2";
  966. //add primary key
  967. dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
  968. ds.Tables.Add(dt.Copy());
  969. //create target dataset (copy of source dataset)
  970. DataSet dsTarget = ds.Copy();
  971. int iTable1RowsCount = dsTarget.Tables["Table1"].Rows.Count;
  972. //Source - add another table, don't exists on the target dataset
  973. ds.Tables.Add(new DataTable("SomeTable"));
  974. ds.Tables["SomeTable"].Columns.Add("Id");
  975. ds.Tables["SomeTable"].Rows.Add(new object[] {777});
  976. //Target - add another table, don't exists on the source dataset
  977. dsTarget.Tables.Add(new DataTable("SmallTable"));
  978. dsTarget.Tables["SmallTable"].Columns.Add("Id");
  979. dsTarget.Tables["SmallTable"].Rows.Add(new object[] {777});
  980. //update existing row
  981. ds.Tables["Table2"].Select("ParentId=1")[0][1] = "OldValue1";
  982. //add new row
  983. object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
  984. ds.Tables["Table2"].Rows.Add(arrAddedRow);
  985. //delete existing rows
  986. foreach (DataRow dr in ds.Tables["Table2"].Select("ParentId=2"))
  987. {
  988. dr.Delete();
  989. }
  990. // Merge - changed values
  991. dsTarget.Merge(ds);
  992. Assert.AreEqual("OldValue1", dsTarget.Tables["Table2"].Select("ParentId=1")[0][1] , "DS182");
  993. // Merge - added values
  994. Assert.AreEqual(arrAddedRow, dsTarget.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS183");
  995. // Merge - deleted row
  996. Assert.AreEqual(0, dsTarget.Tables["Table2"].Select("ParentId=2").Length , "DS184");
  997. //Table1 rows count should be double (no primary key)
  998. // Merge - Unchanged table 1
  999. Assert.AreEqual(iTable1RowsCount * 2, dsTarget.Tables["Table1"].Rows.Count , "DS185");
  1000. //SmallTable rows count should be the same
  1001. // Merge - Unchanged table 2
  1002. Assert.AreEqual(1, dsTarget.Tables["SmallTable"].Rows.Count , "DS186");
  1003. //SomeTable - new table
  1004. // Merge - new table
  1005. Assert.AreEqual(true, dsTarget.Tables["SomeTable"] != null, "DS187");
  1006. }
  1007. [Test] public void Merge_ByDataSetPreserve()
  1008. {
  1009. //create source dataset
  1010. DataSet ds = new DataSet();
  1011. DataTable dt = DataProvider.CreateParentDataTable();
  1012. dt.TableName = "Table1";
  1013. ds.Tables.Add(dt.Copy());
  1014. dt.TableName = "Table2";
  1015. //add primary key
  1016. dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
  1017. ds.Tables.Add(dt.Copy());
  1018. //create target dataset (copy of source dataset)
  1019. DataSet dsTarget1 = ds.Copy();
  1020. DataSet dsTarget2 = ds.Copy();
  1021. int iTable1RowsCount = dsTarget1.Tables["Table1"].Rows.Count;
  1022. //update existing row
  1023. string oldValue = ds.Tables["Table2"].Select("ParentId=1")[0][1].ToString();
  1024. ds.Tables["Table2"].Select("ParentId=1")[0][1] = "NewValue";
  1025. //add new row
  1026. object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
  1027. ds.Tables["Table2"].Rows.Add(arrAddedRow);
  1028. //delete existing rows
  1029. int iDeleteLength = dsTarget1.Tables["Table2"].Select("ParentId=2").Length;
  1030. foreach (DataRow dr in ds.Tables["Table2"].Select("ParentId=2"))
  1031. {
  1032. dr.Delete();
  1033. }
  1034. #region "Merge(ds,true)"
  1035. //only new added rows are merged (preserveChanges = true)
  1036. dsTarget1.Merge(ds,true);
  1037. // Merge - changed values
  1038. Assert.AreEqual(oldValue, dsTarget1.Tables["Table2"].Select("ParentId=1")[0][1] , "DS188");
  1039. // Merge - added values
  1040. Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS189");
  1041. // Merge - deleted row
  1042. Assert.AreEqual(iDeleteLength, dsTarget1.Tables["Table2"].Select("ParentId=2").Length , "DS190");
  1043. #endregion
  1044. #region "Merge(ds,false)"
  1045. //all changes are merged (preserveChanges = false)
  1046. dsTarget2.Merge(ds,false);
  1047. // Merge - changed values
  1048. Assert.AreEqual("NewValue", dsTarget2.Tables["Table2"].Select("ParentId=1")[0][1] , "DS191");
  1049. // Merge - added values
  1050. Assert.AreEqual(arrAddedRow, dsTarget2.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS192");
  1051. // Merge - deleted row
  1052. Assert.AreEqual(0, dsTarget2.Tables["Table2"].Select("ParentId=2").Length , "DS193");
  1053. #endregion
  1054. }
  1055. [Test] public void Merge_ByDataSetPreserveMissingSchemaAction()
  1056. {
  1057. //create source dataset
  1058. DataSet ds = new DataSet();
  1059. DataTable dt = DataProvider.CreateParentDataTable();
  1060. dt.TableName = "Table1";
  1061. dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
  1062. //add table to dataset
  1063. ds.Tables.Add(dt.Copy());
  1064. dt = ds.Tables[0];
  1065. //create target dataset (copy of source dataset)
  1066. DataSet dsTarget = ds.Copy();
  1067. //add new column (for checking MissingSchemaAction)
  1068. DataColumn dc = new DataColumn("NewColumn",typeof(float));
  1069. //make the column to be primary key
  1070. dt.Columns.Add(dc);
  1071. //add new table (for checking MissingSchemaAction)
  1072. ds.Tables.Add(new DataTable("NewTable"));
  1073. ds.Tables["NewTable"].Columns.Add("NewColumn1",typeof(int));
  1074. ds.Tables["NewTable"].Columns.Add("NewColumn2",typeof(long));
  1075. ds.Tables["NewTable"].Rows.Add(new object[] {1,2});
  1076. ds.Tables["NewTable"].Rows.Add(new object[] {3,4});
  1077. ds.Tables["NewTable"].PrimaryKey = new DataColumn[] {ds.Tables["NewTable"].Columns["NewColumn1"]};
  1078. #region "ds,false,MissingSchemaAction.Add)"
  1079. DataSet dsTarget1 = dsTarget.Copy();
  1080. dsTarget1.Merge(ds,false,MissingSchemaAction.Add);
  1081. // Merge MissingSchemaAction.Add - Column
  1082. Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS194");
  1083. // Merge MissingSchemaAction.Add - Table
  1084. Assert.AreEqual(true, dsTarget1.Tables.Contains("NewTable"), "DS195");
  1085. //failed, should be success by MSDN Library documentation
  1086. // // Merge MissingSchemaAction.Add - PrimaryKey
  1087. // Assert.AreEqual(0, dsTarget1.Tables["NewTable"].PrimaryKey.Length, "DS196");
  1088. #endregion
  1089. #region "ds,false,MissingSchemaAction.AddWithKey)"
  1090. //MissingSchemaAction.Add,MissingSchemaAction.AddWithKey - behave the same, checked only Add
  1091. // DataSet dsTarget2 = dsTarget.Copy();
  1092. // dsTarget2.Merge(ds,false,MissingSchemaAction.AddWithKey);
  1093. // // Merge MissingSchemaAction.AddWithKey - Column
  1094. // Assert.AreEqual(true, dsTarget2.Tables["Table1"].Columns.Contains("NewColumn"), "DS197");
  1095. //
  1096. // // Merge MissingSchemaAction.AddWithKey - Table
  1097. // Assert.AreEqual(true, dsTarget2.Tables.Contains("NewTable"), "DS198");
  1098. //
  1099. // // Merge MissingSchemaAction.AddWithKey - PrimaryKey
  1100. // Assert.AreEqual(dsTarget2.Tables["NewTable"].Columns["NewColumn1"], dsTarget2.Tables["NewTable"].PrimaryKey[0], "DS199");
  1101. #endregion
  1102. #region "ds,false,MissingSchemaAction.Error)"
  1103. //Error - throw System.Data.DataException, should throw InvalidOperationException
  1104. // DataSet dsTarget3 ;
  1105. // // Merge MissingSchemaAction.Error
  1106. // dsTarget3 = dsTarget.Copy();
  1107. // try {
  1108. // dsTarget3.Merge(ds,false,MissingSchemaAction.Error);
  1109. // Assert.Fail("DS200: Merge Failed to throw InvalidOperationException");
  1110. // }
  1111. // catch (InvalidOperationException) {}
  1112. // catch (AssertionException exc) {throw exc;}
  1113. // catch (Exception exc)
  1114. // {
  1115. // Assert.Fail("DS201: Merge. Wrong exception type. Got:" + exc);
  1116. // }
  1117. #endregion
  1118. #region "ds,false,MissingSchemaAction.Ignore )"
  1119. DataSet dsTarget4 = dsTarget.Copy();
  1120. dsTarget4.Merge(ds,false,MissingSchemaAction.Ignore );
  1121. // Merge MissingSchemaAction.Ignore - Column
  1122. Assert.AreEqual(false, dsTarget4.Tables["Table1"].Columns.Contains("NewColumn"), "DS202");
  1123. // Merge MissingSchemaAction.Ignore - Table
  1124. Assert.AreEqual(false, dsTarget4.Tables.Contains("NewTable"), "DS203");
  1125. #endregion
  1126. }
  1127. [Test] public void Merge_ByComplexDataSet()
  1128. {
  1129. //create source dataset
  1130. DataSet ds = new DataSet();
  1131. ds.Tables.Add(DataProvider.CreateParentDataTable());
  1132. ds.Tables.Add(DataProvider.CreateChildDataTable());
  1133. ds.Tables["Child"].TableName = "Child2";
  1134. ds.Tables.Add(DataProvider.CreateChildDataTable());
  1135. // Console.WriteLine(ds.Tables[0].TableName + ds.Tables[1].TableName + ds.Tables[2].TableName);
  1136. // Console.WriteLine(ds.Tables[2].Rows.Count.ToString());
  1137. //craete a target dataset to the merge operation
  1138. DataSet dsTarget = ds.Copy();
  1139. //craete a second target dataset to the merge operation
  1140. DataSet dsTarget1 = ds.Copy();
  1141. //------------------ make some changes in the second target dataset schema --------------------
  1142. //add primary key
  1143. dsTarget1.Tables["Parent"].PrimaryKey = new DataColumn[] {dsTarget1.Tables["Parent"].Columns["ParentId"]};
  1144. dsTarget1.Tables["Child"].PrimaryKey = new DataColumn[] {dsTarget1.Tables["Child"].Columns["ParentId"],dsTarget1.Tables["Child"].Columns["ChildId"]};
  1145. //add Foreign Key (different name)
  1146. dsTarget1.Tables["Child2"].Constraints.Add("Child2_FK_2",dsTarget1.Tables["Parent"].Columns["ParentId"],dsTarget1.Tables["Child2"].Columns["ParentId"]);
  1147. //add relation (different name)
  1148. //dsTarget1.Relations.Add("Parent_Child_1",dsTarget1.Tables["Parent"].Columns["ParentId"],dsTarget1.Tables["Child"].Columns["ParentId"]);
  1149. //------------------ make some changes in the source dataset schema --------------------
  1150. //add primary key
  1151. ds.Tables["Parent"].PrimaryKey = new DataColumn[] {ds.Tables["Parent"].Columns["ParentId"]};
  1152. ds.Tables["Child"].PrimaryKey = new DataColumn[] {ds.Tables["Child"].Columns["ParentId"],ds.Tables["Child"].Columns["ChildId"]};
  1153. //unique column
  1154. ds.Tables["Parent"].Columns["String2"].Unique = true; //will not be merged
  1155. //add Foreign Key
  1156. ds.Tables["Child2"].Constraints.Add("Child2_FK",ds.Tables["Parent"].Columns["ParentId"],ds.Tables["Child2"].Columns["ParentId"]);
  1157. //add relation
  1158. ds.Relations.Add("Parent_Child",ds.Tables["Parent"].Columns["ParentId"],ds.Tables["Child"].Columns["ParentId"]);
  1159. //add allow null constraint
  1160. ds.Tables["Parent"].Columns["ParentBool"].AllowDBNull = false; //will not be merged
  1161. //add Indentity column
  1162. ds.Tables["Parent"].Columns.Add("Indentity",typeof(int));
  1163. ds.Tables["Parent"].Columns["Indentity"].AutoIncrement = true;
  1164. ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep = 2;
  1165. //modify default value
  1166. ds.Tables["Child"].Columns["String1"].DefaultValue = "Default"; //will not be merged
  1167. //remove column
  1168. ds.Tables["Child"].Columns.Remove("String2"); //will not be merged
  1169. //-------------------- begin to check ----------------------------------------------
  1170. // merge 1 - make sure the merge method invoked without exceptions
  1171. dsTarget.Merge(ds);
  1172. Assert.AreEqual("Success", "Success", "DS204");
  1173. CompareResults_1("merge 1",ds,dsTarget);
  1174. //merge again,
  1175. // merge 2 - make sure the merge method invoked without exceptions
  1176. dsTarget.Merge(ds);
  1177. Assert.AreEqual("Success", "Success", "DS205");
  1178. CompareResults_1("merge 2",ds,dsTarget);
  1179. // merge second dataset - make sure the merge method invoked without exceptions
  1180. dsTarget1.Merge(ds);
  1181. Assert.AreEqual("Success", "Success", "DS206");
  1182. CompareResults_2("merge 3",ds,dsTarget1);
  1183. }
  1184. [Test]
  1185. public void Merge_RelationWithoutConstraints ()
  1186. {
  1187. DataSet ds = new DataSet ();
  1188. DataTable table1 = ds.Tables.Add ("table1");
  1189. DataTable table2 = ds.Tables.Add ("table2");
  1190. DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
  1191. DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
  1192. DataSet ds1 = ds.Copy ();
  1193. DataRelation rel = ds1.Relations.Add ("rel1", ds1.Tables[0].Columns[0],
  1194. ds1.Tables [1].Columns [0], false);
  1195. ds.Merge (ds1);
  1196. Assert.AreEqual (1, ds.Relations.Count , "#1");
  1197. Assert.AreEqual (0, ds.Tables [0].Constraints.Count , "#2");
  1198. Assert.AreEqual (0, ds.Tables [1].Constraints.Count , "#2");
  1199. }
  1200. [Test]
  1201. public void Merge_DuplicateConstraints ()
  1202. {
  1203. DataSet ds = new DataSet ();
  1204. DataTable table1 = ds.Tables.Add ("table1");
  1205. DataTable table2 = ds.Tables.Add ("table2");
  1206. DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
  1207. DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
  1208. DataSet ds1 = ds.Copy ();
  1209. DataRelation rel = ds.Relations.Add ("rel1", pcol, ccol);
  1210. ds1.Tables [1].Constraints.Add ("fk", ds1.Tables [0].Columns [0], ds1.Tables [1].Columns [0]);
  1211. // No Exceptions shud be thrown
  1212. ds.Merge (ds1);
  1213. Assert.AreEqual (1, table2.Constraints.Count, "#1 Constraints shudnt be duplicated");
  1214. }
  1215. [Test]
  1216. public void Merge_DuplicateConstraints_1 ()
  1217. {
  1218. DataSet ds = new DataSet ();
  1219. DataTable table1 = ds.Tables.Add ("table1");
  1220. DataTable table2 = ds.Tables.Add ("table2");
  1221. DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
  1222. DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
  1223. DataColumn pcol1 = table1.Columns.Add ("col2", typeof (int));
  1224. DataColumn ccol1 = table2.Columns.Add ("col2", typeof (int));
  1225. DataSet ds1 = ds.Copy ();
  1226. table2.Constraints.Add ("fk", pcol, ccol);
  1227. ds1.Tables [1].Constraints.Add ("fk", ds1.Tables [0].Columns ["col2"], ds1.Tables [1].Columns ["col2"]);
  1228. // No Exceptions shud be thrown
  1229. ds.Merge (ds1);
  1230. Assert.AreEqual (2, table2.Constraints.Count, "#1 fk constraint shud be merged");
  1231. Assert.AreEqual ("Constraint1", table2.Constraints [1].ConstraintName, "#2 constraint name shud be changed");
  1232. }
  1233. [Test]
  1234. public void CopyClone_RelationWithoutConstraints ()
  1235. {
  1236. DataSet ds = new DataSet ();
  1237. DataTable table1 = ds.Tables.Add ("table1");
  1238. DataTable table2 = ds.Tables.Add ("table2");
  1239. DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
  1240. DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
  1241. DataRelation rel = ds.Relations.Add ("rel1", pcol, ccol, false);
  1242. DataSet ds1 = ds.Copy ();
  1243. DataSet ds2 = ds.Clone ();
  1244. Assert.AreEqual (1, ds1.Relations.Count, "#1");
  1245. Assert.AreEqual (1, ds2.Relations.Count, "#2");
  1246. Assert.AreEqual (0, ds1.Tables [0].Constraints.Count, "#3");
  1247. Assert.AreEqual (0, ds1.Tables [1].Constraints.Count, "#4");
  1248. Assert.AreEqual (0, ds2.Tables [0].Constraints.Count, "#5");
  1249. Assert.AreEqual (0, ds2.Tables [1].Constraints.Count, "#6");
  1250. }
  1251. [Test]
  1252. [ExpectedException (typeof (DataException))]
  1253. public void Merge_MissingEventHandler ()
  1254. {
  1255. DataSet ds = new DataSet ();
  1256. DataTable table1 = ds.Tables.Add ("table1");
  1257. DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
  1258. DataColumn pcol1 = table1.Columns.Add ("col2", typeof (int));
  1259. DataSet ds1 = ds.Copy ();
  1260. table1.PrimaryKey = new DataColumn[] {pcol};
  1261. ds1.Tables [0].PrimaryKey = new DataColumn[] {ds1.Tables [0].Columns [1]};
  1262. // Exception shud be raised when handler is not set for MergeFailed Event
  1263. ds1.Merge (ds);
  1264. }
  1265. [Test]
  1266. [ExpectedException (typeof(DataException))]
  1267. public void Merge_MissingColumn ()
  1268. {
  1269. DataSet ds = new DataSet ();
  1270. DataTable table1 = ds.Tables.Add ("table1");
  1271. DataTable table2 = ds.Tables.Add ("table2");
  1272. table1.Columns.Add ("col1", typeof (int));
  1273. table2.Columns.Add ("col1", typeof (int));
  1274. DataSet ds1 = ds.Copy ();
  1275. ds1.Tables [0].Columns.Add ("col2");
  1276. ds.Merge (ds1, true, MissingSchemaAction.Error);
  1277. }
  1278. [Test]
  1279. public void Merge_MissingConstraint ()
  1280. {
  1281. DataSet ds = new DataSet ();
  1282. DataTable table1 = ds.Tables.Add ("table1");
  1283. DataTable table2 = ds.Tables.Add ("table2");
  1284. table1.Columns.Add ("col1", typeof (int));
  1285. table2.Columns.Add ("col1", typeof (int));
  1286. try {
  1287. DataSet ds1 = ds.Copy ();
  1288. DataSet ds2 = ds.Copy ();
  1289. ds2.Tables [0].Constraints.Add ("uc", ds2.Tables [0].Columns [0], false);
  1290. ds1.Merge (ds2, true, MissingSchemaAction.Error);
  1291. Assert.Fail ("#1 If uniqueconstraint is missing, exception shud be thrown");
  1292. }catch (DataException e) {
  1293. }
  1294. try {
  1295. DataSet ds1 = ds.Copy ();
  1296. DataSet ds2 = ds.Copy ();
  1297. ds2.Tables [0].Constraints.Add ("fk", ds2.Tables [0].Columns [0], ds2.Tables[1].Columns [0]);
  1298. ds1.Tables [0].Constraints.Add ("uc", ds1.Tables [0].Columns [0],false);
  1299. ds1.Merge (ds2, true, MissingSchemaAction.Error);
  1300. Assert.Fail ("#2 If foreignkeyconstraint is missing, exception shud be thrown");
  1301. }catch (DataException e) {
  1302. }
  1303. try {
  1304. DataSet ds1 = ds.Copy ();
  1305. DataSet ds2 = ds.Copy ();
  1306. ds2.Relations.Add ("rel", ds2.Tables [0].Columns [0], ds2.Tables[1].Columns [0], false);
  1307. ds1.Merge (ds2, true, MissingSchemaAction.Error);
  1308. Assert.Fail ("#2 If datarelation is missing, exception shud be thrown");
  1309. }catch (ArgumentException e) {
  1310. }
  1311. }
  1312. [Test]
  1313. [ExpectedException (typeof (DataException))]
  1314. public void Merge_PrimaryKeys_IncorrectOrder ()
  1315. {
  1316. DataSet ds = new DataSet ();
  1317. DataTable table1 = ds.Tables.Add ("table1");
  1318. DataTable table2 = ds.Tables.Add ("table2");
  1319. DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
  1320. DataColumn pcol1 = table1.Columns.Add ("col2", typeof (int));
  1321. DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
  1322. DataSet ds1 = ds.Copy ();
  1323. table1.PrimaryKey = new DataColumn[] {pcol,pcol1};
  1324. ds1.Tables [0].PrimaryKey = new DataColumn [] {ds1.Tables[0].Columns [1], ds1.Tables [0].Columns [0]};
  1325. // Though the key columns are the same, if the order is incorrect
  1326. // Exception must be raised
  1327. ds1.Merge (ds);
  1328. }
  1329. void CompareResults_1(string Msg,DataSet ds, DataSet dsTarget)
  1330. {
  1331. // check Parent Primary key length
  1332. Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey.Length , ds.Tables["Parent"].PrimaryKey.Length , "DS207");
  1333. // check Child Primary key length
  1334. Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey.Length , ds.Tables["Child"].PrimaryKey.Length , "DS208");
  1335. // check Parent Primary key columns
  1336. Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey[0].ColumnName, ds.Tables["Parent"].PrimaryKey[0].ColumnName , "DS209");
  1337. // check Child Primary key columns[0]
  1338. Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[0].ColumnName, ds.Tables["Child"].PrimaryKey[0].ColumnName , "DS210");
  1339. // check Child Primary key columns[1]
  1340. Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[1].ColumnName, ds.Tables["Child"].PrimaryKey[1].ColumnName , "DS211");
  1341. // check Parent Unique columns
  1342. Assert.AreEqual(dsTarget.Tables["Parent"].Columns["String2"].Unique, ds.Tables["Parent"].Columns["String2"].Unique , "DS212");
  1343. // check Child2 Foreign Key name
  1344. Assert.AreEqual(dsTarget.Tables["Child2"].Constraints[0].ConstraintName , ds.Tables["Child2"].Constraints[0].ConstraintName , "DS213");
  1345. // check dataset relation count
  1346. Assert.AreEqual(dsTarget.Relations.Count , ds.Relations.Count , "DS214");
  1347. // check dataset relation - Parent column
  1348. Assert.AreEqual(dsTarget.Relations[0].ParentColumns[0].ColumnName , ds.Relations[0].ParentColumns[0].ColumnName , "DS215");
  1349. // check dataset relation - Child column
  1350. Assert.AreEqual(dsTarget.Relations[0].ChildColumns[0].ColumnName , ds.Relations[0].ChildColumns[0].ColumnName , "DS216");
  1351. // check allow null constraint
  1352. Assert.AreEqual(true, dsTarget.Tables["Parent"].Columns["ParentBool"].AllowDBNull, "DS217");
  1353. // check Indentity column
  1354. Assert.AreEqual(dsTarget.Tables["Parent"].Columns.Contains("Indentity"), ds.Tables["Parent"].Columns.Contains("Indentity"), "DS218");
  1355. // check Indentity column - AutoIncrementStep
  1356. Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, "DS219");
  1357. // check Indentity column - AutoIncrement
  1358. Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrement, ds.Tables["Parent"].Columns["Indentity"].AutoIncrement, "DS220");
  1359. // check Indentity column - DefaultValue
  1360. Assert.AreEqual(true, dsTarget.Tables["Child"].Columns["String1"].DefaultValue == DBNull.Value , "DS221");
  1361. // check remove colum
  1362. Assert.AreEqual(true, dsTarget.Tables["Child"].Columns.Contains("String2"), "DS222");
  1363. }
  1364. void CompareResults_2(string Msg,DataSet ds, DataSet dsTarget)
  1365. {
  1366. // check Parent Primary key length
  1367. Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey.Length , ds.Tables["Parent"].PrimaryKey.Length , "DS223");
  1368. // check Child Primary key length
  1369. Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey.Length , ds.Tables["Child"].PrimaryKey.Length , "DS224");
  1370. // check Parent Primary key columns
  1371. Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey[0].ColumnName, ds.Tables["Parent"].PrimaryKey[0].ColumnName , "DS225");
  1372. // check Child Primary key columns[0]
  1373. Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[0].ColumnName, ds.Tables["Child"].PrimaryKey[0].ColumnName , "DS226");
  1374. // check Child Primary key columns[1]
  1375. Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[1].ColumnName, ds.Tables["Child"].PrimaryKey[1].ColumnName , "DS227");
  1376. // check Parent Unique columns
  1377. Assert.AreEqual(dsTarget.Tables["Parent"].Columns["String2"].Unique, ds.Tables["Parent"].Columns["String2"].Unique , "DS228");
  1378. // check Child2 Foreign Key name
  1379. Assert.AreEqual("Child2_FK_2" , dsTarget.Tables["Child2"].Constraints[0].ConstraintName, "DS229");
  1380. // check dataset relation count
  1381. Assert.AreEqual(dsTarget.Relations.Count , ds.Relations.Count , "DS230");
  1382. // check dataset relation - Parent column
  1383. Assert.AreEqual(dsTarget.Relations[0].ParentColumns[0].ColumnName , ds.Relations[0].ParentColumns[0].ColumnName , "DS231");
  1384. // check dataset relation - Child column
  1385. Assert.AreEqual(dsTarget.Relations[0].ChildColumns[0].ColumnName , ds.Relations[0].ChildColumns[0].ColumnName , "DS232");
  1386. // check allow null constraint
  1387. Assert.AreEqual(true, dsTarget.Tables["Parent"].Columns["ParentBool"].AllowDBNull, "DS233");
  1388. // check Indentity column
  1389. Assert.AreEqual(dsTarget.Tables["Parent"].Columns.Contains("Indentity"), ds.Tables["Parent"].Columns.Contains("Indentity"), "DS234");
  1390. // check Indentity column - AutoIncrementStep
  1391. Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, "DS235");
  1392. // check Indentity column - AutoIncrement
  1393. Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrement, ds.Tables["Parent"].Columns["Indentity"].AutoIncrement, "DS236");
  1394. // check Indentity column - DefaultValue
  1395. Assert.AreEqual(true, dsTarget.Tables["Child"].Columns["String1"].DefaultValue == DBNull.Value , "DS237");
  1396. // check remove colum
  1397. Assert.AreEqual(true, dsTarget.Tables["Child"].Columns.Contains("String2"), "DS238");
  1398. //TestCase for bug #3168
  1399. // Check Relation.Nested value, TestCase for bug #3168
  1400. DataSet orig = new DataSet();
  1401. DataTable parent = orig.Tables.Add("Parent");
  1402. parent.Columns.Add("Id", typeof(int));
  1403. parent.Columns.Add("col1", typeof(string));
  1404. parent.Rows.Add(new object[] {0, "aaa"});
  1405. DataTable child = orig.Tables.Add("Child");
  1406. child.Columns.Add("ParentId", typeof(int));
  1407. child.Columns.Add("col1", typeof(string));
  1408. child.Rows.Add(new object[] {0, "bbb"});
  1409. orig.Relations.Add("Parent_Child", parent.Columns["Id"], child.Columns["ParentId"]);
  1410. orig.Relations["Parent_Child"].Nested = true;
  1411. DataSet merged = new DataSet();
  1412. merged.Merge(orig);
  1413. Assert.AreEqual(orig.Relations["Parent_Child"].Nested, merged.Relations["Parent_Child"].Nested, "DS239");
  1414. }
  1415. [Test] public void Merge_ByDataTable()
  1416. {
  1417. //create source dataset
  1418. DataSet ds = new DataSet();
  1419. //create datatable
  1420. DataTable dt = DataProvider.CreateParentDataTable();
  1421. dt.TableName = "Table1";
  1422. //add a copy of the datatable to the dataset
  1423. ds.Tables.Add(dt.Copy());
  1424. dt.TableName = "Table2";
  1425. //add primary key
  1426. dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
  1427. ds.Tables.Add(dt.Copy());
  1428. //now the dataset hase two tables
  1429. //create target dataset (copy of source dataset)
  1430. DataSet dsTarget = ds.Copy();
  1431. dt = ds.Tables["Table2"];
  1432. //update existing row
  1433. dt.Select("ParentId=1")[0][1] = "OldValue1";
  1434. //add new row
  1435. object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
  1436. dt.Rows.Add(arrAddedRow);
  1437. //delete existing rows
  1438. foreach (DataRow dr in dt.Select("ParentId=2"))
  1439. {
  1440. dr.Delete();
  1441. }
  1442. // Merge - changed values
  1443. dsTarget.Merge(dt);
  1444. Assert.AreEqual("OldValue1", dsTarget.Tables["Table2"].Select("ParentId=1")[0][1] , "DS240");
  1445. // Merge - added values
  1446. Assert.AreEqual(arrAddedRow, dsTarget.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS241");
  1447. // Merge - deleted row
  1448. Assert.AreEqual(0, dsTarget.Tables["Table2"].Select("ParentId=2").Length , "DS242");
  1449. //test case added due to a reported bug from infogate
  1450. //when merging a DataTable with TableName=null, GH throw null reference exception.
  1451. ds = new DataSet();
  1452. dt = new DataTable();
  1453. dt.Columns.Add("Col1");
  1454. dt.Rows.Add(new object[] {1});
  1455. // Merge - add a table with no name
  1456. ds.Merge(dt);
  1457. Assert.AreEqual(1, ds.Tables.Count, "DS243");
  1458. // Merge - add a table with no name - check Rows.Count
  1459. Assert.AreEqual(dt.Rows.Count , ds.Tables[0].Rows.Count , "DS244");
  1460. }
  1461. [Test] public void Merge_ByDataTablePreserveMissingSchemaAction()
  1462. {
  1463. DataTable dt = DataProvider.CreateParentDataTable();
  1464. dt.TableName = "Table1";
  1465. dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
  1466. //create target dataset (copy of source dataset)
  1467. DataSet dsTarget = new DataSet();
  1468. dsTarget.Tables.Add(dt.Copy());
  1469. //add new column (for checking MissingSchemaAction)
  1470. DataColumn dc = new DataColumn("NewColumn",typeof(float));
  1471. dt.Columns.Add(dc);
  1472. //Update row
  1473. string OldValue = dt.Select("ParentId=1")[0][1].ToString();
  1474. dt.Select("ParentId=1")[0][1] = "NewValue";
  1475. //delete rows
  1476. dt.Select("ParentId=2")[0].Delete();
  1477. //add row
  1478. object[] arrAddedRow = new object[] {99,"NewRowValue1","NewRowValue2",new DateTime(0),0.5,true};
  1479. dt.Rows.Add(arrAddedRow);
  1480. #region "Merge(dt,true,MissingSchemaAction.Ignore )"
  1481. DataSet dsTarget1 = dsTarget.Copy();
  1482. dsTarget1.Merge(dt,true,MissingSchemaAction.Ignore );
  1483. // Merge true,Ignore - Column
  1484. Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS245");
  1485. // Merge true,Ignore - changed values
  1486. Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS246");
  1487. // Merge true,Ignore - added values
  1488. Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table1"].Select("ParentId=99")[0].ItemArray , "DS247");
  1489. // Merge true,Ignore - deleted row
  1490. Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS248");
  1491. #endregion
  1492. #region "Merge(dt,false,MissingSchemaAction.Ignore )"
  1493. dsTarget1 = dsTarget.Copy();
  1494. dsTarget1.Merge(dt,false,MissingSchemaAction.Ignore );
  1495. // Merge true,Ignore - Column
  1496. Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS249");
  1497. // Merge true,Ignore - changed values
  1498. Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS250");
  1499. // Merge true,Ignore - added values
  1500. Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table1"].Select("ParentId=99")[0].ItemArray , "DS251");
  1501. // Merge true,Ignore - deleted row
  1502. Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS252");
  1503. #endregion
  1504. #region "Merge(dt,true,MissingSchemaAction.Add )"
  1505. dsTarget1 = dsTarget.Copy();
  1506. dsTarget1.Merge(dt,true,MissingSchemaAction.Add );
  1507. // Merge true,Add - Column
  1508. Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS253");
  1509. // Merge true,Add - changed values
  1510. Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS254");
  1511. // Merge true,Add - added values
  1512. Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS255");
  1513. // Merge true,Add - deleted row
  1514. Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS256");
  1515. #endregion
  1516. #region "Merge(dt,false,MissingSchemaAction.Add )"
  1517. dsTarget1 = dsTarget.Copy();
  1518. dsTarget1.Merge(dt,false,MissingSchemaAction.Add );
  1519. // Merge true,Add - Column
  1520. Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS257");
  1521. // Merge true,Add - changed values
  1522. Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS258");
  1523. // Merge true,Add - added values
  1524. Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS259");
  1525. // Merge true,Add - deleted row
  1526. Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS260");
  1527. #endregion
  1528. #region "Merge(dt,false/true,MissingSchemaAction.Error )"
  1529. // dsTarget1 = dsTarget.Copy();
  1530. // // Merge true,Error - Column
  1531. // try {
  1532. // dsTarget1.Merge(dt,true,MissingSchemaAction.Error );
  1533. // Assert.Fail("DS261: Merge Failed to throw InvalidOperationException");
  1534. // }
  1535. // catch (InvalidOperationException) {}
  1536. // catch (AssertionException exc) {throw exc;}
  1537. // catch (Exception exc)
  1538. // {
  1539. // Assert.Fail("DS262: Merge. Wrong exception type. Got:" + exc);
  1540. // }
  1541. //
  1542. // // Merge false,Error - Column
  1543. // try {
  1544. // dsTarget1.Merge(dt,false,MissingSchemaAction.Error );
  1545. // Assert.Fail("DS263: Merge Failed to throw InvalidOperationException");
  1546. // }
  1547. // catch (InvalidOperationException) {}
  1548. // catch (AssertionException exc) {throw exc;}
  1549. // catch (Exception exc)
  1550. // {
  1551. // Assert.Fail("DS264: Merge. Wrong exception type. Got:" + exc);
  1552. // }
  1553. #endregion
  1554. }
  1555. [Test] public void Namespace()
  1556. {
  1557. DataSet ds = new DataSet();
  1558. // Checking Namespace default
  1559. Assert.AreEqual(String.Empty, ds.Namespace, "DS265");
  1560. // Checking Namespace set/get
  1561. String s = "MyNamespace";
  1562. ds.Namespace=s;
  1563. Assert.AreEqual(s, ds.Namespace, "DS266");
  1564. }
  1565. [Test] public void Prefix()
  1566. {
  1567. DataSet ds = new DataSet();
  1568. // Checking Prefix default
  1569. Assert.AreEqual(String.Empty, ds.Prefix , "DS267");
  1570. // Checking Prefix set/get
  1571. String s = "MyPrefix";
  1572. ds.Prefix=s;
  1573. Assert.AreEqual(s, ds.Prefix, "DS268");
  1574. }
  1575. [Test] public void ReadXmlSchema_ByStream()
  1576. {
  1577. DataSet ds1 = new DataSet();
  1578. ds1.Tables.Add(DataProvider.CreateParentDataTable());
  1579. ds1.Tables.Add(DataProvider.CreateChildDataTable());
  1580. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  1581. //write xml schema only
  1582. ds1.WriteXmlSchema(ms);
  1583. System.IO.MemoryStream ms1 = new System.IO.MemoryStream(ms.GetBuffer());
  1584. //copy schema
  1585. DataSet ds2 = new DataSet();
  1586. ds2.ReadXmlSchema(ms1);
  1587. //check xml schema
  1588. // ReadXmlSchema - Tables count
  1589. Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS269");
  1590. // ReadXmlSchema - Tables 0 Col count
  1591. Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS270");
  1592. // ReadXmlSchema - Tables 1 Col count
  1593. Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS271");
  1594. //check some colummns types
  1595. // ReadXmlSchema - Tables 0 Col type
  1596. Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS272");
  1597. // ReadXmlSchema - Tables 1 Col type
  1598. Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS273");
  1599. //check that no data exists
  1600. // ReadXmlSchema - Table 1 row count
  1601. Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS274");
  1602. // ReadXmlSchema - Table 2 row count
  1603. Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS275");
  1604. }
  1605. [Test] public void ReadXmlSchema_ByFileName()
  1606. {
  1607. string sTempFileName = "tmpDataSet_ReadWriteXml_43899.xml" ;
  1608. DataSet ds1 = new DataSet();
  1609. ds1.Tables.Add(DataProvider.CreateParentDataTable());
  1610. ds1.Tables.Add(DataProvider.CreateChildDataTable());
  1611. //write xml file, schema only
  1612. ds1.WriteXmlSchema(sTempFileName);
  1613. //copy both data and schema
  1614. DataSet ds2 = new DataSet();
  1615. ds2.ReadXmlSchema(sTempFileName);
  1616. //check xml schema
  1617. // ReadXmlSchema - Tables count
  1618. Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS276");
  1619. // ReadXmlSchema - Tables 0 Col count
  1620. Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS277");
  1621. // ReadXmlSchema - Tables 1 Col count
  1622. Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS278");
  1623. //check some colummns types
  1624. // ReadXmlSchema - Tables 0 Col type
  1625. Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS279");
  1626. // ReadXmlSchema - Tables 1 Col type
  1627. Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS280");
  1628. //check that no data exists
  1629. // ReadXmlSchema - Table 1 row count
  1630. Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS281");
  1631. // ReadXmlSchema - Table 2 row count
  1632. Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS282");
  1633. //try to delete the file
  1634. System.IO.File.Delete(sTempFileName);
  1635. }
  1636. [Test] public void ReadXmlSchema_ByTextReader()
  1637. {
  1638. DataSet ds1 = new DataSet();
  1639. ds1.Tables.Add(DataProvider.CreateParentDataTable());
  1640. ds1.Tables.Add(DataProvider.CreateChildDataTable());
  1641. System.IO.StringWriter sw = new System.IO.StringWriter();
  1642. //write xml file, schema only
  1643. ds1.WriteXmlSchema(sw);
  1644. System.IO.StringReader sr = new System.IO.StringReader(sw.GetStringBuilder().ToString());
  1645. //copy both data and schema
  1646. DataSet ds2 = new DataSet();
  1647. ds2.ReadXmlSchema(sr);
  1648. //check xml schema
  1649. // ReadXmlSchema - Tables count
  1650. Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS283");
  1651. // ReadXmlSchema - Tables 0 Col count
  1652. Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS284");
  1653. // ReadXmlSchema - Tables 1 Col count
  1654. Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS285");
  1655. //check some colummns types
  1656. // ReadXmlSchema - Tables 0 Col type
  1657. Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS286");
  1658. // ReadXmlSchema - Tables 1 Col type
  1659. Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS287");
  1660. //check that no data exists
  1661. // ReadXmlSchema - Table 1 row count
  1662. Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS288");
  1663. // ReadXmlSchema - Table 2 row count
  1664. Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS289");
  1665. }
  1666. [Test] public void ReadXmlSchema_ByXmlReader()
  1667. {
  1668. DataSet ds1 = new DataSet();
  1669. ds1.Tables.Add(DataProvider.CreateParentDataTable());
  1670. ds1.Tables.Add(DataProvider.CreateChildDataTable());
  1671. System.IO.StringWriter sw = new System.IO.StringWriter();
  1672. System.Xml.XmlTextWriter xmlTW = new System.Xml.XmlTextWriter(sw);
  1673. //write xml file, schema only
  1674. ds1.WriteXmlSchema(xmlTW);
  1675. xmlTW.Flush();
  1676. System.IO.StringReader sr = new System.IO.StringReader(sw.ToString());
  1677. System.Xml.XmlTextReader xmlTR = new System.Xml.XmlTextReader(sr);
  1678. //copy both data and schema
  1679. DataSet ds2 = new DataSet();
  1680. ds2.ReadXmlSchema(xmlTR);
  1681. //check xml schema
  1682. // ReadXmlSchema - Tables count
  1683. Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS290");
  1684. // ReadXmlSchema - Tables 0 Col count
  1685. Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS291");
  1686. // ReadXmlSchema - Tables 1 Col count
  1687. Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS292");
  1688. //check some colummns types
  1689. // ReadXmlSchema - Tables 0 Col type
  1690. Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS293");
  1691. // ReadXmlSchema - Tables 1 Col type
  1692. Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS294");
  1693. //check that no data exists
  1694. // ReadXmlSchema - Table 1 row count
  1695. Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS295");
  1696. // ReadXmlSchema - Table 2 row count
  1697. Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS296");
  1698. }
  1699. [Test]
  1700. public void ReadXml_Strg()
  1701. {
  1702. string sTempFileName = "tmpDataSet_ReadWriteXml_43894.xml" ;
  1703. DataSet ds1 = new DataSet();
  1704. ds1.Tables.Add(DataProvider.CreateParentDataTable());
  1705. ds1.Tables.Add(DataProvider.CreateChildDataTable());
  1706. //add data to check GH bug of DataSet.ReadXml of empty strings
  1707. ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
  1708. ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
  1709. ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
  1710. //write xml file, data only
  1711. ds1.WriteXml(sTempFileName);
  1712. //copy both data and schema
  1713. DataSet ds2 = ds1.Copy();
  1714. //clear the data
  1715. ds2.Clear();
  1716. ds2.ReadXml(sTempFileName);
  1717. //check xml data
  1718. // ReadXml - Tables count
  1719. Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS297");
  1720. // ReadXml - Table 1 row count
  1721. Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS298");
  1722. // ReadXml - Table 2 row count
  1723. Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS299");
  1724. //try to delete the file
  1725. System.IO.File.Delete(sTempFileName);
  1726. }
  1727. [Test]
  1728. public void ReadXml_Strm()
  1729. {
  1730. DataSet ds1 = new DataSet();
  1731. ds1.Tables.Add(DataProvider.CreateParentDataTable());
  1732. ds1.Tables.Add(DataProvider.CreateChildDataTable());
  1733. //add data to check GH bug of DataSet.ReadXml of empty strings
  1734. ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
  1735. ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
  1736. ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
  1737. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  1738. //write xml file, data only
  1739. ds1.WriteXml(ms);
  1740. //copy both data and schema
  1741. DataSet ds2 = ds1.Copy();
  1742. //clear the data
  1743. ds2.Clear();
  1744. ms.Position=0;
  1745. ds2.ReadXml(ms);
  1746. //check xml data
  1747. // ReadXml - Tables count
  1748. Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS300");
  1749. // ReadXml - Table 1 row count
  1750. Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS301");
  1751. // ReadXml - Table 2 row count
  1752. Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS302");
  1753. ms.Close();
  1754. }
  1755. [Test] public void ReadXml_Strm2()
  1756. {
  1757. string input = string.Empty;
  1758. System.IO.StringReader sr;
  1759. DataSet ds = new DataSet();
  1760. input += "<?xml version=\"1.0\"?>";
  1761. input += "<Stock name=\"MSFT\">";
  1762. input += " <Company name=\"Microsoft Corp.\"/>";
  1763. input += " <Price type=\"high\">";
  1764. input += " <Value>10.0</Value>";
  1765. input += " <Date>01/20/2000</Date>";
  1766. input += " </Price>";
  1767. input += " <Price type=\"low\">";
  1768. input += " <Value>1.0</Value>";
  1769. input += " <Date>03/21/2002</Date>";
  1770. input += " </Price>";
  1771. input += " <Price type=\"current\">";
  1772. input += " <Value>3.0</Value>";
  1773. input += " <Date>TODAY</Date>";
  1774. input += " </Price>";
  1775. input += "</Stock>";
  1776. sr = new System.IO.StringReader(input);
  1777. ds.ReadXml(sr);
  1778. // Relation Count
  1779. Assert.AreEqual(2 , ds.Relations.Count , "DS303");
  1780. // RelationName 1
  1781. Assert.AreEqual("Stock_Company" , ds.Relations[0].RelationName , "DS304");
  1782. // RelationName 2
  1783. Assert.AreEqual("Stock_Price" , ds.Relations[1].RelationName , "DS305");
  1784. // Tables count
  1785. Assert.AreEqual(3, ds.Tables.Count , "DS306");
  1786. // Tables[0] ChildRelations count
  1787. Assert.AreEqual(2, ds.Tables[0].ChildRelations.Count , "DS307");
  1788. // Tables[0] ChildRelations[0] name
  1789. Assert.AreEqual("Stock_Company", ds.Tables[0].ChildRelations[0].RelationName , "DS308");
  1790. // Tables[0] ChildRelations[1] name
  1791. Assert.AreEqual("Stock_Price", ds.Tables[0].ChildRelations[1].RelationName , "DS309");
  1792. // Tables[1] ChildRelations count
  1793. Assert.AreEqual(0, ds.Tables[1].ChildRelations.Count , "DS310");
  1794. // Tables[2] ChildRelations count
  1795. Assert.AreEqual(0, ds.Tables[2].ChildRelations.Count , "DS311");
  1796. // Tables[0] ParentRelations count
  1797. Assert.AreEqual(0, ds.Tables[0].ParentRelations.Count , "DS312");
  1798. // Tables[1] ParentRelations count
  1799. Assert.AreEqual(1, ds.Tables[1].ParentRelations.Count , "DS313");
  1800. // Tables[1] ParentRelations[0] name
  1801. Assert.AreEqual("Stock_Company", ds.Tables[1].ParentRelations[0].RelationName , "DS314");
  1802. // Tables[2] ParentRelations count
  1803. Assert.AreEqual(1, ds.Tables[2].ParentRelations.Count , "DS315");
  1804. // Tables[2] ParentRelations[0] name
  1805. Assert.AreEqual("Stock_Price", ds.Tables[2].ParentRelations[0].RelationName , "DS316");
  1806. }
  1807. [Test] public void ReadXml_Strm3()
  1808. {
  1809. DataSet ds = new DataSet("TestDataSet");
  1810. string input = string.Empty;
  1811. System.IO.StringReader sr;
  1812. input += "<?xml version=\"1.0\" standalone=\"yes\"?>";
  1813. input += "<Stocks><Stock name=\"MSFT\"><Company name=\"Microsoft Corp.\" /><Price type=\"high\"><Value>10.0</Value>";
  1814. input += "<Date>01/20/2000</Date></Price><Price type=\"low\"><Value>10</Value><Date>03/21/2002</Date></Price>";
  1815. input += "<Price type=\"current\"><Value>3.0</Value><Date>TODAY</Date></Price></Stock><Stock name=\"GE\">";
  1816. input += "<Company name=\"General Electric\" /><Price type=\"high\"><Value>22.23</Value><Date>02/12/2001</Date></Price>";
  1817. input += "<Price type=\"low\"><Value>1.97</Value><Date>04/20/2003</Date></Price><Price type=\"current\"><Value>3.0</Value>";
  1818. input += "<Date>TODAY</Date></Price></Stock></Stocks>";
  1819. sr = new System.IO.StringReader(input);
  1820. ds.EnforceConstraints = false;
  1821. ds.ReadXml(sr);
  1822. //Test that all added columns have "Hidden" mapping type.
  1823. // StockTable.Stock_IdCol.ColumnMapping
  1824. Assert.AreEqual(MappingType.Hidden, ds.Tables["Stock"].Columns["Stock_Id"].ColumnMapping, "DS317");
  1825. // CompanyTable.Stock_IdCol.ColumnMapping
  1826. Assert.AreEqual(MappingType.Hidden, ds.Tables["Company"].Columns["Stock_Id"].ColumnMapping, "DS318");
  1827. // PriceTable.Stock_IdCol.ColumnMapping
  1828. Assert.AreEqual(MappingType.Hidden, ds.Tables["Price"].Columns["Stock_Id"].ColumnMapping, "DS319");
  1829. }
  1830. [Test] public void ReadXml_Strm4()
  1831. {
  1832. m_ds = new DataSet("Stocks");
  1833. string input = string.Empty;
  1834. System.IO.StringReader sr;
  1835. input += "<?xml version=\"1.0\"?>";
  1836. input += "<Stocks>";
  1837. input += " <Stock name=\"MSFT\">";
  1838. input += " <Company name=\"Microsoft Corp.\" />";
  1839. input += " <Company name=\"General Electric\"/>";
  1840. input += " <Price type=\"high\">";
  1841. input += " <Value>10.0</Value>";
  1842. input += " <Date>01/20/2000</Date>";
  1843. input += " </Price>";
  1844. input += " <Price type=\"low\">";
  1845. input += " <Value>1.0</Value>";
  1846. input += " <Date>03/21/2002</Date>";
  1847. input += " </Price>";
  1848. input += " <Price type=\"current\">";
  1849. input += " <Value>3.0</Value>";
  1850. input += " <Date>TODAY</Date>";
  1851. input += " </Price>";
  1852. input += " </Stock>";
  1853. input += " <Stock name=\"GE\">";
  1854. input += " <Company name=\"GE company\"/>";
  1855. input += " <Price type=\"high\">";
  1856. input += " <Value>22.23</Value>";
  1857. input += " <Date>02/12/2001</Date>";
  1858. input += " </Price>";
  1859. input += " <Price type=\"low\">";
  1860. input += " <Value>1.97</Value>";
  1861. input += " <Date>04/20/2003</Date>";
  1862. input += " </Price>";
  1863. input += " <Price type=\"current\">";
  1864. input += " <Value>3.0</Value>";
  1865. input += " <Date>TODAY</Date>";
  1866. input += " </Price>";
  1867. input += " </Stock>";
  1868. input += " <Stock name=\"Intel\">";
  1869. input += " <Company name=\"Intel Corp.\"/>";
  1870. input += " <Company name=\"Test1\" />";
  1871. input += " <Company name=\"Test2\"/>";
  1872. input += " <Price type=\"high\">";
  1873. input += " <Value>15.0</Value>";
  1874. input += " <Date>01/25/2000</Date>";
  1875. input += " </Price>";
  1876. input += " <Price type=\"low\">";
  1877. input += " <Value>1.0</Value>";
  1878. input += " <Date>03/23/2002</Date>";
  1879. input += " </Price>";
  1880. input += " <Price type=\"current\">";
  1881. input += " <Value>3.0</Value>";
  1882. input += " <Date>TODAY</Date>";
  1883. input += " </Price>";
  1884. input += " </Stock>";
  1885. input += " <Stock name=\"Mainsoft\">";
  1886. input += " <Company name=\"Mainsoft Corp.\"/>";
  1887. input += " <Price type=\"high\">";
  1888. input += " <Value>30.0</Value>";
  1889. input += " <Date>01/26/2000</Date>";
  1890. input += " </Price>";
  1891. input += " <Price type=\"low\">";
  1892. input += " <Value>1.0</Value>";
  1893. input += " <Date>03/26/2002</Date>";
  1894. input += " </Price>";
  1895. input += " <Price type=\"current\">";
  1896. input += " <Value>27.0</Value>";
  1897. input += " <Date>TODAY</Date>";
  1898. input += " </Price>";
  1899. input += " </Stock>";
  1900. input += "</Stocks>";
  1901. sr = new System.IO.StringReader(input);
  1902. m_ds.EnforceConstraints = true;
  1903. m_ds.ReadXml(sr);
  1904. this.privateTestCase("TestCase 1", "Company", "name='Microsoft Corp.'", "Stock", "name='MSFT'", "DS320");
  1905. this.privateTestCase("TestCase 2", "Company", "name='General Electric'", "Stock", "name='MSFT'", "DS321");
  1906. this.privateTestCase("TestCase 3", "Price", "Date='01/20/2000'", "Stock", "name='MSFT'", "DS322");
  1907. this.privateTestCase("TestCase 4", "Price", "Date='03/21/2002'", "Stock", "name='MSFT'", "DS323");
  1908. this.privateTestCase("TestCase 5", "Company", "name='GE company'", "Stock", "name='GE'", "DS324");
  1909. this.privateTestCase("TestCase 6", "Price", "Date='02/12/2001'", "Stock", "name='GE'", "DS325");
  1910. this.privateTestCase("TestCase 7", "Price", "Date='04/20/2003'", "Stock", "name='GE'", "DS326");
  1911. this.privateTestCase("TestCase 8", "Company", "name='Intel Corp.'", "Stock", "name='Intel'", "DS327");
  1912. this.privateTestCase("TestCase 9", "Company", "name='Test1'", "Stock", "name='Intel'", "DS328");
  1913. this.privateTestCase("TestCase 10", "Company", "name='Test2'", "Stock", "name='Intel'", "DS329");
  1914. this.privateTestCase("TestCase 11", "Price", "Date='01/25/2000'", "Stock", "name='Intel'", "DS330");
  1915. this.privateTestCase("TestCase 12", "Price", "Date='03/23/2002'", "Stock", "name='Intel'", "DS331");
  1916. this.privateTestCase("TestCase 13", "Company", "name='Mainsoft Corp.'", "Stock", "name='Mainsoft'", "DS332");
  1917. this.privateTestCase("TestCase 12", "Price", "Date='01/26/2000'", "Stock", "name='Mainsoft'", "DS333");
  1918. this.privateTestCase("TestCase 12", "Price", "Date='03/26/2002'", "Stock", "name='Mainsoft'", "DS334");
  1919. }
  1920. private void privateTestCase(string name, string toTable, string toTestSelect, string toCompareTable, string toCompareSelect, string AssertTag)
  1921. {
  1922. DataRow drToTest = m_ds.Tables[toTable].Select(toTestSelect)[0];
  1923. DataRow drToCompare = m_ds.Tables[toCompareTable].Select(toCompareSelect)[0];
  1924. Assert.AreEqual(m_ds.Tables[toTable].Select(toTestSelect)[0]["Stock_Id"], m_ds.Tables[toCompareTable].Select(toCompareSelect)[0]["Stock_Id"], AssertTag);
  1925. }
  1926. [Test]
  1927. public void ReadXml_Strm5()
  1928. {
  1929. string xmlData;
  1930. string name;
  1931. string expected;
  1932. #region "TestCase 1 - Empty string"
  1933. // Empty string
  1934. DataSet ds = new DataSet();
  1935. System.IO.StringReader sr = new System.IO.StringReader (string.Empty);
  1936. System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr);
  1937. try
  1938. {
  1939. ds.ReadXml (xReader);
  1940. Assert.Fail("DS335: ReadXml Failed to throw XmlException");
  1941. }
  1942. catch (System.Xml.XmlException) {}
  1943. catch (AssertionException exc) {throw exc;}
  1944. catch (Exception exc)
  1945. {
  1946. Assert.Fail("DS336: ReadXml. Wrong exception type. Got:" + exc);
  1947. }
  1948. #endregion
  1949. #region "TestCase 2 - Single element"
  1950. name = "Single element";
  1951. expected = "DataSet Name=a Tables count=0";
  1952. xmlData = "<a>1</a>";
  1953. PrivateTestCase(name, expected, xmlData);
  1954. #endregion
  1955. #region "TestCase 3 - Nesting one level single element."
  1956. name = "Nesting one level single element.";
  1957. expected = "DataSet Name=NewDataSet Tables count=1 Table Name=a Rows count=1 Items count=1 1";
  1958. xmlData = "<a><b>1</b></a>";
  1959. PrivateTestCase(name, expected, xmlData);
  1960. #endregion
  1961. #region "TestCase 4 - Nesting one level multiple elements."
  1962. name = "Nesting one level multiple elements.";
  1963. expected = "DataSet Name=NewDataSet Tables count=1 Table Name=a Rows count=1 Items count=3 bb cc dd";
  1964. xmlData = "<a><b>bb</b><c>cc</c><d>dd</d></a>";
  1965. PrivateTestCase(name, expected, xmlData);
  1966. #endregion
  1967. #region "TestCase 5 - Nesting two levels single elements."
  1968. name = "Nesting two levels single elements.";
  1969. expected = "DataSet Name=a Tables count=1 Table Name=b Rows count=1 Items count=1 cc";
  1970. xmlData = "<a><b><c>cc</c></b></a>";
  1971. PrivateTestCase(name, expected, xmlData);
  1972. #endregion
  1973. #region "TestCase 6 - Nesting two levels multiple elements."
  1974. name = "Nesting two levels multiple elements.";
  1975. expected = "DataSet Name=a Tables count=1 Table Name=b Rows count=1 Items count=2 cc dd";
  1976. xmlData = string.Empty;
  1977. xmlData += "<a>";
  1978. xmlData += "<b>";
  1979. xmlData += "<c>cc</c>";
  1980. xmlData += "<d>dd</d>";
  1981. xmlData += "</b>";
  1982. xmlData += "</a>";
  1983. PrivateTestCase(name, expected, xmlData);
  1984. #endregion
  1985. #region "TestCase 7 - Nesting two levels multiple elements."
  1986. name = "Nesting two levels multiple elements.";
  1987. 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";
  1988. xmlData = string.Empty;
  1989. xmlData += "<a>";
  1990. xmlData += "<b>";
  1991. xmlData += "<c>cc</c>";
  1992. xmlData += "<d>dd</d>";
  1993. xmlData += "</b>";
  1994. xmlData += "<e>";
  1995. xmlData += "<c>cc</c>";
  1996. xmlData += "<d>dd</d>";
  1997. xmlData += "</e>";
  1998. xmlData += "</a>";
  1999. PrivateTestCase(name, expected, xmlData);
  2000. #endregion
  2001. #region "TestCase 8 - Nesting three levels single element."
  2002. name = "Nesting three levels single element.";
  2003. xmlData = string.Empty;
  2004. xmlData += "<a>";
  2005. xmlData += "<b>";
  2006. xmlData += "<c>";
  2007. xmlData += "<d>dd</d>";
  2008. xmlData += "</c>";
  2009. xmlData += "</b>";
  2010. xmlData += "</a>";
  2011. 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";
  2012. PrivateTestCase(name, expected, xmlData);
  2013. #endregion
  2014. #region "TestCase 9 - Nesting three levels multiple elements."
  2015. name = "Nesting three levels multiple elements.";
  2016. xmlData = string.Empty;
  2017. xmlData += "<a>";
  2018. xmlData += "<b>";
  2019. xmlData += "<c>";
  2020. xmlData += "<d>dd</d>";
  2021. xmlData += "<e>ee</e>";
  2022. xmlData += "</c>";
  2023. xmlData += "</b>";
  2024. xmlData += "</a>";
  2025. 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";
  2026. PrivateTestCase(name, expected, xmlData);
  2027. #endregion
  2028. #region "TestCase 10 - Nesting three levels multiple elements."
  2029. name = "Nesting three levels multiple elements.";
  2030. xmlData = string.Empty;
  2031. xmlData += "<a>";
  2032. xmlData += "<b>";
  2033. xmlData += "<c>";
  2034. xmlData += "<d>dd</d>";
  2035. xmlData += "<e>ee</e>";
  2036. xmlData += "</c>";
  2037. xmlData += "<f>ff</f>";
  2038. xmlData += "</b>";
  2039. xmlData += "</a>";
  2040. 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";
  2041. PrivateTestCase(name, expected, xmlData);
  2042. #endregion
  2043. #region "TestCase 11 - Nesting three levels multiple elements."
  2044. name = "Nesting three levels multiple elements.";
  2045. xmlData = string.Empty;
  2046. xmlData += "<a>";
  2047. xmlData += "<b>";
  2048. xmlData += "<c>";
  2049. xmlData += "<d>dd</d>";
  2050. xmlData += "<e>ee</e>";
  2051. xmlData += "</c>";
  2052. xmlData += "<f>ff</f>";
  2053. xmlData += "<g>";
  2054. xmlData += "<h>hh</h>";
  2055. xmlData += "<i>ii</i>";
  2056. xmlData += "</g>";
  2057. xmlData += "<j>jj</j>";
  2058. xmlData += "</b>";
  2059. xmlData += "</a>";
  2060. 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";
  2061. PrivateTestCase(name, expected, xmlData);
  2062. #endregion
  2063. #region "TestCase 12 - Nesting three levels multiple elements."
  2064. name = "Nesting three levels multiple elements.";
  2065. xmlData = string.Empty;
  2066. xmlData += "<a>";
  2067. xmlData += "<b>";
  2068. xmlData += "<c>";
  2069. xmlData += "<d>dd</d>";
  2070. xmlData += "<e>ee</e>";
  2071. xmlData += "</c>";
  2072. xmlData += "<f>ff</f>";
  2073. xmlData += "</b>";
  2074. xmlData += "<g>";
  2075. xmlData += "<h>";
  2076. xmlData += "<i>ii</i>";
  2077. xmlData += "<j>jj</j>";
  2078. xmlData += "</h>";
  2079. xmlData += "<f>ff</f>";
  2080. xmlData += "</g>";
  2081. xmlData += "</a>";
  2082. 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";
  2083. PrivateTestCase(name, expected, xmlData);
  2084. #endregion
  2085. #region "TestCase 13 - Nesting three levels multiple elements."
  2086. name = "Nesting three levels multiple elements.";
  2087. xmlData = string.Empty;
  2088. xmlData += "<a>";
  2089. xmlData += "<b>";
  2090. xmlData += "<c>";
  2091. xmlData += "<d>dd</d>";
  2092. xmlData += "<e>ee</e>";
  2093. xmlData += "</c>";
  2094. xmlData += "<f>ff</f>";
  2095. xmlData += "<k>";
  2096. xmlData += "<l>ll</l>";
  2097. xmlData += "<m>mm</m>";
  2098. xmlData += "</k>";
  2099. xmlData += "<n>nn</n>";
  2100. xmlData += "</b>";
  2101. xmlData += "<g>";
  2102. xmlData += "<h>";
  2103. xmlData += "<i>ii</i>";
  2104. xmlData += "<j>jj</j>";
  2105. xmlData += "</h>";
  2106. xmlData += "<o>oo</o>";
  2107. xmlData += "</g>";
  2108. xmlData += "</a>";
  2109. 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";
  2110. PrivateTestCase(name, expected, xmlData);
  2111. #endregion
  2112. #region "TestCase 14 - for Bug 2387 (System.Data.DataSet.ReadXml(..) - ArgumentException while reading specific XML)"
  2113. name = "Specific XML - for Bug 2387";
  2114. 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";
  2115. 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>";
  2116. PrivateTestCase(name, expected, xmlData);
  2117. #endregion
  2118. }
  2119. private void PrivateTestCase(string a_name, string a_expected, string a_xmlData)
  2120. {
  2121. DataSet ds = new DataSet();
  2122. System.IO.StringReader sr = new System.IO.StringReader(a_xmlData) ;
  2123. System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
  2124. ds.ReadXml (xReader);
  2125. Assert.AreEqual(a_expected, this.dataSetDescription(ds), "DS337");
  2126. }
  2127. private string dataSetDescription(DataSet ds)
  2128. {
  2129. string desc = string.Empty;
  2130. desc += "DataSet Name=" + ds.DataSetName;
  2131. desc += " Tables count=" + ds.Tables.Count;
  2132. foreach (DataTable dt in ds.Tables)
  2133. {
  2134. desc += " Table Name=" + dt.TableName;
  2135. desc += " Rows count=" + dt.Rows.Count;
  2136. string[] colNames = new string[dt.Columns.Count];
  2137. for(int i = 0; i < dt.Columns.Count; i++)
  2138. colNames[i] = dt.Columns[i].ColumnName;
  2139. Array.Sort(colNames);
  2140. foreach (DataRow dr in dt.Rows)
  2141. {
  2142. desc += " Items count=" + dr.ItemArray.Length;
  2143. foreach (string name in colNames)
  2144. {
  2145. desc += " " + dr[name].ToString();
  2146. }
  2147. }
  2148. }
  2149. return desc;
  2150. }
  2151. [Test] public void ReadXml_Strm6()
  2152. {
  2153. // TC1
  2154. DataSet ds = new DataSet();
  2155. string xmlData = string.Empty;
  2156. xmlData += "<a>";
  2157. xmlData += "<b>";
  2158. xmlData += "<c>1</c>";
  2159. xmlData += "<c>2</c>";
  2160. xmlData += "<c>3</c>";
  2161. xmlData += "</b>";
  2162. xmlData += "</a>";
  2163. System.IO.StringReader sr = new System.IO.StringReader(xmlData) ;
  2164. System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
  2165. ds.ReadXml (xReader);
  2166. Assert.AreEqual(3, ds.Tables["c"].Rows.Count, "DS338");
  2167. }
  2168. [Test]
  2169. public void ReadXmlSchema_2()
  2170. {
  2171. DataSet ds = new DataSet();
  2172. string xmlData = string.Empty;
  2173. xmlData += "<?xml version=\"1.0\"?>";
  2174. 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\">";
  2175. xmlData += "<xs:element name=\"SiteConfiguration\" msdata:IsDataSet=\"true\" msdata:EnforceConstraints=\"False\">";
  2176. xmlData += "<xs:complexType>";
  2177. xmlData += "<xs:choice maxOccurs=\"unbounded\">";
  2178. xmlData += "<xs:element name=\"Tab\">";
  2179. xmlData += "<xs:complexType>";
  2180. xmlData += "<xs:sequence>";
  2181. xmlData += "<xs:element name=\"Module\" minOccurs=\"0\" maxOccurs=\"unbounded\">";
  2182. xmlData += "<xs:complexType>";
  2183. xmlData += "<xs:attribute name=\"ModuleId\" form=\"unqualified\" type=\"xs:int\" />";
  2184. xmlData += "</xs:complexType>";
  2185. xmlData += "</xs:element>";
  2186. xmlData += "</xs:sequence>";
  2187. xmlData += "<xs:attribute name=\"TabId\" form=\"unqualified\" type=\"xs:int\" />";
  2188. xmlData += "</xs:complexType>";
  2189. xmlData += "</xs:element>";
  2190. xmlData += "</xs:choice>";
  2191. xmlData += "</xs:complexType>";
  2192. xmlData += "<xs:key name=\"TabKey\" msdata:PrimaryKey=\"true\">";
  2193. xmlData += "<xs:selector xpath=\".//mstns:Tab\" />";
  2194. xmlData += "<xs:field xpath=\"@TabId\" />";
  2195. xmlData += "</xs:key>";
  2196. xmlData += "<xs:key name=\"ModuleKey\" msdata:PrimaryKey=\"true\">";
  2197. xmlData += "<xs:selector xpath=\".//mstns:Module\" />";
  2198. xmlData += "<xs:field xpath=\"@ModuleID\" />";
  2199. xmlData += "</xs:key>";
  2200. xmlData += "</xs:element>";
  2201. xmlData += "</xs:schema>";
  2202. ds.ReadXmlSchema(new StringReader(xmlData));
  2203. }
  2204. [Test]
  2205. public void ReadXml_ByTextReader()
  2206. {
  2207. DataSet ds1 = new DataSet();
  2208. ds1.Tables.Add(DataProvider.CreateParentDataTable());
  2209. ds1.Tables.Add(DataProvider.CreateChildDataTable());
  2210. //add data to check GH bug of DataSet.ReadXml of empty strings
  2211. ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
  2212. ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
  2213. ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
  2214. System.IO.StringWriter sw = new System.IO.StringWriter();
  2215. //write xml file, data only
  2216. ds1.WriteXml(sw);
  2217. //copy both data and schema
  2218. DataSet ds2 = ds1.Copy();
  2219. //clear the data
  2220. ds2.Clear();
  2221. System.IO.StringReader sr = new System.IO.StringReader(sw.GetStringBuilder().ToString());
  2222. ds2.ReadXml(sr);
  2223. //check xml data
  2224. // ReadXml - Tables count
  2225. Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS339");
  2226. // ReadXml - Table 1 row count
  2227. Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS340");
  2228. // ReadXml - Table 2 row count
  2229. Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS341");
  2230. sr.Close();
  2231. sw.Close();
  2232. }
  2233. [Test]
  2234. public void ReadXml_ByXmlReader()
  2235. {
  2236. DataSet ds1 = new DataSet();
  2237. ds1.Tables.Add(DataProvider.CreateParentDataTable());
  2238. ds1.Tables.Add(DataProvider.CreateChildDataTable());
  2239. //add data to check GH bug of DataSet.ReadXml of empty strings
  2240. ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
  2241. ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
  2242. ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
  2243. System.IO.StringWriter sw = new System.IO.StringWriter();
  2244. System.Xml.XmlTextWriter xmlTW = new System.Xml.XmlTextWriter(sw);
  2245. //write xml file, data only
  2246. ds1.WriteXml(xmlTW);
  2247. //ds1.WriteXml("C:\\Temp\\q.xml");
  2248. //copy both data and schema
  2249. DataSet ds2 = ds1.Copy();
  2250. //clear the data
  2251. ds2.Clear();
  2252. System.IO.StringReader sr = new System.IO.StringReader(sw.ToString());
  2253. System.Xml.XmlTextReader xmlTR = new System.Xml.XmlTextReader(sr);
  2254. ds2.ReadXml(xmlTR);
  2255. //check xml data
  2256. // ReadXml - Tables count
  2257. Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS342");
  2258. // ReadXml - Table 1 row count
  2259. Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS343");
  2260. // ReadXml - Table 2 row count
  2261. Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS344");
  2262. }
  2263. [Test] public void WriteXmlSchema_ForignKeyConstraint ()
  2264. {
  2265. DataSet ds1 = new DataSet();
  2266. DataTable table1 = ds1.Tables.Add();
  2267. DataTable table2 = ds1.Tables.Add();
  2268. DataColumn col1_1 = table1.Columns.Add ("col1", typeof (int));
  2269. DataColumn col2_1 = table2.Columns.Add ("col1", typeof (int));
  2270. table2.Constraints.Add ("fk", col1_1, col2_1);
  2271. StringWriter sw = new StringWriter ();
  2272. ds1.WriteXmlSchema (sw);
  2273. String xml = sw.ToString ();
  2274. Assert.IsTrue (xml.IndexOf (@"<xs:keyref name=""fk"" refer=""Constraint1"" " +
  2275. @"msdata:ConstraintOnly=""true"">") != -1, "#1");
  2276. }
  2277. [Test] public void WriteXmlSchema_RelationAnnotation ()
  2278. {
  2279. DataSet ds1 = new DataSet();
  2280. DataTable table1 = ds1.Tables.Add();
  2281. DataTable table2 = ds1.Tables.Add();
  2282. DataColumn col1_1 = table1.Columns.Add ("col1", typeof (int));
  2283. DataColumn col2_1 = table2.Columns.Add ("col1", typeof (int));
  2284. ds1.Relations.Add ("rel", col1_1, col2_1, false);
  2285. StringWriter sw = new StringWriter ();
  2286. ds1.WriteXmlSchema (sw);
  2287. String xml = sw.ToString ();
  2288. Assert.IsTrue (xml.IndexOf (@"<msdata:Relationship name=""rel"" msdata:parent=""Table1""" +
  2289. @" msdata:child=""Table2"" msdata:parentkey=""col1"" " +
  2290. @"msdata:childkey=""col1"" />") != -1, "#1");
  2291. }
  2292. [Test] public void WriteXmlSchema_Relations_ForeignKeys ()
  2293. {
  2294. System.IO.MemoryStream ms = null;
  2295. System.IO.MemoryStream ms1 = null;
  2296. DataSet ds1 = new DataSet();
  2297. DataTable table1 = ds1.Tables.Add("Table 1");
  2298. DataTable table2 = ds1.Tables.Add("Table 2");
  2299. DataColumn col1_1 = table1.Columns.Add ("col 1", typeof (int));
  2300. DataColumn col1_2 = table1.Columns.Add ("col 2", typeof (int));
  2301. DataColumn col1_3 = table1.Columns.Add ("col 3", typeof (int));
  2302. DataColumn col1_4 = table1.Columns.Add ("col 4", typeof (int));
  2303. DataColumn col1_5 = table1.Columns.Add ("col 5", typeof (int));
  2304. DataColumn col1_6 = table1.Columns.Add ("col 6", typeof (int));
  2305. DataColumn col1_7 = table1.Columns.Add ("col 7", typeof (int));
  2306. DataColumn col2_1 = table2.Columns.Add ("col 1", typeof (int));
  2307. DataColumn col2_2 = table2.Columns.Add ("col 2", typeof (int));
  2308. DataColumn col2_3 = table2.Columns.Add ("col 3", typeof (int));
  2309. DataColumn col2_4 = table2.Columns.Add ("col 4", typeof (int));
  2310. DataColumn col2_5 = table2.Columns.Add ("col 5", typeof (int));
  2311. DataColumn col2_6 = table2.Columns.Add ("col 6", typeof (int));
  2312. ds1.Relations.Add ("rel 1",
  2313. new DataColumn[] {col1_1, col1_2},
  2314. new DataColumn[] {col2_1, col2_2});
  2315. ds1.Relations.Add ("rel 2",
  2316. new DataColumn[] {col1_3, col1_4},
  2317. new DataColumn[] {col2_3, col2_4},
  2318. false);
  2319. table2.Constraints.Add ("fk 1",
  2320. new DataColumn[] {col1_5, col1_6},
  2321. new DataColumn[] {col2_5, col2_6});
  2322. table1.Constraints.Add ("pk 1", col1_7, true);
  2323. ms = new System.IO.MemoryStream();
  2324. ds1.WriteXmlSchema (ms);
  2325. ms1 = new System.IO.MemoryStream (ms.GetBuffer());
  2326. DataSet ds2 = new DataSet();
  2327. ds2.ReadXmlSchema(ms1);
  2328. Assert.AreEqual (2, ds2.Relations.Count, "#1");
  2329. Assert.AreEqual (3, ds2.Tables [0].Constraints.Count, "#2");
  2330. Assert.AreEqual (2, ds2.Tables [1].Constraints.Count, "#2");
  2331. Assert.IsTrue (ds2.Relations.Contains ("rel 1"), "#3");
  2332. Assert.IsTrue (ds2.Relations.Contains ("rel 2"), "#4");
  2333. Assert.IsTrue (ds2.Tables [0].Constraints.Contains ("pk 1"), "#5");
  2334. Assert.IsTrue (ds2.Tables [1].Constraints.Contains ("fk 1"), "#6");
  2335. Assert.IsTrue (ds2.Tables [1].Constraints.Contains ("rel 1"), "#7");
  2336. Assert.AreEqual (2, ds2.Relations ["rel 1"].ParentColumns.Length, "#8");
  2337. Assert.AreEqual (2, ds2.Relations ["rel 1"].ChildColumns.Length, "#9");
  2338. Assert.AreEqual (2, ds2.Relations ["rel 2"].ParentColumns.Length, "#10");
  2339. Assert.AreEqual (2, ds2.Relations ["rel 2"].ChildColumns.Length, "#11");
  2340. ForeignKeyConstraint fk = (ForeignKeyConstraint)ds2.Tables [1].Constraints ["fk 1"];
  2341. Assert.AreEqual (2, fk.RelatedColumns.Length, "#12");
  2342. Assert.AreEqual (2, fk.Columns.Length, "#13");
  2343. }
  2344. [Test] public void RejectChanges()
  2345. {
  2346. DataSet ds1,ds2 = new DataSet();
  2347. ds2.Tables.Add(DataProvider.CreateParentDataTable());
  2348. ds1 = ds2.Copy();
  2349. //create changes
  2350. ds2.Tables[0].Rows[0][0] = "70";
  2351. ds2.Tables[0].Rows[1].Delete();
  2352. ds2.Tables[0].Rows.Add(new object[] {9,"string1","string2"});
  2353. // RejectChanges
  2354. ds2.RejectChanges();
  2355. Assert.AreEqual(ds2.GetXml(), ds1.GetXml(), "DS345");
  2356. }
  2357. [Test] public void Relations()
  2358. {
  2359. DataTable dtChild1,dtChild2,dtParent;
  2360. DataSet ds = new DataSet();
  2361. //Create tables
  2362. dtChild1 = DataProvider.CreateChildDataTable();
  2363. dtChild1.TableName = "Child";
  2364. dtChild2 = DataProvider.CreateChildDataTable();
  2365. dtChild2.TableName = "CHILD";
  2366. dtParent= DataProvider.CreateParentDataTable();
  2367. //Add tables to dataset
  2368. ds.Tables.Add(dtChild1);
  2369. ds.Tables.Add(dtChild2);
  2370. ds.Tables.Add(dtParent);
  2371. DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild1.Columns["ParentId"]);
  2372. DataRelation drl1 = new DataRelation("Parent-CHILD",dtParent.Columns["ParentId"],dtChild2.Columns["ParentId"]);
  2373. // Checking Relations - default value
  2374. //Check default
  2375. Assert.AreEqual(0, ds.Relations.Count , "DS346");
  2376. ds.Relations.Add(drl);
  2377. // Checking Relations Count
  2378. Assert.AreEqual(1, ds.Relations.Count , "DS347");
  2379. // Checking Relations Value
  2380. Assert.AreEqual(drl, ds.Relations[0] , "DS348");
  2381. // Checking Relations - get by name
  2382. Assert.AreEqual(drl, ds.Relations["Parent-Child"] , "DS349");
  2383. // Checking Relations - get by name case sensetive
  2384. Assert.AreEqual(drl, ds.Relations["PARENT-CHILD"] , "DS350");
  2385. // Checking Relations Count 2
  2386. ds.Relations.Add(drl1);
  2387. Assert.AreEqual(2, ds.Relations.Count , "DS351");
  2388. // Checking Relations - get by name case sensetive,ArgumentException
  2389. try
  2390. {
  2391. DataRelation tmp = ds.Relations["PARENT-CHILD"];
  2392. Assert.Fail("DS352: Relations Failed to throw ArgumentException");
  2393. }
  2394. catch (ArgumentException) {}
  2395. catch (AssertionException exc) {throw exc;}
  2396. catch (Exception exc)
  2397. {
  2398. Assert.Fail("DS353: Relations. Wrong exception type. Got:" + exc);
  2399. }
  2400. }
  2401. [Test] public void Reset()
  2402. {
  2403. DataTable dt1 = DataProvider.CreateParentDataTable();
  2404. DataTable dt2 = DataProvider.CreateChildDataTable();
  2405. dt1.PrimaryKey = new DataColumn[] {dt1.Columns[0]};
  2406. dt2.PrimaryKey = new DataColumn[] {dt2.Columns[0],dt2.Columns[1]};
  2407. DataRelation rel = new DataRelation("Rel",dt1.Columns["ParentId"],dt2.Columns["ParentId"]);
  2408. DataSet ds = new DataSet();
  2409. ds.Tables.AddRange(new DataTable[] {dt1,dt2});
  2410. ds.Relations.Add(rel);
  2411. ds.Reset();
  2412. // Reset - Relations
  2413. Assert.AreEqual(0 , ds.Relations.Count , "DS354");
  2414. // Reset - Tables
  2415. Assert.AreEqual(0 , ds.Tables.Count , "DS355");
  2416. }
  2417. [Test] public void ShouldSerializeRelations()
  2418. {
  2419. // DataSet ShouldSerializeRelations
  2420. newDataSet ds = new newDataSet();
  2421. Assert.AreEqual(true, ds.testMethod(), "DS356");
  2422. }
  2423. class newDataSet:DataSet
  2424. {
  2425. public bool testMethod()
  2426. {
  2427. return ShouldSerializeRelations();
  2428. }
  2429. }
  2430. [Test] public void ShouldSerializeTables()
  2431. {
  2432. // DataSet ShouldSerializeTables
  2433. newDataSet1 ds = new newDataSet1();
  2434. Assert.AreEqual(true, ds.testMethod(), "DS357");
  2435. }
  2436. class newDataSet1:DataSet
  2437. {
  2438. public bool testMethod()
  2439. {
  2440. return ShouldSerializeTables();
  2441. }
  2442. }
  2443. [Test] public void Tables()
  2444. {
  2445. //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.
  2446. //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"].
  2447. //The CaseSensitive property of the DataSet does not affect this behavior. The CaseSensitive property
  2448. DataSet ds = new DataSet();
  2449. DataTable dt1 = new DataTable();
  2450. DataTable dt2 = new DataTable();
  2451. DataTable dt3 = new DataTable();
  2452. dt3.TableName = "Table3";
  2453. DataTable dt4 = new DataTable(dt3.TableName.ToUpper());
  2454. // Checking Tables - default value
  2455. //Check default
  2456. Assert.AreEqual(0, ds.Tables.Count , "DS358");
  2457. ds.Tables.Add(dt1);
  2458. ds.Tables.Add(dt2);
  2459. ds.Tables.Add(dt3);
  2460. // Checking Tables Count
  2461. Assert.AreEqual(3, ds.Tables.Count , "DS359");
  2462. // Checking Tables Value 1
  2463. Assert.AreEqual(dt1, ds.Tables[0] , "DS360");
  2464. // Checking Tables Value 2
  2465. Assert.AreEqual(dt2, ds.Tables[1] , "DS361");
  2466. // Checking Tables Value 3
  2467. Assert.AreEqual(dt3, ds.Tables[2] , "DS362");
  2468. // Checking get table by name.ToUpper
  2469. Assert.AreEqual(dt3, ds.Tables[dt3.TableName.ToUpper()] , "DS363");
  2470. // Checking get table by name.ToLower
  2471. Assert.AreEqual(dt3, ds.Tables[dt3.TableName.ToLower()] , "DS364");
  2472. // Checking Tables add with name case insensetive
  2473. ds.Tables.Add(dt4); //same name as Table3, but different case
  2474. Assert.AreEqual(4, ds.Tables.Count , "DS365");
  2475. // Checking get table by name
  2476. Assert.AreEqual(dt4, ds.Tables[dt4.TableName] , "DS366");
  2477. // Checking get table by name with diferent case, ArgumentException
  2478. try
  2479. {
  2480. DataTable tmp = ds.Tables[dt4.TableName.ToLower()];
  2481. Assert.Fail("DS367: Tables Failed to throw ArgumentException");
  2482. }
  2483. catch (ArgumentException) {}
  2484. catch (AssertionException exc) {throw exc;}
  2485. catch (Exception exc)
  2486. {
  2487. Assert.Fail("DS368: Tables. Wrong exception type. Got:" + exc);
  2488. }
  2489. }
  2490. [Test] public void WriteXml_ByTextWriterXmlWriteMode()
  2491. {
  2492. System.IO.StringReader sr = null;
  2493. System.IO.StringWriter sw = null;
  2494. try // For real
  2495. {
  2496. // ReadXml - DataSetOut
  2497. DataSet oDataset = new DataSet("DataSetOut");
  2498. sw = new System.IO.StringWriter();
  2499. oDataset.WriteXml(sw,System.Data.XmlWriteMode.WriteSchema);
  2500. sr = new System.IO.StringReader(sw.GetStringBuilder().ToString());
  2501. oDataset = new DataSet("DataSetOut");
  2502. oDataset.ReadXml(sr);
  2503. Assert.AreEqual(0, oDataset.Tables.Count , "DS369");
  2504. }
  2505. finally
  2506. {
  2507. sw.Close();
  2508. }
  2509. }
  2510. [Test] public void ctor()
  2511. {
  2512. DataSet ds;
  2513. // ctor
  2514. ds = new DataSet();
  2515. Assert.AreEqual(true, ds != null , "DS370");
  2516. }
  2517. [Test] public void ctor_ByDataSetName()
  2518. {
  2519. DataSet ds = null;
  2520. // ctor
  2521. ds = new DataSet("NewDataSet");
  2522. Assert.AreEqual(true, ds != null , "DS371");
  2523. // ctor - name
  2524. Assert.AreEqual("NewDataSet" , ds.DataSetName , "DS372");
  2525. }
  2526. [Test] public void extendedProperties()
  2527. {
  2528. DataSet ds = new DataSet();
  2529. PropertyCollection pc;
  2530. pc = ds.ExtendedProperties ;
  2531. // Checking ExtendedProperties default
  2532. Assert.AreEqual(true, pc != null, "DS373");
  2533. // Checking ExtendedProperties count
  2534. Assert.AreEqual(0, pc.Count , "DS374");
  2535. }
  2536. #if NET_2_0
  2537. // Test for bug #76517
  2538. [Test] public void SchemaSerializationModeTest ()
  2539. {
  2540. DataSet ds = new DataSet ();
  2541. Assert.AreEqual (SchemaSerializationMode.IncludeSchema,
  2542. ds.SchemaSerializationMode, "#1");
  2543. try {
  2544. ds.SchemaSerializationMode = SchemaSerializationMode.ExcludeSchema;
  2545. Assert.Fail ("#2 InvalidOperationException must be thrown");
  2546. }catch (InvalidOperationException e) {
  2547. //ok
  2548. }
  2549. }
  2550. #endif
  2551. ///<?xml version="1.0" encoding="utf-16"?>
  2552. ///<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  2553. /// <xs:element name="NewDataSet" msdata:IsDataSet="true">
  2554. /// <xs:complexType>
  2555. /// <xs:choice maxOccurs="unbounded">
  2556. /// <xs:element name="Parent">
  2557. /// <xs:complexType>
  2558. /// <xs:sequence>
  2559. /// <xs:element name="ParentId" type="xs:int" minOccurs="0"/>
  2560. /// <xs:element name="String1" type="xs:string" minOccurs="0"/>
  2561. /// <xs:element name="String2" type="xs:string" minOccurs="0"/>
  2562. /// <xs:element name="ParentDateTime" type="xs:dateTime" minOccurs="0"/>
  2563. /// <xs:element name="ParentDouble" type="xs:double" minOccurs="0"/>
  2564. /// <xs:element name="ParentBool" type="xs:boolean" minOccurs="0"/>
  2565. /// </xs:sequence>
  2566. /// </xs:complexType>
  2567. /// </xs:element>
  2568. /// </xs:choice>
  2569. /// </xs:complexType>
  2570. /// </xs:element>
  2571. ///</xs:schema>
  2572. [Test]
  2573. public void ParentDataTableSchema()
  2574. {
  2575. XmlDocument testedSchema;
  2576. XmlNamespaceManager testedSchemaNamepaces;
  2577. InitParentDataTableSchema(out testedSchema, out testedSchemaNamepaces);
  2578. CheckNode("DataSet name", "/xs:schema/xs:element[@name='NewDataSet']", 1, testedSchema, testedSchemaNamepaces);
  2579. CheckNode("Parent datatable name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element[@name='Parent']", 1, testedSchema, testedSchemaNamepaces);
  2580. CheckNode("ParentId column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentId']", 1, testedSchema, testedSchemaNamepaces);
  2581. CheckNode("String1 column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='String1']", 1, testedSchema, testedSchemaNamepaces);
  2582. CheckNode("String2 column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='String1']", 1, testedSchema, testedSchemaNamepaces);
  2583. CheckNode("ParentDateTime column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentDateTime']", 1, testedSchema, testedSchemaNamepaces);
  2584. CheckNode("ParentDouble column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentDouble']", 1, testedSchema, testedSchemaNamepaces);
  2585. CheckNode("ParentBool column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentBool']", 1, testedSchema, testedSchemaNamepaces);
  2586. CheckNode("Int columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:int']", 1, testedSchema, testedSchemaNamepaces);
  2587. CheckNode("string columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:string']", 2, testedSchema, testedSchemaNamepaces);
  2588. CheckNode("dateTime columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:dateTime']", 1, testedSchema, testedSchemaNamepaces);
  2589. CheckNode("double columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:double']", 1, testedSchema, testedSchemaNamepaces);
  2590. CheckNode("boolean columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:boolean']", 1, testedSchema, testedSchemaNamepaces);
  2591. CheckNode("minOccurs columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@minOccurs='0']", 6, testedSchema, testedSchemaNamepaces);
  2592. }
  2593. private void InitParentDataTableSchema(out XmlDocument schemaDocInit, out XmlNamespaceManager namespaceManagerToInit)
  2594. {
  2595. DataSet ds = new DataSet();
  2596. ds.Tables.Add(DataProvider.CreateParentDataTable());
  2597. string strXML = ds.GetXmlSchema();
  2598. schemaDocInit = new XmlDocument();
  2599. schemaDocInit.LoadXml(strXML);
  2600. namespaceManagerToInit = new XmlNamespaceManager(schemaDocInit.NameTable);
  2601. namespaceManagerToInit.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
  2602. namespaceManagerToInit.AddNamespace("msdata", "urn:schemas-microsoft-com:xml-msdata");
  2603. }
  2604. private void CheckNode(string description, string xPath, int expectedNodesCout, XmlDocument schemaDoc, XmlNamespaceManager nm)
  2605. {
  2606. int actualNodeCount = schemaDoc.SelectNodes(xPath, nm).Count;
  2607. Assert.AreEqual(expectedNodesCout,actualNodeCount, "DS75" + description);
  2608. }
  2609. [Test]
  2610. public void WriteXml_Stream()
  2611. {
  2612. {
  2613. DataSet ds = new DataSet();
  2614. string input = "<a><b><c>2</c></b></a>";
  2615. System.IO.StringReader sr = new System.IO.StringReader(input) ;
  2616. System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
  2617. ds.ReadXml (xReader);
  2618. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  2619. System.IO.StringWriter sw = new System.IO.StringWriter(sb);
  2620. System.Xml.XmlTextWriter xWriter = new System.Xml.XmlTextWriter(sw);
  2621. ds.WriteXml(xWriter);
  2622. string output = sb.ToString();
  2623. Assert.AreEqual(input,output, "DS76");
  2624. }
  2625. {
  2626. DataSet ds = new DataSet();
  2627. string input = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><a><b><c>2</c></b></a>";
  2628. string expectedOutput = "<a><b><c>2</c></b></a>";
  2629. System.IO.StringReader sr = new System.IO.StringReader(input) ;
  2630. System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
  2631. ds.ReadXml (xReader);
  2632. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  2633. System.IO.StringWriter sw = new System.IO.StringWriter(sb);
  2634. System.Xml.XmlTextWriter xWriter = new System.Xml.XmlTextWriter(sw);
  2635. ds.WriteXml(xWriter);
  2636. string output = sb.ToString();
  2637. Assert.AreEqual(expectedOutput,output, "DS77");
  2638. }
  2639. {
  2640. DataSet ds = new DataSet("DSName");
  2641. System.IO.StringWriter sr = new System.IO.StringWriter();
  2642. ds.WriteXml(sr);
  2643. Assert.AreEqual("<DSName />",sr.ToString(), "DS78");
  2644. }
  2645. {
  2646. DataSet ds = new DataSet();
  2647. DataTable dt;
  2648. //Create parent table.
  2649. dt = ds.Tables.Add("ParentTable");
  2650. dt.Columns.Add("ParentTable_Id", typeof(int));
  2651. dt.Columns.Add("ParentTableCol", typeof(int));
  2652. dt.Rows.Add(new object[] {0,1});
  2653. //Create child table.
  2654. dt = ds.Tables.Add("ChildTable");
  2655. dt.Columns.Add("ParentTable_Id", typeof(int));
  2656. dt.Columns.Add("ChildTableCol", typeof(string));
  2657. dt.Rows.Add(new object[] {0,"aa"});
  2658. //Add a relation between parent and child table.
  2659. ds.Relations.Add("ParentTable_ChildTable", ds.Tables["ParentTable"].Columns["ParentTable_Id"], ds.Tables["ChildTable"].Columns["ParentTable_Id"], true);
  2660. ds.Relations["ParentTable_ChildTable"].Nested=true;
  2661. //Reomve the Parent_Child relation.
  2662. dt = ds.Tables["ChildTable"];
  2663. dt.ParentRelations.Remove("ParentTable_ChildTable");
  2664. //Remove the constraint created automatically to enforce the "ParentTable_ChildTable" relation.
  2665. dt.Constraints.Remove("ParentTable_ChildTable");
  2666. //Remove the child table from the dataset.
  2667. ds.Tables.Remove("ChildTable");
  2668. //Get the xml representation of the dataset.
  2669. System.IO.StringWriter sr = new System.IO.StringWriter();
  2670. ds.WriteXml(sr);
  2671. string xml = sr.ToString();
  2672. Assert.AreEqual(-1,xml.IndexOf("<ChildTable>"), "DS79");
  2673. }
  2674. }
  2675. [Test]
  2676. public void WriteXmlSchema_ConstraintNameWithSpaces ()
  2677. {
  2678. DataSet ds = new DataSet ();
  2679. DataTable table1 = ds.Tables.Add ("table1");
  2680. DataTable table2 = ds.Tables.Add ("table2");
  2681. table1.Columns.Add ("col1", typeof (int));
  2682. table2.Columns.Add ("col1", typeof (int));
  2683. table1.Constraints.Add ("uc 1", table1.Columns [0], false);
  2684. table2.Constraints.Add ("fc 1", table1.Columns [0], table2.Columns [0]);
  2685. StringWriter sw = new StringWriter ();
  2686. //should not throw an exception
  2687. ds.WriteXmlSchema (sw);
  2688. }
  2689. [Test]
  2690. public void ReadWriteXmlSchema_Nested ()
  2691. {
  2692. DataSet ds = new DataSet ("dataset");
  2693. ds.Tables.Add ("table1");
  2694. ds.Tables.Add ("table2");
  2695. ds.Tables[0].Columns.Add ("col");
  2696. ds.Tables[1].Columns.Add ("col");
  2697. ds.Relations.Add ("rel", ds.Tables [0].Columns [0],ds.Tables [1].Columns [0], true);
  2698. ds.Relations [0].Nested = true;
  2699. MemoryStream ms = new MemoryStream ();
  2700. ds.WriteXmlSchema (ms);
  2701. DataSet ds1 = new DataSet ();
  2702. ds1.ReadXmlSchema (new MemoryStream (ms.GetBuffer ()));
  2703. // no new relation, and <table>_Id columns, should get created when
  2704. // Relation.Nested = true
  2705. Assert.AreEqual (1, ds1.Relations.Count, "#1");
  2706. Assert.AreEqual (1, ds1.Tables [0].Columns.Count, "#2");
  2707. Assert.AreEqual (1, ds1.Tables [1].Columns.Count, "#3");
  2708. }
  2709. [Test]
  2710. public void ReadXmlSchema_Nested ()
  2711. {
  2712. //when Relation.Nested = false, and the schema is nested, create new relations on <table>_Id
  2713. //columns.
  2714. DataSet ds = new DataSet ();
  2715. ds.ReadXmlSchema ("Test/System.Data/schemas/test017.xsd");
  2716. Assert.AreEqual (2, ds.Relations.Count, "#1");
  2717. Assert.AreEqual (3, ds.Tables [0].Columns.Count, "#2");
  2718. Assert.AreEqual (3, ds.Tables [1].Columns.Count, "#3");
  2719. Assert.AreEqual ("table1_Id_0", ds.Tables [0].Columns [2].ColumnName, "#4");
  2720. Assert.AreEqual ("table1_Id_0", ds.Tables [0].PrimaryKey [0].ColumnName, "#5");
  2721. }
  2722. [Test]
  2723. public void ReadXml_Diffgram_MissingSchema ()
  2724. {
  2725. DataSet ds = new DataSet ();
  2726. ds.Tables.Add ("table");
  2727. ds.Tables [0].Columns.Add ("col1");
  2728. ds.Tables [0].Columns.Add ("col2");
  2729. ds.Tables [0].Rows.Add (new object[] {"a", "b"});
  2730. ds.Tables [0].Rows.Add (new object[] {"a", "b"});
  2731. MemoryStream ms = new MemoryStream ();
  2732. ds.WriteXml (ms, XmlWriteMode.DiffGram);
  2733. DataSet ds1 = new DataSet ();
  2734. ds1.Tables.Add ("table");
  2735. ds1.Tables [0].Columns.Add ("col1");
  2736. // When table schema is missing, it shud load up the data
  2737. // for the existing schema
  2738. ds1.ReadXml (new MemoryStream (ms.GetBuffer ()), XmlReadMode.DiffGram);
  2739. Assert.AreEqual (2, ds1.Tables [0].Rows.Count, "#1");
  2740. Assert.AreEqual (1, ds1.Tables [0].Columns.Count, "#2");
  2741. Assert.AreEqual ("a", ds1.Tables [0].Rows [0][0], "#3");
  2742. Assert.AreEqual ("a", ds1.Tables [0].Rows [1][0], "#4");
  2743. }
  2744. [Test]
  2745. public void WriteXml_Morethan2Relations ()
  2746. {
  2747. DataSet ds = new DataSet ();
  2748. DataTable p1 = ds.Tables.Add ("parent1");
  2749. DataTable p2 = ds.Tables.Add ("parent2");
  2750. DataTable p3 = ds.Tables.Add ("parent3");
  2751. DataTable c1 = ds.Tables.Add ("child");
  2752. c1.Columns.Add ("col1");
  2753. c1.Columns.Add ("col2");
  2754. c1.Columns.Add ("col3");
  2755. c1.Columns.Add ("col4");
  2756. p1.Columns.Add ("col1");
  2757. p2.Columns.Add ("col1");
  2758. p3.Columns.Add ("col1");
  2759. ds.Relations.Add ("rel1", p1.Columns [0], c1.Columns [0], false);
  2760. ds.Relations.Add ("rel2", p2.Columns [0], c1.Columns [1], false);
  2761. ds.Relations.Add ("rel3", p3.Columns [0], c1.Columns [2], false);
  2762. ds.Relations [2].Nested = true;
  2763. p1.Rows.Add (new object[] {"p1"});
  2764. p2.Rows.Add (new object[] {"p2"});
  2765. p3.Rows.Add (new object[] {"p3"});
  2766. c1.Rows.Add (new object[] {"p1","p2","p3","c1"});
  2767. StringWriter sw = new StringWriter ();
  2768. XmlTextWriter xw = new XmlTextWriter (sw);
  2769. ds.WriteXml (xw);
  2770. string dataset_xml = sw.ToString ();
  2771. string child_xml = "<child><col1>p1</col1><col2>p2</col2><col3>p3</col3><col4>c1</col4></child>";
  2772. //the child table data must not be repeated.
  2773. Assert.AreEqual (dataset_xml.IndexOf (child_xml), dataset_xml.LastIndexOf (child_xml), "#1");
  2774. }
  2775. [Test]
  2776. public void MergeTest_ColumnTypeMismatch ()
  2777. {
  2778. DataSet dataSet = new DataSet ();
  2779. dataSet.Tables.Add (new DataTable ());
  2780. dataSet.Tables [0].Columns.Add (new DataColumn ("id", typeof (int)));
  2781. dataSet.Tables [0].Columns.Add (new DataColumn ("name", typeof (string)));
  2782. DataSet ds = new DataSet ();
  2783. ds.Tables.Add (new DataTable ());
  2784. ds.Tables [0].Columns.Add (new DataColumn ("id", typeof (string)));
  2785. try {
  2786. ds.Merge (dataSet, true, MissingSchemaAction.Add);
  2787. Assert.Fail ("#1");
  2788. } catch (DataException e) {}
  2789. ds = new DataSet ();
  2790. ds.Tables.Add (new DataTable ());
  2791. ds.Tables [0].Columns.Add (new DataColumn("id", typeof (string)));
  2792. ds.Merge (dataSet, true, MissingSchemaAction.Ignore);
  2793. Assert.AreEqual ("Table1", ds.Tables [0].TableName, "#2");
  2794. Assert.AreEqual (1, ds.Tables.Count, "#3");
  2795. Assert.AreEqual (1, ds.Tables [0].Columns.Count, "#4");
  2796. Assert.AreEqual (typeof (string), ds.Tables [0].Columns [0].DataType, "#5");
  2797. }
  2798. #if NET_2_0
  2799. [Test]
  2800. public void LoadTest1 ()
  2801. {
  2802. DataSet ds1 = new DataSet ();
  2803. DataSet ds2 = new DataSet ();
  2804. DataTable dt1 = new DataTable ("T1");
  2805. DataTable dt2 = new DataTable ("T2");
  2806. DataTable dt3 = new DataTable ("T1");
  2807. DataTable dt4 = new DataTable ("T2");
  2808. dt1.Columns.Add ("ID", typeof (int));
  2809. dt1.Columns.Add ("Name", typeof (string));
  2810. dt2.Columns.Add ("EmpNO", typeof (int));
  2811. dt2.Columns.Add ("EmpName", typeof (string));
  2812. dt1.Rows.Add (new object[] {1, "Andrews"});
  2813. dt1.Rows.Add (new object[] {2, "Mathew"});
  2814. dt1.Rows.Add (new object[] {3, "Jaccob"});
  2815. dt2.Rows.Add (new object[] {1, "Arul"});
  2816. dt2.Rows.Add (new object[] {2, "Jothi"});
  2817. dt2.Rows.Add (new object[] {3, "Murugan"});
  2818. ds2.Tables.Add (dt1);
  2819. ds2.Tables.Add (dt2);
  2820. ds1.Tables.Add (dt3);
  2821. ds1.Tables.Add (dt4);
  2822. DataTableReader reader = ds2.CreateDataReader ();
  2823. //ds1.Load (reader, LoadOption.PreserveChanges, dt3, dt4);
  2824. ds1.Load (reader, LoadOption.OverwriteChanges, dt3, dt4);
  2825. Assertion.AssertEquals ("DataSet Tables count mistmatch", ds2.Tables.Count, ds1.Tables.Count);
  2826. int i = 0;
  2827. foreach (DataTable dt in ds1.Tables) {
  2828. DataTable dt5 = ds2.Tables[i];
  2829. Assertion.AssertEquals ("Table " + dt.TableName + " row count mistmatch", dt5.Rows.Count, dt.Rows.Count);
  2830. int j = 0;
  2831. DataRow row1;
  2832. foreach (DataRow row in dt.Rows) {
  2833. row1 = dt5.Rows[j];
  2834. for (int k = 0; k < dt.Columns.Count; k++) {
  2835. Assertion.AssertEquals ("DataRow " + k + " mismatch", row1[k], row[k]);
  2836. }
  2837. j++;
  2838. }
  2839. i++;
  2840. }
  2841. }
  2842. [Test]
  2843. public void LoadTest2 ()
  2844. {
  2845. DataSet ds1 = new DataSet ();
  2846. DataSet ds2 = new DataSet ();
  2847. DataTable dt1 = new DataTable ("T1");
  2848. DataTable dt2 = new DataTable ("T2");
  2849. DataTable dt3 = new DataTable ("T1");
  2850. DataTable dt4 = new DataTable ("T2");
  2851. dt1.Columns.Add ("ID", typeof (int));
  2852. dt1.Columns.Add ("Name", typeof (string));
  2853. dt2.Columns.Add ("EmpNO", typeof (int));
  2854. dt2.Columns.Add ("EmpName", typeof (string));
  2855. dt1.Rows.Add (new object[] {1, "Andrews"});
  2856. dt1.Rows.Add (new object[] {2, "Mathew"});
  2857. dt1.Rows.Add (new object[] {3, "Jaccob"});
  2858. dt2.Rows.Add (new object[] {1, "Arul"});
  2859. dt2.Rows.Add (new object[] {2, "Jothi"});
  2860. dt2.Rows.Add (new object[] {3, "Murugan"});
  2861. ds2.Tables.Add (dt1);
  2862. ds2.Tables.Add (dt2);
  2863. ds1.Tables.Add (dt3);
  2864. ds1.Tables.Add (dt4);
  2865. DataTableReader reader = ds2.CreateDataReader ();
  2866. //ds1.Load (reader, LoadOption.PreserveChanges, dt3, dt4);
  2867. ds1.Load (reader, LoadOption.OverwriteChanges, dt3, dt4);
  2868. Assertion.AssertEquals ("DataSet Tables count mistmatch", ds2.Tables.Count, ds1.Tables.Count);
  2869. int i = 0;
  2870. foreach (DataTable dt in ds1.Tables) {
  2871. DataTable dt5 = ds2.Tables[i];
  2872. Assertion.AssertEquals ("Table " + dt.TableName + " row count mistmatch", dt5.Rows.Count, dt.Rows.Count);
  2873. int j = 0;
  2874. DataRow row1;
  2875. foreach (DataRow row in dt.Rows) {
  2876. row1 = dt5.Rows[j];
  2877. for (int k = 0; k < dt.Columns.Count; k++) {
  2878. Assertion.AssertEquals ("DataRow " + k + " mismatch", row1[k], row[k]);
  2879. }
  2880. j++;
  2881. }
  2882. i++;
  2883. }
  2884. }
  2885. #endif
  2886. }
  2887. }