DataRelationTest.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. //
  2. // DataRelationTest.cs - NUnit Test Cases for DataRelation
  3. //
  4. // Authors:
  5. // Ville Palo ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. //
  8. // (C) 2003 Ville Palo
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using NUnit.Framework;
  34. using System;
  35. using System.Data;
  36. namespace MonoTests.System.Data
  37. {
  38. [TestFixture]
  39. public class DataRelationTest : Assertion
  40. {
  41. private DataSet Set = null;
  42. private DataTable Mom = null;
  43. private DataTable Child = null;
  44. [SetUp]
  45. public void GetReady()
  46. {
  47. Set = new DataSet ();
  48. Mom = new DataTable ("Mom");
  49. Child = new DataTable ("Child");
  50. Set.Tables.Add (Mom);
  51. Set.Tables.Add (Child);
  52. DataColumn Col = new DataColumn ("Name");
  53. DataColumn Col2 = new DataColumn ("ChildName");
  54. Mom.Columns.Add (Col);
  55. Mom.Columns.Add (Col2);
  56. DataColumn Col3 = new DataColumn ("Name");
  57. DataColumn Col4 = new DataColumn ("Age");
  58. Col4.DataType = Type.GetType ("System.Int16");
  59. Child.Columns.Add (Col3);
  60. Child.Columns.Add (Col4);
  61. }
  62. [Test]
  63. public void Foreign ()
  64. {
  65. DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
  66. Set.Relations.Add (Relation);
  67. DataRow Row = Mom.NewRow ();
  68. Row [0] = "Teresa";
  69. Row [1] = "Jack";
  70. Mom.Rows.Add (Row);
  71. Row = Mom.NewRow ();
  72. Row [0] = "Teresa";
  73. Row [1] = "Dick";
  74. Mom.Rows.Add (Row);
  75. Row = Mom.NewRow ();
  76. Row [0] = "Mary";
  77. Row [1] = "Harry";
  78. Row = Child.NewRow ();
  79. Row [0] = "Jack";
  80. Row [1] = 16;
  81. Child.Rows.Add (Row);
  82. Row = Child.NewRow ();
  83. Row [0] = "Dick";
  84. Row [1] = 56;
  85. Child.Rows.Add (Row);
  86. AssertEquals ("test#01", 2, Child.Rows.Count);
  87. Row = Mom.Rows [0];
  88. Row.Delete ();
  89. AssertEquals ("test#02", 1, Child.Rows.Count);
  90. Row = Mom.NewRow ();
  91. Row [0] = "Teresa";
  92. Row [1] = "Dick";
  93. try {
  94. Mom.Rows.Add (Row);
  95. Fail ("test#03");
  96. } catch (Exception e) {
  97. AssertEquals ("test#04", typeof (ConstraintException), e.GetType ());
  98. AssertEquals ("test#05", "Column 'ChildName' is constrained to be unique. Value 'Dick' is already present.", e.Message);
  99. }
  100. Row = Mom.NewRow ();
  101. Row [0] = "Teresa";
  102. Row [1] = "Mich";
  103. Mom.Rows.Add (Row);
  104. AssertEquals ("test#06", 1, Child.Rows.Count);
  105. Row = Child.NewRow ();
  106. Row [0] = "Jack";
  107. Row [1] = 16;
  108. try {
  109. Child.Rows.Add (Row);
  110. Fail ("test#07");
  111. } catch (Exception e) {
  112. AssertEquals ("test#08", typeof (InvalidConstraintException), e.GetType ());
  113. AssertEquals ("test#09", "ForeignKeyConstraint Rel requires the child key values (Jack) to exist in the parent table.", e.Message);
  114. }
  115. }
  116. [Test]
  117. [ExpectedException (typeof(InvalidConstraintException))]
  118. public void InvalidConstraintException ()
  119. {
  120. // Parent Columns and Child Columns don't have type-matching columns.
  121. DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [1], true);
  122. }
  123. [Test]
  124. [ExpectedException (typeof (InvalidConstraintException))]
  125. public void InvalidConstraintException2 ()
  126. {
  127. // Parent Columns and Child Columns don't have type-matching columns.
  128. Child.Columns [1].DataType = Mom.Columns [1].DataType;
  129. DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [1], true);
  130. Set.Relations.Add (Relation);
  131. AssertEquals("test#01", 1, Set.Relations.Count);
  132. Child.Columns [1].DataType = Type.GetType ("System.Double");
  133. }
  134. [Test]
  135. public void DataSetRelations ()
  136. {
  137. DataRelation Relation;
  138. AssertEquals ("test#01", 0, Set.Relations.Count);
  139. AssertEquals ("test#02", 0, Mom.ParentRelations.Count);
  140. AssertEquals ("test#03", 0, Mom.ChildRelations.Count);
  141. AssertEquals ("test#04", 0, Child.ParentRelations.Count);
  142. AssertEquals ("test#05", 0, Child.ChildRelations.Count);
  143. Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
  144. Set.Relations.Add (Relation);
  145. AssertEquals ("test#06", 1, Set.Relations.Count);
  146. AssertEquals ("test#07", 0, Mom.ParentRelations.Count);
  147. AssertEquals ("test#08", 1, Mom.ChildRelations.Count);
  148. AssertEquals ("test#09", 1, Child.ParentRelations.Count);
  149. AssertEquals ("test#10", 0, Child.ChildRelations.Count);
  150. Relation = Set.Relations [0];
  151. AssertEquals ("test#11", 1, Relation.ParentColumns.Length);
  152. AssertEquals ("test#12", 1, Relation.ChildColumns.Length);
  153. AssertEquals ("test#13", "Rel", Relation.ChildKeyConstraint.ConstraintName);
  154. AssertEquals ("test#14", "Constraint1", Relation.ParentKeyConstraint.ConstraintName);
  155. }
  156. [Test]
  157. public void Constraints ()
  158. {
  159. AssertEquals ("test#01", 0, Mom.Constraints.Count);
  160. AssertEquals ("test#02", 0, Child.Constraints.Count);
  161. DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
  162. Set.Relations.Add (Relation);
  163. AssertEquals ("test#03", 1, Mom.Constraints.Count);
  164. AssertEquals ("test#04", 1, Child.Constraints.Count);
  165. AssertEquals ("test#05", typeof (ForeignKeyConstraint), Child.Constraints [0].GetType ());
  166. AssertEquals ("test#05", typeof (UniqueConstraint), Mom.Constraints [0].GetType ());
  167. }
  168. [Test]
  169. public void Creation ()
  170. {
  171. DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
  172. Set.Relations.Add (Relation);
  173. DataRelation Test = null;
  174. AssertEquals ("test#01", 1, Mom.ChildRelations.Count);
  175. AssertEquals ("test#02", 0, Child.ChildRelations.Count);
  176. AssertEquals ("test#03", 0, Mom.ParentRelations.Count);
  177. AssertEquals ("test#04", 1, Child.ParentRelations.Count);
  178. Test = Child.ParentRelations [0];
  179. AssertEquals ("test#05", "Rel", Test.ToString ());
  180. AssertEquals ("test#06", "Rel", Test.RelationName);
  181. AssertEquals ("test#07", "Mom", Test.ParentTable.TableName);
  182. AssertEquals ("test#08", 1, Test.ParentKeyConstraint.Columns.Length);
  183. AssertEquals ("test#09", false, Test.ParentKeyConstraint.IsPrimaryKey);
  184. AssertEquals ("test#10", 1, Test.ParentColumns.Length);
  185. AssertEquals ("test#11", false, Test.Nested);
  186. AssertEquals ("test#12", 0, Test.ExtendedProperties.Count);
  187. AssertEquals ("test#13", "Child", Test.ChildTable.TableName);
  188. AssertEquals ("test#14", "Rel", Test.ChildKeyConstraint.ConstraintName);
  189. AssertEquals ("test#15", 1, Test.ChildColumns.Length);
  190. }
  191. [Test]
  192. public void Creation2 ()
  193. {
  194. DataSet Set = new DataSet ();
  195. DataTable Mom2 = new DataTable ("Mom");
  196. DataTable Child2 = new DataTable ("Child");
  197. DataTable Hubby = new DataTable ("Hubby");
  198. Set.Tables.Add (Mom2);
  199. Set.Tables.Add (Child2);
  200. Set.Tables.Add (Hubby);
  201. DataColumn Col = new DataColumn ("Name");
  202. DataColumn Col2 = new DataColumn ("ChildName");
  203. DataColumn Col3 = new DataColumn ("hubby");
  204. Mom2.Columns.Add (Col);
  205. Mom2.Columns.Add (Col2);
  206. Mom2.Columns.Add (Col3);
  207. DataColumn Col4 = new DataColumn ("Name");
  208. DataColumn Col5 = new DataColumn ("Age");
  209. DataColumn Col6 = new DataColumn ("father");
  210. Child2.Columns.Add (Col4);
  211. Child2.Columns.Add (Col5);
  212. Child2.Columns.Add (Col6);
  213. DataColumn Col7 = new DataColumn ("Name");
  214. DataColumn Col8 = new DataColumn ("Age");
  215. Hubby.Columns.Add (Col7);
  216. Hubby.Columns.Add (Col8);
  217. DataColumn [] Parents = new DataColumn [2];
  218. Parents [0] = Col2;
  219. Parents [1] = Col3;
  220. DataColumn [] Childs = new DataColumn [2];
  221. Childs [0] = Col4;
  222. Childs [1] = Col7;
  223. DataRelation Relation = null;
  224. try {
  225. Relation = new DataRelation ("Rel", Parents, Childs);
  226. Fail ("test#01");
  227. } catch (InvalidConstraintException e) {
  228. // AssertEquals ("test#02", typeof (InvalidConstraintException), e.GetType ());
  229. // AssertEquals ("test#03", "Cannot create a Key from Columns that belong to different tables.", e.Message);
  230. }
  231. Childs [1] = Col6;
  232. Relation = new DataRelation ("Rel", Parents, Childs);
  233. Set.Relations.Add (Relation);
  234. DataRelation Test = null;
  235. AssertEquals ("test#01", 1, Mom2.ChildRelations.Count);
  236. AssertEquals ("test#02", 0, Child2.ChildRelations.Count);
  237. AssertEquals ("test#03", 0, Mom2.ParentRelations.Count);
  238. AssertEquals ("test#04", 1, Child2.ParentRelations.Count);
  239. Test = Child2.ParentRelations [0];
  240. AssertEquals ("test#05", "Rel", Test.ToString ());
  241. AssertEquals ("test#06", "Rel", Test.RelationName);
  242. AssertEquals ("test#07", "Mom", Test.ParentTable.TableName);
  243. AssertEquals ("test#08", 2, Test.ParentKeyConstraint.Columns.Length);
  244. AssertEquals ("test#09", false, Test.ParentKeyConstraint.IsPrimaryKey);
  245. AssertEquals ("test#10", 2, Test.ParentColumns.Length);
  246. AssertEquals ("test#11", false, Test.Nested);
  247. AssertEquals ("test#12", 0, Test.ExtendedProperties.Count);
  248. AssertEquals ("test#13", "Child", Test.ChildTable.TableName);
  249. AssertEquals ("test#14", "Rel", Test.ChildKeyConstraint.ConstraintName);
  250. AssertEquals ("test#15", 2, Test.ChildColumns.Length);
  251. AssertEquals ("test#16", 1, Mom2.Constraints.Count);
  252. AssertEquals ("test#17", "Constraint1", Mom2.Constraints [0].ToString ());
  253. AssertEquals ("test#18", 1, Child2.Constraints.Count);
  254. AssertEquals ("test#19", 0, Hubby.Constraints.Count);
  255. }
  256. [Test]
  257. public void Creation3 ()
  258. {
  259. DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0], false);
  260. Set.Relations.Add (Relation);
  261. DataRelation Test = null;
  262. AssertEquals ("test#01", 1, Mom.ChildRelations.Count);
  263. AssertEquals ("test#02", 0, Child.ChildRelations.Count);
  264. AssertEquals ("test#03", 0, Mom.ParentRelations.Count);
  265. AssertEquals ("test#04", 1, Child.ParentRelations.Count);
  266. Test = Child.ParentRelations [0];
  267. AssertEquals ("test#05", "Rel", Test.ToString ());
  268. AssertEquals ("test#06", "Rel", Test.RelationName);
  269. AssertEquals ("test#07", "Mom", Test.ParentTable.TableName);
  270. Assert ("test#08", Test.ParentKeyConstraint == null);
  271. Assert ("test#09", Test.ParentKeyConstraint == null);
  272. AssertEquals ("test#10", 1, Test.ParentColumns.Length);
  273. AssertEquals ("test#11", false, Test.Nested);
  274. AssertEquals ("test#12", 0, Test.ExtendedProperties.Count);
  275. AssertEquals ("test#13", "Child", Test.ChildTable.TableName);
  276. Assert ("test#14", Test.ChildKeyConstraint == null);
  277. AssertEquals ("test#15", 1, Test.ChildColumns.Length);
  278. AssertEquals ("test#16", 0, Mom.Constraints.Count);
  279. AssertEquals ("test#17", 0, Child.Constraints.Count);
  280. }
  281. [Test]
  282. public void Creation4 ()
  283. {
  284. DataRelation Relation = new DataRelation ("Rel", "Mom", "Child",
  285. new string [] {"ChildName"},
  286. new string [] {"Name"}, true);
  287. try {
  288. Set.Relations.Add (Relation);
  289. Fail ("test#01");
  290. } catch (Exception e) {
  291. AssertEquals ("test#02", typeof (NullReferenceException), e.GetType ());
  292. }
  293. try {
  294. Set.Relations.AddRange (new DataRelation [] {Relation});
  295. Fail ("test#03");
  296. } catch (Exception e) {
  297. AssertEquals ("test#04", typeof (NullReferenceException), e.GetType ());
  298. }
  299. Set.BeginInit ();
  300. Set.Relations.AddRange (new DataRelation [] {Relation});
  301. Set.EndInit ();
  302. DataRelation Test = null;
  303. AssertEquals ("test#01", 1, Mom.ChildRelations.Count);
  304. AssertEquals ("test#02", 0, Child.ChildRelations.Count);
  305. AssertEquals ("test#03", 0, Mom.ParentRelations.Count);
  306. AssertEquals ("test#04", 1, Child.ParentRelations.Count);
  307. Test = Child.ParentRelations [0];
  308. AssertEquals ("test#05", "Rel", Test.ToString ());
  309. AssertEquals ("test#06", "Rel", Test.RelationName);
  310. AssertEquals ("test#07", "Mom", Test.ParentTable.TableName);
  311. AssertEquals ("test#08", true, Test.ParentKeyConstraint == null);
  312. AssertEquals ("test#10", 1, Test.ParentColumns.Length);
  313. AssertEquals ("test#11", true, Test.Nested);
  314. AssertEquals ("test#12", 0, Test.ExtendedProperties.Count);
  315. AssertEquals ("test#13", "Child", Test.ChildTable.TableName);
  316. AssertEquals ("test#14", true, Test.ChildKeyConstraint == null);
  317. AssertEquals ("test#15", 1, Test.ChildColumns.Length);
  318. }
  319. [Test]
  320. public void RelationFromSchema ()
  321. {
  322. DataSet Set = new DataSet ();
  323. Set.ReadXmlSchema ("Test/System.Data/store.xsd");
  324. DataTable Table = Set.Tables [0];
  325. AssertEquals ("test#01", false, Table.CaseSensitive);
  326. AssertEquals ("test#02", 1, Table.ChildRelations.Count);
  327. AssertEquals ("test#03", 0, Table.ParentRelations.Count);
  328. AssertEquals ("test#04", 1, Table.Constraints.Count);
  329. AssertEquals ("test#05", 1, Table.PrimaryKey.Length);
  330. AssertEquals ("test#06", 0, Table.Rows.Count);
  331. AssertEquals ("test#07", "bookstore", Table.TableName);
  332. AssertEquals ("test#08", 1, Table.Columns.Count);
  333. DataRelation Relation = Table.ChildRelations [0];
  334. AssertEquals ("test#09", 1, Relation.ChildColumns.Length);
  335. AssertEquals ("test#10", "bookstore_book", Relation.ChildKeyConstraint.ConstraintName);
  336. AssertEquals ("test#11", 1, Relation.ChildKeyConstraint.Columns.Length);
  337. AssertEquals ("test#12", "book", Relation.ChildTable.TableName);
  338. AssertEquals ("test#13", "NewDataSet", Relation.DataSet.DataSetName);
  339. AssertEquals ("test#14", 0, Relation.ExtendedProperties.Count);
  340. AssertEquals ("test#15", true, Relation.Nested);
  341. AssertEquals ("test#16", 1, Relation.ParentColumns.Length);
  342. AssertEquals ("test#17", "Constraint1", Relation.ParentKeyConstraint.ConstraintName);
  343. AssertEquals ("test#18", "bookstore", Relation.ParentTable.TableName);
  344. AssertEquals ("test#19", "bookstore_book", Relation.RelationName);
  345. Table = Set.Tables [1];
  346. AssertEquals ("test#20", false, Table.CaseSensitive);
  347. AssertEquals ("test#21", 1, Table.ChildRelations.Count);
  348. AssertEquals ("test#22", 1, Table.ParentRelations.Count);
  349. AssertEquals ("test#23", 2, Table.Constraints.Count);
  350. AssertEquals ("test#24", 1, Table.PrimaryKey.Length);
  351. AssertEquals ("test#25", 0, Table.Rows.Count);
  352. AssertEquals ("test#26", "book", Table.TableName);
  353. AssertEquals ("test#27", 5, Table.Columns.Count);
  354. Relation = Table.ChildRelations [0];
  355. AssertEquals ("test#28", 1, Relation.ChildColumns.Length);
  356. AssertEquals ("test#29", "book_author", Relation.ChildKeyConstraint.ConstraintName);
  357. AssertEquals ("test#30", 1, Relation.ChildKeyConstraint.Columns.Length);
  358. AssertEquals ("test#31", "author", Relation.ChildTable.TableName);
  359. AssertEquals ("test#32", "NewDataSet", Relation.DataSet.DataSetName);
  360. AssertEquals ("test#33", 0, Relation.ExtendedProperties.Count);
  361. AssertEquals ("test#34", true, Relation.Nested);
  362. AssertEquals ("test#35", 1, Relation.ParentColumns.Length);
  363. AssertEquals ("test#36", "Constraint1", Relation.ParentKeyConstraint.ConstraintName);
  364. AssertEquals ("test#37", "book", Relation.ParentTable.TableName);
  365. AssertEquals ("test#38", "book_author", Relation.RelationName);
  366. Table = Set.Tables [2];
  367. AssertEquals ("test#39", false, Table.CaseSensitive);
  368. AssertEquals ("test#40", 0, Table.ChildRelations.Count);
  369. AssertEquals ("test#41", 1, Table.ParentRelations.Count);
  370. AssertEquals ("test#42", 1, Table.Constraints.Count);
  371. AssertEquals ("test#43", 0, Table.PrimaryKey.Length);
  372. AssertEquals ("test#44", 0, Table.Rows.Count);
  373. AssertEquals ("test#45", "author", Table.TableName);
  374. AssertEquals ("test#46", 3, Table.Columns.Count);
  375. }
  376. [Test]
  377. public void ChildRows ()
  378. {
  379. DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
  380. Set.Relations.Add (Relation);
  381. DataRow TempRow = Mom.NewRow ();
  382. TempRow [0] = "teresa";
  383. TempRow [1] = "john";
  384. Mom.Rows.Add (TempRow);
  385. TempRow = Mom.NewRow ();
  386. TempRow [0] = "teresa";
  387. TempRow [1] = "Dick";
  388. Mom.Rows.Add (TempRow);
  389. TempRow = Child.NewRow ();
  390. TempRow [0] = "john";
  391. TempRow [1] = "15";
  392. Child.Rows.Add (TempRow);
  393. TempRow = Child.NewRow ();
  394. TempRow [0] = "Dick";
  395. TempRow [1] = "10";
  396. Child.Rows.Add (TempRow);
  397. DataRow Row = Mom.Rows [1];
  398. TempRow = Row.GetChildRows ("Rel") [0];
  399. AssertEquals ("test#01", "Dick", TempRow [0]);
  400. AssertEquals ("test#02", "10", TempRow [1].ToString ());
  401. TempRow = TempRow.GetParentRow ("Rel");
  402. AssertEquals ("test#03", "teresa", TempRow [0]);
  403. AssertEquals ("test#04", "Dick", TempRow [1]);
  404. Row = Child.Rows [0];
  405. TempRow = Row.GetParentRows ("Rel") [0];
  406. AssertEquals ("test#05", "teresa", TempRow [0]);
  407. AssertEquals ("test#06", "john", TempRow [1]);
  408. }
  409. }
  410. }