UniqueConstraintTest.cs 6.0 KB

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