ForeignKeyConstraintTest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // ForeignKeyConstraintTest.cs - NUnit Test Cases for [explain here]
  2. //
  3. // Franklin Wise ([email protected])
  4. //
  5. // (C) Franklin Wise
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.Data;
  10. namespace MonoTests.System.Data
  11. {
  12. public class ForeignKeyConstraintTest : TestCase
  13. {
  14. private DataSet _ds;
  15. //NOTE: fk constraints only work when the table is part of a DataSet
  16. public ForeignKeyConstraintTest() : base ("MonoTests.System.Data.ForeignKeyConstraintTest") {}
  17. public ForeignKeyConstraintTest(string name) : base(name) {}
  18. protected override void SetUp()
  19. {
  20. _ds = new DataSet();
  21. //Setup DataTable
  22. DataTable table;
  23. table = new DataTable("TestTable");
  24. table.Columns.Add("Col1",typeof(int));
  25. table.Columns.Add("Col2",typeof(int));
  26. table.Columns.Add("Col3",typeof(int));
  27. _ds.Tables.Add(table);
  28. table = new DataTable("TestTable2");
  29. table.Columns.Add("Col1",typeof(int));
  30. table.Columns.Add("Col2",typeof(int));
  31. table.Columns.Add("Col3",typeof(int));
  32. _ds.Tables.Add(table);
  33. }
  34. protected override void TearDown() {}
  35. public static ITest Suite
  36. {
  37. get
  38. {
  39. return new TestSuite(typeof(ForeignKeyConstraintTest));
  40. }
  41. }
  42. public void TestCtorExceptions ()
  43. {
  44. ForeignKeyConstraint fkc;
  45. DataTable localTable = new DataTable();
  46. localTable.Columns.Add("Col1",typeof(int));
  47. localTable.Columns.Add("Col2",typeof(bool));
  48. //Null
  49. try
  50. {
  51. fkc = new ForeignKeyConstraint((DataColumn)null,(DataColumn)null);
  52. Assertion.Fail("Failed to throw ArgumentNullException.");
  53. }
  54. catch (ArgumentNullException) {}
  55. catch (AssertionFailedError exc) {throw exc;}
  56. catch (Exception exc)
  57. {
  58. Assertion.Fail("A1: Wrong Exception type. " + exc.ToString());
  59. }
  60. //zero length collection
  61. try
  62. {
  63. fkc = new ForeignKeyConstraint(new DataColumn[]{},new DataColumn[]{});
  64. Assertion.Fail("B1: Failed to throw ArgumentException.");
  65. }
  66. catch (ArgumentException) {}
  67. catch (AssertionFailedError exc) {throw exc;}
  68. catch (Exception exc)
  69. {
  70. Assertion.Fail("A2: Wrong Exception type. " + exc.ToString());
  71. }
  72. //different datasets
  73. try
  74. {
  75. fkc = new ForeignKeyConstraint(_ds.Tables[0].Columns[0], localTable.Columns[0]);
  76. Assertion.Fail("Failed to throw InvalidOperationException.");
  77. }
  78. catch (InvalidOperationException) {}
  79. catch (AssertionFailedError exc) {throw exc;}
  80. catch (Exception exc)
  81. {
  82. Assertion.Fail("A3: Wrong Exception type. " + exc.ToString());
  83. }
  84. //different dataTypes
  85. try
  86. {
  87. fkc = new ForeignKeyConstraint(_ds.Tables[0].Columns[0], localTable.Columns[1]);
  88. Assertion.Fail("Failed to throw InvalidOperationException.");
  89. }
  90. catch (InvalidOperationException) {}
  91. catch (AssertionFailedError exc) {throw exc;}
  92. catch (Exception exc)
  93. {
  94. Assertion.Fail("A4: Wrong Exception type. " + exc.ToString());
  95. }
  96. }
  97. public void TestCtorExceptions2 ()
  98. {
  99. DataColumn col = new DataColumn("MyCol1",typeof(int));
  100. ForeignKeyConstraint fkc;
  101. //Columns must belong to a Table
  102. try
  103. {
  104. fkc = new ForeignKeyConstraint(col, _ds.Tables[0].Columns[0]);
  105. Assertion.Fail("FTT1: Failed to throw ArgumentException.");
  106. }
  107. catch (ArgumentException) {}
  108. catch (AssertionFailedError exc) {throw exc;}
  109. catch (Exception exc)
  110. {
  111. Assertion.Fail("WET1: Wrong Exception type. " + exc.ToString());
  112. }
  113. //Columns must belong to the same table
  114. //InvalidConstraintException
  115. DataColumn [] difTable = new DataColumn [] {_ds.Tables[0].Columns[2],
  116. _ds.Tables[1].Columns[0]};
  117. try
  118. {
  119. fkc = new ForeignKeyConstraint(difTable,new DataColumn[] {
  120. _ds.Tables[0].Columns[1],
  121. _ds.Tables[0].Columns[0]});
  122. Assertion.Fail("FTT2: Failed to throw InvalidConstraintException.");
  123. }
  124. catch (InvalidConstraintException) {}
  125. catch (AssertionFailedError exc) {throw exc;}
  126. catch (Exception exc)
  127. {
  128. Assertion.Fail("WET2: Wrong Exception type. " + exc.ToString());
  129. }
  130. //parent columns and child columns should be the same length
  131. //ArgumentException
  132. DataColumn [] twoCol =
  133. new DataColumn [] {_ds.Tables[0].Columns[0],_ds.Tables[0].Columns[1]};
  134. try
  135. {
  136. fkc = new ForeignKeyConstraint(twoCol,
  137. new DataColumn[] { _ds.Tables[0].Columns[0]});
  138. Assertion.Fail("FTT3: Failed to throw ArgumentException.");
  139. }
  140. catch (ArgumentException) {}
  141. catch (AssertionFailedError exc) {throw exc;}
  142. catch (Exception exc)
  143. {
  144. Assertion.Fail("WET3: Wrong Exception type. " + exc.ToString());
  145. }
  146. //InvalidOperation: Parent and child are the same column.
  147. try
  148. {
  149. fkc = new ForeignKeyConstraint( _ds.Tables[0].Columns[0],
  150. _ds.Tables[0].Columns[0] );
  151. Assertion.Fail("FTT4: Failed to throw InvalidOperationException.");
  152. }
  153. catch (InvalidOperationException) {}
  154. catch (AssertionFailedError exc) {throw exc;}
  155. catch (Exception exc)
  156. {
  157. Assertion.Fail("WET4: Wrong Exception type. " + exc.ToString());
  158. }
  159. }
  160. public void TestEqualsAndHashCode()
  161. {
  162. DataTable tbl = _ds.Tables[0];
  163. DataTable tbl2 = _ds.Tables[1];
  164. ForeignKeyConstraint fkc = new ForeignKeyConstraint(
  165. new DataColumn[] {tbl.Columns[0], tbl.Columns[1]} ,
  166. new DataColumn[] {tbl2.Columns[0], tbl2.Columns[1]} );
  167. ForeignKeyConstraint fkc2 = new ForeignKeyConstraint(
  168. new DataColumn[] {tbl.Columns[0], tbl.Columns[1]} ,
  169. new DataColumn[] {tbl2.Columns[0], tbl2.Columns[1]} );
  170. ForeignKeyConstraint fkcDiff =
  171. new ForeignKeyConstraint( tbl.Columns[1], tbl.Columns[2]);
  172. Assertion.Assert( "Equals failed. 1" , fkc.Equals(fkc2));
  173. Assertion.Assert( "Equals failed. 2" , fkc2.Equals(fkc));
  174. Assertion.Assert( "Equals failed. 3" , fkc.Equals(fkc));
  175. Assertion.Assert( "Equals failed diff. 1" , fkc.Equals(fkcDiff) == false);
  176. Assertion.Assert( "Hash Code Failed. 1", fkc.GetHashCode() == fkc2.GetHashCode() );
  177. Assertion.Assert( "Hash Code Failed. 2", fkc.GetHashCode() != fkcDiff.GetHashCode() );
  178. }
  179. }
  180. }