ConstraintCollectionTest2.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 MonoTests.System.Data.Utils;
  30. using System;
  31. using System.Collections;
  32. using System.ComponentModel;
  33. using System.Data;
  34. namespace MonoTests.System.Data
  35. {
  36. [TestFixture] public class ConstraintCollectionTest2
  37. {
  38. private bool CollectionChangedFlag = false;
  39. [Test] public void CanRemove_ParentForeign()
  40. {
  41. DataSet ds = DataProvider.CreateForigenConstraint();
  42. Assert.AreEqual(false, ds.Tables["parent"].Constraints.CanRemove(ds.Tables["parent"].Constraints[0]), "CN1");
  43. }
  44. [Test] public void CanRemove_ChildForeign()
  45. {
  46. DataSet ds = DataProvider.CreateForigenConstraint();
  47. Assert.AreEqual(true, ds.Tables["child"].Constraints.CanRemove(ds.Tables["child"].Constraints[0]), "CN2");
  48. }
  49. [Test] public void CanRemove_ParentAndChildForeign()
  50. {
  51. DataSet ds = DataProvider.CreateForigenConstraint();
  52. //remove the forigen and ask about the unique
  53. ds.Tables["child"].Constraints.Remove(ds.Tables["child"].Constraints[0]);
  54. Assert.AreEqual(true, ds.Tables["parent"].Constraints.CanRemove(ds.Tables["parent"].Constraints[0]), "CN3");
  55. }
  56. // FIXME. This test isn't complete.
  57. public void CanRemove_Unique()
  58. {
  59. DataTable dt = DataProvider.CreateUniqueConstraint();
  60. //remove the forigen and ask about the unique
  61. dt.Constraints.Remove(dt.Constraints[0]);
  62. Assert.AreEqual(true, dt.Constraints.CanRemove(dt.Constraints[0]), "CN4");
  63. }
  64. [Test] public void Clear_Foreign()
  65. {
  66. DataSet ds = DataProvider.CreateForigenConstraint();
  67. foreach(DataTable dt in ds.Tables)
  68. {
  69. dt.Constraints.Clear();
  70. }
  71. Assert.AreEqual(0, ds.Tables[0].Constraints.Count, "CN5");
  72. Assert.AreEqual(0, ds.Tables[0].Constraints.Count, "CN6");
  73. }
  74. [Test] public void Clear_Unique()
  75. {
  76. DataTable dt = DataProvider.CreateUniqueConstraint();
  77. int rowsCount = dt.Rows.Count;
  78. dt.Constraints.Clear();
  79. DataRow dr = dt.NewRow();
  80. dr[0] = 1;
  81. dt.Rows.Add(dr);
  82. Assert.AreEqual(rowsCount+1, dt.Rows.Count, "CN7"); //Just checking that no expection ocuured
  83. }
  84. [Test] public void CollectionChanged()
  85. {
  86. DataTable dt = DataProvider.CreateParentDataTable();
  87. CollectionChangedFlag = false;
  88. dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);
  89. dt = DataProvider.CreateUniqueConstraint(dt);
  90. Assert.AreEqual(true, CollectionChangedFlag, "CN8");
  91. }
  92. [Test] public void Contains_ByName()
  93. {
  94. DataSet ds = DataProvider.CreateForigenConstraint();
  95. //changing the constraints's name
  96. ds.Tables["child"].Constraints[0].ConstraintName = "name1";
  97. ds.Tables["parent"].Constraints[0].ConstraintName = "name2";
  98. Assert.AreEqual(true, ds.Tables["child"].Constraints.Contains("name1"), "CN9");
  99. Assert.AreEqual(false, ds.Tables["child"].Constraints.Contains("xxx"), "CN10");
  100. Assert.AreEqual(true, ds.Tables["parent"].Constraints.Contains("name2"), "CN11");
  101. Assert.AreEqual(false, ds.Tables["parent"].Constraints.Contains("xxx"), "CN12");
  102. }
  103. [Test] public void CopyTo()
  104. {
  105. DataTable dt = DataProvider.CreateUniqueConstraint();
  106. dt.Constraints.Add("constraint2",dt.Columns["String1"],true);
  107. object[] ar = new object[2];
  108. dt.Constraints.CopyTo(ar,0);
  109. Assert.AreEqual(2, ar.Length, "CN13");
  110. }
  111. [Test] public void Count()
  112. {
  113. DataTable dt = DataProvider.CreateUniqueConstraint();
  114. Assert.AreEqual(1, dt.Constraints.Count, "CN14");
  115. //Add
  116. dt.Constraints.Add("constraint2",dt.Columns["String1"],false);
  117. Assert.AreEqual(2, dt.Constraints.Count, "CN15");
  118. //Remove
  119. dt.Constraints.Remove("constraint2");
  120. Assert.AreEqual(1, dt.Constraints.Count, "CN16");
  121. }
  122. [Test] public void GetEnumerator()
  123. {
  124. DataTable dt = DataProvider.CreateUniqueConstraint();
  125. dt.Constraints.Add("constraint2",dt.Columns["String1"],false);
  126. int counter=0;
  127. IEnumerator myEnumerator = dt.Constraints.GetEnumerator();
  128. while (myEnumerator.MoveNext())
  129. {
  130. counter++;
  131. }
  132. Assert.AreEqual(2, counter, "CN17");
  133. }
  134. [Test] public void IndexOf()
  135. {
  136. DataTable dt = DataProvider.CreateUniqueConstraint();
  137. Assert.AreEqual(0, dt.Constraints.IndexOf(dt.Constraints[0]), "CN18");
  138. //Add new constraint
  139. Constraint con = new UniqueConstraint(dt.Columns["String1"],false);
  140. dt.Constraints.Add(con);
  141. Assert.AreEqual(1, dt.Constraints.IndexOf(con), "CN19");
  142. //Remove it and try to look for it
  143. dt.Constraints.Remove(con);
  144. Assert.AreEqual(-1, dt.Constraints.IndexOf(con), "CN20");
  145. }
  146. [Test] public void IndexOf_ByName()
  147. {
  148. DataTable dt = DataProvider.CreateUniqueConstraint();
  149. dt.Constraints[0].ConstraintName="name1";
  150. Assert.AreEqual(0, dt.Constraints.IndexOf("name1"), "CN21");
  151. //Add new constraint
  152. Constraint con = new UniqueConstraint(dt.Columns["String1"],false);
  153. con.ConstraintName="name2";
  154. dt.Constraints.Add(con);
  155. Assert.AreEqual(1, dt.Constraints.IndexOf("name2"), "CN22");
  156. //Remove it and try to look for it
  157. dt.Constraints.Remove(con);
  158. Assert.AreEqual(-1, dt.Constraints.IndexOf("name2"), "CN23");
  159. }
  160. [Test] public void IndexOf_SameColumns ()
  161. {
  162. DataSet ds = new DataSet ();
  163. DataTable table1 = ds.Tables.Add ("table1");
  164. DataTable table2 = ds.Tables.Add ("table2");
  165. DataColumn pcol = table1.Columns.Add ("col1");
  166. DataColumn ccol = table2.Columns.Add ("col1");
  167. ds.Relations.Add ("fk_rel", pcol, ccol);
  168. ForeignKeyConstraint fk = new ForeignKeyConstraint ("fk", pcol, ccol);
  169. Assert.AreEqual (-1, ds.Tables [1].Constraints.IndexOf (fk), "#1");
  170. }
  171. [Test]
  172. public void Add_RelationFirst_ConstraintNext()
  173. {
  174. DataSet ds = new DataSet ();
  175. DataTable table1 = ds.Tables.Add ("table1");
  176. DataTable table2 = ds.Tables.Add ("table2");
  177. DataColumn pcol = table1.Columns.Add ("col1");
  178. DataColumn ccol = table2.Columns.Add ("col1");
  179. ds.Relations.Add ("fk_rel", pcol, ccol);
  180. try {
  181. table2.Constraints.Add ("fk_cons", pcol, ccol);
  182. Assert.Fail ("#1 Cannot add duplicate fk constraint");
  183. }catch (DataException e) {
  184. }
  185. try {
  186. table1.Constraints.Add ("pk_cons", pcol, false);
  187. Assert.Fail ("#2 Cannot add duplicate unique constraint");
  188. }catch (DataException e) {
  189. }
  190. }
  191. [Test]
  192. public void Add_ConstraintFirst_RelationNext ()
  193. {
  194. DataSet ds = new DataSet ();
  195. DataTable table1 = ds.Tables.Add ("table1");
  196. DataTable table2 = ds.Tables.Add ("table2");
  197. DataColumn pcol = table1.Columns.Add ("col1");
  198. DataColumn ccol = table2.Columns.Add ("col1");
  199. table2.Constraints.Add ("fk_cons", pcol, ccol);
  200. // Should not throw DataException
  201. ds.Relations.Add ("fk_rel", pcol, ccol);
  202. Assert.AreEqual (1, table2.Constraints.Count, "#1 duplicate constraint shudnt be added");
  203. Assert.AreEqual (1, table1.Constraints.Count, "#2 duplicate constraint shudnt be added");
  204. Assert.AreEqual ("fk_cons", table2.Constraints [0].ConstraintName, "#3 shouldnt be overwritten");
  205. Assert.AreEqual ("Constraint1", table1.Constraints [0].ConstraintName, "#4 shouldnt be overwritten");
  206. }
  207. [Test] public void IsReadOnly()
  208. {
  209. DataTable dt = DataProvider.CreateUniqueConstraint();
  210. Assert.AreEqual(false, dt.Constraints.IsReadOnly, "CN24");
  211. }
  212. [Test] public void IsSynchronized()
  213. {
  214. DataTable dt = DataProvider.CreateUniqueConstraint();
  215. Assert.AreEqual(false, dt.Constraints.IsSynchronized, "CN25");
  216. ConstraintCollection col = (ConstraintCollection)dt.Constraints.SyncRoot;
  217. // lock(dt.Constraints.SyncRoot)
  218. // {
  219. // Assert.AreEqual(true, col.IsSynchronized, "CN26");
  220. //}
  221. }
  222. [Test] public void Remove()
  223. {
  224. DataTable dt = DataProvider.CreateUniqueConstraint();
  225. dt.Constraints.Remove(dt.Constraints[0]);
  226. Assert.AreEqual(0, dt.Constraints.Count, "CN27");
  227. }
  228. [Test] public void Remove_CheckUnique ()
  229. {
  230. DataTable table = new DataTable ();
  231. DataColumn col1 = table.Columns.Add ("col1");
  232. DataColumn col2 = table.Columns.Add ("col2");
  233. Assert.IsFalse (col1.Unique, "#1");
  234. Constraint uc = table.Constraints.Add ("", col1, false);
  235. Assert.IsTrue (col1.Unique, "#2 col shud be set to unique");
  236. table.Constraints.Remove (uc);
  237. Assert.IsFalse (col1.Unique, "#3 col should no longer be unique");
  238. table.PrimaryKey = new DataColumn[] {col2};
  239. try {
  240. table.Constraints.Remove (table.Constraints [0]);
  241. Assert.Fail ("#4 Cannot Remove PrimaryKey");
  242. } catch (ArgumentException) {
  243. }
  244. }
  245. [Test] public void Remove_ByNameSimple()
  246. {
  247. DataTable dt = DataProvider.CreateUniqueConstraint();
  248. dt.Constraints[0].ConstraintName = "constraint1";
  249. dt.Constraints.Remove("constraint1");
  250. Assert.AreEqual(0, dt.Constraints.Count, "CN28");
  251. }
  252. [Test] public void Remove_ByNameWithAdd()
  253. {
  254. DataTable dt = DataProvider.CreateUniqueConstraint();
  255. dt.Constraints[0].ConstraintName = "constraint1";
  256. Constraint con = new UniqueConstraint(dt.Columns["String1"],false);
  257. dt.Constraints.Add(con);
  258. dt.Constraints.Remove(con);
  259. Assert.AreEqual(1, dt.Constraints.Count, "CN29");
  260. Assert.AreEqual("constraint1", dt.Constraints[0].ConstraintName, "CN30");
  261. }
  262. [Test] public void Remove_CollectionChangedEvent()
  263. {
  264. DataTable dt = DataProvider.CreateUniqueConstraint();
  265. CollectionChangedFlag = false;
  266. dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);
  267. dt.Constraints.Remove(dt.Constraints[0]);
  268. Assert.AreEqual(true, CollectionChangedFlag, "CN31"); //Checking that event has raised
  269. }
  270. [Test] public void Remove_ByNameCollectionChangedEvent()
  271. {
  272. DataTable dt = DataProvider.CreateUniqueConstraint();
  273. CollectionChangedFlag = false;
  274. dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);
  275. dt.Constraints.Remove("constraint1");
  276. Assert.AreEqual(true, CollectionChangedFlag, "CN32"); //Checking that event has raised
  277. }
  278. [Test] public void add_CollectionChanged()
  279. {
  280. DataTable dt = DataProvider.CreateParentDataTable();
  281. CollectionChangedFlag = false;
  282. dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);
  283. dt = DataProvider.CreateUniqueConstraint(dt);
  284. Assert.AreEqual(true, CollectionChangedFlag, "CN33");
  285. }
  286. private void Constraints_CollectionChangedHandler(object sender, CollectionChangeEventArgs e)
  287. {
  288. CollectionChangedFlag = true;
  289. }
  290. [Test]
  291. public void Remove_Constraint ()
  292. {
  293. DataTable table1 = new DataTable ("table1");
  294. DataTable table2 = new DataTable ("table2");
  295. DataColumn col1 = table1.Columns.Add ("col1", typeof (int));
  296. DataColumn col2 = table1.Columns.Add ("col2", typeof (int));
  297. DataColumn col3 = table2.Columns.Add ("col1", typeof (int));
  298. Constraint c1 = table1.Constraints.Add ("unique1", col1, false);
  299. Constraint c2 = table1.Constraints.Add ("unique2", col2, false);
  300. Constraint c3 = table2.Constraints.Add ("fk", col1, col3);
  301. table1.Constraints.Remove (c1);
  302. table1.Constraints.Remove (c2);
  303. table2.Constraints.Remove (c3);
  304. Assert.AreEqual (0, table1.Constraints.Count, "#1");
  305. Assert.AreEqual (0, table2.Constraints.Count, "#2");
  306. DataSet ds = new DataSet ();
  307. ds.Tables.Add (table1);
  308. ds.Tables.Add (table2);
  309. c1 = table1.Constraints.Add ("unique1", col1, false);
  310. c2 = table1.Constraints.Add ("unique2", col2, false);
  311. c3 = table2.Constraints.Add ("fk", col1, col3);
  312. try {
  313. table1.Constraints.Remove (c1);
  314. Assert.Fail ("#3");
  315. } catch (ArgumentException) {
  316. }
  317. Assert.AreEqual (2, table1.Constraints.Count, "#4");
  318. table1.Constraints.Remove (c2);
  319. Assert.AreEqual (1, table1.Constraints.Count, "#5");
  320. table2.Constraints.Remove (c3);
  321. Assert.AreEqual (1, table1.Constraints.Count, "#6");
  322. Assert.AreEqual (0, table2.Constraints.Count, "#7");
  323. table1.Constraints.Remove (c1);
  324. Assert.AreEqual (0, table1.Constraints.Count, "#8");
  325. }
  326. public delegate void testExceptionMethodCallback();
  327. [Test]
  328. public void Add_Constraint()
  329. {
  330. DataTable dt = DataProvider.CreateUniqueConstraint();
  331. Assert.AreEqual(1,dt.Constraints.Count,"ccac#1");
  332. Assert.AreEqual("Constraint1",dt.Constraints[0].ConstraintName,"ccac#2");
  333. DataSet ds = DataProvider.CreateForigenConstraint();
  334. Assert.AreEqual(1,ds.Tables[1].Constraints.Count,"ccac#3");
  335. Assert.AreEqual(1,ds.Tables[0].Constraints.Count,"ccac#4");
  336. ArrayList arr = new ArrayList(1);
  337. arr.Add(new ConstraintException());
  338. TestException( new testExceptionMethodCallback(DataProvider.TryToBreakUniqueConstraint),arr);
  339. arr = new ArrayList(1);
  340. arr.Add(new InvalidConstraintException());
  341. TestException( new testExceptionMethodCallback(DataProvider.TryToBreakForigenConstraint),arr);
  342. }
  343. public void TestException(testExceptionMethodCallback dlg,IList exceptionList)
  344. {
  345. try {
  346. dlg();
  347. Assert.Fail("ccac#A", ((Exception)exceptionList[0]).ToString());
  348. }
  349. catch(Exception ex) {
  350. foreach(Exception expectedEx in exceptionList)
  351. if ( (expectedEx.GetType()) == (ex.GetType()) )
  352. return;
  353. Assert.Fail("ccac#B");
  354. }
  355. }
  356. [Test]
  357. public void Add_SDB1()
  358. {
  359. DataTable dt = DataProvider.CreateParentDataTable();
  360. dt.Constraints.Add("UniqueConstraint",dt.Columns["ParentId"],true);
  361. Assert.AreEqual(1,dt.Constraints.Count,1);
  362. Assert.AreEqual("UniqueConstraint",dt.Constraints[0].ConstraintName,"CN34");
  363. }
  364. [Test]
  365. public void Add_SDB2()
  366. {
  367. DataTable dt = DataProvider.CreateParentDataTable();
  368. dt.Constraints.Add("UniqueConstraint",dt.Columns["ParentId"],false);
  369. Assert.AreEqual(1,dt.Constraints.Count,"CN34");
  370. Assert.AreEqual("UniqueConstraint",dt.Constraints[0].ConstraintName,"CN35");
  371. }
  372. [Test]
  373. public void Add_SDB3()
  374. {
  375. DataTable dt = DataProvider.CreateParentDataTable();
  376. dt.Constraints.Add("UniqueConstraint",dt.Columns["ParentId"],true);
  377. //Break the constraint
  378. ArrayList arr = new ArrayList(1);
  379. arr.Add(new ConstraintException());
  380. TestException( new testExceptionMethodCallback(DataProvider.TryToBreakUniqueConstraint),arr);
  381. }
  382. [Test]
  383. [ExpectedException(typeof(ConstraintException))]
  384. public void Add_SDB4()
  385. {
  386. DataTable dt = DataProvider.CreateParentDataTable();
  387. dt.Constraints.Add("UniqueConstraint",dt.Columns["ParentId"],false);
  388. //Break the constraint --> but we shouldn't get the excption --> wrong assumpation
  389. //TODO:check the right thing
  390. DataProvider.TryToBreakUniqueConstraint();
  391. Assert.AreEqual(2,dt.Select("ParentId=1").Length,"CN36");
  392. }
  393. [Test]
  394. public void Add_Constraint_Column_Column()
  395. {
  396. DataTable parent = DataProvider.CreateParentDataTable();
  397. DataTable child = DataProvider.CreateChildDataTable();
  398. child.Constraints.Add("ForigenConstraint",parent.Columns[0],child.Columns[0]);
  399. Assert.AreEqual(1,parent.Constraints.Count,"ccaccc#1");
  400. Assert.AreEqual(1,child.Constraints.Count,"ccaccc#2");
  401. Assert.AreEqual("ForigenConstraint",child.Constraints[0].ConstraintName,"ccaccc#3");
  402. parent = DataProvider.CreateParentDataTable();
  403. child = DataProvider.CreateChildDataTable();
  404. child.Constraints.Add("ForigenConstraint",parent.Columns[0],child.Columns[0]);
  405. ArrayList arr = new ArrayList(1);
  406. arr.Add(new InvalidConstraintException());
  407. TestException( new testExceptionMethodCallback(DataProvider.TryToBreakForigenConstraint),arr);
  408. Assert.AreEqual(1,parent.Constraints.Count,"ccaccc#4");
  409. Assert.AreEqual(1,child.Constraints.Count,"ccaccc#5");
  410. }
  411. [Test]
  412. public void AddRange_C1()
  413. {
  414. DataTable dt = new DataTable();
  415. dt.Constraints.AddRange(null);
  416. Assert.AreEqual(0,dt.Constraints.Count,"ccarc#1");
  417. }
  418. [Test]
  419. public void AddRange_C2()
  420. {
  421. DataSet ds = new DataSet();
  422. ds.Tables.Add(DataProvider.CreateParentDataTable());
  423. ds.Tables.Add(DataProvider.CreateChildDataTable());
  424. ds.Tables[1].Constraints.AddRange(GetConstraintArray(ds)); //Cuz foreign key belongs to child table
  425. Assert.AreEqual(2,ds.Tables[1].Constraints.Count,"ccarc#2");
  426. Assert.AreEqual(1,ds.Tables[0].Constraints.Count,"ccarc#3");
  427. }
  428. [Test]
  429. [ExpectedException(typeof(ArgumentException))]
  430. public void AddRange_C3()
  431. {
  432. DataSet ds = new DataSet();
  433. ds.Tables.Add(DataProvider.CreateParentDataTable());
  434. ds.Tables.Add(DataProvider.CreateChildDataTable());
  435. Constraint badConstraint = new UniqueConstraint(ds.Tables[0].Columns[0]);
  436. ds.Tables[1].Constraints.AddRange(new Constraint[] {badConstraint}); //Cuz foreign key belongs to child table
  437. }
  438. [Test]
  439. public void AddRange_C4()
  440. {
  441. ArrayList arr = new ArrayList(1);
  442. arr.Add(new ArgumentException());
  443. TestException(new testExceptionMethodCallback(AddRange_C3),arr);
  444. }
  445. private Constraint[] GetConstraintArray(DataSet ds)
  446. {
  447. DataTable parent = ds.Tables[0];
  448. DataTable child = ds.Tables[1];
  449. Constraint[] constArray = new Constraint[2];
  450. //Create unique
  451. constArray[0] = new UniqueConstraint("Unique1",child.Columns["ChildDouble"]);
  452. //Create foreign
  453. constArray[1] = new ForeignKeyConstraint(parent.Columns[0],child.Columns[1]);
  454. return constArray;
  455. }
  456. [Test]
  457. public void Item()
  458. {
  459. DataTable dt = DataProvider.CreateUniqueConstraint();
  460. dt.Constraints[0].ConstraintName = "constraint1";
  461. Assert.AreEqual("constraint1",dt.Constraints[0].ConstraintName,"cci#1");
  462. Assert.AreEqual("constraint1",dt.Constraints["constraint1"].ConstraintName,"cci#2");
  463. ArrayList arr = new ArrayList(1);
  464. arr.Add(new IndexOutOfRangeException());
  465. TestException(new testExceptionMethodCallback(Item2),arr);
  466. }
  467. private void Item2()
  468. {
  469. DataTable dt = DataProvider.CreateUniqueConstraint();
  470. dt.Constraints[1].ConstraintName = "error";
  471. }
  472. private bool collectionChanged=false;
  473. [Test]
  474. public void RemoveAt_Integer()
  475. {
  476. DataTable dt = DataProvider.CreateUniqueConstraint();
  477. dt.Constraints.RemoveAt(0);
  478. Assert.AreEqual(0,dt.Constraints.Count,"ccrai#1");
  479. dt = DataProvider.CreateUniqueConstraint();
  480. Constraint con = new UniqueConstraint(dt.Columns["String1"],false);
  481. dt.Constraints[0].ConstraintName = "constraint1";
  482. con.ConstraintName="constraint2";
  483. dt.Constraints.Add(con);
  484. dt.Constraints.RemoveAt(0);
  485. Assert.AreEqual(1,dt.Constraints.Count,"ccrai#2");
  486. Assert.AreEqual("constraint2",dt.Constraints[0].ConstraintName,"ccrai#3");
  487. dt = DataProvider.CreateUniqueConstraint();
  488. dt.Constraints.CollectionChanged+=new CollectionChangeEventHandler(Constraints_CollectionChanged);
  489. dt.Constraints.RemoveAt(0);
  490. Assert.AreEqual(true,collectionChanged,"ccrai#4"); //Checking that event has raised
  491. ArrayList arr = new ArrayList(1);
  492. arr.Add(new IndexOutOfRangeException());
  493. TestException(new testExceptionMethodCallback(RemoveAt_I),arr);
  494. }
  495. private void Constraints_CollectionChanged(object sender, CollectionChangeEventArgs e)
  496. {
  497. collectionChanged = true;
  498. }
  499. private void RemoveAt_I()
  500. {
  501. DataTable dt = DataProvider.CreateUniqueConstraint();
  502. dt.Constraints.RemoveAt(2);
  503. }
  504. [Test]
  505. public void RemoveTest ()
  506. {
  507. DataTable table = new DataTable ();
  508. table.Columns.Add ("col1");
  509. Constraint c = table.Constraints.Add ("c", table.Columns [0], false);
  510. try {
  511. table.Constraints.Remove ("sdfs");
  512. Assert.Fail ("#1");
  513. } catch (ArgumentException e) {
  514. Assert.AreEqual ("Constraint 'sdfs' does not belong to this DataTable.",
  515. e.Message, "#2");
  516. }
  517. table.Constraints.Remove (c);
  518. Assert.AreEqual (0, table.Constraints.Count, "#3");
  519. // No exception shud be raised
  520. table.Constraints.Add (c);
  521. Assert.AreEqual (1, table.Constraints.Count, "#4");
  522. }
  523. }
  524. }