PageRenderTime 38ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/System.Data/Test/System.Data/DataTableTest2.cs

https://bitbucket.org/danipen/mono
C# | 2554 lines | 1789 code | 410 blank | 355 comment | 140 complexity | 1599bbed6e63ae4f048fc62ea33bcaa2 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // Authors:
  2. // Rafael Mizrahi <rafim@mainsoft.com>
  3. // Erez Lotan <erezl@mainsoft.com>
  4. // Oren Gurfinkel <oreng@mainsoft.com>
  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 System;
  29. using System.Collections;
  30. using System.Data;
  31. using System.Globalization;
  32. using System.Collections.Generic;
  33. using MonoTests.System.Data.Utils;
  34. using NUnit.Framework;
  35. namespace MonoTests_System.Data
  36. {
  37. [TestFixture]
  38. public class DataTableTest2
  39. {
  40. private bool _EventTriggered;
  41. private bool EventRaised;
  42. private bool EventValues;
  43. class ProtectedTestClass : DataTable
  44. {
  45. public ProtectedTestClass ()
  46. {
  47. this.Columns.Add ("Id", typeof (int));
  48. this.Columns.Add ("Value", typeof (string));
  49. this.Rows.Add (new object[] {1, "one"});
  50. this.Rows.Add (new object[] {2, "two"});
  51. this.AcceptChanges ();
  52. }
  53. public void OnColumnChanged_Test ()
  54. {
  55. OnColumnChanged (new DataColumnChangeEventArgs (
  56. this.Rows [0], this.Columns ["Value"],
  57. "NewValue"));
  58. }
  59. public void OnColumnChanging_Test ()
  60. {
  61. OnColumnChanging (new DataColumnChangeEventArgs (
  62. this.Rows [0], this.Columns ["Value"],
  63. "NewValue"));
  64. }
  65. public void OnRemoveColumn_Test ()
  66. {
  67. OnRemoveColumn (this.Columns [0]);
  68. }
  69. public DataTable CreateInstance_Test ()
  70. {
  71. return CreateInstance ();
  72. }
  73. public void OnRowChanged_Test (DataRowAction drAction)
  74. {
  75. base.OnRowChanged (new DataRowChangeEventArgs (this.Rows [0], drAction));
  76. }
  77. public void OnRowChanging_Test (DataRowAction drAction)
  78. {
  79. base.OnRowChanging (new DataRowChangeEventArgs (this.Rows [0], drAction));
  80. }
  81. public void OnRowDeleted_Test (DataRowAction drAction)
  82. {
  83. base.OnRowDeleted (new DataRowChangeEventArgs (this.Rows [0], drAction));
  84. }
  85. public void OnRowDeleting_Test (DataRowAction drAction)
  86. {
  87. base.OnRowDeleting (new DataRowChangeEventArgs (this.Rows [0], drAction));
  88. }
  89. }
  90. [Test]
  91. public void AcceptChanges ()
  92. {
  93. String sNewValue = "NewValue";
  94. DataRow drModified,drDeleted,drAdded;
  95. DataTable dt = DataProvider.CreateParentDataTable();
  96. drModified = dt.Rows[0];
  97. drModified[1] = sNewValue; //DataRowState = Modified, DataRowVersion = Proposed
  98. drDeleted = dt.Rows[1];
  99. drDeleted.Delete(); //DataRowState = Deleted
  100. drAdded = dt.NewRow();
  101. dt.Rows.Add(drAdded); //DataRowState = Added
  102. dt.AcceptChanges();
  103. // AcceptChanges - Unchanged1
  104. Assert.AreEqual(DataRowState.Unchanged, drModified.RowState, "DT1");
  105. // AcceptChanges - Current
  106. Assert.AreEqual(sNewValue, drModified[1,DataRowVersion.Current], "DT2");
  107. // AcceptChanges - Unchanged2
  108. Assert.AreEqual(DataRowState.Unchanged, drAdded.RowState, "DT3");
  109. // AcceptChanges - Detached
  110. Assert.AreEqual(DataRowState.Detached, drDeleted.RowState, "DT4");
  111. }
  112. [Test]
  113. public void ChildRelations ()
  114. {
  115. DataTable dtChild,dtParent;
  116. DataSet ds = new DataSet();
  117. //Create tables
  118. dtChild = DataProvider.CreateChildDataTable();
  119. dtParent= DataProvider.CreateParentDataTable();
  120. //Add tables to dataset
  121. ds.Tables.Add(dtChild);
  122. ds.Tables.Add(dtParent);
  123. DataRelationCollection drlCollection;
  124. DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  125. // Checking ChildRelations - default value
  126. //Check default
  127. drlCollection = dtParent.ChildRelations;
  128. Assert.AreEqual(0, drlCollection.Count, "DT5");
  129. ds.Relations.Add(drl);
  130. drlCollection = dtParent.ChildRelations;
  131. // Checking ChildRelations Count
  132. Assert.AreEqual(1, drlCollection.Count, "DT6");
  133. // Checking ChildRelations Value
  134. Assert.AreEqual(drl, drlCollection [0], "DT7");
  135. }
  136. [Test]
  137. public void Clear ()
  138. {
  139. DataTable dt = DataProvider.CreateParentDataTable();
  140. dt.Clear();
  141. Assert.AreEqual(0, dt.Rows.Count, "DT8");
  142. }
  143. [Test]
  144. public void Clone ()
  145. {
  146. DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
  147. dt2.Constraints.Add ("Unique", dt2.Columns[0], true);
  148. dt2.Columns[0].DefaultValue = 7;
  149. dt1 = dt2.Clone ();
  150. for (int i = 0; i < dt2.Constraints.Count; i++) {
  151. // Clone - Constraints[{0}],i)
  152. Assert.AreEqual (dt2.Constraints [i].ConstraintName,
  153. dt1.Constraints[i].ConstraintName, "DT9");
  154. }
  155. for (int i = 0; i < dt2.Columns.Count; i++) {
  156. // Clone - Columns[{0}].ColumnName,i)
  157. Assert.AreEqual(dt2.Columns[i].ColumnName, dt1.Columns[i].ColumnName, "DT10");
  158. // Clone - Columns[{0}].DataType,i)
  159. Assert.AreEqual(dt2.Columns[i].DataType, dt1.Columns[i].DataType, "DT11");
  160. }
  161. }
  162. [Test]
  163. public void ColumnChanged ()
  164. {
  165. DataTable dt = DataProvider.CreateParentDataTable();
  166. dt.ColumnChanged += new DataColumnChangeEventHandler (Column_Changed);
  167. _EventTriggered = false;
  168. // ColumnChanged - EventTriggered
  169. dt.Rows[0][1] = "NewValue";
  170. Assert.IsTrue (_EventTriggered, "DT12");
  171. _EventTriggered = false;
  172. dt.ColumnChanged -= new DataColumnChangeEventHandler (Column_Changed);
  173. // ColumnChanged - NO EventTriggered
  174. dt.Rows[0][1] = "VeryNewValue";
  175. Assert.IsFalse (_EventTriggered, "DT13");
  176. }
  177. private void Column_Changed (object sender, DataColumnChangeEventArgs e)
  178. {
  179. _EventTriggered = true;
  180. }
  181. [Test]
  182. public void ColumnChanging ()
  183. {
  184. DataTable dt = DataProvider.CreateParentDataTable();
  185. dt.ColumnChanging += new DataColumnChangeEventHandler (Column_Changeding);
  186. _EventTriggered = false;
  187. // ColumnChanged - EventTriggered
  188. dt.Rows[0][1] = "NewValue";
  189. Assert.IsTrue (_EventTriggered, "DT14");
  190. _EventTriggered = false;
  191. dt.ColumnChanging -= new DataColumnChangeEventHandler (Column_Changeding);
  192. // ColumnChanged - NO EventTriggered
  193. dt.Rows[0][1] = "VeryNewValue";
  194. Assert.IsFalse (_EventTriggered, "DT15");
  195. }
  196. private void Column_Changeding (object sender, DataColumnChangeEventArgs e)
  197. {
  198. _EventTriggered = true;
  199. }
  200. [Test]
  201. public void Columns ()
  202. {
  203. DataTable dtParent = DataProvider.CreateParentDataTable();
  204. DataColumnCollection dcl = dtParent.Columns;
  205. Assert.IsNotNull (dcl, "#A1");
  206. Assert.AreEqual(6, dcl.Count, "#A2");
  207. dtParent.Columns.Add(new DataColumn("Test"));
  208. Assert.AreEqual(7, dcl.Count, "#A3");
  209. DataColumn tmp = dtParent.Columns["TEST"];
  210. Assert.AreEqual(dtParent.Columns["Test"], tmp, "#A4");
  211. dtParent.Columns.Add(new DataColumn("test"));
  212. Assert.AreEqual(8, dcl.Count, "#A5");
  213. try {
  214. tmp = dtParent.Columns ["TEST"];
  215. Assert.Fail("#B1");
  216. } catch (ArgumentException ex) {
  217. // The given name 'TEST' matches at least two
  218. // names in the collection object with different
  219. // cases, but does not match either of them with
  220. // the same case
  221. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  222. Assert.IsNull (ex.InnerException, "#B3");
  223. Assert.IsNotNull (ex.Message, "#B4");
  224. Assert.IsTrue (ex.Message.IndexOf ("'TEST'") != -1, "#B5");
  225. Assert.IsNull (ex.ParamName, "#B6");
  226. }
  227. }
  228. [Test]
  229. public void Compute ()
  230. {
  231. DataTable dt = DataProvider.CreateChildDataTable();
  232. //Get expected
  233. DataRow[] drArr = dt.Select("ParentId=1");
  234. Int64 iExSum = 0;
  235. foreach (DataRow dr in drArr)
  236. iExSum += (int)dr["ChildId"];
  237. object objCompute = null;
  238. // Compute - sum values
  239. objCompute = dt.Compute ("Sum(ChildId)", "ParentId=1");
  240. Assert.AreEqual(Int64.Parse (objCompute.ToString()), Int64.Parse(iExSum.ToString()), "DT23");
  241. // Compute - sum type
  242. Assert.AreEqual(typeof (Int64), objCompute.GetType (), "DT24");
  243. //get expected
  244. double iExAvg = 0;
  245. drArr = dt.Select("ParentId=5");
  246. foreach (DataRow dr in drArr)
  247. iExAvg += (double)dr["ChildDouble"];
  248. iExAvg = iExAvg / drArr.Length;
  249. // Compute - Avg value
  250. objCompute = dt.Compute("Avg(ChildDouble)", "ParentId=5");
  251. Assert.AreEqual(double.Parse(objCompute.ToString()), double.Parse(iExAvg.ToString()), "DT25");
  252. // Compute - Avg type
  253. Assert.AreEqual(typeof(double), objCompute.GetType(), "DT26");
  254. }
  255. [Test]
  256. public void Constraints ()
  257. {
  258. DataTable dtParent;
  259. ConstraintCollection consColl;
  260. dtParent= DataProvider.CreateParentDataTable();
  261. consColl = dtParent.Constraints;
  262. // Checking Constraints != null
  263. Assert.IsNotNull (consColl, "DT27");
  264. // Checking Constraints Count
  265. Assert.AreEqual (0, consColl.Count, "DT28");
  266. // Checking Constraints Count
  267. //Add primary key
  268. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
  269. Assert.AreEqual (1, consColl.Count, "DT29");
  270. }
  271. [Test]
  272. public void Copy ()
  273. {
  274. DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
  275. dt2.Constraints.Add("Unique",dt2.Columns[0],true);
  276. dt2.Columns[0].DefaultValue=7;
  277. dt1 = dt2.Copy();
  278. for (int i = 0; i < dt2.Constraints.Count; i++) {
  279. // Copy - Constraints[{0}],i)
  280. Assert.AreEqual (dt2.Constraints[i].ConstraintName,
  281. dt1.Constraints[i].ConstraintName, "DT30");
  282. }
  283. for (int i = 0; i < dt2.Columns.Count; i++) {
  284. // Copy - Columns[{0}].ColumnName,i)
  285. Assert.AreEqual (dt2.Columns [i].ColumnName, dt1.Columns [i].ColumnName, "DT31");
  286. // Copy - Columns[{0}].DataType,i)
  287. Assert.AreEqual (dt2.Columns [i].DataType, dt1.Columns[i].DataType, "DT32");
  288. }
  289. DataRow[] drArr1,drArr2;
  290. drArr1 = dt1.Select (string.Empty);
  291. drArr2 = dt2.Select (string.Empty);
  292. for (int i = 0; i < drArr1.Length; i++) {
  293. // Copy - Data [ParentId]{0} ,i)
  294. Assert.AreEqual (drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT33");
  295. // Copy - Data [String1]{0} ,i)
  296. Assert.AreEqual (drArr2[i]["String1"], drArr1[i]["String1"], "DT34");
  297. // Copy - Data [String2]{0} ,i)
  298. Assert.AreEqual (drArr2[i]["String2"], drArr1[i]["String2"], "DT35");
  299. }
  300. }
  301. [Test]
  302. public void CreateInstance ()
  303. {
  304. // CreateInstance
  305. ProtectedTestClass C = new ProtectedTestClass();
  306. DataTable dt = C.CreateInstance_Test();
  307. Assert.IsNotNull(dt, "DT36");
  308. }
  309. [Test]
  310. public void DataSet ()
  311. {
  312. DataTable dtParent;
  313. DataSet ds;
  314. dtParent= DataProvider.CreateParentDataTable();
  315. ds = dtParent.DataSet;
  316. // Checking DataSet == null
  317. Assert.IsNull(ds, "DT37");
  318. // Checking DataSet != null
  319. ds = new DataSet("MyDataSet");
  320. ds.Tables.Add(dtParent);
  321. Assert.IsNotNull(dtParent.DataSet, "DT38");
  322. // Checking DataSet Name
  323. Assert.AreEqual("MyDataSet", dtParent.DataSet.DataSetName, "DT39");
  324. }
  325. [Test]
  326. public void DefaultView ()
  327. {
  328. DataTable dtParent;
  329. DataView dv;
  330. dtParent= DataProvider.CreateParentDataTable();
  331. dv = dtParent.DefaultView;
  332. Assert.IsNotNull (dv, "DT40");
  333. }
  334. [Test]
  335. public void EndLoadData ()
  336. {
  337. DataTable dt = DataProvider.CreateParentDataTable();
  338. dt.Columns[0].AllowDBNull = false;
  339. // EndLoadData
  340. dt.BeginLoadData();
  341. dt.LoadDataRow(new object [] { null, "A", "B" }, false);
  342. try {
  343. dt.EndLoadData ();
  344. Assert.Fail ("#1");
  345. } catch (ConstraintException ex) {
  346. // Failed to enable constraints. One or more rows
  347. // contain values violating non-null, unique, or
  348. // foreign-key constraints
  349. Assert.AreEqual (typeof (ConstraintException), ex.GetType (), "#2");
  350. Assert.IsNull (ex.InnerException, "#3");
  351. Assert.IsNotNull (ex.Message, "#4");
  352. }
  353. }
  354. [Test] // LoadDataRow (Object [], Boolean)
  355. public void LoadDataRow1_Column_ReadOnly ()
  356. {
  357. DataTable dt = new DataTable ("myTable");
  358. DataColumn dcId = new DataColumn ("Id", typeof (int));
  359. dcId.ReadOnly = true;
  360. dt.Columns.Add (dcId);
  361. DataColumn dcName = new DataColumn ("Name", typeof (string));
  362. dcName.ReadOnly = true;
  363. dt.Columns.Add (dcName);
  364. DataColumn dcPassword = new DataColumn ("Password", typeof (string));
  365. dt.Columns.Add (dcPassword);
  366. dt.PrimaryKey = new DataColumn [] { dcId };
  367. dt.Rows.Add (new object [] { 5, "Mono", "guess" });
  368. dt.AcceptChanges ();
  369. dt.LoadDataRow (new object [] { 5, "SysData", "what" }, true);
  370. Assert.AreEqual (1, dt.Rows.Count, "#1");
  371. DataRow row = dt.Rows.Find (5);
  372. Assert.IsNotNull (row, "#2");
  373. Assert.AreEqual (5, row [dcId], "#3");
  374. Assert.AreEqual ("SysData", row [dcName], "#4");
  375. Assert.AreEqual ("what", row [dcPassword], "#5");
  376. Assert.AreEqual (DataRowState.Unchanged, row.RowState, "#6");
  377. }
  378. [Test]
  379. public void LoadDataRow_DuplicateValues ()
  380. {
  381. DataTable table = new DataTable ();
  382. table.Columns.Add ("col1", typeof (int));
  383. table.Columns.Add ("col2", typeof (int));
  384. table.PrimaryKey = new DataColumn[] {table.Columns [0]};
  385. table.BeginLoadData ();
  386. table.LoadDataRow (new object[] {1, 1}, false);
  387. table.LoadDataRow (new object[] {1, 10}, false);
  388. try {
  389. table.EndLoadData ();
  390. Assert.Fail ("#1");
  391. } catch (ConstraintException ex) {
  392. // Failed to enable constraints. One or more rows
  393. // contain values violating non-null, unique, or
  394. // foreign-key constraints
  395. Assert.AreEqual (typeof (ConstraintException), ex.GetType (), "#2");
  396. Assert.IsNull (ex.InnerException, "#3");
  397. Assert.IsNotNull (ex.Message, "#4");
  398. }
  399. }
  400. [Test]
  401. public void LoadDataRow_WithoutBeginLoadData ()
  402. {
  403. DataTable table = new DataTable ();
  404. table.Columns.Add ("col1", typeof (int));
  405. table.Columns.Add ("col2", typeof (int));
  406. table.PrimaryKey = new DataColumn[] {table.Columns [0]};
  407. table.Rows.Add (new object[] {1,1});
  408. table.AcceptChanges ();
  409. table.LoadDataRow (new object[] {10,1}, false);
  410. DataRow row = table.Rows.Find (10);
  411. Assert.IsNotNull (row, "#1");
  412. Assert.AreEqual (1, row [1], "#2");
  413. Assert.AreEqual (DataRowState.Added, row.RowState, "#3");
  414. table.AcceptChanges ();
  415. table.LoadDataRow (new object[] {10,2}, true);
  416. row = table.Rows.Find (10);
  417. Assert.IsNotNull (row, "#4");
  418. Assert.AreEqual (2, row [1], "#5");
  419. Assert.AreEqual (DataRowState.Unchanged, row.RowState, "#6");
  420. table.LoadDataRow (new object[] {1,2}, false);
  421. row = table.Rows.Find (1);
  422. Assert.IsNotNull (row, "#7");
  423. Assert.AreEqual (2, row [1], "#8");
  424. Assert.AreEqual (DataRowState.Modified, table.Rows.Find (1).RowState, "#9");
  425. table.LoadDataRow (new object[] {1,3}, true);
  426. row = table.Rows.Find (1);
  427. Assert.IsNotNull (row, "#10");
  428. Assert.AreEqual (3, row [1], "#11");
  429. Assert.AreEqual (DataRowState.Unchanged, table.Rows.Find (1).RowState, "#12");
  430. }
  431. [Test]
  432. public void EndLoadData_MergeDuplcateValues ()
  433. {
  434. DataTable table = new DataTable ();
  435. table.Columns.Add ("col1", typeof (int));
  436. table.Columns.Add ("col2", typeof (int));
  437. table.PrimaryKey = new DataColumn[] {table.Columns [0]};
  438. table.Rows.Add (new object[] {1, 500});
  439. table.AcceptChanges ();
  440. table.BeginLoadData ();
  441. table.LoadDataRow (new object[] {1, 1}, false);
  442. table.LoadDataRow (new object[] {1, 10}, false);
  443. table.LoadDataRow (new object[] {1, 100}, false);
  444. table.EndLoadData ();
  445. Assert.AreEqual (1, table.Rows.Count, "#1");
  446. Assert.AreEqual (100, table.Rows [0][1], "#2");
  447. }
  448. [Test]
  449. public void GetChanges ()
  450. {
  451. DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
  452. dt2.Constraints.Add("Unique",dt2.Columns[0],true);
  453. dt2.Columns[0].DefaultValue=7;
  454. //make some changes
  455. dt2.Rows[0].Delete();
  456. dt2.Rows[1].Delete();
  457. dt2.Rows[2].Delete();
  458. dt2.Rows[3].Delete();
  459. dt1 = dt2.GetChanges();
  460. for (int i = 0; i < dt2.Constraints.Count; i++) {
  461. // GetChanges - Constraints[{0}],i)
  462. Assert.AreEqual (dt2.Constraints[i].ConstraintName,
  463. dt1.Constraints[i].ConstraintName, "DT43");
  464. }
  465. for (int i = 0; i < dt2.Columns.Count; i++) {
  466. // GetChanges - Columns[{0}].ColumnName,i)
  467. Assert.AreEqual (dt2.Columns [i].ColumnName, dt1.Columns [i].ColumnName, "DT44");
  468. // GetChanges - Columns[{0}].DataType,i)
  469. Assert.AreEqual (dt2.Columns [i].DataType, dt1.Columns [i].DataType, "DT45");
  470. }
  471. DataRow [] drArr1,drArr2;
  472. drArr1 = dt1.Select (string.Empty, string.Empty,DataViewRowState.Deleted);
  473. drArr2 = dt2.Select (string.Empty, string.Empty,DataViewRowState.Deleted);
  474. for (int i = 0; i < drArr1.Length; i++) {
  475. // GetChanges - Data [ParentId]{0} ,i)
  476. Assert.AreEqual (drArr1 [i] ["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId",DataRowVersion.Original], "DT46");
  477. // GetChanges - Data [String1]{0} ,i)
  478. Assert.AreEqual (drArr1 [i] ["String1", DataRowVersion.Original],drArr2[i]["String1",DataRowVersion.Original], "DT47");
  479. // GetChanges - Data [String2]{0} ,i)
  480. Assert.AreEqual (drArr1 [i] ["String2", DataRowVersion.Original],drArr2[i]["String2",DataRowVersion.Original], "DT48");
  481. }
  482. }
  483. [Test]
  484. public void GetChanges_ByDataRowState ()
  485. {
  486. DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
  487. dt2.Constraints.Add("Unique",dt2.Columns[0],true);
  488. dt2.Columns[0].DefaultValue=7;
  489. //make some changes
  490. dt2.Rows[0].Delete(); //DataRowState.Deleted
  491. dt2.Rows[1].Delete(); //DataRowState.Deleted
  492. dt2.Rows[2].BeginEdit();
  493. dt2.Rows[2]["String1"] = "Changed"; //DataRowState.Modified
  494. dt2.Rows[2].EndEdit();
  495. dt2.Rows.Add(new object[] {"99", "Temp1", "Temp2"}); //DataRowState.Added
  496. // *********** Checking GetChanges - DataRowState.Deleted ************
  497. dt1 = null;
  498. dt1 = dt2.GetChanges(DataRowState.Deleted);
  499. CheckTableSchema (dt1,dt2,DataRowState.Deleted.ToString());
  500. DataRow[] drArr1,drArr2;
  501. drArr1 = dt1.Select (string.Empty, string.Empty, DataViewRowState.Deleted);
  502. drArr2 = dt2.Select (string.Empty, string.Empty, DataViewRowState.Deleted);
  503. for (int i = 0; i < drArr1.Length; i++) {
  504. // GetChanges(Deleted) - Data [ParentId]{0} ,i)
  505. Assert.AreEqual (drArr1 [i] ["ParentId", DataRowVersion.Original], drArr2 [i] ["ParentId", DataRowVersion.Original], "DT49");
  506. // GetChanges(Deleted) - Data [String1]{0} ,i)
  507. Assert.AreEqual (drArr1 [i] ["String1", DataRowVersion.Original], drArr2 [i] ["String1", DataRowVersion.Original], "DT50");
  508. // GetChanges(Deleted) - Data [String2]{0} ,i)
  509. Assert.AreEqual (drArr1 [i] ["String2", DataRowVersion.Original],drArr2 [i] ["String2", DataRowVersion.Original], "DT51");
  510. }
  511. // *********** Checking GetChanges - DataRowState.Modified ************
  512. dt1 = null;
  513. dt1 = dt2.GetChanges(DataRowState.Modified);
  514. CheckTableSchema (dt1,dt2,DataRowState.Modified.ToString());
  515. drArr1 = dt1.Select (string.Empty, string.Empty);
  516. drArr2 = dt2.Select (string.Empty, string.Empty, DataViewRowState.ModifiedCurrent);
  517. for (int i = 0; i < drArr1.Length; i++) {
  518. // GetChanges(Modified) - Data [ParentId]{0} ,i)
  519. Assert.AreEqual (drArr2 [i] ["ParentId"], drArr1 [i] ["ParentId"], "DT52");
  520. // GetChanges(Modified) - Data [String1]{0} ,i)
  521. Assert.AreEqual (drArr2 [i] ["String1"], drArr1 [i] ["String1"], "DT53");
  522. // GetChanges(Modified) - Data [String2]{0} ,i)
  523. Assert.AreEqual (drArr2 [i] ["String2"], drArr1 [i] ["String2"], "DT54");
  524. }
  525. // *********** Checking GetChanges - DataRowState.Added ************
  526. dt1 = null;
  527. dt1 = dt2.GetChanges(DataRowState.Added);
  528. CheckTableSchema (dt1,dt2,DataRowState.Added.ToString());
  529. drArr1 = dt1.Select (string.Empty, string.Empty);
  530. drArr2 = dt2.Select (string.Empty, string.Empty, DataViewRowState.Added);
  531. for (int i = 0; i < drArr1.Length; i++) {
  532. // GetChanges(Added) - Data [ParentId]{0} ,i)
  533. Assert.AreEqual (drArr2 [i] ["ParentId"], drArr1 [i] ["ParentId"], "DT55");
  534. // GetChanges(Added) - Data [String1]{0} ,i)
  535. Assert.AreEqual (drArr2 [i] ["String1"], drArr1 [i] ["String1"], "DT56");
  536. // GetChanges(Added) - Data [String2]{0} ,i)
  537. Assert.AreEqual (drArr2 [i] ["String2"], drArr1 [i] ["String2" ], "DT57");
  538. }
  539. // *********** Checking GetChanges - DataRowState.Unchanged ************
  540. dt1 = null;
  541. dt1 = dt2.GetChanges(DataRowState.Unchanged);
  542. CheckTableSchema (dt1,dt2,DataRowState.Unchanged .ToString());
  543. drArr1 = dt1.Select (string.Empty, string.Empty);
  544. drArr2 = dt2.Select (string.Empty, string.Empty, DataViewRowState.Unchanged);
  545. for (int i = 0; i < drArr1.Length; i++) {
  546. // GetChanges(Unchanged) - Data [ParentId]{0} ,i)
  547. Assert.AreEqual (drArr2 [i] ["ParentId"], drArr1 [i] ["ParentId"], "DT58");
  548. // GetChanges(Unchanged) - Data [String1]{0} ,i)
  549. Assert.AreEqual (drArr2 [i] ["String1"], drArr1 [i] ["String1"], "DT59");
  550. // GetChanges(Unchanged) - Data [String2]{0} ,i)
  551. Assert.AreEqual (drArr2 [i] ["String2"], drArr1 [i] ["String2" ], "DT60");
  552. }
  553. }
  554. private void CheckTableSchema (DataTable dt1, DataTable dt2, string Description)
  555. {
  556. for (int i = 0; i < dt2.Constraints.Count; i++) {
  557. // GetChanges - Constraints[{0}] - {1},i,Description)
  558. Assert.AreEqual (dt2.Constraints [i].ConstraintName,
  559. dt1.Constraints [i].ConstraintName, "DT61");
  560. }
  561. for (int i = 0; i < dt2.Columns.Count; i++) {
  562. // GetChanges - Columns[{0}].ColumnName - {1},i,Description)
  563. Assert.AreEqual (dt2.Columns [i].ColumnName, dt1.Columns [i].ColumnName, "DT62");
  564. // GetChanges - Columns[{0}].DataType {1},i,Description)
  565. Assert.AreEqual (dt2.Columns [i].DataType, dt1.Columns [i].DataType, "DT63");
  566. }
  567. }
  568. [Test]
  569. public void GetErrors()
  570. {
  571. DataTable dt = DataProvider.CreateParentDataTable();
  572. DataRow[] drArr = new DataRow[3];
  573. drArr[0] = dt.Rows[0];
  574. drArr[1] = dt.Rows[2];
  575. drArr[2] = dt.Rows[5];
  576. drArr[0].RowError = "Error1";
  577. drArr[1].RowError = "Error2";
  578. drArr[2].RowError = "Error3";
  579. // GetErrors
  580. Assert.AreEqual(dt.GetErrors(), drArr, "DT64");
  581. }
  582. [Test]
  583. public new void GetHashCode ()
  584. {
  585. DataTable dt = DataProvider.CreateParentDataTable();
  586. int iHashCode;
  587. iHashCode = dt.GetHashCode();
  588. for (int i = 0; i < 10; i++) {
  589. // HashCode - i= + i.ToString()
  590. Assert.AreEqual(dt.GetHashCode (), iHashCode, "DT65");
  591. }
  592. }
  593. [Test]
  594. public new void GetType ()
  595. {
  596. DataTable dt = DataProvider.CreateParentDataTable ();
  597. Type tmpType = typeof (DataTable);
  598. // GetType
  599. Assert.AreEqual (tmpType, dt.GetType(), "DT66");
  600. }
  601. [Test]
  602. public void HasErrors ()
  603. {
  604. DataTable dtParent;
  605. dtParent= DataProvider.CreateParentDataTable();
  606. // Checking HasErrors default
  607. Assert.AreEqual(false, dtParent.HasErrors, "DT67");
  608. // Checking HasErrors Get
  609. dtParent.Rows[0].RowError = "Error on row 0";
  610. dtParent.Rows[2].RowError = "Error on row 2";
  611. Assert.AreEqual(true, dtParent.HasErrors, "DT68");
  612. }
  613. [Test]
  614. public void ImportRow ()
  615. {
  616. DataTable dt1, dt2;
  617. dt1 = DataProvider.CreateParentDataTable ();
  618. dt2 = DataProvider.CreateParentDataTable ();
  619. DataRow dr = dt2.NewRow ();
  620. dr.ItemArray = new object [] { 99, string.Empty, string.Empty };
  621. dt2.Rows.Add (dr);
  622. // ImportRow - Values
  623. dt1.ImportRow (dr);
  624. Assert.AreEqual (dr.ItemArray, dt1.Rows [dt1.Rows.Count - 1].ItemArray, "DT69");
  625. // ImportRow - DataRowState
  626. Assert.AreEqual (dr.RowState, dt1.Rows [dt1.Rows.Count - 1].RowState, "DT70");
  627. }
  628. [Test]
  629. public void LoadDataRow ()
  630. {
  631. DataTable dt;
  632. DataRow dr;
  633. dt = DataProvider.CreateParentDataTable();
  634. dt.PrimaryKey= new DataColumn[] {dt.Columns[0]}; //add ParentId as Primary Key
  635. dt.Columns["String1"].DefaultValue = "Default";
  636. dr = dt.Select("ParentId=1")[0];
  637. //Update existing row without accept changes
  638. dt.BeginLoadData ();
  639. dt.LoadDataRow (new object [] { 1, null, "Changed" }, false);
  640. dt.EndLoadData ();
  641. // LoadDataRow(update1) - check column String1
  642. Assert.AreEqual (dr ["String1"], dt.Columns ["String1"].DefaultValue, "DT71");
  643. // LoadDataRow(update1) - check column String2
  644. Assert.AreEqual (dr ["String2"], "Changed", "DT72");
  645. // LoadDataRow(update1) - check row state
  646. Assert.AreEqual (DataRowState.Modified, dr.RowState, "DT73");
  647. //Update existing row with accept changes
  648. dr = dt.Select ("ParentId=2") [0];
  649. dt.BeginLoadData ();
  650. dt.LoadDataRow (new object [] { 2, null, "Changed"}, true);
  651. dt.EndLoadData ();
  652. // LoadDataRow(update2) - check row state
  653. Assert.AreEqual (DataRowState.Unchanged, dr.RowState, "DT74");
  654. //Add New row without accept changes
  655. dt.BeginLoadData();
  656. dt.LoadDataRow(new object [] { 99, null, "Changed" }, false);
  657. dt.EndLoadData();
  658. // LoadDataRow(insert1) - check column String2
  659. dr = dt.Select("ParentId=99")[0];
  660. Assert.AreEqual("Changed", dr ["String2"], "DT75");
  661. // LoadDataRow(insert1) - check row state
  662. Assert.AreEqual(DataRowState.Added, dr.RowState, "DT76");
  663. //Add New row with accept changes
  664. dt.BeginLoadData ();
  665. dt.LoadDataRow (new object [] { 100, null, "Changed" }, true);
  666. dt.EndLoadData ();
  667. // LoadDataRow(insert2) - check row and values
  668. dr = dt.Select ("ParentId=100") [0];
  669. Assert.AreEqual("Changed", dr ["String2"], "DT77");
  670. // LoadDataRow(insert2) - check row state
  671. Assert.AreEqual (DataRowState.Unchanged, dr.RowState, "DT78");
  672. }
  673. [Test]
  674. public void Locale ()
  675. {
  676. DataTable dtParent;
  677. DataSet ds = new DataSet ("MyDataSet");
  678. dtParent= DataProvider.CreateParentDataTable ();
  679. ds.Tables.Add (dtParent);
  680. CultureInfo culInfo = CultureInfo.CurrentCulture;
  681. // Checking Locale default from system
  682. Assert.AreEqual (culInfo, dtParent.Locale, "DT79");
  683. // Checking Locale default from dataset
  684. culInfo = new CultureInfo ("fr");
  685. ds.Locale = culInfo;
  686. Assert.AreEqual (culInfo, dtParent.Locale, "DT80");
  687. // Checking Locale get/set
  688. culInfo = new CultureInfo ("nl-BE");
  689. dtParent.Locale = culInfo;
  690. Assert.AreEqual(culInfo, dtParent.Locale, "DT81");
  691. }
  692. [Test]
  693. public void MinimumCapacity ()
  694. {
  695. // i get default=50, according to MSDN the value should be 25
  696. // // Checking MinimumCapacity default = 25
  697. // Assert.AreEqual(25, dtParent.MinimumCapacity, "DT82");
  698. // EndCase(null);
  699. DataTable dt = new DataTable ();
  700. // Checking MinimumCapacity get/set int.MaxValue
  701. dt.MinimumCapacity = int.MaxValue;
  702. Assert.AreEqual (int.MaxValue, dt.MinimumCapacity, "DT83");
  703. // Checking MinimumCapacity get/set 0
  704. dt.MinimumCapacity = 0;
  705. Assert.AreEqual (0, dt.MinimumCapacity, "DT84");
  706. // // Checking MinimumCapacity get/set int.MinValue
  707. // dtParent.MinimumCapacity = int.MinValue;
  708. // Assert.AreEqual(int.MinValue, dtParent.MinimumCapacity, "DT85");
  709. // EndCase(null);
  710. }
  711. [Test]
  712. public void Namespace ()
  713. {
  714. DataTable dtParent = new DataTable ();
  715. // Checking Namespace default
  716. Assert.AreEqual (String.Empty, dtParent.Namespace, "DT86");
  717. // Checking Namespace set/get
  718. String s = "MyNamespace";
  719. dtParent.Namespace = s;
  720. Assert.AreEqual (s, dtParent.Namespace, "DT87");
  721. }
  722. [Test]
  723. public void NewRow ()
  724. {
  725. DataTable dt;
  726. DataRow dr;
  727. dt = DataProvider.CreateParentDataTable();
  728. // NewRow
  729. dr = dt.NewRow();
  730. Assert.IsNotNull (dr, "DT88");
  731. }
  732. [Test]
  733. public void OnColumnChanged ()
  734. {
  735. ProtectedTestClass dt = new ProtectedTestClass();
  736. EventRaised = false;
  737. dt.OnColumnChanged_Test ();
  738. // OnColumnChanged Event 1
  739. Assert.AreEqual (false, EventRaised, "DT89");
  740. EventRaised = false;
  741. EventValues = false;
  742. dt.ColumnChanged += new DataColumnChangeEventHandler (OnColumnChanged_Handler);
  743. dt.OnColumnChanged_Test();
  744. // OnColumnChanged Event 2
  745. Assert.AreEqual (true, EventRaised, "DT90");
  746. // OnColumnChanged Values
  747. Assert.AreEqual (true, EventValues, "DT91");
  748. dt.ColumnChanged -= new DataColumnChangeEventHandler (OnColumnChanged_Handler);
  749. }
  750. private void OnColumnChanged_Handler(Object sender,DataColumnChangeEventArgs e)
  751. {
  752. DataTable dt = (DataTable)sender;
  753. if ((e.Column.Equals(dt.Columns["Value"])) && (e.Row.Equals(dt.Rows[0])) && (e.ProposedValue.Equals("NewValue"))) {
  754. EventValues = true;
  755. } else {
  756. EventValues = false;
  757. }
  758. EventRaised = true;
  759. }
  760. [Test]
  761. public void OnColumnChanging ()
  762. {
  763. ProtectedTestClass dt = new ProtectedTestClass ();
  764. EventRaised = false;
  765. dt.OnColumnChanging_Test ();
  766. // OnColumnChanging Event 1
  767. Assert.AreEqual (false, EventRaised, "DT92");
  768. EventRaised = false;
  769. EventValues = false;
  770. dt.ColumnChanging += new DataColumnChangeEventHandler(OnColumnChanging_Handler);
  771. dt.OnColumnChanging_Test();
  772. // OnColumnChanging Event 2
  773. Assert.AreEqual (true, EventRaised, "DT93");
  774. // OnColumnChanging Values
  775. Assert.AreEqual (true, EventValues, "DT94");
  776. dt.ColumnChanging -= new DataColumnChangeEventHandler (OnColumnChanging_Handler);
  777. }
  778. private void OnColumnChanging_Handler (Object sender, DataColumnChangeEventArgs e)
  779. {
  780. DataTable dt = (DataTable)sender;
  781. if ((e.Column.Equals(dt.Columns["Value"])) && (e.Row.Equals(dt.Rows[0])) && (e.ProposedValue.Equals("NewValue"))) {
  782. EventValues = true;
  783. } else {
  784. EventValues = false;
  785. }
  786. EventRaised = true;
  787. }
  788. [Test]
  789. public void OnRemoveColumn ()
  790. {
  791. ProtectedTestClass dt = new ProtectedTestClass();
  792. dt.OnRemoveColumn_Test();
  793. }
  794. [Test]
  795. public void ParentRelations ()
  796. {
  797. DataTable dtChild,dtParent;
  798. DataSet ds = new DataSet();
  799. //Create tables
  800. dtChild = DataProvider.CreateChildDataTable();
  801. dtParent= DataProvider.CreateParentDataTable();
  802. //Add tables to dataset
  803. ds.Tables.Add(dtChild);
  804. ds.Tables.Add(dtParent);
  805. DataRelationCollection drlCollection;
  806. DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  807. // Checking ParentRelations - default value
  808. //Check default
  809. drlCollection = dtChild.ParentRelations;
  810. Assert.AreEqual(0, drlCollection.Count, "DT96");
  811. ds.Relations.Add(drl);
  812. drlCollection = dtChild.ParentRelations;
  813. // Checking ParentRelations Count
  814. Assert.AreEqual(1, drlCollection.Count, "DT97");
  815. // Checking ParentRelations Value
  816. Assert.AreEqual(drl, drlCollection[0], "DT98");
  817. }
  818. [Test]
  819. public void Prefix ()
  820. {
  821. DataTable dtParent = new DataTable();
  822. // Checking Prefix default
  823. Assert.AreEqual(String.Empty, dtParent.Prefix, "DT99");
  824. // Checking Prefix set/get
  825. String s = "MyPrefix";
  826. dtParent.Prefix=s;
  827. Assert.AreEqual(s, dtParent.Prefix, "DT100");
  828. }
  829. [Test]
  830. public void RejectChanges ()
  831. {
  832. String sNewValue = "NewValue";
  833. DataRow drModified, drDeleted, drAdded;
  834. DataTable dt = DataProvider.CreateParentDataTable ();
  835. drModified = dt.Rows [0];
  836. drModified [1] = sNewValue; //DataRowState = Modified, DataRowVersion = Proposed
  837. drDeleted = dt.Rows [1];
  838. drDeleted.Delete (); //DataRowState = Deleted
  839. drAdded = dt.NewRow ();
  840. dt.Rows.Add (drAdded); //DataRowState = Added
  841. dt.RejectChanges ();
  842. // RejectChanges - Unchanged1
  843. Assert.AreEqual (DataRowState.Unchanged, drModified.RowState, "DT101");
  844. // RejectChanges - Unchanged2
  845. Assert.AreEqual (DataRowState.Detached, drAdded.RowState, "DT102");
  846. // RejectChanges - Detached
  847. Assert.AreEqual (DataRowState.Unchanged, drDeleted.RowState, "DT103");
  848. }
  849. [Test]
  850. public void Reset ()
  851. {
  852. DataTable dt1 = DataProvider.CreateParentDataTable ();
  853. DataTable dt2 = DataProvider.CreateChildDataTable ();
  854. dt1.PrimaryKey = new DataColumn [] { dt1.Columns [0] };
  855. dt2.PrimaryKey = new DataColumn [] {dt2.Columns [0], dt2.Columns [1]};
  856. DataRelation rel = new DataRelation ("Rel", dt1.Columns ["ParentId"], dt2.Columns ["ParentId"]);
  857. DataSet ds = new DataSet ();
  858. ds.Tables.AddRange (new DataTable [] { dt1, dt2 });
  859. ds.Relations.Add (rel);
  860. dt2.Reset ();
  861. // Reset - ParentRelations
  862. Assert.AreEqual (0, dt2.ParentRelations.Count, "DT104");
  863. // Reset - Constraints
  864. Assert.AreEqual (0, dt2.Constraints.Count, "DT105");
  865. // Reset - Rows
  866. Assert.AreEqual (0, dt2.Rows.Count, "DT106");
  867. // Reset - Columns
  868. Assert.AreEqual (0, dt2.Columns.Count, "DT107");
  869. }
  870. [Test]
  871. public void RowChanged ()
  872. {
  873. DataTable dt = DataProvider.CreateParentDataTable();
  874. dt.RowChanged += new DataRowChangeEventHandler (Row_Changed);
  875. _EventTriggered = false;
  876. // RowChanged - 1
  877. dt.Rows[0][1] = "NewValue";
  878. Assert.AreEqual (true, _EventTriggered, "DT108");
  879. _EventTriggered = false;
  880. // RowChanged - 2
  881. dt.Rows[0].BeginEdit ();
  882. dt.Rows[0][1] = "NewValue";
  883. Assert.AreEqual (false, _EventTriggered, "DT109");
  884. _EventTriggered = false;
  885. // RowChanged - 3
  886. dt.Rows[0].EndEdit ();
  887. Assert.AreEqual (true, _EventTriggered, "DT110");
  888. _EventTriggered = false;
  889. dt.RowChanged -= new DataRowChangeEventHandler (Row_Changed);
  890. // RowChanged - 4
  891. dt.Rows[0][1] = "NewValue A";
  892. Assert.AreEqual (false, _EventTriggered, "DT111");
  893. }
  894. private void Row_Changed (object sender, DataRowChangeEventArgs e)
  895. {
  896. _EventTriggered = true;
  897. }
  898. [Test]
  899. public void RowChanging ()
  900. {
  901. DataTable dt = DataProvider.CreateParentDataTable();
  902. dt.RowChanging += new DataRowChangeEventHandler (Row_Changing);
  903. _EventTriggered = false;
  904. // RowChanging - 1
  905. dt.Rows[0][1] = "NewValue";
  906. Assert.AreEqual (true, _EventTriggered, "DT112");
  907. _EventTriggered = false;
  908. // RowChanging - 2
  909. dt.Rows[0].BeginEdit ();
  910. dt.Rows[0][1] = "NewValue";
  911. Assert.AreEqual(false, _EventTriggered, "DT113");
  912. _EventTriggered = false;
  913. // RowChanging - 3
  914. dt.Rows[0].EndEdit ();
  915. Assert.AreEqual (true, _EventTriggered, "DT114");
  916. _EventTriggered = false;
  917. dt.RowChanging -= new DataRowChangeEventHandler (Row_Changing);
  918. // RowChanging - 4
  919. dt.Rows[0][1] = "NewValue A";
  920. Assert.AreEqual (false, _EventTriggered, "DT115");
  921. }
  922. private void Row_Changing (object sender, DataRowChangeEventArgs e)
  923. {
  924. _EventTriggered = true;
  925. }
  926. [Test]
  927. public void RowDeleted ()
  928. {
  929. DataTable dt = DataProvider.CreateParentDataTable();
  930. dt.RowDeleted += new DataRowChangeEventHandler (Row_Deleted);
  931. _EventTriggered = false;
  932. // RowDeleted - 1
  933. dt.Rows[0].Delete();
  934. Assert.IsTrue (_EventTriggered, "DT116");
  935. _EventTriggered = false;
  936. dt.RowDeleted -= new DataRowChangeEventHandler (Row_Deleted);
  937. // RowDeleted - 2
  938. dt.Rows[1].Delete ();
  939. Assert.IsFalse (_EventTriggered, "DT117");
  940. }
  941. private void Row_Deleted (object sender, DataRowChangeEventArgs e)
  942. {
  943. _EventTriggered = true;
  944. }
  945. [Test]
  946. public void RowDeleting ()
  947. {
  948. DataTable dt = DataProvider.CreateParentDataTable();
  949. dt.RowDeleting += new DataRowChangeEventHandler (Row_Deleting);
  950. _EventTriggered = false;
  951. // RowDeleting - 1
  952. dt.Rows [0].Delete ();
  953. Assert.IsTrue (_EventTriggered, "DT118");
  954. _EventTriggered = false;
  955. dt.RowDeleting -= new DataRowChangeEventHandler (Row_Deleting);
  956. // RowDeleting - 2
  957. dt.Rows [1].Delete ();
  958. Assert.IsFalse (_EventTriggered, "DT119");
  959. }
  960. private void Row_Deleting (object sender, DataRowChangeEventArgs e)
  961. {
  962. _EventTriggered = true;
  963. }
  964. [Test]
  965. public void Rows ()
  966. {
  967. DataTable dtParent;
  968. dtParent = DataProvider.CreateParentDataTable();
  969. // Checking Rows
  970. Assert.IsNotNull (dtParent.Rows, "DT120");
  971. // Checking rows count
  972. Assert.IsTrue (dtParent.Rows.Count > 0, "DT121");
  973. }
  974. [Test]
  975. public void Select ()
  976. {
  977. DataTable dt = DataProvider.CreateParentDataTable();
  978. DataRow[] drSelect = dt.Select ();
  979. DataRow[] drResult = new DataRow [dt.Rows.Count];
  980. dt.Rows.CopyTo (drResult, 0);
  981. // Select
  982. Assert.AreEqual(drResult, drSelect, "DT122");
  983. }
  984. [Test]
  985. public void Select_ByFilter ()
  986. {
  987. DataSet ds = new DataSet();
  988. ds.Tables.Add(DataProvider.CreateParentDataTable ());
  989. DataTable dt = DataProvider.CreateChildDataTable ();
  990. ds.Tables.Add (dt);
  991. DataRow[] drSelect = null;
  992. ArrayList al = new ArrayList ();
  993. //add column with special name
  994. DataColumn dc = new DataColumn("Column#",typeof(int));
  995. dc.DefaultValue=-1;
  996. dt.Columns.Add(dc);
  997. //put some values
  998. dt.Rows[0][dc] = 100;
  999. dt.Rows[1][dc] = 200;
  1000. dt.Rows[2][dc] = 300;
  1001. dt.Rows[4][dc] = -400;
  1002. //for trim function
  1003. dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + " \t\n ";
  1004. dt.Rows[0]["String1"] = " \t\n " + dt.Rows[0]["String1"];
  1005. dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + " ";
  1006. ds.Tables[0].Rows[0]["ParentBool"] = DBNull.Value;
  1007. ds.Tables[0].Rows[2]["ParentBool"] = DBNull.Value;
  1008. ds.Tables[0].Rows[3]["ParentBool"] = DBNull.Value;
  1009. //-------------------------------------------------------------
  1010. al.Clear();
  1011. foreach (DataRow dr in dt.Rows) {
  1012. if ((int)dr["ChildId"] == 1)
  1013. al.Add(dr);
  1014. }
  1015. // Select_S - ChildId=1
  1016. drSelect = dt.Select ("ChildId=1");
  1017. Assert.AreEqual (al.ToArray(), drSelect, "DT123");
  1018. //-------------------------------------------------------------
  1019. al.Clear();
  1020. foreach (DataRow dr in dt.Rows) {
  1021. if ((int)dr["ChildId"] == 1)
  1022. al.Add(dr);
  1023. }
  1024. // Select_S - ChildId='1'
  1025. drSelect = dt.Select ("ChildId='1'");
  1026. Assert.AreEqual (al.ToArray(), drSelect, "DT124");
  1027. //-------------------------------------------------------------
  1028. // Select_S - ChildId= '1' (whitespace in filter string.
  1029. drSelect = dt.Select("ChildId= '1'");
  1030. Assert.AreEqual (al.ToArray(), drSelect, "DT125");
  1031. //-------------------------------------------------------------
  1032. al.Clear();
  1033. foreach (DataRow dr in dt.Rows)
  1034. if (dr["String1"].ToString() == "1-String1")
  1035. al.Add(dr);
  1036. // Select_S - String1='1-String1'
  1037. drSelect = dt.Select ("String1='1-String1'");
  1038. Assert.AreEqual (al.ToArray(), drSelect, "DT126");
  1039. //-------------------------------------------------------------
  1040. al.Clear();
  1041. foreach (DataRow dr in dt.Rows)
  1042. if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1")
  1043. al.Add(dr);
  1044. // Select_S - ChildId=1 and String1='1-String1'
  1045. drSelect = dt.Select ("ChildId=1 and String1='1-String1'");
  1046. Assert.AreEqual (al.ToArray(), drSelect, "DT127");
  1047. //-------------------------------------------------------------
  1048. al.Clear();
  1049. foreach (DataRow dr in dt.Rows)
  1050. if ((int)dr["ChildId"] + (int)dr["ParentId"] >= 4)
  1051. al.Add(dr);
  1052. // Select_S - ChildId+ParentId >= 4
  1053. drSelect = dt.Select ("ChildId+ParentId >= 4");
  1054. CompareUnSorted(drSelect, al.ToArray());
  1055. //-------------------------------------------------------------
  1056. al.Clear();
  1057. foreach (DataRow dr in dt.Rows) {
  1058. if ((((int)dr["ChildId"] - (int)dr["ParentId"]) * -1) != 0)
  1059. al.Add(dr);
  1060. }
  1061. // Select_S - ChildId-ParentId) * -1 <> 0
  1062. drSelect = dt.Select ("(ChildId-ParentId) * -1 <> 0");
  1063. CompareUnSorted(drSelect, al.ToArray());
  1064. //-------------------------------------------------------------
  1065. al.Clear();
  1066. foreach (DataRow dr in dt.Rows)
  1067. if ((double)dr["ChildDouble"] < ((int)dr["ParentId"]) % 4)
  1068. al.Add(dr);
  1069. // Select_S - ChildDouble < ParentId % 4
  1070. drSelect = dt.Select ("ChildDouble < ParentId % 4");
  1071. CompareUnSorted(drSelect, al.ToArray());
  1072. //-------------------------------------------------------------
  1073. al.Clear();
  1074. foreach (DataRow dr in dt.Rows)
  1075. if ((double)dr["ChildDouble"] == 10 || (double)dr["ChildDouble"] == 20 || (double)dr["ChildDouble"] == 25)
  1076. al.Add (dr);
  1077. // Select_S - ChildDouble in (10,20,25)
  1078. drSelect = dt.Select("ChildDouble in (10,20,25)");
  1079. CompareUnSorted(drSelect, al.ToArray());
  1080. //-------------------------------------------------------------
  1081. al.Clear();
  1082. foreach (DataRow dr in dt.Rows)
  1083. if (dr["String2"].ToString().IndexOf("1-S") >= 0)
  1084. al.Add (dr);
  1085. // Select_S - String2 like '%1-S%'
  1086. drSelect = dt.Select ("String2 like '%1-S%'");
  1087. Assert.AreEqual (al.ToArray (), drSelect, "DT128");
  1088. //-------------------------------------------------------------
  1089. //If a column name contains one of the above characters,(ex. #\/=><+-*%&|^'" and so on) the name must be wrapped in brackets. For example to use a column named "Column#" in an expression, you would write "[Column#]":
  1090. al.Clear();
  1091. foreach (DataRow dr in dt.Rows)
  1092. if ((int)dr["Column#"] <= 0)
  1093. al.Add (dr);
  1094. // Select_S - [Column#] <= 0
  1095. drSelect = dt.Select ("[Column#] <= 0 ");
  1096. CompareUnSorted (drSelect, al.ToArray());
  1097. //-------------------------------------------------------------
  1098. al.Clear();
  1099. foreach (DataRow dr in dt.Rows)
  1100. if ((int)dr["Column#"] <= 0)
  1101. al.Add (dr);
  1102. // Select_S - [Column#] <= 0
  1103. drSelect = dt.Select ("[Column#] <= 0");
  1104. CompareUnSorted (drSelect, al.ToArray());
  1105. //-------------------------------------------------------------
  1106. al.Clear();
  1107. foreach (DataRow dr in dt.Rows)
  1108. if (((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(2000,12,12)) > 0)
  1109. al.Add(dr);
  1110. // Select_S - ChildDateTime > #12/12/2000#
  1111. drSelect = dt.Select("ChildDateTime > #12/12/2000# ");
  1112. CompareUnSorted (drSelect, al.ToArray ());
  1113. //-------------------------------------------------------------
  1114. al.Clear();
  1115. foreach (DataRow dr in dt.Rows)
  1116. if (((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(1999,1,12,12,06,30)) > 0)
  1117. al.Add(dr);
  1118. // Select_S - ChildDateTime > #1/12/1999 12:06:30 PM#
  1119. drSelect = dt.Select ("ChildDateTime > #1/12/1999 12:06:30 PM# ");
  1120. CompareUnSorted (drSelect, al.ToArray ());
  1121. //-------------------------------------------------------------
  1122. al.Clear();
  1123. foreach (DataRow dr in dt.Rows)
  1124. if (((DateTime) dr ["ChildDateTime"]).CompareTo (new DateTime (2005, 12, 03, 17, 06, 30)) >= 0 || ((DateTime) dr ["ChildDateTime"]).CompareTo (new DateTime (1980, 11, 03)) <= 0)
  1125. al.Add (dr);
  1126. // Select_S - ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980#
  1127. drSelect = dt.Select ("ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980# ");
  1128. CompareUnSorted (drSelect, al.ToArray());
  1129. #if LATER
  1130. //-------------------------------------------------------------
  1131. al.Clear();
  1132. foreach (DataRow dr in dt.Rows)
  1133. if (dr["ChildDouble"].ToString().Length > 10)
  1134. al.Add(dr);
  1135. // Select_S - Len(Convert(ChildDouble,'System.String')) > 10
  1136. drSelect = dt.Select ("Len(Convert(ChildDouble,'System.String')) > 10");
  1137. Assert.AreEqual (al.ToArray(), drSelect, "DT129");
  1138. #endif
  1139. //-------------------------------------------------------------
  1140. al.Clear();
  1141. foreach (DataRow dr in dt.Rows)
  1142. if (dr["String1"].ToString().Trim().Substring(0,2) == "1-")
  1143. al.Add(dr);
  1144. // Select_S - SubString(Trim(String1),1,2) = '1-'
  1145. drSelect = dt.Select ("SubString(Trim(String1),1,2) = '1-'");
  1146. Assert.AreEqual (al.ToArray(), drSelect, "DT130");
  1147. //-------------------------------------------------------------
  1148. /*
  1149. al.Clear();
  1150. foreach (DataRow dr in ds.Tables[0].Rows)
  1151. if (dr.IsNull("ParentBool") || (bool)dr["ParentBool"])
  1152. al.Add(dr);
  1153. // Select_S - IsNull(ParentBool,true)
  1154. drSelect = ds.Tables[0].Select("IsNull(ParentBool,true) ");
  1155. Assert.AreEqual (al.ToArray(), drSelect, "DT131");
  1156. */
  1157. //-------------------------------------------------------------
  1158. al.Clear();
  1159. // Select_S - Relation not exists, Exception
  1160. try {
  1161. drSelect = dt.Select ("Parent.ParentId = ChildId");
  1162. Assert.Fail ("#A1");
  1163. } catch (IndexOutOfRangeException ex) {
  1164. // Cannot find relation 0
  1165. Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#A2");
  1166. Assert.IsNull (ex.InnerException, "#A3");
  1167. Assert.IsNotNull (ex.Message, "#A4");
  1168. }
  1169. //-------------------------------------------------------------
  1170. al.Clear();
  1171. ds.Relations.Add(new DataRelation("ParentChild",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
  1172. foreach (DataRow dr in dt.Rows)
  1173. if ((int)dr["ChildId"] == (int)dr.GetParentRow("ParentChild")["ParentId"])
  1174. al.Add(dr);
  1175. // Select_S - Parent.ParentId = ChildId
  1176. drSelect = dt.Select ("Parent.ParentId = ChildId");
  1177. Assert.AreEqual (al.ToArray(), drSelect, "DT134");
  1178. }
  1179. private void CompareUnSorted(Array a, Array b)
  1180. {
  1181. string msg = string.Format("Failed while comparing(Array a ={0} ({1}), Array b = {2} ({3}))]", a.ToString(), a.GetType().FullName, b.ToString(), b.GetType().FullName);
  1182. foreach (object item in a) {
  1183. if (Array.IndexOf(b, item) < 0) //b does not contain the current item.
  1184. Assert.Fail(msg);
  1185. }
  1186. foreach (object item in b) {
  1187. if (Array.IndexOf(a, item) < 0) //a does not contain the current item.
  1188. Assert.Fail(msg);
  1189. }
  1190. }
  1191. [Test]
  1192. public void Select_ByFilterDataViewRowState ()
  1193. {
  1194. DataTable dt = DataProvider.CreateParentDataTable();
  1195. DataRow[] drSelect, drResult;
  1196. dt.Rows[0].Delete();
  1197. dt.Rows[1]["ParentId"] = 1;
  1198. dt.Rows[2]["ParentId"] = 1;
  1199. dt.Rows[3].Delete();
  1200. dt.Rows.Add(new object[] {1, "A", "B"});
  1201. dt.Rows.Add(new object[] {1, "C", "D"});
  1202. dt.Rows.Add(new object[] {1, "E", "F"});
  1203. drSelect = dt.Select ("ParentId=1", string.Empty, DataViewRowState.Added);
  1204. drResult = GetResultRows(dt,DataRowState.Added);
  1205. // Select_SSD DataViewRowState.Added
  1206. Assert.AreEqual (drResult, drSelect, "DT135");
  1207. drSelect = dt.Select ("ParentId=1", string.Empty, DataViewRowState.CurrentRows);
  1208. drResult = GetResultRows (dt, DataRowState.Unchanged | DataRowState.Added | DataRowState.Modified);
  1209. // Select_SSD DataViewRowState.CurrentRows
  1210. Assert.AreEqual (drResult, drSelect, "DT136");
  1211. drSelect = dt.Select ("ParentId=1", string.Empty, DataViewRowState.Deleted);
  1212. drResult = GetResultRows (dt, DataRowState.Deleted);
  1213. // Select_SSD DataViewRowState.Deleted
  1214. Assert.AreEqual (drResult, drSelect, "DT137");
  1215. drSelect = dt.Select ("ParentId=1", string.Empty, DataViewRowState.ModifiedCurrent | DataViewRowState.ModifiedOriginal);
  1216. drResult = GetResultRows (dt,DataRowState.Modified);
  1217. // Select_SSD ModifiedCurrent or ModifiedOriginal
  1218. Assert.AreEqual (drResult, drSelect, "DT138");
  1219. }
  1220. private DataRow [] GetResultRows (DataTable dt, DataRowState State)
  1221. {
  1222. ArrayList al = new ArrayList ();
  1223. DataRowVersion drVer = DataRowVersion.Current;
  1224. //From MSDN - The row the default version for the current DataRowState.
  1225. // For a DataRowState value of Added, Modified or Current,
  1226. // the default version is Current.
  1227. // For a DataRowState of Deleted, the version is Original.
  1228. // For a DataRowState value of Detached, the version is Proposed.
  1229. if ( ((State & DataRowState.Added) > 0)
  1230. | ((State & DataRowState.Modified) > 0)
  1231. | ((State & DataRowState.Unchanged) > 0))
  1232. drVer = DataRowVersion.Current;
  1233. if ( (State & DataRowState.Deleted) > 0
  1234. | (State & DataRowState.Detached) > 0)
  1235. drVer = DataRowVersion.Original;
  1236. foreach (DataRow dr in dt.Rows) {
  1237. if (dr.HasVersion(drVer) && ((int)dr["ParentId", drVer] == 1) && ((dr.RowState & State) > 0))
  1238. al.Add(dr);
  1239. }
  1240. DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
  1241. return result;
  1242. }
  1243. [Test]
  1244. public void TableName ()
  1245. {
  1246. DataTable dtParent = new DataTable();
  1247. // Checking TableName default
  1248. Assert.AreEqual(String.Empty, dtParent.TableName, "DT139");
  1249. // Checking TableName set/get
  1250. String s = "MyTable";
  1251. dtParent.TableName=s;
  1252. Assert.AreEqual(s, dtParent.TableName, "DT140");
  1253. }
  1254. [Test]
  1255. public new void ToString ()
  1256. {
  1257. DataTable dt = DataProvider.CreateParentDataTable();
  1258. dt.DisplayExpression = dt.Columns[0].ColumnName;
  1259. string sToString = dt.TableName + " + " + dt.DisplayExpression;
  1260. Assert.AreEqual (sToString, dt.ToString (), "DT141");
  1261. }
  1262. [Test]
  1263. public void CaseSensitive ()
  1264. {
  1265. DataTable dtParent = new DataTable();
  1266. // Checking default
  1267. Assert.IsFalse (dtParent.CaseSensitive, "DT142");
  1268. // Checking set/get
  1269. dtParent.CaseSensitive = true;
  1270. Assert.IsTrue (dtParent.CaseSensitive, "DT143");
  1271. }
  1272. [Test]
  1273. public void ctor ()
  1274. {
  1275. DataTable dt = new DataTable ();
  1276. Assert.IsNotNull(dt, "DT144");
  1277. }
  1278. [Test]
  1279. public void ctor_ByName ()
  1280. {
  1281. string sName = "MyName";
  1282. DataTable dt = new DataTable (sName);
  1283. Assert.IsNotNull (dt, "DT145");
  1284. Assert.AreEqual(sName, dt.TableName, "DT146");
  1285. }
  1286. [Test]
  1287. public void DisplayExpression ()
  1288. {
  1289. DataTable dtParent;
  1290. dtParent= DataProvider.CreateParentDataTable ();
  1291. // Checking DisplayExpression default
  1292. Assert.AreEqual (String.Empty, dtParent.DisplayExpression, "DT147");
  1293. // Checking DisplayExpression Set/Get
  1294. dtParent.DisplayExpression = dtParent.Columns[0].ColumnName;
  1295. Assert.AreEqual (dtParent.Columns[0].ColumnName, dtParent.DisplayExpression, "DT148");
  1296. }
  1297. [Test]
  1298. public void ExtendedProperties ()
  1299. {
  1300. DataTable dtParent;
  1301. PropertyCollection pc;
  1302. dtParent= DataProvider.CreateParentDataTable ();
  1303. pc = dtParent.ExtendedProperties;
  1304. // Checking ExtendedProperties default
  1305. Assert.IsNotNull (pc, "DT149");
  1306. // Checking ExtendedProperties count
  1307. Assert.AreEqual (0, pc.Count, "DT150");
  1308. }
  1309. [Test]
  1310. [Category ("NotWorking")]
  1311. public void PrimaryKey()
  1312. {
  1313. DataTable dtParent;
  1314. dtParent = DataProvider.CreateParentDataTable();
  1315. // Checking PrimaryKey default
  1316. Assert.AreEqual (0, dtParent.PrimaryKey.Length, "DT151");
  1317. // Checking PrimaryKey set/get
  1318. DataColumn[] dcArr = new DataColumn[] {dtParent.Columns[0]};
  1319. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
  1320. Assert.AreEqual (dcArr, dtParent.PrimaryKey, "DT152");
  1321. dtParent.PrimaryKey=null;
  1322. DataSet ds = new DataSet();
  1323. DataRow dr = null;
  1324. ds.Tables.Add(dtParent);
  1325. //check primary key - ColumnType String, ds.CaseSensitive = false;
  1326. ds.CaseSensitive = false;
  1327. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["String1"]};
  1328. // check primary key - ColumnType String, ds.CaseSensitive = false;
  1329. dr = dtParent.NewRow();
  1330. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1331. dr["String1"] = dr["String1"].ToString().ToUpper();
  1332. try {
  1333. dtParent.Rows.Add(dr);
  1334. Assert.Fail("#A1");
  1335. } catch (ConstraintException ex) {
  1336. // Column 'String1' is constrained to be unique.
  1337. // Value '1-STRING1' is already present
  1338. Assert.AreEqual (typeof (ConstraintException), ex.GetType (), "#A2");
  1339. Assert.IsNull (ex.InnerException, "#A3");
  1340. Assert.IsNotNull (ex.Message, "#A4");
  1341. Assert.IsTrue (ex.Message.IndexOf ("'String1'") != -1, "#A5");
  1342. Assert.IsTrue (ex.Message.IndexOf ("'1-STRING1'") != -1, "#A6");
  1343. }
  1344. if (dr.RowState != DataRowState.Detached)
  1345. dtParent.Rows.Remove(dr);
  1346. //check primary key - ColumnType String, ds.CaseSensitive = true;
  1347. ds.CaseSensitive = true;
  1348. // check primary key ConstraintException - ColumnType String, ds.CaseSensitive = true;
  1349. dr = dtParent.NewRow();
  1350. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1351. dr["String1"] = dr["String1"].ToString();
  1352. try {
  1353. dtParent.Rows.Add(dr);
  1354. Assert.Fail("#B1");
  1355. } catch (ConstraintException ex) {
  1356. // Column 'String1' is constrained to be unique.
  1357. // Value '1-String1' is already present
  1358. Assert.AreEqual (typeof (ConstraintException), ex.GetType (), "#B2");
  1359. Assert.IsNull (ex.InnerException, "#B3");
  1360. Assert.IsNotNull (ex.Message, "#B4");
  1361. Assert.IsTrue (ex.Message.IndexOf ("'String1'") != -1, "#B5");
  1362. Assert.IsTrue (ex.Message.IndexOf ("'1-String1'") != -1, "#B6");
  1363. }
  1364. if (dr.RowState != DataRowState.Detached)
  1365. dtParent.Rows.Remove(dr);
  1366. //check primary key - ColumnType String, ds.CaseSensitive = true;
  1367. ds.CaseSensitive = true;
  1368. // check primary key - ColumnType String, ds.CaseSensitive = true;
  1369. dr = dtParent.NewRow();
  1370. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1371. dr["String1"] = dr["String1"].ToString().ToUpper();
  1372. dtParent.Rows.Add(dr);
  1373. Assert.AreEqual(true, dtParent.Rows.Contains(dr["String1"]), "DT157");
  1374. if (dr.RowState != DataRowState.Detached)
  1375. dtParent.Rows.Remove(dr);
  1376. dtParent.PrimaryKey=null;
  1377. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDateTime"]};
  1378. // check primary key - ColumnType DateTime
  1379. dr = dtParent.NewRow();
  1380. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1381. dr["ParentDateTime"] = DateTime.Now;
  1382. dtParent.Rows.Add(dr);
  1383. Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDateTime"]), "DT158");
  1384. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1385. // check primary key ConstraintException- ColumnType DateTime
  1386. dr = dtParent.NewRow();
  1387. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1388. dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
  1389. try {
  1390. dtParent.Rows.Add(dr);
  1391. Assert.Fail("#C1");
  1392. } catch (ConstraintException ex) {
  1393. // Column 'ParentDateTime' is constrained to be
  1394. // unique. Value '1/1/2005 12:00:00 AM' is
  1395. // already present
  1396. Assert.AreEqual (typeof (ConstraintException), ex.GetType (), "#C2");
  1397. Assert.IsNull (ex.InnerException, "#C3");
  1398. Assert.IsNotNull (ex.Message, "#C4");
  1399. Assert.IsTrue (ex.Message.IndexOf ("'ParentDateTime'") != -1, "#C5");
  1400. Assert.IsTrue (ex.Message.IndexOf ("'1/1/2005 12:00:00 AM'") != -1, "#C6");
  1401. }
  1402. if (dr.RowState != DataRowState.Detached)
  1403. dtParent.Rows.Remove(dr);
  1404. dtParent.PrimaryKey=null;
  1405. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"]};
  1406. // check primary key - ColumnType ParentDouble, value=Epsilon
  1407. dr = dtParent.NewRow();
  1408. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1409. dr["ParentDouble"] = Double.Epsilon;
  1410. dtParent.Rows.Add(dr);
  1411. Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDouble"]), "DT161");
  1412. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1413. // check primary key ConstraintException - ColumnType ParentDouble
  1414. dr = dtParent.NewRow();
  1415. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1416. dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
  1417. try {
  1418. dtParent.Rows.Add(dr);
  1419. Assert.Fail("#D1");
  1420. } catch (ConstraintException ex) {
  1421. // Column 'ParentDouble' is constrained to be
  1422. // unique. Value '1.534' is already present
  1423. Assert.AreEqual (typeof (ConstraintException), ex.GetType (), "#D2");
  1424. Assert.IsNull (ex.InnerException, "#D3");
  1425. Assert.IsNotNull (ex.Message, "#D4");
  1426. Assert.IsTrue (ex.Message.IndexOf ("'ParentDouble'") != -1, "#D5");
  1427. Assert.IsTrue (ex.Message.IndexOf ("'1.534'") != -1, "#D6");
  1428. }
  1429. if (dr.RowState != DataRowState.Detached)
  1430. dtParent.Rows.Remove(dr);
  1431. //
  1432. // SubTest
  1433. //
  1434. dtParent.PrimaryKey = null;
  1435. // check primary key ConstraintException - ColumnType ParentBool
  1436. try {
  1437. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentBool"]};
  1438. Assert.Fail("#E1");
  1439. } catch (ArgumentException ex) {
  1440. // These columns don't currently have unique values
  1441. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#E2");
  1442. Assert.IsNull (ex.InnerException, "#E3");
  1443. Assert.IsNotNull (ex.Message, "#A4");
  1444. Assert.IsNull (ex.ParamName, "#A5");
  1445. }
  1446. if (dr.RowState != DataRowState.Detached)
  1447. dtParent.Rows.Remove(dr);
  1448. //
  1449. // SubTest
  1450. //
  1451. dtParent.PrimaryKey=null;
  1452. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"],dtParent.Columns["ParentDateTime"]};
  1453. // check primary key - ColumnType Double,DateTime test1
  1454. dr = dtParent.NewRow();
  1455. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1456. dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
  1457. dr["ParentDateTime"] = DateTime.Now;
  1458. dtParent.Rows.Add(dr);
  1459. Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT166");
  1460. if (dr.RowState != DataRowState.Detached)
  1461. dtParent.Rows.Remove(dr);
  1462. // check primary key - ColumnType Double,DateTime test2
  1463. dr = dtParent.NewRow();
  1464. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1465. dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
  1466. dr["ParentDouble"] = 99.399;
  1467. dtParent.Rows.Add(dr);
  1468. Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT167");
  1469. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1470. // check primary key ConstraintException - ColumnType Double,DateTime
  1471. dr = dtParent.NewRow();
  1472. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1473. dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
  1474. dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
  1475. try {
  1476. dtParent.Rows.Add (dr);
  1477. Assert.Fail("#F1");
  1478. } catch (ConstraintException ex) {
  1479. // Column 'ParentDouble, ParentDateTime' is
  1480. // constrained to be unique. Value '1.534,
  1481. // 1/1/2005 12:00:00 AM' is already present
  1482. Assert.AreEqual (typeof (ConstraintException), ex.GetType (), "#F2");
  1483. Assert.IsNull (ex.InnerException, "#F3");
  1484. Assert.IsNotNull (ex.Message, "#F4");
  1485. Assert.IsTrue (ex.Message.IndexOf ("'ParentDouble, ParentDateTime'") != -1, "#F5");
  1486. Assert.IsTrue (ex.Message.IndexOf ("'1.534, 1/1/2005 12:00:00 AM'") != -1, "#F6");
  1487. }
  1488. if (dr.RowState != DataRowState.Detached)
  1489. dtParent.Rows.Remove(dr);
  1490. DataTable dtChild = DataProvider.CreateChildDataTable();
  1491. ds.Tables.Add(dtChild);
  1492. dtParent.PrimaryKey = null;
  1493. //this test was addedd to check java exception:
  1494. //System.ArgumentException: Cannot remove UniqueConstraint because the ForeignKeyConstraint myRelation exists.
  1495. // check add primary key with relation
  1496. ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
  1497. //the following line will cause java to fail
  1498. ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
  1499. Assert.AreEqual (2, ds.Tables[0].PrimaryKey.Length, "DT170");
  1500. }
  1501. [Test] // bug #319089
  1502. public void Compute_WithoutSchemaData_Test ()
  1503. {
  1504. DataSet ds = new DataSet ("TestData");
  1505. DataTable table = ds.Tables.Add ("TestTable");
  1506. table.Columns.Add ("Id");
  1507. table.Columns.Add ("Value");
  1508. table.Rows.Add (new object[] {"1", "4.5"});
  1509. table.Rows.Add (new object[] {"2", "7.5"});
  1510. table.Rows.Add (new object[] {"3", "2.5"});
  1511. table.Rows.Add (new object[] {"4", "3.5"});
  1512. Assert.AreEqual ("1",
  1513. table.Compute ("Min(Id)", String.Empty), "#1");
  1514. Assert.AreEqual ("4",
  1515. table.Compute ("Max(Id)", String.Empty), "#2");
  1516. Assert.AreEqual ("2.5",
  1517. table.Compute ("Min(Value)", String.Empty), "#3");
  1518. Assert.AreEqual ("7.5",
  1519. table.Compute ("Max(Value)", String.Empty), "#4");
  1520. }
  1521. [Test]
  1522. public void BeginLoadData ()
  1523. {
  1524. DataTable dt = DataProvider.CreateParentDataTable ();
  1525. dt.Columns [0].AllowDBNull = false;
  1526. try {
  1527. //if BeginLoadData has not been called, an exception will be throw
  1528. dt.LoadDataRow (new object [] {null, "A", "B"}, false);
  1529. Assert.Fail ("#A1");
  1530. } catch (NoNullAllowedException ex) {
  1531. // Column 'ParentId' does not allow nulls
  1532. Assert.AreEqual (typeof (NoNullAllowedException), ex.GetType (), "#A2");
  1533. Assert.IsNull (ex.InnerException, "#A3");
  1534. Assert.IsNotNull (ex.Message, "#A4");
  1535. Assert.IsTrue (ex.Message.IndexOf ("'ParentId'") != -1, "#A5");
  1536. }
  1537. DataTable dt1 = DataProvider.CreateUniqueConstraint();
  1538. dt1.BeginLoadData ();
  1539. DataRow dr = dt1.NewRow ();
  1540. dr [0] = 3;
  1541. dt1.Rows.Add (dr);
  1542. try {
  1543. dt1.EndLoadData ();
  1544. Assert.Fail ("#B1");
  1545. } catch (ConstraintException ex) {
  1546. // Failed to enable constraints. One or more rows
  1547. // contain values violating non-null, unique, or
  1548. // foreign-key constraints
  1549. Assert.AreEqual (typeof (ConstraintException), ex.GetType (), "#B2");
  1550. Assert.IsNull (ex.InnerException, "#B3");
  1551. Assert.IsNotNull (ex.Message, "#B4");
  1552. Assert.AreEqual (2, dt1.GetErrors().Length, "#B5");
  1553. Assert.IsTrue (dt1.GetErrors () [0].RowError.Length > 10, "#B6");
  1554. Assert.IsTrue (dt1.GetErrors () [1].RowError.Length > 10, "#B7");
  1555. }
  1556. DataSet ds = DataProvider.CreateForigenConstraint ();
  1557. ds.Tables [0].BeginLoadData ();
  1558. ds.Tables [0].Rows [0] [0] = 10; //Forigen constraint violation
  1559. try {
  1560. ds.Tables [0].EndLoadData ();
  1561. Assert.Fail ("#C1");
  1562. } catch (ConstraintException ex) {
  1563. // Failed to enable constraints. One or more
  1564. // rows contain values violating non-null,
  1565. // unique, or foreign-key constraints
  1566. Assert.AreEqual (typeof (ConstraintException), ex.GetType (), "#C2");
  1567. Assert.IsNull (ex.InnerException, "#C3");
  1568. Assert.IsNotNull (ex.Message, "#C4");
  1569. Assert.AreEqual(3,ds.Tables[1].GetErrors().Length, "#C5");
  1570. for (int index =0; index < 3; index++)
  1571. Assert.IsTrue (ds.Tables [1].GetErrors () [index].RowError.Length > 10, "#C6");
  1572. }
  1573. }
  1574. private DataRowAction drExpectedAction;
  1575. [Test]
  1576. public void OnRowChanged ()
  1577. {
  1578. ProtectedTestClass dt = new ProtectedTestClass ();
  1579. EventRaised = false;
  1580. dt.OnRowChanged_Test (DataRowAction.Nothing);
  1581. Assert.IsFalse (EventRaised, "DT181");
  1582. dt.RowChanged += new DataRowChangeEventHandler(OnRowChanged_Handler);
  1583. foreach (int i in Enum.GetValues (typeof (DataRowAction))) {
  1584. EventRaised = false;
  1585. EventValues = false;
  1586. drExpectedAction = (DataRowAction) i;
  1587. dt.OnRowChanged_Test (drExpectedAction);
  1588. Assert.IsTrue (EventRaised, "DT182");
  1589. Assert.IsTrue (EventValues, "DT183");
  1590. }
  1591. dt.RowChanged -= new DataRowChangeEventHandler(OnRowChanged_Handler);
  1592. }
  1593. private void OnRowChanged_Handler (Object sender, DataRowChangeEventArgs e)
  1594. {
  1595. DataTable dt = (DataTable)sender;
  1596. if (dt.Rows [0].Equals (e.Row) && e.Action == drExpectedAction)
  1597. EventValues = true;
  1598. EventRaised = true;
  1599. }
  1600. [Test]
  1601. public void OnRowChanging ()
  1602. {
  1603. ProtectedTestClass dt = new ProtectedTestClass();
  1604. EventRaised = false;
  1605. dt.OnRowChanging_Test (DataRowAction.Nothing);
  1606. Assert.IsFalse (EventRaised, "DT184");
  1607. dt.RowChanging += new DataRowChangeEventHandler(OnRowChanging_Handler);
  1608. foreach (int i in Enum.GetValues (typeof (DataRowAction))) {
  1609. EventRaised = false;
  1610. EventValues = false;
  1611. drExpectedAction = (DataRowAction) i;
  1612. dt.OnRowChanging_Test (drExpectedAction);
  1613. Assert.IsTrue (EventRaised, "DT185");
  1614. Assert.IsTrue (EventValues, "DT186");
  1615. }
  1616. dt.RowChanging -= new DataRowChangeEventHandler(OnRowChanging_Handler);
  1617. }
  1618. private void OnRowChanging_Handler (Object sender,DataRowChangeEventArgs e)
  1619. {
  1620. DataTable dt = (DataTable) sender;
  1621. if (dt.Rows [0].Equals (e.Row) && e.Action == drExpectedAction)
  1622. EventValues = true;
  1623. EventRaised = true;
  1624. }
  1625. [Test]
  1626. public void OnRowDeleted ()
  1627. {
  1628. ProtectedTestClass dt = new ProtectedTestClass();
  1629. EventRaised = false;
  1630. dt.OnRowDeleted_Test (DataRowAction.Nothing);
  1631. Assert.IsFalse (EventRaised, "DT187");
  1632. dt.RowDeleted += new DataRowChangeEventHandler(OnRowDeleted_Handler);
  1633. foreach (int i in Enum.GetValues (typeof (DataRowAction))) {
  1634. EventRaised = false;
  1635. EventValues = false;
  1636. drExpectedAction = (DataRowAction) i;
  1637. dt.OnRowDeleted_Test (drExpectedAction);
  1638. Assert.IsTrue (EventRaised, "DT188");
  1639. Assert.IsTrue (EventValues, "DT189");
  1640. }
  1641. dt.RowDeleted -= new DataRowChangeEventHandler (OnRowDeleted_Handler);
  1642. }
  1643. private void OnRowDeleted_Handler (Object sender,DataRowChangeEventArgs e)
  1644. {
  1645. DataTable dt = (DataTable)sender;
  1646. if (dt.Rows [0].Equals (e.Row) && e.Action == drExpectedAction)
  1647. EventValues = true;
  1648. EventRaised = true;
  1649. }
  1650. [Test]
  1651. public void OnRowDeleting ()
  1652. {
  1653. ProtectedTestClass dt = new ProtectedTestClass();
  1654. EventRaised = false;
  1655. dt.OnRowDeleting_Test (DataRowAction.Nothing);
  1656. Assert.IsFalse (EventRaised, "DT190");
  1657. dt.RowDeleting += new DataRowChangeEventHandler(OnRowDeleting_Handler);
  1658. foreach (int i in Enum.GetValues (typeof(DataRowAction))) {
  1659. EventRaised = false;
  1660. EventValues = false;
  1661. drExpectedAction = (DataRowAction) i;
  1662. dt.OnRowDeleting_Test (drExpectedAction);
  1663. Assert.IsTrue (EventRaised, "DT191");
  1664. Assert.IsTrue (EventValues, "DT192");
  1665. }
  1666. dt.RowDeleting -= new DataRowChangeEventHandler(OnRowDeleting_Handler);
  1667. }
  1668. [Test]
  1669. public void BeginInit_PrimaryKey_1 ()
  1670. {
  1671. DataTable table = new DataTable ();
  1672. DataColumn col = table.Columns.Add ("col", typeof (int));
  1673. table.PrimaryKey = new DataColumn[] {col};
  1674. table.AcceptChanges ();
  1675. Assert.AreEqual (1, table.PrimaryKey.Length, "#1");
  1676. table.BeginInit ();
  1677. DataColumn col2 = new DataColumn ("col2", typeof (int));
  1678. table.Columns.AddRange (new DataColumn[] {col2});
  1679. table.PrimaryKey = new DataColumn[] {col2};
  1680. table.EndInit ();
  1681. Assert.AreEqual (1, table.PrimaryKey.Length, "#2");
  1682. Assert.AreEqual ("col2", table.PrimaryKey[0].ColumnName, "#3");
  1683. }
  1684. [Test]
  1685. public void BeginInit_PrimaryKey_2()
  1686. {
  1687. DataTable table = new DataTable ();
  1688. DataColumn col = table.Columns.Add ("col", typeof (int));
  1689. table.PrimaryKey = new DataColumn[] {col};
  1690. table.AcceptChanges ();
  1691. // ms.net behavior.
  1692. table.BeginInit ();
  1693. DataColumn col1 = new DataColumn ("col1", typeof (int));
  1694. table.Columns.AddRange (new DataColumn[] {col1});
  1695. UniqueConstraint uc = new UniqueConstraint (string.Empty, new String[] {"col1"}, true);
  1696. table.Constraints.AddRange (new Constraint[] {uc});
  1697. try {
  1698. table.EndInit ();
  1699. Assert.Fail ("#1");
  1700. } catch (ArgumentException ex) {
  1701. // Cannot add primary key constraint since primary
  1702. // key is already set for the table
  1703. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1704. Assert.IsNull (ex.InnerException, "#3");
  1705. Assert.IsNotNull (ex.Message, "#4");
  1706. Assert.IsNull (ex.ParamName, "#5");
  1707. }
  1708. }
  1709. [Test]
  1710. public void BeginInit_PrimaryKey_3 ()
  1711. {
  1712. DataTable table = new DataTable ();
  1713. DataColumn col1 = table.Columns.Add ("col1", typeof (int));
  1714. DataColumn col2 = table.Columns.Add ("col2", typeof (int));
  1715. // ms.net behavior
  1716. table.BeginInit ();
  1717. UniqueConstraint uc = new UniqueConstraint (string.Empty, new String[] {"col1"}, true);
  1718. table.Constraints.AddRange (new Constraint[] {uc});
  1719. table.PrimaryKey = new DataColumn [] {col2};
  1720. table.EndInit ();
  1721. Assert.AreEqual ("col1", table.PrimaryKey[0].ColumnName, "#1");
  1722. }
  1723. [Test]
  1724. public void PrimaryKey_OnFailing ()
  1725. {
  1726. DataTable table = new DataTable ();
  1727. DataColumn col1 = table.Columns.Add ("col1", typeof (int));
  1728. table.PrimaryKey = new DataColumn[] {col1};
  1729. try {
  1730. table.PrimaryKey = new DataColumn [] { new DataColumn ("col2", typeof (int)) };
  1731. Assert.Fail ("#1");
  1732. } catch (ArgumentException ex) {
  1733. // Column must belong to a table
  1734. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1735. Assert.IsNull (ex.InnerException, "#3");
  1736. Assert.IsNotNull (ex.Message, "#4");
  1737. Assert.IsNull (ex.ParamName, "#5");
  1738. }
  1739. Assert.AreEqual ("col1", table.PrimaryKey [0].ColumnName, "#6");
  1740. }
  1741. [Test]
  1742. public void BeginInit_Cols_Constraints ()
  1743. {
  1744. DataTable table = new DataTable ();
  1745. // if both cols and constraints are added after BeginInit, the cols
  1746. // should be added, before the constraints are added/validated
  1747. table.BeginInit ();
  1748. DataColumn col1 = new DataColumn ("col1", typeof (int));
  1749. table.Columns.AddRange (new DataColumn[] {col1});
  1750. UniqueConstraint uc = new UniqueConstraint (string.Empty, new String[] {"col1"}, false);
  1751. table.Constraints.AddRange (new Constraint[] {uc});
  1752. // no exception shud be thrown
  1753. table.EndInit ();
  1754. Assert.AreEqual (1, table.Constraints.Count, "#1");
  1755. }
  1756. [Test]
  1757. public void LoadDataRow_ExistingData ()
  1758. {
  1759. DataSet ds = new DataSet ();
  1760. DataTable table = ds.Tables.Add ();
  1761. table.Columns.Add ("col1", typeof (int));
  1762. table.Columns.Add ("col2", typeof (int));
  1763. table.PrimaryKey = new DataColumn[] {table.Columns [0]};
  1764. table.BeginLoadData ();
  1765. table.LoadDataRow (new object[] {1,10}, true);
  1766. table.LoadDataRow (new object[] {2,10}, true);
  1767. table.LoadDataRow (new object[] {3,10}, true);
  1768. table.LoadDataRow (new object[] {4,10}, true);
  1769. table.EndLoadData ();
  1770. Assert.AreEqual (4, table.Rows.Count, "#1");
  1771. Assert.AreEqual (10, table.Rows [0][1], "#2");
  1772. Assert.AreEqual (10, table.Rows [1][1], "#3");
  1773. Assert.AreEqual (10, table.Rows [2][1], "#4");
  1774. Assert.AreEqual (10, table.Rows [3][1], "#5");
  1775. table.BeginLoadData ();
  1776. table.LoadDataRow (new object[] {1,100}, true);
  1777. table.LoadDataRow (new object[] {2,100}, true);
  1778. table.LoadDataRow (new object[] {3,100}, true);
  1779. table.LoadDataRow (new object[] {4,100}, true);
  1780. table.EndLoadData ();
  1781. Assert.AreEqual (4, table.Rows.Count, "#6");
  1782. Assert.AreEqual (100, table.Rows [0][1], "#7");
  1783. Assert.AreEqual (100, table.Rows [1][1], "#8");
  1784. Assert.AreEqual (100, table.Rows [2][1], "#7");
  1785. Assert.AreEqual (100, table.Rows [3][1], "#10");
  1786. }
  1787. [Test]
  1788. public void LoadDataRow_DefaultValueError ()
  1789. {
  1790. DataTable table = new DataTable ();
  1791. table.Columns.Add ("col1", typeof (int));
  1792. table.Columns.Add ("col2", typeof (int));
  1793. table.PrimaryKey = new DataColumn[] {table.Columns [0]};
  1794. table.BeginLoadData ();
  1795. // should not throw exception
  1796. table.LoadDataRow (new object[] {1}, true);
  1797. table.EndLoadData ();
  1798. Assert.AreEqual (1, table.Rows [0][0], "#1");
  1799. Assert.AreEqual (DBNull.Value, table.Rows [0][1], "#2");
  1800. }
  1801. [Test]
  1802. public void RejectChanges_CheckIndex ()
  1803. {
  1804. DataTable table = new DataTable ();
  1805. table.Columns.Add ("col1", typeof (int));
  1806. table.PrimaryKey = new DataColumn[] {table.Columns [0]};
  1807. table.Rows.Add (new object[] {1});
  1808. table.AcceptChanges ();
  1809. table.Rows [0][0] = 10;
  1810. table.RejectChanges ();
  1811. Assert.IsNotNull (table.Rows.Find (1));
  1812. }
  1813. private void OnRowDeleting_Handler (Object sender,DataRowChangeEventArgs e)
  1814. {
  1815. DataTable dt = (DataTable)sender;
  1816. if (dt.Rows [0].Equals (e.Row) && e.Action == drExpectedAction)
  1817. EventValues = true;
  1818. EventRaised = true;
  1819. }
  1820. [Test]
  1821. public void Select_StringString ()
  1822. {
  1823. DataTable dt = DataProvider.CreateChildDataTable ();
  1824. DataRow [] drSelect;
  1825. List<DataRow> al;
  1826. //add some rows
  1827. dt.Rows.Add (new object[] {99,88, "bla", "wowww"});
  1828. dt.Rows.Add (new object[] {999,888, string.Empty, "woowww"});
  1829. //get excepted resault
  1830. al = new List<DataRow> ();
  1831. foreach (DataRow dr in dt.Rows) {
  1832. if ((int) dr["ChildId"] == 1)
  1833. al.Add (dr);
  1834. }
  1835. //al.Reverse();
  1836. al.Sort (new DataRowsComparer ("ParentId", "Desc"));
  1837. drSelect = dt.Select ("ChildId=1", "ParentId Desc");
  1838. Assert.AreEqual (al.ToArray (), drSelect, "DT193");
  1839. //get excepted resault
  1840. al = new List<DataRow>();
  1841. foreach (DataRow dr in dt.Rows) {
  1842. if (dr ["String1"].ToString () == "1-String1")
  1843. al.Add (dr);
  1844. }
  1845. //al.Reverse();
  1846. al.Sort (new DataRowsComparer ("ParentId", "Desc"));
  1847. drSelect = dt.Select("String1='1-String1'", "ParentId Desc");
  1848. Assert.AreEqual (al.ToArray(),drSelect, "DT194");
  1849. //get excepted resault
  1850. al = new List<DataRow> ();
  1851. foreach (DataRow dr in dt.Rows) {
  1852. if ((int) dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1")
  1853. al.Add (dr);
  1854. }
  1855. //al.Reverse();
  1856. al.Sort (new DataRowsComparer ("ParentId", "Desc"));
  1857. drSelect = dt.Select ("ChildId=1 and String1='1-String1'", "ParentId Desc");
  1858. Assert.AreEqual(al.ToArray (), drSelect, "DT195");
  1859. //get excepted resault
  1860. al = new List<DataRow> ();
  1861. foreach (DataRow dr in dt.Rows) {
  1862. if (dr ["String1"].ToString ().Length < 4)
  1863. al.Add (dr);
  1864. }
  1865. //al.Reverse();
  1866. al.Sort(new DataRowsComparer("ParentId", "Desc"));
  1867. drSelect = dt.Select("Len(String1) < 4 ", "ParentId Desc");
  1868. Assert.AreEqual(al.ToArray(),drSelect, "DT196");
  1869. }
  1870. [Test]
  1871. [Category ("NotWorking")]
  1872. public void Select_StringString_1 ()
  1873. {
  1874. DataTable dt = DataProvider.CreateChildDataTable ();
  1875. DataRow [] drSelect;
  1876. List<DataRow> al;
  1877. //add some rows
  1878. dt.Rows.Add (new object[] {99,88, "bla", "wowww"});
  1879. dt.Rows.Add (new object[] {999,888, string.Empty, "woowww"});
  1880. //get excepted resault
  1881. al = new List<DataRow> ();
  1882. foreach (DataRow dr in dt.Rows) {
  1883. if (dr ["String1"].ToString ().IndexOf ("String") > 0)
  1884. al.Add (dr);
  1885. }
  1886. //al.Reverse();
  1887. al.Sort (new DataRowsComparer ("ParentId", "Desc"));
  1888. drSelect = dt.Select ("String1 like '%%String*' ", "ParentId Desc");
  1889. Assert.AreEqual (al.ToArray (), drSelect, "DT197");
  1890. //get excepted resault
  1891. al = new List<DataRow> ();
  1892. foreach (DataRow dr in dt.Rows) {
  1893. if (((int) dr ["ChildId"] == 2) || ((int) dr ["ChildId"] == 3))
  1894. al.Add (dr);
  1895. }
  1896. //al.Reverse();
  1897. al.Sort (new DataRowsComparer ("ParentId", "Desc"));
  1898. drSelect = dt.Select ("ChildId in (2,3) ", "ParentId Desc");
  1899. Assert.AreEqual (al.ToArray (), drSelect, "DT198");
  1900. //get excepted resault
  1901. al = new List<DataRow> ();
  1902. foreach (DataRow dr in dt.Rows) {
  1903. if ((((int) dr ["ChildId"] * (int) dr ["ParentId"]) > 5))
  1904. al.Add (dr);
  1905. }
  1906. al.Sort (new DataRowsComparer ("ChildId", "Asc"));
  1907. drSelect = dt.Select ("ChildId * ParentId > 5 ", "ChildId Asc");
  1908. // Cannot check this way as ArrayList.Sort uses unstable sort and
  1909. // so the Order of the elements is not preserved when elements are equal
  1910. //Assert.AreEqual (al.ToArray(),drSelect, "DT199");
  1911. Assert.AreEqual (al.Count, drSelect.Length, "#DT199");
  1912. for (int i = 0; i < al.Count; ++i){
  1913. // check the datarow is present and that the sort order is correct
  1914. Assert.IsTrue (Array.IndexOf (drSelect, al[i]) != -1, "#DT_199_2_"+i);
  1915. Assert.AreEqual (((DataRow)al[i])["ChildId"], drSelect[i]["ChildId"], "#DT_199_1" +i);
  1916. }
  1917. //get excepted resault
  1918. al = new List<DataRow> ();
  1919. foreach (DataRow dr in dt.Rows) {
  1920. if (dr ["String2"].ToString ().Substring (2, 3) == "Str")
  1921. al.Add (dr);
  1922. }
  1923. al.Sort (new DataRowsComparer ("ParentId", "Desc"));
  1924. drSelect = dt.Select ("SubString(String2,3,3) like 'Str' ", "ParentId Desc");
  1925. Assert.AreEqual (al.ToArray (), drSelect, "DT200");
  1926. }
  1927. [Test]
  1928. [Category ("NotWorking")]
  1929. public void Select_StringString_2 ()
  1930. {
  1931. DataTable dt = DataProvider.CreateParentDataTable ();
  1932. try {
  1933. dt.Select (dt.Columns [0].ColumnName + "=1", "[x");
  1934. Assert.Fail ("#1");
  1935. } catch (ArgumentException ex) {
  1936. // [x isn't a valid Sort string entry
  1937. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1938. Assert.IsNull (ex.InnerException, "#3");
  1939. Assert.IsNotNull (ex.Message, "#4");
  1940. Assert.IsTrue (ex.Message.IndexOf ("[x") != -1, "#5");
  1941. Assert.IsNull (ex.ParamName, "#6");
  1942. }
  1943. }
  1944. [Test]
  1945. public void Select_StringString_3 ()
  1946. {
  1947. DataTable dt = DataProvider.CreateParentDataTable ();
  1948. //Select - parse sort string checking 1");
  1949. try {
  1950. dt.Select (dt.Columns [0].ColumnName, dt.Columns [0].ColumnName + "1");
  1951. Assert.Fail ("#1");
  1952. } catch (IndexOutOfRangeException ex) {
  1953. // Cannot find column ParentId1
  1954. Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#2");
  1955. Assert.IsNull (ex.InnerException, "#3");
  1956. Assert.IsNotNull (ex.Message, "#4");
  1957. Assert.IsTrue (ex.Message.IndexOf ("ParentId1") != -1, "#5");
  1958. }
  1959. }
  1960. [Test]
  1961. public void Select_NonBooleanFilter ()
  1962. {
  1963. DataTable table = new DataTable ();
  1964. table.Columns.Add ("col1", typeof (int));
  1965. DataRow[] row = table.Select ("col1*10");
  1966. Assert.AreEqual (0, row.Length, "#A");
  1967. // No exception shud be thrown
  1968. // The filter created earlier (if cached), will raise an EvaluateException
  1969. // and so shouldnt be cached
  1970. for (int i=0; i < 5; ++i)
  1971. table.Rows.Add (new object [] { i });
  1972. try {
  1973. table.Select ("col1*10");
  1974. Assert.Fail ("#B1");
  1975. } catch (EvaluateException ex) {
  1976. // Filter expression 'col1*10' does not evaluate
  1977. // to a Boolean term
  1978. Assert.AreEqual (typeof (EvaluateException), ex.GetType (), "#B2");
  1979. Assert.IsNull (ex.InnerException, "#B3");
  1980. Assert.IsNotNull (ex.Message, "#B4");
  1981. //Assert.IsTrue (ex.Message.IndexOf ("'col1*10'") != -1, "#B5");
  1982. //Assert.IsTrue (ex.Message.IndexOf ("Boolean") != -1, "#B6");
  1983. }
  1984. }
  1985. [Test]
  1986. public void Select_BoolColumn ()
  1987. {
  1988. DataTable table = new DataTable ();
  1989. table.Columns.Add ("col1", typeof (int));
  1990. table.Columns.Add ("col2", typeof (bool));
  1991. for (int i = 0; i < 5; i++)
  1992. table.Rows.Add (new object [] { i });
  1993. DataRow [] result;
  1994. try {
  1995. result = table.Select ("col1");
  1996. Assert.Fail ("#1");
  1997. } catch (EvaluateException ex) {
  1998. // Filter expression 'col1' does not evaluate to
  1999. // a Boolean term
  2000. Assert.AreEqual (typeof (EvaluateException), ex.GetType (), "#2");
  2001. Assert.IsNull (ex.InnerException, "#3");
  2002. Assert.IsNotNull (ex.Message, "#4");
  2003. //Assert.IsTrue (ex.Message.IndexOf ("'col1'") != -1, "#5");
  2004. //Assert.IsTrue (ex.Message.IndexOf ("Boolean") != -1, "#6");
  2005. }
  2006. //col2 is a boolean expression, and a null value translates to
  2007. //false.
  2008. result = table.Select ("col2");
  2009. Assert.AreEqual (0, result.Length);
  2010. }
  2011. [Test]
  2012. public void Select_OrderOfRows ()
  2013. {
  2014. DataTable table = new DataTable ();
  2015. table.Columns.Add ("col1", typeof (int));
  2016. table.Columns.Add ("col2", typeof (int));
  2017. for (int i = 0; i < 10; i++)
  2018. table.Rows.Add (new object [] { 10 - i, i });
  2019. DataRow[] result = table.Select ("col1 > 5");
  2020. Assert.AreEqual (6, result [0][0], "# incorrect sorting order");
  2021. }
  2022. #if NET_2_0
  2023. [Test]
  2024. public void DataTable_Clone ()
  2025. {
  2026. DataTable table = new DataTable ();
  2027. table.Columns.Add ("col1", typeof (DateTime));
  2028. table.Columns [0].DateTimeMode = DataSetDateTime.Local;
  2029. DataTable table1 = table.Clone ();
  2030. Assert.AreEqual (DataSetDateTime.Local, table1.Columns [0].DateTimeMode, "#1");
  2031. //Test any other new prop in 2.0
  2032. }
  2033. [Test]
  2034. public void Merge_SchemaTest ()
  2035. {
  2036. DataTable table1 = new DataTable ("t1");
  2037. table1.Columns.Add ("col1", typeof (int));
  2038. DataTable table2 = new DataTable ("t2");
  2039. table2.Columns.Add ("col2", typeof (int));
  2040. DataTable table3;
  2041. table3 = table1.Clone ();
  2042. table3.Merge (table2);
  2043. Assert.AreEqual (2, table3.Columns.Count, "#1");
  2044. table3 = table1.Clone ();
  2045. table3.Merge (table2, false, MissingSchemaAction.Ignore);
  2046. Assert.AreEqual (1, table3.Columns.Count, "#2");
  2047. // source constraints are ignored
  2048. table2.Constraints.Add ("uc", table2.Columns [0], false);
  2049. table3 = table1.Clone ();
  2050. table3.Merge (table2);
  2051. Assert.AreEqual (0, table3.Constraints.Count, "#3");
  2052. // source PK is merged depending on MissingSchemaAction
  2053. table2.PrimaryKey = new DataColumn[] {table2.Columns[0]};
  2054. table3 = table1.Clone ();
  2055. table3.Merge (table2);
  2056. Assert.AreEqual (1, table3.Constraints.Count, "#4");
  2057. table3 = table1.Clone ();
  2058. table3.Merge (table2, false, MissingSchemaAction.Ignore);
  2059. Assert.AreEqual (0, table3.Constraints.Count, "#5");
  2060. //FIXME : If both source and target have PK, then
  2061. // shud be the exception raised when schema is merged?
  2062. // ms.net throws a nullreference exception.
  2063. // If any data is merged, exception is anyways raised.
  2064. /*
  2065. table1.PrimaryKey = new DataColumn[] {table1.Columns[0]};
  2066. table3 = table1.Clone ();
  2067. try {
  2068. table3.Merge(table2);
  2069. Assert.Fail("#6");
  2070. } catch (DataException e) {}
  2071. */
  2072. table3.Merge (table2,false,MissingSchemaAction.Ignore);
  2073. table1.PrimaryKey = null;
  2074. // DateTime columns, DataType match only if DateTimeMode matches
  2075. table1.Columns.Add ("col_datetime", typeof (DateTime));
  2076. table2.Columns.Add ("col_datetime", typeof (DateTime));
  2077. table1.Columns ["col_datetime"].DateTimeMode = DataSetDateTime.Local;
  2078. table2.Columns ["col_datetime"].DateTimeMode = DataSetDateTime.Unspecified;
  2079. table3 = table1.Clone ();
  2080. try {
  2081. table3.Merge (table2);
  2082. Assert.Fail ("#7");
  2083. } catch (DataException) {
  2084. // <target>.col_datetime and <source>.col_datetime
  2085. // have conflicting properties: DataType property mismatch.
  2086. }
  2087. table1.Columns ["col_datetime"].DateTimeMode = DataSetDateTime.Unspecified;
  2088. table2.Columns ["col_datetime"].DateTimeMode = DataSetDateTime.UnspecifiedLocal;
  2089. table3 = table1.Clone ();
  2090. table3.Merge (table2);
  2091. Assert.AreEqual (DataSetDateTime.Unspecified, table3.Columns ["col_datetime"].DateTimeMode, "#9");
  2092. }
  2093. [Test]
  2094. public void Merge_TestData ()
  2095. {
  2096. DataTable t1 = new DataTable ("t1");
  2097. DataTable t2 = new DataTable ("t2");
  2098. t1.Columns.Add ("c1", typeof (int));
  2099. t1.Columns.Add ("c2", typeof (int));
  2100. t2.Columns.Add ("c1", typeof (int));
  2101. t2.Columns.Add ("c2", typeof (int));
  2102. t1.Rows.Add (new object[] {1, 1});
  2103. t1.Rows.Add (new object[] {2, 2});
  2104. t2.Rows.Add (new object[] {1, 5});
  2105. t2.Rows.Add (new object[] {1, 10});
  2106. DataTable t3 = t1.Copy ();
  2107. // When primary key is not defined, rows are not merged.
  2108. t3.Merge (t2);
  2109. Assert.AreEqual (4, t3.Rows.Count, "#1");
  2110. t1.PrimaryKey = new DataColumn[] {t1.Columns[0]};
  2111. t3 = t1.Copy ();
  2112. t3.Merge (t2);
  2113. Assert.AreEqual (2, t3.Rows.Count, "#2");
  2114. Assert.AreEqual (10, t3.Rows [0][1], "#3");
  2115. t3 = t1.Copy ();
  2116. t3.Merge (t2, true);
  2117. Assert.AreEqual (2, t3.Rows.Count, "#4");
  2118. Assert.AreEqual (1, t3.Rows [0][1], "#5");
  2119. }
  2120. #endif
  2121. internal class DataRowsComparer : IComparer<DataRow>
  2122. {
  2123. private string _columnName;
  2124. private string _direction;
  2125. public DataRowsComparer(string columnName, string direction)
  2126. {
  2127. _columnName = columnName;
  2128. if (direction.ToLower() != "asc" && direction.ToLower() != "desc")
  2129. throw new ArgumentException("Direction can only be one of: 'asc' or 'desc'");
  2130. _direction = direction;
  2131. }
  2132. public int Compare(DataRow drX, DataRow drY)
  2133. {
  2134. object objX = drX[_columnName];
  2135. object objY = drY[_columnName];
  2136. int compareResult = Comparer.Default.Compare(objX, objY);
  2137. //If we are comparing desc we need to reverse the result.
  2138. if (_direction.ToLower() == "desc")
  2139. compareResult = -compareResult;
  2140. return compareResult;
  2141. }
  2142. }
  2143. }
  2144. }