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

/src/NHibernate.Test/Linq/NestedSelectsTests.cs

https://github.com/ngbrown/nhibernate-core
C# | 465 lines | 402 code | 62 blank | 1 comment | 1 complexity | 6684d627099e33e9bb91f3ec1eb46b3c MD5 | raw file
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using NHibernate.DomainModel.Northwind.Entities;
  6. using NUnit.Framework;
  7. namespace NHibernate.Test.Linq
  8. {
  9. [TestFixture]
  10. public class NestedSelectsTests : LinqTestCase
  11. {
  12. [Test]
  13. public void OrdersIdWithOrderLinesId()
  14. {
  15. var orders = db.Orders
  16. .Select(o => new
  17. {
  18. o.OrderId,
  19. OrderLinesIds = o.OrderLines.Select(ol => ol.Id).ToArray()
  20. })
  21. .ToList();
  22. Assert.That(orders.Count, Is.EqualTo(830));
  23. }
  24. [Test]
  25. public void OrdersOrderLinesId()
  26. {
  27. var orders = db.Orders
  28. .Select(o => new
  29. {
  30. OrderLinesIds = o.OrderLines.Select(ol => ol.Id).ToArray()
  31. })
  32. .ToList();
  33. Assert.That(orders.Count, Is.EqualTo(830));
  34. }
  35. [Test]
  36. public void OrdersIdWithOrderLinesIdShouldBeNotLazy()
  37. {
  38. var orders = db.Orders
  39. .Select(o => new
  40. {
  41. o.OrderId,
  42. OrderLinesIds = o.OrderLines.Select(ol => ol.Id)
  43. })
  44. .ToList();
  45. Assert.That(orders.Count, Is.EqualTo(830));
  46. Assert.That(orders[0].OrderLinesIds, Is.InstanceOf<ReadOnlyCollection<long>>());
  47. }
  48. [Test]
  49. public void OrdersIdWithOrderLinesIdToArray()
  50. {
  51. var orders = db.Orders
  52. .Select(o => new
  53. {
  54. o.OrderId,
  55. OrderLinesIds = o.OrderLines.Select(ol => ol.Id).ToArray()
  56. })
  57. .ToArray();
  58. Assert.That(orders.Length, Is.EqualTo(830));
  59. }
  60. [Test]
  61. public void OrdersIdWithOrderLinesIdAndDiscount()
  62. {
  63. var orders = db.Orders
  64. .Select(o =>
  65. new
  66. {
  67. o.OrderId,
  68. OrderLines = o.OrderLines.Select(ol =>
  69. new
  70. {
  71. ol.Id,
  72. ol.Discount
  73. }).ToArray()
  74. })
  75. .ToList();
  76. Assert.That(orders.Count, Is.EqualTo(830));
  77. }
  78. [Test]
  79. public void OrdersIdAndDateWithOrderLinesIdAndDiscount()
  80. {
  81. var orders = db.Orders
  82. .Select(o =>
  83. new
  84. {
  85. o.OrderId,
  86. o.OrderDate,
  87. OrderLines = o.OrderLines.Select(ol =>
  88. new
  89. {
  90. ol.Id,
  91. ol.Discount
  92. }).ToArray()
  93. })
  94. .ToList();
  95. Assert.That(orders.Count, Is.EqualTo(830));
  96. }
  97. [Test]
  98. public void TimesheetIdAndUserLastLoginDates()
  99. {
  100. var timesheets = db.Timesheets
  101. .Select(o =>
  102. new
  103. {
  104. o.Id,
  105. Users = o.Users.Select(x => x.LastLoginDate).ToArray()
  106. })
  107. .ToList();
  108. Assert.That(timesheets.Count, Is.EqualTo(3));
  109. Assert.That(timesheets[0].Users, Is.Not.Empty);
  110. }
  111. [Test]
  112. public void TimesheetIdAndUserLastLoginDatesAndEntriesIds()
  113. {
  114. var timesheets = db.Timesheets
  115. .Select(o =>
  116. new
  117. {
  118. o.Id,
  119. LastLoginDates = o.Users.Select(u => u.LastLoginDate).ToArray(),
  120. EntriesIds = o.Entries.Select(e => e.Id).ToArray()
  121. })
  122. .ToList();
  123. Assert.That(timesheets.Count, Is.EqualTo(3));
  124. Assert.That(timesheets[0].LastLoginDates, Is.Not.Empty);
  125. }
  126. [Test(Description = "NH-2986")]
  127. public void TimesheetIdAndUsersTransparentProjection()
  128. {
  129. var timesheets = db.Timesheets
  130. .Select(o =>
  131. new
  132. {
  133. o.Id,
  134. Users = o.Users.Select(x => x)
  135. })
  136. .ToList();
  137. Assert.That(timesheets.Count, Is.EqualTo(3));
  138. Assert.That(timesheets[0].Users, Is.Not.Empty);
  139. }
  140. [Test(Description = "NH-2986")]
  141. public void TimesheetAndUsersTransparentProjection()
  142. {
  143. var timesheets = db.Timesheets
  144. .Select(o =>
  145. new
  146. {
  147. o,
  148. Users = o.Users.Select(x => x)
  149. })
  150. .ToList();
  151. Assert.That(timesheets.Count, Is.EqualTo(3));
  152. Assert.That(timesheets[0].Users, Is.Not.Empty);
  153. }
  154. [Test(Description = "NH-2986")]
  155. public void TimesheetUsersTransparentProjection()
  156. {
  157. var timesheets = db.Timesheets
  158. .Select(o =>
  159. new
  160. {
  161. Users = o.Users.Select(x => x)
  162. })
  163. .ToList();
  164. Assert.That(timesheets.Count, Is.EqualTo(3));
  165. Assert.That(timesheets[0].Users, Is.Not.Empty);
  166. }
  167. [Test(Description = "NH-2986")]
  168. public void TimesheetIdAndUsersAndEntriesTransparentProjection()
  169. {
  170. var timesheets = db.Timesheets
  171. .Select(o =>
  172. new
  173. {
  174. o.Id,
  175. Users = o.Users.Select(x => x),
  176. Entries = o.Entries.Select(x => x)
  177. })
  178. .ToList();
  179. Assert.That(timesheets.Count, Is.EqualTo(3));
  180. Assert.That(timesheets[0].Users, Is.Not.Empty);
  181. }
  182. [Test(Description = "NH-2986")]
  183. public void TimesheetAndUsersAndEntriesTransparentProjection()
  184. {
  185. var timesheets = db.Timesheets
  186. .Select(o =>
  187. new
  188. {
  189. o,
  190. Users = o.Users.Select(x => x),
  191. Entries = o.Entries.Select(x => x)
  192. })
  193. .ToList();
  194. Assert.That(timesheets.Count, Is.EqualTo(3));
  195. Assert.That(timesheets[0].Users, Is.Not.Empty);
  196. }
  197. [Test(Description = "NH-2986")]
  198. public void TimesheetUsersAndEntriesTransparentProjection()
  199. {
  200. var timesheets = db.Timesheets
  201. .Select(o =>
  202. new
  203. {
  204. Users = o.Users.Select(x => x),
  205. Entries = o.Entries.Select(x => x)
  206. })
  207. .ToList();
  208. Assert.That(timesheets.Count, Is.EqualTo(3));
  209. Assert.That(timesheets[0].Users, Is.Not.Empty);
  210. }
  211. [Test(Description = "NH-3333")]
  212. public void TimesheetIdAndUsers()
  213. {
  214. var timesheets = db.Timesheets
  215. .Select(o =>
  216. new
  217. {
  218. o.Id,
  219. o.Users
  220. })
  221. .ToList();
  222. Assert.That(timesheets.Count, Is.EqualTo(3));
  223. Assert.That(timesheets[0].Users, Is.Not.Empty);
  224. }
  225. [Test(Description = "NH-3333")]
  226. public void TimesheetAndUsers()
  227. {
  228. var timesheets = db.Timesheets
  229. .Select(o =>
  230. new
  231. {
  232. o,
  233. o.Users
  234. })
  235. .ToList();
  236. Assert.That(timesheets.Count, Is.EqualTo(3));
  237. Assert.That(timesheets[0].Users, Is.Not.Empty);
  238. }
  239. [Test(Description = "NH-3333")]
  240. public void TimesheetUsers()
  241. {
  242. var timesheets = db.Timesheets
  243. .Select(o =>
  244. new
  245. {
  246. o.Users
  247. })
  248. .ToList();
  249. Assert.That(timesheets.Count, Is.EqualTo(3));
  250. Assert.That(timesheets[0].Users, Is.Not.Empty);
  251. }
  252. [Test(Description = "NH-3333")]
  253. public void TimesheetIdAndUsersAndEntries()
  254. {
  255. var timesheets = db.Timesheets
  256. .Select(o =>
  257. new
  258. {
  259. o.Id,
  260. o.Users,
  261. o.Entries
  262. })
  263. .ToList();
  264. Assert.That(timesheets.Count, Is.EqualTo(3));
  265. Assert.That(timesheets[0].Users, Is.Not.Empty);
  266. }
  267. [Test(Description = "NH-3333")]
  268. public void TimesheetAndUsersAndEntries()
  269. {
  270. var timesheets = db.Timesheets
  271. .Select(o =>
  272. new
  273. {
  274. o,
  275. o.Users,
  276. o.Entries
  277. })
  278. .ToList();
  279. Assert.That(timesheets.Count, Is.EqualTo(3));
  280. Assert.That(timesheets[0].Users, Is.Not.Empty);
  281. }
  282. [Test(Description = "NH-3333")]
  283. public void TimesheetUsersAndEntries()
  284. {
  285. var timesheets = db.Timesheets
  286. .Select(o =>
  287. new
  288. {
  289. o.Users,
  290. o.Entries
  291. })
  292. .ToList();
  293. Assert.That(timesheets.Count, Is.EqualTo(3));
  294. Assert.That(timesheets[0].Users, Is.Not.Empty);
  295. }
  296. [Test]
  297. public void EmployeesIdAndWithSubordinatesId()
  298. {
  299. var emplyees = db.Employees
  300. .Select(o =>
  301. new
  302. {
  303. o.EmployeeId,
  304. SubordinatesIds = o.Subordinates.Select(so => so.EmployeeId).ToArray()
  305. })
  306. .ToList();
  307. Assert.That(emplyees.Count, Is.EqualTo(9));
  308. }
  309. [Test]
  310. public void OrdersIdWithOrderLinesNestedWhereId()
  311. {
  312. var orders = db.Orders
  313. .Select(o => new
  314. {
  315. o.OrderId,
  316. OrderLinesIds = o.OrderLines
  317. .Where(ol => ol.Discount > 1)
  318. .Select(ol => ol.Id)
  319. .ToArray()
  320. })
  321. .ToList();
  322. Assert.That(orders.Count, Is.EqualTo(830));
  323. Assert.That(orders[0].OrderLinesIds, Is.Empty);
  324. }
  325. [Test]
  326. public void NoNestedSelects_AnyOnGroupBySubquery()
  327. {
  328. var subQuery = from vms in db.Animals
  329. group vms by vms.Father
  330. into vmReqs
  331. select vmReqs.Max(x => x.Id);
  332. var outerQuery = from vm in db.Animals
  333. where subQuery.Any(x => vm.Id == x)
  334. select vm;
  335. var animals = outerQuery.ToList();
  336. Assert.That(animals.Count, Is.EqualTo(2));
  337. }
  338. //NH-3155
  339. [Test]
  340. public void NoNestedSelects_ContainsOnGroupBySubquery()
  341. {
  342. var subQuery = from vms in db.Animals
  343. where vms.BodyWeight > 0
  344. group vms by vms.Father
  345. into vmReqs
  346. select vmReqs.Max(x => x.Id);
  347. var outerQuery = from vm in db.Animals
  348. where subQuery.Contains(vm.Id)
  349. select vm;
  350. var animals = outerQuery.ToList();
  351. Assert.That(animals.Count, Is.EqualTo(2));
  352. }
  353. [Test]
  354. public void CanSelectWithWhereSubQuery()
  355. {
  356. var query = from timesheet in db.Timesheets
  357. select new
  358. {
  359. timesheet.Id,
  360. Entries = timesheet.Entries.Where(e => e.NumberOfHours >= 0).ToList()
  361. };
  362. var list = query.ToList();
  363. Assert.AreEqual(3, list.Count);
  364. }
  365. [Test(Description = "GH-2540")]
  366. public void CanSelectWithAsQueryableAndWhereSubQuery()
  367. {
  368. var query = from timesheet in db.Timesheets
  369. select new
  370. {
  371. timesheet.Id,
  372. Entries = timesheet.Entries.AsQueryable().Where(e => e.NumberOfHours >= 0).ToList()
  373. };
  374. var list = query.ToList();
  375. Assert.AreEqual(3, list.Count);
  376. }
  377. [Test(Description = "GH-2540")]
  378. public void CanSelectWithAsQueryableAndWhereSubQueryToArray()
  379. {
  380. var query = from timesheet in db.Timesheets
  381. select new
  382. {
  383. timesheet.Id,
  384. Entries = timesheet.Entries.AsQueryable().Where(e => e.NumberOfHours >= 0).ToArray()
  385. };
  386. var list = query.ToList();
  387. Assert.AreEqual(3, list.Count);
  388. }
  389. [Test(Description = "GH-2540")]
  390. public void CanSelectWithAsQueryableAndWhereSubQueryWithExternalPredicate()
  391. {
  392. Expression<Func<TimesheetEntry, bool>> predicate = e => e.NumberOfHours >= 0;
  393. var query = from timesheet in db.Timesheets
  394. select new
  395. {
  396. timesheet.Id,
  397. Entries = timesheet.Entries.AsQueryable().Where(predicate).ToList()
  398. };
  399. var list = query.ToList();
  400. Assert.AreEqual(3, list.Count);
  401. }
  402. }
  403. }