5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
27 using System.Collections.Generic;
\r
30 using NUnit.Framework;
\r
32 using System.Data.Linq;
\r
38 namespace Test_NUnit_MySql
\r
40 namespace Test_NUnit_OracleODP
\r
42 namespace Test_NUnit_Oracle
\r
44 namespace Test_NUnit_PostgreSql
\r
46 namespace Test_NUnit_Sqlite
\r
48 namespace Test_NUnit_Ingres
\r
49 #elif MSSQL && MONO_STRICT
\r
50 namespace Test_NUnit_MsSql_Strict
\r
52 namespace Test_NUnit_MsSql
\r
54 namespace Test_NUnit_Firebird
\r
58 public class CompositePK_Test : TestBase
\r
60 protected void cleanup(Northwind db)
\r
64 // Get the name of the Order Details table properly evaluating the Annotation
\r
65 string tableName = null;// db.Vendor.GetSqlFieldSafeName("order details"); //eg. "[Order Details]"
\r
66 foreach (object obj in typeof(OrderDetail).GetCustomAttributes(true))
\r
68 if (obj is System.Data.Linq.Mapping.TableAttribute)
\r
70 tableName = ((System.Data.Linq.Mapping.TableAttribute)obj).Name;
\r
73 string sql = string.Format("DELETE FROM {0} WHERE OrderID=3 AND ProductID=2", tableName);
\r
74 db.ExecuteCommand(sql);
\r
82 public void CP1_DeletePreviousRows()
\r
84 //delete any rows from previous testing
\r
85 Northwind db = CreateDB();
\r
86 // PC: this test was wrong, DeleteOnSubmit requires the object to be attached
\r
87 // (by query result or manually, we chose here the query result)
\r
88 //var orderDetail = new OrderDetail { OrderID = 3, ProductID = 2 };
\r
89 //db.OrderDetails.DeleteOnSubmit(orderDetail);
\r
90 var toDelete = from o in db.OrderDetails where o.OrderID == 3 && o.ProductID == 2 select o;
\r
91 db.OrderDetails.DeleteAllOnSubmit(toDelete);
\r
96 public void CP2_UpdateTableWithCompositePK()
\r
98 Northwind db = CreateDB();
\r
101 var orderDetail = new OrderDetail
\r
108 db.OrderDetails.InsertOnSubmit(orderDetail);
\r
109 db.SubmitChanges();
\r
111 orderDetail.UnitPrice = 40;
\r
112 db.SubmitChanges();
\r
114 OrderDetail orderDetail2 = (from c in db.OrderDetails
\r
115 where c.UnitPrice == 40
\r
116 select c).Single();
\r
118 Assert.IsTrue(object.ReferenceEquals(orderDetail, orderDetail2), "Must be same object");
\r
120 Assert.AreEqual(3, orderDetail2.OrderID);
\r
121 Assert.AreEqual(2, orderDetail2.ProductID);
\r
122 Assert.AreEqual(40, orderDetail2.UnitPrice);
\r
124 db.OrderDetails.DeleteOnSubmit(orderDetail);
\r
125 db.SubmitChanges();
\r
130 public void CP3_DeleteTableWithCompositePK()
\r
132 Northwind db = CreateDB();
\r
134 int initialCount = db.OrderDetails.Count();
\r
136 var orderDetail = new OrderDetail { OrderID = 2, ProductID = 2 };
\r
137 db.OrderDetails.InsertOnSubmit(orderDetail);
\r
138 db.SubmitChanges();
\r
140 Assert.AreEqual(db.OrderDetails.Count(), initialCount + 1);
\r
141 db.OrderDetails.DeleteOnSubmit(orderDetail);
\r
142 db.SubmitChanges();
\r
144 Assert.AreEqual(db.OrderDetails.Count(), initialCount);
\r
148 [ExpectedException(typeof(ChangeConflictException))]
\r
149 public void CP4_UnchangedColumnShouldNotUpdated()
\r
151 Random rand = new Random();
\r
153 Northwind db = CreateDB();
\r
154 var orderDetail = new OrderDetail { OrderID = 1, ProductID=2};
\r
155 db.OrderDetails.Attach(orderDetail);
\r
157 float newDiscount = 15 + (float)rand.NextDouble();
\r
158 orderDetail.Discount = newDiscount;
\r
159 db.SubmitChanges();
\r
161 //this test is bad conceptually, for this reason last two lines has been commented and now a changeConflictException is expected.
\r
162 //This is the behaviour in linq2sl.
\r
164 //var orderDetail2 = db.OrderDetails.Single(od => od.OrderID == 1);
\r
165 //Assert.AreEqual((float)orderDetail2.Discount, newDiscount);
\r
168 [Test(Description = "Check that both keys are used to determine identity")]
\r
169 public void CP5_Composite_ObjectIdentity()
\r
171 Northwind db = CreateDB();
\r
172 var q = db.OrderDetails.Where(od => od.ProductID == 2 && od.OrderID == 1);
\r
173 OrderDetail row1 = q.Single();
\r
174 OrderDetail row2 = q.Single();
\r
175 Assert.IsTrue(object.ReferenceEquals(row1, row2));
\r