PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Data.Linq/src/DbLinq/Test/Providers/ReadTests_Join.cs

https://github.com/ekovalenko-softheme/mono
C# | 336 lines | 256 code | 41 blank | 39 comment | 28 complexity | 9f53ce86da75f9ab3bd53c7215f37ae4 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense
  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.Globalization;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. using System.Linq;
  31. using System.Linq.Expressions;
  32. using NUnit.Framework;
  33. using Test_NUnit;
  34. using nwind;
  35. #if MONO_STRICT
  36. using System.Data.Linq;
  37. #else
  38. using DbLinq.Data.Linq;
  39. #endif
  40. // test ns
  41. #if MYSQL
  42. namespace Test_NUnit_MySql
  43. #elif ORACLE && ODP
  44. namespace Test_NUnit_OracleODP
  45. #elif ORACLE
  46. namespace Test_NUnit_Oracle
  47. #elif POSTGRES
  48. namespace Test_NUnit_PostgreSql
  49. #elif SQLITE
  50. namespace Test_NUnit_Sqlite
  51. #elif INGRES
  52. namespace Test_NUnit_Ingres
  53. #elif MSSQL && L2SQL
  54. namespace Test_NUnit_MsSql_Strict
  55. #elif MSSQL
  56. namespace Test_NUnit_MsSql
  57. #elif FIREBIRD
  58. namespace Test_NUnit_Firebird
  59. #endif
  60. {
  61. [TestFixture]
  62. public class ReadTests_Join : TestBase
  63. {
  64. #if !DEBUG && (SQLITE || (MSSQL && !L2SQL))
  65. [Explicit]
  66. #endif
  67. [Test(Description = "example by Frans Brouma: select all customers that have no orders")]
  68. public void LeftJoin_DefaultIfEmpty()
  69. {
  70. //example by Frans Brouma on Matt Warren's site
  71. //select all customers that have no orders
  72. //http://blogs.msdn.com/mattwar/archive/2007/09/04/linq-building-an-iqueryable-provider-part-vii.aspx
  73. //http://weblogs.asp.net/fbouma/archive/2007/11/23/developing-linq-to-llblgen-pro-part-9.aspx
  74. Northwind db = CreateDB();
  75. var q = from c in db.Customers
  76. join o in db.Orders on c.CustomerID equals o.CustomerID into oc
  77. from x in oc.DefaultIfEmpty()
  78. where x.OrderID == null
  79. select c;
  80. var list = q.ToList();
  81. Assert.IsTrue(list.Count > 0);
  82. int countPARIS = list.Count(item => item.CustomerID == "PARIS");
  83. Assert.IsTrue(countPARIS == 1);
  84. }
  85. [Test]
  86. public void LeftOuterJoin_Suppliers()
  87. {
  88. //http://blogs.class-a.nl/blogs/anko/archive/2008/03/14/linq-to-sql-outer-joins.aspx
  89. //example by Anko Duizer (NL)
  90. Northwind db = CreateDB();
  91. var query = from s in db.Suppliers
  92. join c in db.Customers on s.City equals c.City into temp
  93. from t in temp.DefaultIfEmpty()
  94. select new
  95. {
  96. SupplierName = s.CompanyName,
  97. CustomerName = t.CompanyName,
  98. City = s.City
  99. };
  100. var list = query.ToList();
  101. bool foundMelb = false, foundNull = false;
  102. foreach (var item in list)
  103. {
  104. foundMelb = foundMelb || item.City == "Melbourne";
  105. foundNull = foundNull || item.City == null;
  106. }
  107. Assert.IsTrue(foundMelb, "Expected rows with City=Melbourne");
  108. Assert.IsFalse(foundNull, "Expected no rows with City=null");
  109. }
  110. // picrap: commented out, it doesn't build because of db.Orderdetails (again, a shared source file...)
  111. [Test(Description = "Problem discovered by Laurent")]
  112. public void Join_Laurent()
  113. {
  114. Northwind db = CreateDB();
  115. var q1 = (from p in db.Products
  116. join o in db.OrderDetails on p.ProductID equals o.ProductID
  117. where p.ProductID > 1
  118. select new
  119. {
  120. p.ProductName,
  121. o.OrderID,
  122. o.ProductID,
  123. }
  124. ).ToList();
  125. Assert.IsTrue(q1.Count > 0);
  126. }
  127. #if !DEBUG && (SQLITE || POSTGRES || MSSQL)
  128. // L2SQL: System.InvalidOperationException : The type 'Test_NUnit_MsSql_Strict.ReadTests_Join+Northwind1+ExtendedOrder' is not mapped as a Table.
  129. [Explicit]
  130. #endif
  131. [Test]
  132. public void RetrieveParentAssociationProperty()
  133. {
  134. Northwind dbo = CreateDB();
  135. Northwind1 db = new Northwind1(dbo.Connection);
  136. var t = db.GetTable<Northwind1.ExtendedOrder>();
  137. var q = from order in t
  138. select new
  139. {
  140. order.OrderID,
  141. order.CustomerShipCity.ContactName
  142. };
  143. var list = q.ToList();
  144. Assert.IsTrue(list.Count > 0);
  145. }
  146. #if !DEBUG && (SQLITE || POSTGRES || MSSQL)
  147. // L2SQL: System.InvalidOperationException : The type 'Test_NUnit_MsSql_Strict.ReadTests_Join+Northwind1+ExtendedOrder' is not mapped as a Table.
  148. [Explicit]
  149. #endif
  150. [Test]
  151. public void DifferentParentAndAssociationPropertyNames()
  152. {
  153. Northwind dbo = CreateDB();
  154. Northwind1 db = new Northwind1(dbo.Connection);
  155. var query = db.GetTable<Northwind1.ExtendedOrder>() as IQueryable<Northwind1.ExtendedOrder>;
  156. var q2 = query.Select(e => new Northwind1.ExtendedOrder
  157. {
  158. OrderID = e.OrderID,
  159. ShipAddress = e.CustomerShipCity.ContactName
  160. });
  161. var list = q2.ToList();
  162. Assert.IsTrue(list.Count > 0);
  163. }
  164. #if !DEBUG && (SQLITE || POSTGRES || MSSQL)
  165. // L2SQL: System.InvalidOperationException : The type 'Test_NUnit_MsSql_Strict.ReadTests_Join+Northwind1+ExtendedOrder' is not mapped as a Table.
  166. [Explicit]
  167. #endif
  168. [Test]
  169. public void SelectCustomerContactNameFromOrder()
  170. {
  171. Northwind dbo = CreateDB();
  172. Northwind1 db = new Northwind1(dbo.Connection);
  173. var t = db.GetTable<Northwind1.ExtendedOrder>();
  174. var q = from order in t
  175. select new
  176. {
  177. order.CustomerContactName
  178. };
  179. var list = q.ToList();
  180. Assert.AreEqual(db.Orders.Count(), list.Count());
  181. foreach (var s in list)
  182. Assert.AreEqual("Test", s);
  183. }
  184. public class Northwind1 : Northwind
  185. {
  186. public Northwind1(System.Data.IDbConnection connection)
  187. : base(connection) { }
  188. // Linq-SQL requires this: [System.Data.Linq.Mapping.Table(Name = "orders")]
  189. public class ExtendedOrder : Order
  190. {
  191. #if MONO_STRICT
  192. System.Data.Linq
  193. #else
  194. DbLinq.Data.Linq
  195. #endif
  196. .EntityRef<Customer> _x_Customer;
  197. [System.Data.Linq.Mapping.Association(Storage = "_x_Customer",
  198. ThisKey = "ShipCity", Name =
  199. #if MYSQL
  200. "orders_ibfk_1"
  201. #elif ORACLE
  202. "SYS_C004742"
  203. #elif POSTGRES
  204. "fk_order_customer"
  205. #elif SQLITE
  206. "fk_Orders_1"
  207. #elif INGRES
  208. "fk_order_customer"
  209. #elif MSSQL
  210. "fk_order_customer"
  211. #elif FIREBIRD
  212. "??" // TODO: correct FK name
  213. #else
  214. #error unknown target
  215. #endif
  216. )]
  217. public Customer CustomerShipCity
  218. {
  219. get { return _x_Customer.Entity; }
  220. set { _x_Customer.Entity = value; }
  221. }
  222. public string CustomerContactName
  223. {
  224. get
  225. {
  226. return "Test";
  227. }
  228. }
  229. }
  230. public Table<ExtendedOrder> ExtendedOrders
  231. {
  232. get { return base.GetTable<ExtendedOrder>(); }
  233. }
  234. }
  235. [Test]
  236. [ExpectedException(typeof(NotSupportedException))]
  237. public void WhereBeforeSelect()
  238. {
  239. Northwind db = CreateDB();
  240. var t = db.GetTable<Order>();
  241. var query = t.Where(o => o.OrderID != 0);
  242. query = query.Select(dok => new Order
  243. {
  244. OrderID = dok.OrderID,
  245. OrderDate = dok.OrderDate,
  246. ShipCity = dok.Customer.ContactName,
  247. Freight = dok.Freight
  248. });
  249. var list = query.ToList();
  250. }
  251. /// <summary>
  252. /// Reported by pwy.mail in http://code.google.com/p/dblinq2007/issues/detail?id=66
  253. /// </summary>
  254. [Test]
  255. public void OrdersLazyLoad()
  256. {
  257. Northwind db = CreateDB();
  258. var q =
  259. from c in db.Customers
  260. select c;
  261. foreach (var c in q)
  262. {
  263. Console.WriteLine(c.Address);
  264. foreach (var o in c.Orders)
  265. Console.WriteLine(o.OrderID);
  266. }
  267. }
  268. [Test]
  269. public void JoinWhere()
  270. {
  271. Northwind db = CreateDB();
  272. var custID = "BT___";
  273. var custOderInfos = from o in db.Orders
  274. join em in db.Employees on o.EmployeeID equals em.EmployeeID
  275. where o.CustomerID == custID
  276. select new { o, em };
  277. var l = custOderInfos.ToList();
  278. }
  279. #if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
  280. [Explicit]
  281. #endif
  282. [Test]
  283. // submitted by bryan costanich
  284. public void ImplicitLeftOuterJoin()
  285. {
  286. var db = CreateDB();
  287. var dbItems =
  288. (from a in db.Products
  289. from b in db.Suppliers
  290. where a.SupplierID == b.SupplierID
  291. select a);
  292. var list = dbItems.ToList();
  293. }
  294. }
  295. }