UniqueConstraintTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // UniqueConstraintTest.cs - NUnit Test Cases for testing the class System.Data.UniqueConstraint
  2. //
  3. // Franklin Wise <[email protected]>
  4. //
  5. // (C) 2002 Franklin Wise
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.Data;
  10. namespace MonoTests.System.Data
  11. {
  12. public class UniqueConstraintTest : TestCase
  13. {
  14. private DataTable _table;
  15. public UniqueConstraintTest() : base ("MonoTests.System.Data.UniqueConstraintTest") {}
  16. public UniqueConstraintTest(string name) : base(name) {}
  17. public void PublicSetup() {this.SetUp();}
  18. protected override void SetUp() {
  19. //Setup DataTable
  20. _table = new DataTable("TestTable");
  21. _table.Columns.Add("Col1",typeof(int));
  22. _table.Columns.Add("Col2",typeof(int));
  23. _table.Columns.Add("Col3",typeof(int));
  24. }
  25. protected override void TearDown() {}
  26. public static ITest Suite {
  27. get {
  28. return new TestSuite(typeof(UniqueConstraintTest));
  29. }
  30. }
  31. public void TestCtorExceptions() {
  32. //UniqueConstraint(string name, DataColumn column, bool isPrimaryKey)
  33. UniqueConstraint cst;
  34. //must have DataTable exception
  35. try{
  36. //Should throw an ArgumentException
  37. //Can only add DataColumns that are attached
  38. //to a DataTable
  39. cst = new UniqueConstraint(new DataColumn(""));
  40. Assertion.Fail("Failed to throw ArgumentException.");
  41. }
  42. catch (ArgumentException) {}
  43. catch (AssertionFailedError exc) {throw exc;}
  44. catch {
  45. Assertion.Fail("A1: Wrong Exception type.");
  46. }
  47. //Null exception
  48. try {
  49. //Should throw argument null exception
  50. cst = new UniqueConstraint((DataColumn)null);
  51. }
  52. catch (ArgumentNullException) {}
  53. catch (AssertionFailedError exc) {throw exc;}
  54. catch {
  55. Assertion.Fail("A2: Wrong Exception type.");
  56. }
  57. try {
  58. //Should throw exception
  59. //must have at least one valid column
  60. //InvalidConstraintException is thrown by msft ver
  61. cst = new UniqueConstraint(new DataColumn [] {});
  62. Assertion.Fail("B1: Failed to throw InvalidConstraintException.");
  63. }
  64. catch (InvalidConstraintException) {}
  65. catch (AssertionFailedError exc) {throw exc;}
  66. catch {
  67. Assertion.Fail("A3: Wrong Exception type.");
  68. }
  69. DataTable dt = new DataTable("Table1");
  70. dt.Columns.Add("Col1",typeof(int));
  71. DataTable dt2 = new DataTable("Table2");
  72. dt2.Columns.Add("Col1",typeof(int));
  73. DataSet ds = new DataSet();
  74. ds.Tables.Add(dt);
  75. ds.Tables.Add(dt2);
  76. //columns from two different tables.
  77. try {
  78. //next line should throw
  79. //can't have columns from two different tables
  80. cst = new UniqueConstraint(new DataColumn [] {
  81. dt.Columns[0], dt2.Columns[0]});
  82. Assertion.Fail("B2: Failed to throw InvalidConstraintException");
  83. }
  84. catch (InvalidConstraintException) {}
  85. catch (AssertionFailedError exc) {throw exc;}
  86. catch {
  87. Assertion.Fail("A4: Wrong Exception type.");
  88. }
  89. }
  90. public void TestCtor() {
  91. UniqueConstraint cst;
  92. //Success case
  93. try {
  94. cst = new UniqueConstraint(_table.Columns[0]);
  95. }
  96. catch (Exception exc) {
  97. Assertion.Fail("A1: Failed to ctor. " + exc.ToString());
  98. }
  99. try {
  100. cst = new UniqueConstraint( new DataColumn [] {
  101. _table.Columns[0], _table.Columns[1]});
  102. }
  103. catch (Exception exc) {
  104. Assertion.Fail("A2: Failed to ctor. " + exc.ToString());
  105. }
  106. //table is set on ctor
  107. cst = new UniqueConstraint(_table.Columns[0]);
  108. Assertion.AssertSame("B1", cst.Table, _table);
  109. //table is set on ctor
  110. cst = new UniqueConstraint( new DataColumn [] {
  111. _table.Columns[0], _table.Columns[1]});
  112. Assertion.AssertSame ("B2", cst.Table, _table);
  113. cst = new UniqueConstraint("MyName",_table.Columns[0],true);
  114. //Test ctor parm set for ConstraintName & IsPrimaryKey
  115. Assertion.AssertEquals("ConstraintName not set in ctor.",
  116. "MyName", cst.ConstraintName);
  117. Assertion.AssertEquals("IsPrimaryKey not set in ctor.",
  118. true, cst.IsPrimaryKey);
  119. }
  120. public void TestEqualsAndHashCode() {
  121. UniqueConstraint cst = new UniqueConstraint( new DataColumn [] {
  122. _table.Columns[0], _table.Columns[1]});
  123. UniqueConstraint cst2 = new UniqueConstraint( new DataColumn [] {
  124. _table.Columns[1], _table.Columns[0]});
  125. UniqueConstraint cst3 = new UniqueConstraint(_table.Columns[0]);
  126. UniqueConstraint cst4 = new UniqueConstraint(_table.Columns[2]);
  127. //true
  128. Assertion.Assert(cst.Equals(cst2) == true);
  129. //false
  130. Assertion.Assert("A1", cst.Equals(23) == false);
  131. Assertion.Assert("A2", cst.Equals(cst3) == false);
  132. Assertion.Assert("A3", cst3.Equals(cst) == false);
  133. Assertion.Assert("A4", cst.Equals(cst4) == false);
  134. //true
  135. Assertion.Assert("HashEquals", cst.GetHashCode() == cst2.GetHashCode());
  136. //false
  137. Assertion.Assert("Hash Not Equals", (cst.GetHashCode() == cst3.GetHashCode()) == false);
  138. }
  139. }
  140. }