/src/DbLinq/Test/Providers/CompositePK_Test.cs

http://dblinq2007.googlecode.com/ · C# · 199 lines · 137 code · 28 blank · 34 comment · 14 complexity · 77249cd245a71dbcd351bfeda56d76ec MD5 · raw file

  1. #region MIT license
  2. //
  3. // MIT license
  4. //
  5. // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #endregion
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using System.Text;
  30. using NUnit.Framework;
  31. using Test_NUnit;
  32. using System.Data.Linq;
  33. using nwind;
  34. // test ns
  35. #if MYSQL
  36. namespace Test_NUnit_MySql
  37. #elif ORACLE && ODP
  38. namespace Test_NUnit_OracleODP
  39. #elif ORACLE
  40. namespace Test_NUnit_Oracle
  41. #elif POSTGRES
  42. namespace Test_NUnit_PostgreSql
  43. #elif SQLITE
  44. namespace Test_NUnit_Sqlite
  45. #elif INGRES
  46. namespace Test_NUnit_Ingres
  47. #elif MSSQL && L2SQL
  48. namespace Test_NUnit_MsSql_Strict
  49. #elif MSSQL
  50. namespace Test_NUnit_MsSql
  51. #elif FIREBIRD
  52. namespace Test_NUnit_Firebird
  53. #endif
  54. {
  55. [TestFixture]
  56. public class CompositePK_Test : TestBase
  57. {
  58. const short TestQuantity = short.MaxValue;
  59. protected void cleanup(Northwind db)
  60. {
  61. try
  62. {
  63. // Get the name of the Order Details table properly evaluating the Annotation
  64. string tableName = null;// db.Vendor.GetSqlFieldSafeName("order details"); //eg. "[Order Details]"
  65. foreach (object obj in typeof(OrderDetail).GetCustomAttributes(true))
  66. {
  67. if (obj is System.Data.Linq.Mapping.TableAttribute)
  68. {
  69. tableName = ((System.Data.Linq.Mapping.TableAttribute)obj).Name;
  70. }
  71. }
  72. string sql = string.Format("DELETE FROM {0} WHERE Quantity={1}", tableName, TestQuantity);
  73. db.ExecuteCommand(sql);
  74. }
  75. catch (Exception)
  76. {
  77. }
  78. }
  79. [Test]
  80. public void CP1_DeletePreviousRows()
  81. {
  82. //delete any rows from previous testing
  83. Northwind db = CreateDB();
  84. // PC: this test was wrong, DeleteOnSubmit requires the object to be attached
  85. // (by query result or manually, we chose here the query result)
  86. //var orderDetail = new OrderDetail { OrderID = 3, ProductID = 2 };
  87. //db.OrderDetails.DeleteOnSubmit(orderDetail);
  88. var toDelete = from o in db.OrderDetails where o.OrderID == 3 && o.ProductID == 2 select o;
  89. db.OrderDetails.DeleteAllOnSubmit(toDelete);
  90. db.SubmitChanges();
  91. }
  92. [Test]
  93. public void CP2_UpdateTableWithCompositePK()
  94. {
  95. Northwind db = CreateDB();
  96. cleanup(db);
  97. var order = db.Orders.First();
  98. var product = db.Products.First();
  99. var startUnitPrice = 33000;
  100. var endUnitPrice = 34000;
  101. var orderDetail = new OrderDetail
  102. {
  103. OrderID = order.OrderID,
  104. ProductID = product.ProductID,
  105. Quantity = TestQuantity,
  106. UnitPrice = startUnitPrice
  107. };
  108. db.OrderDetails.InsertOnSubmit(orderDetail);
  109. db.SubmitChanges();
  110. orderDetail.UnitPrice = endUnitPrice;
  111. db.SubmitChanges();
  112. OrderDetail orderDetail2 = (from c in db.OrderDetails
  113. where c.UnitPrice == endUnitPrice
  114. select c).Single();
  115. Assert.IsTrue(object.ReferenceEquals(orderDetail, orderDetail2), "Must be same object");
  116. Assert.AreEqual(order.OrderID, orderDetail2.OrderID);
  117. Assert.AreEqual(product.ProductID, orderDetail2.ProductID);
  118. Assert.AreEqual(endUnitPrice, orderDetail2.UnitPrice);
  119. db.OrderDetails.DeleteOnSubmit(orderDetail);
  120. db.SubmitChanges();
  121. }
  122. [Test]
  123. public void CP3_DeleteTableWithCompositePK()
  124. {
  125. Northwind db = CreateDB();
  126. cleanup(db);
  127. int initialCount = db.OrderDetails.Count();
  128. var order = db.Orders.First();
  129. var product = db.Products.First();
  130. var orderDetail = new OrderDetail {
  131. OrderID = order.OrderID,
  132. ProductID = product.ProductID,
  133. Quantity = TestQuantity
  134. };
  135. db.OrderDetails.InsertOnSubmit(orderDetail);
  136. db.SubmitChanges();
  137. Assert.AreEqual(db.OrderDetails.Count(), initialCount + 1);
  138. db.OrderDetails.DeleteOnSubmit(orderDetail);
  139. db.SubmitChanges();
  140. Assert.AreEqual(db.OrderDetails.Count(), initialCount);
  141. }
  142. #if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
  143. [Explicit]
  144. #endif
  145. [Test]
  146. [ExpectedException(typeof(ChangeConflictException))]
  147. public void CP4_UnchangedColumnShouldNotUpdated()
  148. {
  149. Random rand = new Random();
  150. Northwind db = CreateDB();
  151. var orderDetail = new OrderDetail { OrderID = 1, ProductID=2};
  152. db.OrderDetails.Attach(orderDetail);
  153. float newDiscount = 15 + (float)rand.NextDouble();
  154. orderDetail.Discount = newDiscount;
  155. db.SubmitChanges();
  156. //this test is bad conceptually, for this reason last two lines has been commented and now a changeConflictException is expected.
  157. //This is the behaviour in linq2sl.
  158. //var orderDetail2 = db.OrderDetails.Single(od => od.OrderID == 1);
  159. //Assert.AreEqual((float)orderDetail2.Discount, newDiscount);
  160. }
  161. [Test(Description = "Check that both keys are used to determine identity")]
  162. public void CP5_Composite_ObjectIdentity()
  163. {
  164. Northwind db = CreateDB();
  165. var d = db.OrderDetails.First();
  166. var q = db.OrderDetails.Where(od => od.ProductID == d.ProductID && od.OrderID == d.OrderID);
  167. OrderDetail row1 = q.Single();
  168. Assert.IsTrue(object.ReferenceEquals(d, row1));
  169. }
  170. }
  171. }