DataRowTest2.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  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.IO;
  31. using System.ComponentModel;
  32. using System.Data;
  33. using MonoTests.System.Data.Utils;
  34. namespace MonoTests.System.Data
  35. {
  36. [TestFixture] public class DataRowTest2
  37. {
  38. bool _rowChanged;
  39. [SetUp]
  40. public void SetUp ()
  41. {
  42. _rowChanged = false;
  43. }
  44. [Test] public void AcceptChanges()
  45. {
  46. DataTable myTable = new DataTable("myTable");
  47. DataRow myRow;
  48. myRow = myTable.NewRow();
  49. myTable.Rows.Add(myRow);
  50. // DataRow AcceptChanges
  51. // DataRowState.Added -> DataRowState.Unchanged
  52. myTable.AcceptChanges();
  53. Assert.AreEqual(DataRowState.Unchanged , myRow.RowState , "DRW1");
  54. }
  55. [Test] public void CancelEdit()
  56. {
  57. DataTable myTable = new DataTable("myTable");
  58. DataColumn dc = new DataColumn("Id",typeof(int));
  59. dc.Unique=true;
  60. myTable.Columns.Add(dc);
  61. myTable.Rows.Add(new object[] {1});
  62. myTable.Rows.Add(new object[] {2});
  63. myTable.Rows.Add(new object[] {3});
  64. DataRow myRow = myTable.Rows[0];
  65. myRow.BeginEdit();
  66. myRow[0] = 7;
  67. myRow.CancelEdit();
  68. // DataRow CancelEdit
  69. Assert.AreEqual(true , (int)myRow[0] == 1, "DRW2");
  70. }
  71. [Test] public void ClearErrors()
  72. {
  73. DataTable dt = new DataTable("myTable");
  74. DataRow dr = dt.NewRow();
  75. dr.RowError = "err";
  76. // DataRow ClearErrors
  77. Assert.AreEqual(true , dr.HasErrors , "DRW3");
  78. // DataRow ClearErrors
  79. dr.ClearErrors();
  80. Assert.AreEqual(false , dr.HasErrors , "DRW4");
  81. }
  82. [Test] public void Delete()
  83. {
  84. DataTable myTable = new DataTable("myTable");
  85. DataColumn dc = new DataColumn("Id",typeof(int));
  86. dc.Unique=true;
  87. myTable.Columns.Add(dc);
  88. myTable.Rows.Add(new object[] {1});
  89. myTable.Rows.Add(new object[] {2});
  90. myTable.Rows.Add(new object[] {3});
  91. myTable.AcceptChanges();
  92. DataRow myRow = myTable.Rows[0];
  93. myRow.Delete();
  94. // Delete1
  95. Assert.AreEqual(DataRowState.Deleted , myRow.RowState , "DRW5");
  96. // Delete2
  97. myTable.AcceptChanges();
  98. Assert.AreEqual(DataRowState.Detached , myRow.RowState , "DRW6");
  99. }
  100. [Test] public void EndEdit()
  101. {
  102. DataTable myTable = new DataTable("myTable");
  103. DataColumn dc = new DataColumn("Id",typeof(int));
  104. dc.Unique=true;
  105. myTable.Columns.Add(dc);
  106. myTable.Rows.Add(new object[] {1});
  107. myTable.Rows.Add(new object[] {2});
  108. myTable.Rows.Add(new object[] {3});
  109. DataRow myRow = myTable.Rows[0];
  110. int iProposed;
  111. //After calling the DataRow object's BeginEdit method, if you change the value, the Current and Proposed values become available
  112. myRow.BeginEdit();
  113. myRow[0] = 7;
  114. iProposed = (int)myRow[0,DataRowVersion.Proposed];
  115. myRow.EndEdit();
  116. // EndEdit
  117. Assert.AreEqual(iProposed , (int)myRow[0,DataRowVersion.Current] , "DRW7");
  118. }
  119. [Test] public void Equals()
  120. {
  121. DataTable myTable = new DataTable("myTable");
  122. DataRow dr1,dr2;
  123. dr1 = myTable.NewRow();
  124. dr2 = myTable.NewRow();
  125. // not equals
  126. Assert.AreEqual(false , dr1.Equals(dr2), "DRW8");
  127. dr1=dr2;
  128. // equals
  129. Assert.AreEqual(true , dr1.Equals(dr2), "DRW9");
  130. }
  131. [Test] public void GetChildRows_ByDataRealtion()
  132. {
  133. DataRow dr;
  134. DataRow[] drArrExcepted,drArrResult;
  135. DataTable dtChild,dtParent;
  136. DataSet ds = new DataSet();
  137. //Create tables
  138. dtChild = DataProvider.CreateChildDataTable();
  139. dtParent= DataProvider.CreateParentDataTable();
  140. //Add tables to dataset
  141. ds.Tables.Add(dtChild);
  142. ds.Tables.Add(dtParent);
  143. dr = dtParent.Rows[0];
  144. //Add Relation
  145. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  146. ds.Relations.Add(dRel);
  147. //Get Excepted result
  148. drArrExcepted = dtChild.Select("ParentId=" + dr["ParentId"]);
  149. //Get Result
  150. drArrResult = dr.GetChildRows(dRel);
  151. // GetChildRows_D
  152. Assert.AreEqual(drArrExcepted, drArrResult, "DRW10");
  153. }
  154. [Test] public void GetChildRows_ByDataRealtionDataRowVersion()
  155. {
  156. DataRow drParent;
  157. DataRow[] drArrExcepted,drArrResult;
  158. DataTable dtChild,dtParent;
  159. DataSet ds = new DataSet();
  160. //Create tables
  161. dtChild = DataProvider.CreateChildDataTable();
  162. dtParent= DataProvider.CreateParentDataTable();
  163. //Add tables to dataset
  164. ds.Tables.Add(dtChild);
  165. ds.Tables.Add(dtParent);
  166. //Add Relation
  167. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  168. ds.Relations.Add(dRel);
  169. drParent = dtParent.Rows[0];
  170. // Teting: DateTime.Now.ToShortTimeString()
  171. //Get Excepted result
  172. drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
  173. //Get Result DataRowVersion.Current
  174. drArrResult = drParent.GetChildRows(dRel,DataRowVersion.Current);
  175. Assert.AreEqual(drArrExcepted, drArrResult, "DRW11");
  176. // Teting: DataRow.GetParentRows_D_D
  177. //Get Excepted result
  178. drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.OriginalRows );
  179. //Get Result DataRowVersion.Current
  180. drArrResult = drParent.GetChildRows(dRel,DataRowVersion.Original );
  181. Assert.AreEqual(drArrExcepted, drArrResult, "DRW12");
  182. // Teting: DataRow.GetParentRows_D_D
  183. //Get Excepted result, in this case Current = Default
  184. drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows);
  185. //Get Result DataRowVersion.Current
  186. drArrResult = drParent.GetChildRows(dRel,DataRowVersion.Default );
  187. Assert.AreEqual(drArrExcepted, drArrResult, "DRW13");
  188. // Teting: DataRow.GetParentRows_D_D
  189. drParent.BeginEdit();
  190. drParent["String1"] = "Value";
  191. //Get Excepted result
  192. drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
  193. //Get Result DataRowVersion.Current
  194. drArrResult = drParent.GetChildRows(dRel,DataRowVersion.Proposed );
  195. Assert.AreEqual(drArrExcepted, drArrResult, "DRW14");
  196. }
  197. [Test] public void GetChildRows_ByName()
  198. {
  199. DataRow dr;
  200. DataRow[] drArrExcepted,drArrResult;
  201. DataTable dtChild,dtParent;
  202. DataSet ds = new DataSet();
  203. //Create tables
  204. dtChild = DataProvider.CreateChildDataTable();
  205. dtParent= DataProvider.CreateParentDataTable();
  206. //Add tables to dataset
  207. ds.Tables.Add(dtChild);
  208. ds.Tables.Add(dtParent);
  209. dr = dtParent.Rows[0];
  210. //Add Relation
  211. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  212. ds.Relations.Add(dRel);
  213. //Get Excepted result
  214. drArrExcepted = dtChild.Select("ParentId=" + dr["ParentId"]);
  215. //Get Result
  216. drArrResult = dr.GetChildRows("Parent-Child");
  217. // GetChildRows_S
  218. Assert.AreEqual(drArrExcepted, drArrResult, "DRW15");
  219. }
  220. [Test] public void GetChildRows_ByNameDataRowVersion()
  221. {
  222. DataRow drParent;
  223. DataRow[] drArrExcepted,drArrResult;
  224. DataTable dtChild,dtParent;
  225. DataSet ds = new DataSet();
  226. //Create tables
  227. dtChild = DataProvider.CreateChildDataTable();
  228. dtParent= DataProvider.CreateParentDataTable();
  229. //Add tables to dataset
  230. ds.Tables.Add(dtChild);
  231. ds.Tables.Add(dtParent);
  232. //Add Relation
  233. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  234. ds.Relations.Add(dRel);
  235. drParent = dtParent.Rows[0];
  236. // GetChildRows_SD 1
  237. //Get Excepted result
  238. drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
  239. //Get Result DataRowVersion.Current
  240. drArrResult = drParent.GetChildRows("Parent-Child",DataRowVersion.Current);
  241. Assert.AreEqual(drArrExcepted, drArrResult, "DRW16");
  242. // GetChildRows_SD 2
  243. //Get Excepted result
  244. drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.OriginalRows );
  245. //Get Result DataRowVersion.Current
  246. drArrResult = drParent.GetChildRows("Parent-Child",DataRowVersion.Original );
  247. Assert.AreEqual(drArrExcepted, drArrResult, "DRW17");
  248. // GetParentRows_SD 3
  249. //Get Excepted result, in this case Current = Default
  250. drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows);
  251. //Get Result DataRowVersion.Current
  252. drArrResult = drParent.GetChildRows("Parent-Child",DataRowVersion.Default );
  253. Assert.AreEqual(drArrExcepted, drArrResult, "DRW18");
  254. // GetParentRows_SD 4
  255. drParent.BeginEdit();
  256. drParent["String1"] = "Value";
  257. //Get Excepted result
  258. drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
  259. //Get Result DataRowVersion.Current
  260. drArrResult = drParent.GetChildRows("Parent-Child",DataRowVersion.Proposed );
  261. Assert.AreEqual(drArrExcepted, drArrResult, "DRW19");
  262. }
  263. [Test] public void GetColumnError_ByIndex()
  264. {
  265. string sColErr = "Error!";
  266. DataTable dt = new DataTable("myTable");
  267. DataColumn dc = new DataColumn("Column1");
  268. dt.Columns.Add(dc);
  269. DataRow dr = dt.NewRow();
  270. // GetColumnError 1
  271. Assert.AreEqual(String.Empty , dr.GetColumnError(0) , "DRW20");
  272. dr.SetColumnError(0,sColErr );
  273. // GetColumnError 2
  274. Assert.AreEqual(sColErr , dr.GetColumnError(0) , "DRW21");
  275. }
  276. [Test] public void GetColumnError_ByName()
  277. {
  278. string sColErr = "Error!";
  279. DataTable dt = new DataTable("myTable");
  280. DataColumn dc = new DataColumn("Column1");
  281. dt.Columns.Add(dc);
  282. DataRow dr = dt.NewRow();
  283. // GetColumnError 1
  284. Assert.AreEqual(String.Empty , dr.GetColumnError("Column1") , "DRW22");
  285. dr.SetColumnError("Column1",sColErr );
  286. // GetColumnError 2
  287. Assert.AreEqual(sColErr , dr.GetColumnError("Column1") , "DRW23");
  288. }
  289. [Test] public void GetColumnsInError()
  290. {
  291. string sColErr = "Error!";
  292. DataColumn[] dcArr;
  293. DataTable dt = new DataTable("myTable");
  294. //init some columns
  295. dt.Columns.Add(new DataColumn());
  296. dt.Columns.Add(new DataColumn());
  297. dt.Columns.Add(new DataColumn());
  298. dt.Columns.Add(new DataColumn());
  299. dt.Columns.Add(new DataColumn());
  300. //init some rows
  301. dt.Rows.Add(new object[] {});
  302. dt.Rows.Add(new object[] {});
  303. dt.Rows.Add(new object[] {});
  304. DataRow dr = dt.Rows[1];
  305. dcArr = dr.GetColumnsInError();
  306. // GetColumnsInError 1
  307. Assert.AreEqual(0, dcArr.Length , "DRW24");
  308. dr.SetColumnError(0,sColErr);
  309. dr.SetColumnError(2,sColErr);
  310. dr.SetColumnError(4,sColErr);
  311. dcArr = dr.GetColumnsInError();
  312. // GetColumnsInError 2
  313. Assert.AreEqual(3, dcArr.Length , "DRW25");
  314. //check that the right columns taken
  315. // GetColumnsInError 3
  316. Assert.AreEqual(dt.Columns[0], dcArr[0], "DRW26");
  317. // GetColumnsInError 4
  318. Assert.AreEqual(dt.Columns[2], dcArr[1], "DRW27");
  319. // GetColumnsInError 5
  320. Assert.AreEqual(dt.Columns[4], dcArr[2], "DRW28");
  321. }
  322. [Test] public new void GetHashCode()
  323. {
  324. int iHashCode;
  325. DataRow dr;
  326. DataTable dt = new DataTable();
  327. dr = dt.NewRow();
  328. iHashCode = dr.GetHashCode();
  329. for (int i=0; i<10; i++)
  330. { //must return the same value each time
  331. // GetHashCode #" + i
  332. Assert.AreEqual(dr.GetHashCode() , iHashCode , "DRW29");
  333. }
  334. }
  335. [Test] public void GetParentRow_ByDataRelation()
  336. {
  337. DataRow drExcepted,drResult,drChild;
  338. DataTable dtChild,dtParent;
  339. DataSet ds = new DataSet();
  340. //Create tables
  341. dtChild = DataProvider.CreateChildDataTable();
  342. dtParent = DataProvider.CreateParentDataTable();
  343. //Add tables to dataset
  344. ds.Tables.Add(dtChild);
  345. ds.Tables.Add(dtParent);
  346. //Add Relation
  347. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  348. ds.Relations.Add(dRel);
  349. //Excepted result
  350. drExcepted = dtParent.Rows[0];
  351. //Get Result
  352. drChild = dtChild.Select("ParentId=" + drExcepted["ParentId"])[0];
  353. drResult = drChild.GetParentRow(dRel);
  354. // GetParentRow_D
  355. Assert.AreEqual(drExcepted.ItemArray, drResult.ItemArray , "DRW30");
  356. }
  357. [Test] public void GetParentRow_ByDataRelationDataRowVersion()
  358. {
  359. DataRow drParent,drChild;
  360. DataRow drArrExcepted,drArrResult;
  361. DataTable dtChild,dtParent;
  362. DataSet ds = new DataSet();
  363. //Create tables
  364. dtChild = DataProvider.CreateChildDataTable();
  365. dtParent= DataProvider.CreateParentDataTable();
  366. //Add tables to dataset
  367. ds.Tables.Add(dtChild);
  368. ds.Tables.Add(dtParent);
  369. //Add Relation
  370. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  371. ds.Relations.Add(dRel);
  372. drParent = dtParent.Rows[0];
  373. drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
  374. // GetParentRow_DD 1
  375. //Get Excepted result
  376. drArrExcepted = drParent;
  377. //Get Result DataRowVersion.Current
  378. drArrResult = drChild.GetParentRow(dRel,DataRowVersion.Current);
  379. Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW31");
  380. // GetParentRow_DD 2
  381. //Get Excepted result
  382. drArrExcepted = drParent;
  383. //Get Result DataRowVersion.Current
  384. drArrResult = drChild.GetParentRow(dRel,DataRowVersion.Original );
  385. Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW32");
  386. // GetParentRow_DD 3
  387. //Get Excepted result, in this case Current = Default
  388. drArrExcepted = drParent;
  389. //Get Result DataRowVersion.Current
  390. drArrResult = drChild.GetParentRow(dRel,DataRowVersion.Default );
  391. Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW33");
  392. // GetParentRow_DD 4
  393. drChild.BeginEdit();
  394. drChild["String1"] = "Value";
  395. //Get Excepted result
  396. drArrExcepted = drParent;
  397. //Get Result DataRowVersion.Current
  398. drArrResult = drChild.GetParentRow(dRel,DataRowVersion.Proposed );
  399. Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW34");
  400. }
  401. [Test] public void GetParentRow_ByName()
  402. {
  403. DataRow drExcepted,drResult,drChild;
  404. DataTable dtChild,dtParent;
  405. DataSet ds = new DataSet();
  406. //Create tables
  407. dtChild = DataProvider.CreateChildDataTable();
  408. dtParent = DataProvider.CreateParentDataTable();
  409. //Add tables to dataset
  410. ds.Tables.Add(dtChild);
  411. ds.Tables.Add(dtParent);
  412. //Add Relation
  413. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  414. ds.Relations.Add(dRel);
  415. //Excepted result
  416. drExcepted = dtParent.Rows[0];
  417. //Get Result
  418. drChild = dtChild.Select("ParentId=" + drExcepted["ParentId"])[0];
  419. drResult = drChild.GetParentRow("Parent-Child");
  420. // GetParentRow_S
  421. Assert.AreEqual(drExcepted.ItemArray, drResult.ItemArray , "DRW35");
  422. }
  423. [Test] public void GetParentRow_ByNameDataRowVersion()
  424. {
  425. DataRow drParent,drChild;
  426. DataRow drArrExcepted,drArrResult;
  427. DataTable dtChild,dtParent;
  428. DataSet ds = new DataSet();
  429. //Create tables
  430. dtChild = DataProvider.CreateChildDataTable();
  431. dtParent= DataProvider.CreateParentDataTable();
  432. //Add tables to dataset
  433. ds.Tables.Add(dtChild);
  434. ds.Tables.Add(dtParent);
  435. //Add Relation
  436. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  437. ds.Relations.Add(dRel);
  438. drParent = dtParent.Rows[0];
  439. drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
  440. // GetParentRow_SD 1
  441. //Get Excepted result
  442. drArrExcepted = drParent;
  443. //Get Result DataRowVersion.Current
  444. drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Current);
  445. Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW36");
  446. // GetParentRow_SD 2
  447. //Get Excepted result
  448. drArrExcepted = drParent;
  449. //Get Result DataRowVersion.Current
  450. drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Original );
  451. Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW37");
  452. // GetParentRow_SD 3
  453. //Get Excepted result, in this case Current = Default
  454. drArrExcepted = drParent;
  455. //Get Result DataRowVersion.Current
  456. drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Default );
  457. Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW38");
  458. // GetParentRow_SD 4
  459. drChild.BeginEdit();
  460. drChild["String1"] = "Value";
  461. //Get Excepted result
  462. drArrExcepted = drParent;
  463. //Get Result DataRowVersion.Current
  464. drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Proposed );
  465. Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW39");
  466. }
  467. [Test] public void GetParentRows_ByDataRelation()
  468. {
  469. DataRow dr;
  470. DataRow[] drArrExcepted,drArrResult;
  471. DataTable dtChild,dtParent;
  472. DataSet ds = new DataSet();
  473. //Create tables
  474. dtChild = DataProvider.CreateChildDataTable();
  475. dtParent = DataProvider.CreateParentDataTable();
  476. //Add tables to dataset
  477. ds.Tables.Add(dtChild);
  478. ds.Tables.Add(dtParent);
  479. dr = dtParent.Rows[0];
  480. //Duplicate several rows in order to create Many to Many relation
  481. dtParent.ImportRow(dr);
  482. dtParent.ImportRow(dr);
  483. dtParent.ImportRow(dr);
  484. //Add Relation
  485. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"],false);
  486. ds.Relations.Add(dRel);
  487. //Get Excepted result
  488. drArrExcepted = dtParent.Select("ParentId=" + dr["ParentId"]);
  489. dr = dtChild.Select("ParentId=" + dr["ParentId"])[0];
  490. //Get Result
  491. drArrResult = dr.GetParentRows(dRel);
  492. // GetParentRows_D
  493. Assert.AreEqual(drArrExcepted, drArrResult, "DRW40");
  494. }
  495. [Test] public void GetParentRows_ByName()
  496. {
  497. DataRow dr;
  498. DataRow[] drArrExcepted,drArrResult;
  499. DataTable dtChild,dtParent;
  500. DataSet ds = new DataSet();
  501. //Create tables
  502. dtChild = DataProvider.CreateChildDataTable();
  503. dtParent = DataProvider.CreateParentDataTable();
  504. //Add tables to dataset
  505. ds.Tables.Add(dtChild);
  506. ds.Tables.Add(dtParent);
  507. dr = dtParent.Rows[0];
  508. //Duplicate several rows in order to create Many to Many relation
  509. dtParent.ImportRow(dr);
  510. dtParent.ImportRow(dr);
  511. dtParent.ImportRow(dr);
  512. //Add Relation
  513. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"],false);
  514. ds.Relations.Add(dRel);
  515. //Get Excepted result
  516. drArrExcepted = dtParent.Select("ParentId=" + dr["ParentId"]);
  517. dr = dtChild.Select("ParentId=" + dr["ParentId"])[0];
  518. //Get Result
  519. drArrResult = dr.GetParentRows("Parent-Child");
  520. // GetParentRows_S
  521. Assert.AreEqual(drArrExcepted, drArrResult, "DRW41");
  522. }
  523. [Test] public void GetParentRows_ByNameDataRowVersion()
  524. {
  525. DataRow drParent,drChild;
  526. DataRow[] drArrExcepted,drArrResult;
  527. DataTable dtChild,dtParent;
  528. DataSet ds = new DataSet();
  529. //Create tables
  530. dtChild = DataProvider.CreateChildDataTable();
  531. dtParent= DataProvider.CreateParentDataTable();
  532. //Add tables to dataset
  533. ds.Tables.Add(dtChild);
  534. ds.Tables.Add(dtParent);
  535. //Add Relation
  536. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"],false);
  537. ds.Relations.Add(dRel);
  538. //Create several copies of the first row
  539. drParent = dtParent.Rows[0]; //row[0] has versions: Default,Current,Original
  540. dtParent.ImportRow(drParent); //row[1] has versions: Default,Current,Original
  541. dtParent.ImportRow(drParent); //row[2] has versions: Default,Current,Original
  542. dtParent.ImportRow(drParent); //row[3] has versions: Default,Current,Original
  543. dtParent.ImportRow(drParent); //row[4] has versions: Default,Current,Original
  544. dtParent.ImportRow(drParent); //row[5] has versions: Default,Current,Original
  545. dtParent.AcceptChanges();
  546. //Get the first child row for drParent
  547. drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
  548. DataRow[] drTemp = dtParent.Select("ParentId=" + drParent["ParentId"]);
  549. // Console.WriteLine("********");
  550. // foreach (DataRow d in drTemp)
  551. // {
  552. // CheckRowVersion(d);
  553. // }
  554. drTemp[0].BeginEdit();
  555. drTemp[0]["String1"] = "NewValue"; //row now has versions: Proposed,Current,Original,Default
  556. drTemp[1].BeginEdit();
  557. drTemp[1]["String1"] = "NewValue"; //row now has versions: Proposed,Current,Original,Default
  558. // Console.WriteLine("********");
  559. // foreach (DataRow d in drTemp)
  560. // {
  561. // CheckRowVersion(d);
  562. // }
  563. // Console.WriteLine("********");
  564. // Check DataRowVersion.Current
  565. //Check DataRowVersion.Current
  566. drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
  567. drArrResult = drChild.GetParentRows("Parent-Child",DataRowVersion.Current);
  568. Assert.AreEqual(drArrExcepted, drArrResult, "DRW42");
  569. //Check DataRowVersion.Current
  570. // Teting: DataRow.GetParentRows_D_D ,DataRowVersion.Original
  571. drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.OriginalRows );
  572. drArrResult = drChild.GetParentRows("Parent-Child",DataRowVersion.Original );
  573. Assert.AreEqual(drArrExcepted, drArrResult, "DRW43");
  574. //Check DataRowVersion.Default
  575. // Teting: DataRow.GetParentRows_D_D ,DataRowVersion.Default
  576. drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows);
  577. drArrResult = drChild.GetParentRows("Parent-Child",DataRowVersion.Default );
  578. Assert.AreEqual(drArrExcepted, drArrResult, "DRW44");
  579. /* .Net don't work as expected
  580. //Check DataRowVersion.Proposed
  581. // Teting: DataRow.GetParentRows_D_D ,DataRowVersion.Proposed
  582. drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.ModifiedCurrent);
  583. //drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.ModifiedOriginal );
  584. drArrResult = drChild.GetParentRows("Parent-Child",DataRowVersion.Proposed );
  585. Assert.AreEqual(drArrExcepted, drArrResult, "DRW45");
  586. */
  587. }
  588. private void CheckRowVersion(DataRow dr)
  589. {
  590. Console.WriteLine("");
  591. if (dr.HasVersion(DataRowVersion.Current)) Console.WriteLine("Has " + DataRowVersion.Current.ToString());
  592. if (dr.HasVersion(DataRowVersion.Default)) Console.WriteLine("Has " + DataRowVersion.Default.ToString());
  593. if (dr.HasVersion(DataRowVersion.Original)) Console.WriteLine("Has " + DataRowVersion.Original.ToString());
  594. if (dr.HasVersion(DataRowVersion.Proposed)) Console.WriteLine("Has " + DataRowVersion.Proposed.ToString());
  595. }
  596. [Test] public new void GetType()
  597. {
  598. Type myType;
  599. DataTable dt = new DataTable();
  600. DataRow dr = dt.NewRow();
  601. myType = typeof(DataRow);
  602. // GetType
  603. Assert.AreEqual(typeof(DataRow), myType , "DRW46");
  604. }
  605. [Test] public void HasErrors()
  606. {
  607. DataTable dt = new DataTable("myTable");
  608. DataRow dr = dt.NewRow();
  609. // HasErrors (default)
  610. Assert.AreEqual(false, dr.HasErrors, "DRW47");
  611. dr.RowError = "Err";
  612. // HasErrors (set/get)
  613. Assert.AreEqual(true , dr.HasErrors , "DRW48");
  614. }
  615. [Test] public void HasVersion_ByDataRowVersion()
  616. {
  617. DataTable t = new DataTable("atable");
  618. t.Columns.Add("id", typeof(int));
  619. t.Columns.Add("name", typeof(string));
  620. t.Columns[0].DefaultValue = 1;
  621. t.Columns[1].DefaultValue = "something";
  622. // row r is detached
  623. DataRow r = t.NewRow();
  624. // HasVersion Test #10
  625. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Current) , "DRW49");
  626. // HasVersion Test #11
  627. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Original) , "DRW50");
  628. // HasVersion Test #12
  629. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW51");
  630. // HasVersion Test #13
  631. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Proposed) , "DRW52");
  632. r[0] = 4;
  633. r[1] = "four";
  634. // HasVersion Test #20
  635. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Current) , "DRW53");
  636. // HasVersion Test #21
  637. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Original) , "DRW54");
  638. // HasVersion Test #22
  639. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW55");
  640. // HasVersion Test #23
  641. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Proposed) , "DRW56");
  642. t.Rows.Add(r);
  643. // now it is "added"
  644. // HasVersion Test #30
  645. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Current) , "DRW57");
  646. // HasVersion Test #31
  647. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Original) , "DRW58");
  648. // HasVersion Test #32
  649. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW59");
  650. // HasVersion Test #33
  651. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW60");
  652. t.AcceptChanges();
  653. // now it is "unchanged"
  654. // HasVersion Test #40
  655. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Current) , "DRW61");
  656. // HasVersion Test #41
  657. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Original) , "DRW62");
  658. // HasVersion Test #42
  659. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW63");
  660. // HasVersion Test #43
  661. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW64");
  662. r.BeginEdit();
  663. r[1] = "newvalue";
  664. // HasVersion Test #50
  665. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Current) , "DRW65");
  666. // HasVersion Test #51
  667. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Original) , "DRW66");
  668. // HasVersion Test #52
  669. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW67");
  670. // HasVersion Test #53
  671. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Proposed) , "DRW68");
  672. r.EndEdit();
  673. // now it is "modified"
  674. // HasVersion Test #60
  675. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Current) , "DRW69");
  676. // HasVersion Test #61
  677. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Original) , "DRW70");
  678. // HasVersion Test #62
  679. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW71");
  680. // HasVersion Test #63
  681. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW72");
  682. // this or t.AcceptChanges
  683. r.AcceptChanges();
  684. // now it is "unchanged" again
  685. // HasVersion Test #70
  686. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Current) , "DRW73");
  687. // HasVersion Test #71
  688. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Original) , "DRW74");
  689. // HasVersion Test #72
  690. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW75");
  691. // HasVersion Test #73
  692. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW76");
  693. r.Delete();
  694. // now it is "deleted"
  695. // HasVersion Test #80
  696. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Current) , "DRW77");
  697. // HasVersion Test #81
  698. Assert.AreEqual(true , r.HasVersion(DataRowVersion.Original) , "DRW78");
  699. // HasVersion Test #82
  700. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Default) , "DRW79");
  701. // HasVersion Test #83
  702. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW80");
  703. r.AcceptChanges();
  704. // back to detached
  705. // HasVersion Test #90
  706. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Current) , "DRW81");
  707. // HasVersion Test #91
  708. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Original) , "DRW82");
  709. // HasVersion Test #92
  710. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Default) , "DRW83");
  711. // HasVersion Test #93
  712. Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW84");
  713. }
  714. [Test] public void IsNull_ByDataColumn()
  715. {
  716. DataTable dt = new DataTable();
  717. DataColumn dc0 = new DataColumn("Col0",typeof(int));
  718. DataColumn dc1 = new DataColumn("Col1",typeof(int));
  719. dt.Columns.Add(dc0);
  720. dt.Columns.Add(dc1);
  721. dt.Rows.Add(new object[] {1234});
  722. DataRow dr = dt.Rows[0];
  723. // IsNull_I 2
  724. Assert.AreEqual(false , dr.IsNull(dc0) , "DRW85");
  725. // IsNull_I 2
  726. Assert.AreEqual(true , dr.IsNull(dc1) , "DRW86");
  727. }
  728. [Test] public void IsNull_ByDataColumnDataRowVersion()
  729. {
  730. DataTable dt = new DataTable();
  731. DataColumn dc0 = new DataColumn("Col0",typeof(int));
  732. DataColumn dc1 = new DataColumn("Col1",typeof(int));
  733. dt.Columns.Add(dc0);
  734. dt.Columns.Add(dc1);
  735. dt.Rows.Add(new object[] {1234});
  736. DataRow dr = dt.Rows[0];
  737. // IsNull - col0 Current
  738. Assert.AreEqual(false, dr.IsNull(dc0,DataRowVersion.Current) , "DRW87");
  739. // IsNull - col1 Current
  740. Assert.AreEqual(true, dr.IsNull(dc1,DataRowVersion.Current) , "DRW88");
  741. // IsNull - col0 Default
  742. Assert.AreEqual(false, dr.IsNull(dc0,DataRowVersion.Default) , "DRW89");
  743. // IsNull - col1 Default
  744. Assert.AreEqual(true, dr.IsNull(dc1,DataRowVersion.Default) , "DRW90");
  745. dr.BeginEdit();
  746. dr[0] = 9; //Change value, Create RowVersion Proposed
  747. // IsNull - col0 Proposed
  748. Assert.AreEqual(false, dr.IsNull(dc0,DataRowVersion.Proposed) , "DRW91");
  749. // IsNull - col1 Proposed
  750. Assert.AreEqual(true, dr.IsNull(dc1,DataRowVersion.Proposed) , "DRW92");
  751. dr.AcceptChanges();
  752. dr.Delete();
  753. // IsNull - col0 Original
  754. Assert.AreEqual(false, dr.IsNull(dc0,DataRowVersion.Original) , "DRW93");
  755. }
  756. [Test] public void IsNull_ByIndex()
  757. {
  758. DataTable dt = new DataTable();
  759. DataColumn dc0 = new DataColumn("Col0",typeof(int));
  760. DataColumn dc1 = new DataColumn("Col1",typeof(int));
  761. dt.Columns.Add(dc0);
  762. dt.Columns.Add(dc1);
  763. dt.Rows.Add(new object[] {1234});
  764. DataRow dr = dt.Rows[0];
  765. // IsNull_I 2
  766. Assert.AreEqual(false , dr.IsNull(0) , "DRW94");
  767. // IsNull_I 2
  768. Assert.AreEqual(true , dr.IsNull(1) , "DRW95");
  769. }
  770. [Test] public void IsNull_ByName()
  771. {
  772. DataTable dt = new DataTable();
  773. DataColumn dc0 = new DataColumn("Col0",typeof(int));
  774. DataColumn dc1 = new DataColumn("Col1",typeof(int));
  775. dt.Columns.Add(dc0);
  776. dt.Columns.Add(dc1);
  777. dt.Rows.Add(new object[] {1234});
  778. DataRow dr = dt.Rows[0];
  779. #region --- assignment ----
  780. // IsNull_S 1
  781. Assert.AreEqual(false, dr.IsNull("Col0"), "DRW96");
  782. // IsNull_S 2
  783. Assert.AreEqual(true, dr.IsNull("Col1"), "DRW97");
  784. #endregion
  785. #region --- bug 3124 ---
  786. // IsNull_S 1
  787. MemoryStream st = new MemoryStream();
  788. StreamWriter sw = new StreamWriter(st);
  789. sw.Write("<?xml version=\"1.0\" standalone=\"yes\"?><NewDataSet>");
  790. sw.Write("<Table><EmployeeNo>9</EmployeeNo></Table>");
  791. sw.Write("</NewDataSet>");
  792. sw.Flush();
  793. st.Position=0;
  794. DataSet ds = new DataSet();
  795. ds.ReadXml(st);
  796. // Here we add the expression column
  797. ds.Tables[0].Columns.Add("ValueListValueMember", typeof(object), "EmployeeNo");
  798. foreach( DataRow row in ds.Tables[0].Rows )
  799. {
  800. // Console.WriteLine(row["ValueListValueMember"].ToString() + " " );
  801. if( row.IsNull("ValueListValueMember") == true )
  802. Assert.AreEqual("Failed", "SubTest", "DRW98");
  803. else
  804. Assert.AreEqual("Passed", "Passed", "DRW99");
  805. }
  806. #endregion
  807. }
  808. [Test] public void Item()
  809. {
  810. // init table with columns
  811. DataTable myTable = new DataTable("myTable");
  812. myTable.Columns.Add(new DataColumn("Id",typeof(int)));
  813. myTable.Columns.Add(new DataColumn("Name",typeof(string)));
  814. DataColumn dc = myTable.Columns[0];
  815. myTable.Rows.Add(new object[] {1,"Ofer"});
  816. myTable.Rows.Add(new object[] {2,"Ofer"});
  817. myTable.AcceptChanges();
  818. DataRow myRow = myTable.Rows[0];
  819. //Start checking
  820. // Item - index
  821. Assert.AreEqual(1 , (int)myRow[0] , "DRW100");
  822. // Item - string
  823. Assert.AreEqual(1 , (int)myRow["Id"] , "DRW101");
  824. // Item - Column
  825. Assert.AreEqual(1 , (int)myRow[dc] , "DRW102");
  826. // Item - index,Current
  827. Assert.AreEqual(1 , (int)myRow[0,DataRowVersion.Current ] , "DRW103");
  828. // Item - string,Current
  829. Assert.AreEqual(1 , (int)myRow["Id",DataRowVersion.Current] , "DRW104");
  830. // Item - columnn,Current
  831. Assert.AreEqual(1 , (int)myRow[dc,DataRowVersion.Current] , "DRW105");
  832. // testMore();
  833. }
  834. /*public void testMore()
  835. {
  836. DataTable dt = DataProvider.CreateParentDataTable();
  837. dt.Rows[0].BeginEdit();
  838. dt.Rows[0][0] = 10;
  839. dt.Rows[0].EndEdit();
  840. dt.AcceptChanges();
  841. }*/
  842. [Test] public void RejectChanges()
  843. {
  844. DataTable dt = new DataTable();
  845. DataColumn dc0 = new DataColumn("Col0",typeof(int));
  846. DataColumn dc1 = new DataColumn("Col1",typeof(int));
  847. dt.Columns.Add(dc0);
  848. dt.Columns.Add(dc1);
  849. dt.Rows.Add(new object[] {1234});
  850. dt.AcceptChanges();
  851. DataRow dr = dt.Rows[0];
  852. dr[0] = 567;
  853. dr[1] = 789;
  854. dr.RejectChanges();
  855. // RejectChanges - row 0
  856. Assert.AreEqual(1234 , (int)dr[0], "DRW106");
  857. // RejectChanges - row 1
  858. Assert.AreEqual(DBNull.Value , dr[1] , "DRW107");
  859. dr.Delete();
  860. dr.RejectChanges();
  861. // RejectChanges - count
  862. Assert.AreEqual(1 , dt.Rows.Count , "DRW108");
  863. }
  864. [Test] public void RowState()
  865. {
  866. DataTable myTable = new DataTable("myTable");
  867. DataColumn dc = new DataColumn("Name",typeof(string));
  868. myTable.Columns.Add(dc);
  869. DataRow myRow;
  870. // Create a new DataRow.
  871. myRow = myTable.NewRow();
  872. // Detached row.
  873. // Detached
  874. Assert.AreEqual(DataRowState.Detached , myRow.RowState , "DRW109");
  875. myTable.Rows.Add(myRow);
  876. // New row.
  877. // Added
  878. Assert.AreEqual(DataRowState.Added , myRow.RowState , "DRW110");
  879. myTable.AcceptChanges();
  880. // Unchanged row.
  881. // Unchanged
  882. Assert.AreEqual(DataRowState.Unchanged , myRow.RowState , "DRW111");
  883. myRow["Name"] = "Scott";
  884. // Modified row.
  885. // Modified
  886. Assert.AreEqual(DataRowState.Modified , myRow.RowState , "DRW112");
  887. myRow.Delete();
  888. // Deleted row.
  889. // Deleted
  890. Assert.AreEqual(DataRowState.Deleted , myRow.RowState , "DRW113");
  891. }
  892. [Test] public void SetColumnError_ByDataColumnError()
  893. {
  894. string sColErr = "Error!";
  895. DataTable dt = new DataTable("myTable");
  896. DataColumn dc = new DataColumn("Column1");
  897. dt.Columns.Add(dc);
  898. DataRow dr = dt.NewRow();
  899. // empty string
  900. Assert.AreEqual(String.Empty, dr.GetColumnError(dc) , "DRW114");
  901. dr.SetColumnError(dc,sColErr );
  902. // error string
  903. Assert.AreEqual(sColErr, dr.GetColumnError(dc) , "DRW115");
  904. }
  905. [Test] public void SetColumnError_ByIndexError()
  906. {
  907. string sColErr = "Error!";
  908. DataTable dt = new DataTable("myTable");
  909. DataColumn dc = new DataColumn("Column1");
  910. dt.Columns.Add(dc);
  911. DataRow dr = dt.NewRow();
  912. // empty string
  913. Assert.AreEqual(String.Empty , dr.GetColumnError(0) , "DRW116");
  914. dr.SetColumnError(0,sColErr );
  915. // error string
  916. Assert.AreEqual(sColErr , dr.GetColumnError(0) , "DRW117");
  917. dr.SetColumnError (0, "");
  918. Assert.AreEqual("", dr.GetColumnError (0) , "DRW118");
  919. }
  920. [Test] public void SetColumnError_ByColumnNameError()
  921. {
  922. string sColErr = "Error!";
  923. DataTable dt = new DataTable("myTable");
  924. DataColumn dc = new DataColumn("Column1");
  925. dt.Columns.Add(dc);
  926. DataRow dr = dt.NewRow();
  927. // empty string
  928. Assert.AreEqual(String.Empty, dr.GetColumnError("Column1") , "DRW118");
  929. dr.SetColumnError("Column1",sColErr );
  930. // error string
  931. Assert.AreEqual(sColErr, dr.GetColumnError("Column1") , "DRW119");
  932. }
  933. [Test] public void SetParentRow_ByDataRow()
  934. {
  935. DataRow drParent,drChild;
  936. DataRow drArrExcepted,drArrResult;
  937. DataTable dtChild,dtParent;
  938. DataSet ds = new DataSet();
  939. //Create tables
  940. dtChild = DataProvider.CreateChildDataTable();
  941. dtParent= DataProvider.CreateParentDataTable();
  942. //Add tables to dataset
  943. ds.Tables.Add(dtChild);
  944. ds.Tables.Add(dtParent);
  945. //Add Relation
  946. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  947. ds.Relations.Add(dRel);
  948. drParent = dtParent.Rows[0];
  949. drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
  950. drChild.SetParentRow(drParent);
  951. //Get Excepted result
  952. drArrExcepted = drParent;
  953. //Get Result DataRowVersion.Current
  954. drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Current);
  955. // SetParentRow
  956. Assert.AreEqual(drArrExcepted , drArrResult, "DRW120");
  957. }
  958. [Test]
  959. public void testMore()
  960. {
  961. DataSet ds = DataProvider.CreateForigenConstraint();
  962. DataRow drParent = ds.Tables[0].Rows[0];
  963. //DataRow[] drArray = ds.Tables[1].Rows[0].GetParentRows(ds.Tables[1].ParentRelations[0]);
  964. ds.Tables[1].Rows[0].SetParentRow(drParent);
  965. }
  966. [Test]
  967. public void test()
  968. {
  969. // test SetParentRow
  970. DataTable parent = DataProvider.CreateParentDataTable();
  971. DataTable child = DataProvider.CreateChildDataTable();
  972. DataRow dr = parent.Rows[0];
  973. dr.Delete();
  974. parent.AcceptChanges();
  975. child.Rows[0].SetParentRow(dr);
  976. }
  977. public void checkForLoops()
  978. {
  979. DataSet ds = new DataSet();
  980. //Create tables
  981. DataTable dtChild = DataProvider.CreateChildDataTable();
  982. DataTable dtParent= DataProvider.CreateParentDataTable();
  983. //Add tables to dataset
  984. ds.Tables.Add(dtChild);
  985. ds.Tables.Add(dtParent);
  986. dtChild.Rows.Clear();
  987. dtParent.Rows.Clear();
  988. dtParent.ChildRelations.Add(dtParent.Columns[0],dtChild.Columns[0]);
  989. dtChild.ChildRelations.Add(dtChild.Columns[0],dtParent.Columns[0]);
  990. dtChild.Rows[0].SetParentRow(dtParent.Rows[0]);
  991. dtParent.Rows[0].SetParentRow(dtChild.Rows[0]);
  992. }
  993. public void checkForLoopsAdvenced()
  994. {
  995. //Create tables
  996. DataTable dtChild = new DataTable();
  997. dtChild.Columns.Add("Col1",typeof(int));
  998. dtChild.Columns.Add("Col2",typeof(int));
  999. DataRelation drl = new DataRelation("drl1",dtChild.Columns[0],dtChild.Columns[1]);
  1000. dtChild.ChildRelations.Add(drl);
  1001. dtChild.Rows[0].SetParentRow(dtChild.Rows[1]);
  1002. dtChild.Rows[1].SetParentRow(dtChild.Rows[0]);
  1003. }
  1004. [Test] public void SetParentRow_ByDataRowDataRelation()
  1005. {
  1006. DataRow drParent,drChild;
  1007. DataRow drArrExcepted,drArrResult;
  1008. DataTable dtChild,dtParent;
  1009. DataSet ds = new DataSet();
  1010. //Create tables
  1011. dtChild = DataProvider.CreateChildDataTable();
  1012. dtParent= DataProvider.CreateParentDataTable();
  1013. //Add tables to dataset
  1014. ds.Tables.Add(dtChild);
  1015. ds.Tables.Add(dtParent);
  1016. //Add Relation
  1017. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  1018. ds.Relations.Add(dRel);
  1019. drParent = dtParent.Rows[0];
  1020. drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
  1021. drChild.SetParentRow(drParent ,dRel);
  1022. //Get Excepted result
  1023. drArrExcepted = drParent;
  1024. //Get Result DataRowVersion.Current
  1025. drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Current);
  1026. // SetParentRow
  1027. Assert.AreEqual(drArrExcepted , drArrResult, "DRW121");
  1028. }
  1029. [Test] public void Table()
  1030. {
  1031. DataTable dt1,dt2;
  1032. dt2 = new DataTable("myTable");
  1033. DataRow dr = dt2.NewRow();
  1034. dt1 = dr.Table;
  1035. // ctor
  1036. Assert.AreEqual(dt2, dt1 , "DRW122");
  1037. }
  1038. [Test] public new void ToString()
  1039. {
  1040. DataRow dr;
  1041. DataTable dtParent;
  1042. dtParent= DataProvider.CreateParentDataTable();
  1043. dr = dtParent.Rows[0];
  1044. // ToString
  1045. Assert.AreEqual(true, dr.ToString().ToLower().StartsWith("system.data.datarow") , "DRW123");
  1046. }
  1047. [Test] public void DataRow_RowError()
  1048. {
  1049. DataTable dt = new DataTable ("myTable");
  1050. DataRow dr = dt.NewRow ();
  1051. Assert.AreEqual ( dr.RowError, string.Empty );
  1052. dr.RowError = "Err";
  1053. Assert.AreEqual ( dr.RowError , "Err" );
  1054. }
  1055. [Test]
  1056. [ExpectedException (typeof (ConstraintException))]
  1057. public void DataRow_RowError2()
  1058. {
  1059. DataTable dt1 = DataProvider.CreateUniqueConstraint();
  1060. dt1.BeginLoadData();
  1061. DataRow dr = dt1.NewRow();
  1062. dr[0] = 3;
  1063. dt1.Rows.Add(dr);
  1064. dt1.EndLoadData();
  1065. }
  1066. [Test]
  1067. [ExpectedException (typeof (ConstraintException))]
  1068. public void DataRow_RowError3()
  1069. {
  1070. DataSet ds= DataProvider.CreateForigenConstraint();
  1071. ds.Tables[0].BeginLoadData();
  1072. ds.Tables[0].Rows[0][0] = 10;
  1073. ds.Tables[0].EndLoadData(); //Foreign constraint violation
  1074. }
  1075. [Test]
  1076. public void TestRowErrors ()
  1077. {
  1078. DataTable table = new DataTable ();
  1079. DataColumn col1 = table.Columns.Add ("col1", typeof (int));
  1080. DataColumn col2 = table.Columns.Add ("col2", typeof (int));
  1081. DataColumn col3 = table.Columns.Add ("col3", typeof (int));
  1082. col1.AllowDBNull = false;
  1083. table.Constraints.Add ("uc", new DataColumn[] {col2,col3}, false);
  1084. table.BeginLoadData ();
  1085. table.Rows.Add (new object[] {null,1,1});
  1086. table.Rows.Add (new object[] {1,1,1});
  1087. try {
  1088. table.EndLoadData ();
  1089. Assert.Fail ("#0");
  1090. } catch (ConstraintException) {}
  1091. Assert.IsTrue (table.HasErrors, "#1");
  1092. DataRow[] rows = table.GetErrors ();
  1093. Assert.AreEqual (2, rows.Length, "#2");
  1094. Assert.AreEqual ("Column 'col1' does not allow DBNull.Value.", table.Rows [0].RowError, "#3");
  1095. Assert.AreEqual ("Column 'col2, col3' is constrained to be unique. Value '1, 1' is already present."
  1096. , table.Rows [1].RowError, "#4");
  1097. Assert.AreEqual (table.Rows [0].RowError, table.Rows [0].GetColumnError (0), "#5");
  1098. Assert.AreEqual (table.Rows [1].RowError, table.Rows [0].GetColumnError (1), "#6");
  1099. Assert.AreEqual (table.Rows [1].RowError, table.Rows [0].GetColumnError (2), "#7");
  1100. Assert.AreEqual ("", table.Rows [1].GetColumnError (0), "#8");
  1101. Assert.AreEqual (table.Rows [1].RowError, table.Rows [1].GetColumnError (1), "#9");
  1102. Assert.AreEqual (table.Rows [1].RowError, table.Rows [1].GetColumnError (2), "#10");
  1103. }
  1104. [Test]
  1105. public void BeginEdit()
  1106. {
  1107. DataTable myTable = new DataTable("myTable");
  1108. DataColumn dc = new DataColumn("Id",typeof(int));
  1109. dc.Unique=true;
  1110. myTable.Columns.Add(dc);
  1111. myTable.Rows.Add(new object[] {1});
  1112. myTable.Rows.Add(new object[] {2});
  1113. myTable.Rows.Add(new object[] {3});
  1114. DataRow myRow = myTable.Rows[0];
  1115. try
  1116. {
  1117. myRow[0] = 2; //row[0] now conflict with row[1]
  1118. Assert.Fail("DRW121: failed to throw ConstraintException");
  1119. }
  1120. catch (ConstraintException) {}
  1121. catch (AssertionException exc) {throw exc;}
  1122. catch (Exception exc)
  1123. {
  1124. Assert.Fail("DRW122: Add. Wrong exception type. Got:" + exc);
  1125. }
  1126. //Will NOT! throw exception
  1127. myRow.BeginEdit();
  1128. myRow[0] = 2; //row[0] now conflict with row[1]
  1129. DataTable dt = DataProvider.CreateParentDataTable();
  1130. DataRow dr = dt.Rows[0];
  1131. dr.Delete();
  1132. try
  1133. {
  1134. dr.BeginEdit();
  1135. Assert.Fail("DRW123: failed to throw DeletedRowInaccessibleException");
  1136. }
  1137. catch (DeletedRowInaccessibleException) {}
  1138. catch (AssertionException exc) {throw exc;}
  1139. catch (Exception exc)
  1140. {
  1141. Assert.Fail("DRW124: Add. Wrong exception type. Got:" + exc);
  1142. }
  1143. }
  1144. [Test]
  1145. public void GetChildRows_DataRelation()
  1146. {
  1147. DataRow dr;
  1148. DataRow[] drArrExcepted,drArrResult;
  1149. DataTable dtChild,dtParent;
  1150. DataSet ds = new DataSet();
  1151. //Create tables
  1152. dtChild = DataProvider.CreateChildDataTable();
  1153. dtParent= DataProvider.CreateParentDataTable();
  1154. //Add tables to dataset
  1155. ds.Tables.Add(dtChild);
  1156. ds.Tables.Add(dtParent);
  1157. dr = dtParent.Rows[0];
  1158. //Add Relation
  1159. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  1160. ds.Relations.Add(dRel);
  1161. //Get Excepted result
  1162. drArrExcepted = dtChild.Select("ParentId=" + dr["ParentId"]);
  1163. //Get Result
  1164. drArrResult = dr.GetChildRows(dRel);
  1165. Assert.AreEqual(drArrExcepted, drArrResult, "DRW125");
  1166. }
  1167. [Test]
  1168. public void GetParentRows_DataRelation_DataRowVersion()
  1169. {
  1170. DataRow drParent,drChild;
  1171. DataRow[] drArrExcepted,drArrResult;
  1172. DataTable dtChild,dtParent;
  1173. DataSet ds = new DataSet();
  1174. //Create tables
  1175. dtChild = DataProvider.CreateChildDataTable();
  1176. dtParent= DataProvider.CreateParentDataTable();
  1177. //Add tables to dataset
  1178. ds.Tables.Add(dtChild);
  1179. ds.Tables.Add(dtParent);
  1180. drParent = dtParent.Rows[0];
  1181. drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
  1182. //Duplicate several rows in order to create Many to Many relation
  1183. dtParent.ImportRow(drParent);
  1184. dtParent.ImportRow(drParent);
  1185. dtParent.ImportRow(drParent);
  1186. //Add Relation
  1187. DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"],false);
  1188. ds.Relations.Add(dRel);
  1189. //Get Excepted result
  1190. drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
  1191. //Get Result DataRowVersion.Current
  1192. drArrResult = drChild.GetParentRows(dRel,DataRowVersion.Current);
  1193. Assert.AreEqual(drArrExcepted, drArrResult, "DRW126");
  1194. //Get Excepted result
  1195. drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.OriginalRows );
  1196. //Get Result DataRowVersion.Current
  1197. drArrResult = drChild.GetParentRows(dRel,DataRowVersion.Original );
  1198. Assert.AreEqual(drArrExcepted, drArrResult, "DRW127");
  1199. //Get Excepted result, in this case Current = Default
  1200. drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows);
  1201. //Get Result DataRowVersion.Current
  1202. drArrResult = drChild.GetParentRows(dRel,DataRowVersion.Default );
  1203. Assert.AreEqual(drArrExcepted, drArrResult, "DRW128");
  1204. try
  1205. {
  1206. DataTable dtOtherParent = DataProvider.CreateParentDataTable();
  1207. DataTable dtOtherChild = DataProvider.CreateChildDataTable();
  1208. DataRelation drl = new DataRelation("newRelation",dtOtherParent.Columns[0],dtOtherChild.Columns[0]);
  1209. drChild.GetParentRows(drl,DataRowVersion.Current);
  1210. Assert.Fail("DRW129: failed to throw ArgumentException");
  1211. }
  1212. catch (ArgumentException) {}
  1213. catch (AssertionException exc) {throw exc;}
  1214. catch (Exception exc)
  1215. {
  1216. Assert.Fail("DRW130: Add. Wrong exception type. Got:" + exc);
  1217. }
  1218. }
  1219. [Test]
  1220. public void ItemArray()
  1221. {
  1222. DataTable dt = GetDataTable();
  1223. DataRow dr = dt.Rows[0];
  1224. Assert.AreEqual(1, (int)dr.ItemArray[0] , "DRW131" );
  1225. Assert.AreEqual("Ofer", (string)dr.ItemArray[1] , "DRW132" );
  1226. dt = GetDataTable();
  1227. dr = dt.Rows[0];
  1228. //Changing row via itemArray
  1229. dt.Rows[0].ItemArray = new object[] {2,"Oren"};
  1230. Assert.AreEqual(2, (Int32)dr.ItemArray[0] , "DRW133" );
  1231. Assert.AreEqual("Oren", (string)dr.ItemArray[1] , "DRW134" );
  1232. try
  1233. {
  1234. dt.Rows[0].ItemArray = new object[] {2,"Oren","some1else"};
  1235. Assert.Fail("DRW135: failed to throw ArgumentException");
  1236. }
  1237. catch (ArgumentException) {}
  1238. catch (AssertionException exc) {throw exc;}
  1239. catch (Exception exc)
  1240. {
  1241. Assert.Fail("DRW136: Add. Wrong exception type. Got:" + exc);
  1242. }
  1243. }
  1244. [Test]
  1245. public void ItemArray_NewTable ()
  1246. {
  1247. DataTable dt = new DataTable("Customers");
  1248. dt.Columns.Add("name", typeof (string));
  1249. dt.Columns.Add("address", typeof (string));
  1250. dt.Columns.Add("phone", typeof (string));
  1251. DataRow dr = dt.NewRow();
  1252. dr["name"] = "myName";
  1253. dr["address"] = "myAddress";
  1254. dr["phone"] = "myPhone";
  1255. // Should not throw RowNotInTableException
  1256. object[] obj = dr.ItemArray;
  1257. }
  1258. private DataTable GetDataTable()
  1259. {
  1260. DataTable dt = new DataTable("myTable");
  1261. dt.Columns.Add("Id",typeof(int));
  1262. dt.Columns.Add("Name",typeof(string));
  1263. DataRow dr = dt.NewRow();
  1264. dr.ItemArray = new object[] {1,"Ofer"};
  1265. dt.Rows.Add(dr);
  1266. return dt;
  1267. }
  1268. [Test]
  1269. public void RowError()
  1270. {
  1271. DataTable dt = new DataTable("myTable");
  1272. DataRow dr = dt.NewRow();
  1273. Assert.AreEqual(string.Empty , dr.RowError, "DRW137");
  1274. dr.RowError = "Err";
  1275. Assert.AreEqual("Err", dr.RowError , "DRW138" );
  1276. DataTable dt1 = DataProvider.CreateUniqueConstraint();
  1277. try
  1278. {
  1279. dt1.BeginLoadData();
  1280. dr = dt1.NewRow();
  1281. dr[0] = 3;
  1282. dt1.Rows.Add(dr);
  1283. dt1.EndLoadData();
  1284. Assert.Fail("DRW139: failed to throw ConstraintException");
  1285. }
  1286. catch (ConstraintException)
  1287. {
  1288. Assert.AreEqual(2,dt1.GetErrors().Length,"DRW141");
  1289. Assert.AreEqual(true,dt1.GetErrors()[0].RowError.Length > 10,"DRW142");
  1290. Assert.AreEqual(true,dt1.GetErrors()[1].RowError.Length > 10,"DRW143");
  1291. }
  1292. catch (AssertionException exc) {throw exc;}
  1293. catch (Exception exc)
  1294. {
  1295. Assert.Fail("DRW144: Wrong exception type. Got:" + exc);
  1296. }
  1297. DataSet ds=null;
  1298. try
  1299. {
  1300. ds= DataProvider.CreateForigenConstraint();
  1301. ds.Tables[0].BeginLoadData();
  1302. ds.Tables[0].Rows[0][0] = 10; //Forigen constraint violation
  1303. //ds.Tables[0].AcceptChanges();
  1304. ds.Tables[0].EndLoadData();
  1305. Assert.Fail("DRW139: failed to throw ConstraintException");
  1306. }
  1307. catch (ConstraintException)
  1308. {
  1309. Assert.AreEqual(3,ds.Tables[1].GetErrors().Length,"DRW145");
  1310. for(int index=0;index<3;index++)
  1311. {
  1312. Assert.AreEqual(true,ds.Tables[1].GetErrors()[index].RowError.Length > 10,"RDW146");
  1313. }
  1314. }
  1315. catch (AssertionException exc) {throw exc;}
  1316. catch (Exception exc)
  1317. {
  1318. Assert.Fail("DRW147: Wrong exception type. Got:" + exc);
  1319. }
  1320. }
  1321. [Test]
  1322. public void bug78885 ()
  1323. {
  1324. DataSet ds = new DataSet ();
  1325. DataTable t = ds.Tables.Add ("table");
  1326. DataColumn id;
  1327. id = t.Columns.Add ("userID", Type.GetType ("System.Int32"));
  1328. id.AutoIncrement = true;
  1329. t.Columns.Add ("name", Type.GetType ("System.String"));
  1330. t.Columns.Add ("address", Type.GetType ("System.String"));
  1331. t.Columns.Add ("zipcode", Type.GetType ("System.Int32"));
  1332. t.PrimaryKey = new DataColumn [] { id };
  1333. DataRow tempRow;
  1334. tempRow = t.NewRow ();
  1335. tempRow ["name"] = "Joan";
  1336. tempRow ["address"] = "Balmes 152";
  1337. tempRow ["zipcode"] = "1";
  1338. t.Rows.Add (tempRow);
  1339. t.RowChanged += new DataRowChangeEventHandler (RowChangedHandler);
  1340. /* neither of the calls to EndEdit below generate a RowChangedHandler on MS. the first one does on mono */
  1341. t.DefaultView [0].BeginEdit ();
  1342. t.DefaultView [0].EndEdit (); /* this generates a call to the row changed handler */
  1343. t.DefaultView [0].EndEdit (); /* this doesn't */
  1344. Assert.IsFalse (_rowChanged);
  1345. }
  1346. private void RowChangedHandler (object sender, DataRowChangeEventArgs e)
  1347. {
  1348. _rowChanged = true;
  1349. }
  1350. #if NET_2_0
  1351. string SetAddedModified_ErrMsg = "SetAdded and SetModified can only be called on DataRows with Unchanged DataRowState.";
  1352. [Test]
  1353. public void SetAdded_test()
  1354. {
  1355. DataTable table = new DataTable();
  1356. DataRow row = table.NewRow();
  1357. try {
  1358. row.SetAdded();
  1359. Assert.Fail ("#1");
  1360. } catch (InvalidOperationException e) {
  1361. Assert.AreEqual (SetAddedModified_ErrMsg, e.Message, "#2");
  1362. }
  1363. table.Columns.Add("col1", typeof(int));
  1364. table.Columns.Add("col2", typeof(int));
  1365. table.Columns.Add("col3", typeof(int));
  1366. row = table.Rows.Add(new object[] { 1, 2, 3 });
  1367. Assert.AreEqual(DataRowState.Added, row.RowState, "#1");
  1368. try {
  1369. row.SetAdded();
  1370. Assert.Fail ("#2");
  1371. } catch (InvalidOperationException e) {
  1372. Assert.AreEqual (SetAddedModified_ErrMsg, e.Message, "#2");
  1373. }
  1374. Assert.AreEqual(DataRowState.Added, row.RowState, "#2");
  1375. row.AcceptChanges();
  1376. row[0] = 10;
  1377. Assert.AreEqual(DataRowState.Modified, row.RowState, "#5");
  1378. try {
  1379. row.SetAdded();
  1380. Assert.Fail ("#3");
  1381. } catch (InvalidOperationException e) {
  1382. Assert.AreEqual (SetAddedModified_ErrMsg, e.Message, "#2");
  1383. }
  1384. row.AcceptChanges();
  1385. Assert.AreEqual(DataRowState.Unchanged, row.RowState, "#3");
  1386. row.SetAdded();
  1387. Assert.AreEqual(DataRowState.Added, row.RowState, "#4");
  1388. }
  1389. [Test]
  1390. public void setAdded_testRollback ()
  1391. {
  1392. DataTable table = new DataTable ();
  1393. table.Columns.Add ("col1", typeof (int));
  1394. table.Columns.Add ("col2", typeof (int));
  1395. table.Rows.Add (new object[] {1,1});
  1396. table.AcceptChanges ();
  1397. table.Rows [0].SetAdded ();
  1398. table.RejectChanges ();
  1399. Assert.AreEqual (0, table.Rows.Count, "#1");
  1400. }
  1401. [Test]
  1402. public void SetModified_test()
  1403. {
  1404. DataTable table = new DataTable();
  1405. DataRow row = table.NewRow();
  1406. try {
  1407. row.SetModified ();
  1408. } catch (InvalidOperationException) {}
  1409. table.Columns.Add("col1", typeof(int));
  1410. table.Columns.Add("col2", typeof(int));
  1411. table.Columns.Add("col3", typeof(int));
  1412. row = table.Rows.Add(new object[] { 1, 2, 3 });
  1413. Assert.AreEqual(DataRowState.Added, row.RowState, "#1");
  1414. try {
  1415. row.SetModified();
  1416. Assert.Fail ("#1");
  1417. } catch (InvalidOperationException e) {
  1418. Assert.AreEqual (SetAddedModified_ErrMsg, e.Message, "#2");
  1419. }
  1420. row.AcceptChanges();
  1421. row[0] = 10;
  1422. Assert.AreEqual(DataRowState.Modified, row.RowState, "#5");
  1423. try {
  1424. row.SetModified ();
  1425. Assert.Fail ("#2");
  1426. } catch (InvalidOperationException e) {
  1427. Assert.AreEqual (SetAddedModified_ErrMsg, e.Message, "#2");
  1428. }
  1429. row.AcceptChanges();
  1430. Assert.AreEqual(DataRowState.Unchanged, row.RowState, "#3");
  1431. row.SetModified ();
  1432. Assert.AreEqual(DataRowState.Modified, row.RowState, "#4");
  1433. }
  1434. [Test]
  1435. public void setModified_testRollback()
  1436. {
  1437. DataTable table = new DataTable();
  1438. table.Columns.Add("col1", typeof(int));
  1439. table.Columns.Add("col2", typeof(int));
  1440. DataRow row = table.Rows.Add(new object[] { 1, 1 });
  1441. table.AcceptChanges();
  1442. row.SetModified ();
  1443. Assert.AreEqual(row.RowState, DataRowState.Modified, "#0");
  1444. Assert.AreEqual(1, row [0, DataRowVersion.Current], "#1");
  1445. Assert.AreEqual(1, row [0, DataRowVersion.Original], "#2");
  1446. table.RejectChanges ();
  1447. Assert.AreEqual(row.RowState, DataRowState.Unchanged, "#3");
  1448. }
  1449. #endif
  1450. }
  1451. }