PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NHibernate.Test/Linq/PagingTests.cs

https://github.com/okb/nhibernate-core
C# | 427 lines | 342 code | 64 blank | 21 comment | 0 complexity | 74eec76ef7f3ad3d5cfeda55b5fa94af MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0, CC-BY-SA-3.0
  1. using System.Linq;
  2. using NHibernate.Cfg;
  3. using NUnit.Framework;
  4. namespace NHibernate.Test.Linq
  5. {
  6. public class ProductProjection
  7. {
  8. public int ProductId { get; set; }
  9. public string Name { get; set; }
  10. }
  11. [TestFixture]
  12. public class PagingTests : LinqTestCase
  13. {
  14. [Test]
  15. public void PageBetweenProjections()
  16. {
  17. // NH-3326
  18. var list = db.Products
  19. .Select(p => new { p.ProductId, p.Name })
  20. .Skip(5).Take(10)
  21. .Select(a => new { a.Name, a.ProductId })
  22. .ToList();
  23. Assert.That(list, Has.Count.EqualTo(10));
  24. }
  25. [Test]
  26. public void PageBetweenProjectionsReturningNestedAnonymous()
  27. {
  28. // The important part in this query is that the outer select
  29. // grabs the entire element from the inner select, plus more.
  30. // NH-3326
  31. var list = db.Products
  32. .Select(p => new { p.ProductId, p.Name })
  33. .Skip(5).Take(10)
  34. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  35. .ToList();
  36. Assert.That(list, Has.Count.EqualTo(10));
  37. }
  38. [Test]
  39. public void PageBetweenProjectionsReturningNestedClass()
  40. {
  41. // NH-3326
  42. var list = db.Products
  43. .Select(p => new ProductProjection { ProductId = p.ProductId, Name = p.Name })
  44. .Skip(5).Take(10)
  45. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  46. .ToList();
  47. Assert.That(list, Has.Count.EqualTo(10));
  48. }
  49. [Test]
  50. public void PageBetweenProjectionsReturningOrderedNestedAnonymous()
  51. {
  52. // Variation of NH-3326 with order
  53. var list = db.Products
  54. .Select(p => new { p.ProductId, p.Name })
  55. .OrderBy(x => x.ProductId)
  56. .Skip(5).Take(10)
  57. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  58. .ToList();
  59. Assert.That(list, Has.Count.EqualTo(10));
  60. }
  61. [Test]
  62. public void PageBetweenProjectionsReturningOrderedNestedClass()
  63. {
  64. // Variation of NH-3326 with order
  65. var list = db.Products
  66. .Select(p => new ProductProjection { ProductId = p.ProductId, Name = p.Name })
  67. .OrderBy(x => x.ProductId)
  68. .Skip(5).Take(10)
  69. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  70. .ToList();
  71. Assert.That(list, Has.Count.EqualTo(10));
  72. }
  73. [Test]
  74. public void PageBetweenProjectionsReturningOrderedConstrainedNestedAnonymous()
  75. {
  76. // Variation of NH-3326 with where
  77. var list = db.Products
  78. .Select(p => new { p.ProductId, p.Name })
  79. .Where(p => p.ProductId > 0)
  80. .OrderBy(x => x.ProductId)
  81. .Skip(5).Take(10)
  82. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  83. .ToList();
  84. Assert.That(list, Has.Count.EqualTo(10));
  85. }
  86. [Test]
  87. public void PageBetweenProjectionsReturningOrderedConstrainedNestedClass()
  88. {
  89. // Variation of NH-3326 with where
  90. var list = db.Products
  91. .Select(p => new ProductProjection { ProductId = p.ProductId, Name = p.Name })
  92. .Where(p => p.ProductId > 0)
  93. .OrderBy(x => x.ProductId)
  94. .Skip(5).Take(10)
  95. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  96. .ToList();
  97. Assert.That(list, Has.Count.EqualTo(10));
  98. }
  99. [Test, Ignore("Not supported")]
  100. public void PagedProductsWithOuterWhereClauseOrderedNestedAnonymous()
  101. {
  102. // NH-2588 and NH-3326
  103. var inMemoryIds = db.Products.ToList()
  104. .OrderByDescending(x => x.ProductId)
  105. .Select(p => new { p.ProductId, p.Name, p.UnitsInStock })
  106. .Skip(10).Take(20)
  107. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  108. .Where(x => x.ProductId > 0)
  109. .ToList();
  110. var ids = db.Products
  111. .OrderByDescending(x => x.ProductId)
  112. .Select(p => new { p.ProductId, p.Name, p.UnitsInStock })
  113. .Skip(10).Take(20)
  114. .Where(x => x.ProductId > 0)
  115. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  116. .ToList();
  117. Assert.That(ids, Is.EqualTo(inMemoryIds));
  118. }
  119. [Test, Ignore("Not supported")]
  120. public void PagedProductsWithOuterWhereClauseOrderedNestedAnonymousEquivalent()
  121. {
  122. // NH-2588 and NH-3326
  123. var inMemoryIds = db.Products.ToList()
  124. .OrderByDescending(x => x.ProductId)
  125. .Select(p => new { p.ProductId, p.Name, p.UnitsInStock })
  126. .Skip(10).Take(20)
  127. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  128. .Where(x => x.ProductId > 0)
  129. .ToList();
  130. var subquery = db.Products
  131. .OrderByDescending(x => x.ProductId)
  132. .Select(p => new { p.ProductId, p.Name, p.UnitsInStock })
  133. .Skip(10).Take(20);
  134. var ids = db.Products
  135. .Select(p => new { p.ProductId, p.Name, p.UnitsInStock })
  136. .Where(x => subquery.Contains(x))
  137. .Where(x => x.ProductId > 0)
  138. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  139. .ToList();
  140. Assert.That(ids, Is.EqualTo(inMemoryIds));
  141. }
  142. [Test, Ignore("Not supported")]
  143. public void PagedProductsWithOuterWhereClauseOrderedNestedClass()
  144. {
  145. // NH-2588 and NH-3326
  146. var inMemoryIds = db.Products.ToList()
  147. .OrderByDescending(x => x.ProductId)
  148. .Select(p => new ProductProjection { ProductId = p.ProductId, Name = p.Name })
  149. .Skip(10).Take(20)
  150. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  151. .Where(x => x.ProductId > 0)
  152. .ToList();
  153. var ids = db.Products
  154. .OrderByDescending(x => x.ProductId)
  155. .Select(p => new ProductProjection { ProductId = p.ProductId, Name = p.Name })
  156. .Skip(10).Take(20)
  157. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  158. .Where(x => x.ProductId > 0)
  159. .ToList();
  160. Assert.That(ids, Is.EqualTo(inMemoryIds));
  161. }
  162. [Test, Ignore("Not supported")]
  163. public void PagedProductsWithOuterWhereClauseOrderedNestedClassEquivalent()
  164. {
  165. // NH-2588 and NH-3326
  166. var inMemoryIds = db.Products.ToList()
  167. .OrderByDescending(x => x.ProductId)
  168. .Select(p => new ProductProjection { ProductId = p.ProductId, Name = p.Name })
  169. .Skip(10).Take(20)
  170. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  171. .Where(x => x.ProductId > 0)
  172. .ToList();
  173. var subquery = db.Products
  174. .OrderByDescending(x => x.ProductId)
  175. .Select(p => new ProductProjection { ProductId = p.ProductId, Name = p.Name })
  176. .Skip(10).Take(20);
  177. var ids = db.Products
  178. .Select(p => new ProductProjection { ProductId = p.ProductId, Name = p.Name })
  179. .Where(x => subquery.Contains(x))
  180. .Where(x => x.ProductId > 0)
  181. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  182. .ToList();
  183. Assert.That(ids, Is.EqualTo(inMemoryIds));
  184. }
  185. [Test]
  186. public void Customers1to5()
  187. {
  188. var q = (from c in db.Customers select c.CustomerId).Take(5);
  189. var query = q.ToList();
  190. Assert.AreEqual(5, query.Count);
  191. }
  192. [Test]
  193. public void Customers11to20()
  194. {
  195. var query = (from c in db.Customers
  196. orderby c.CustomerId
  197. select c.CustomerId).Skip(10).Take(10).ToList();
  198. Assert.AreEqual(query[0], "BSBEV");
  199. Assert.AreEqual(10, query.Count);
  200. }
  201. [Test]
  202. [Ignore("Multiple Takes (or Skips) not handled correctly")]
  203. public void CustomersChainedTake()
  204. {
  205. var q = (from c in db.Customers
  206. orderby c.CustomerId
  207. select c.CustomerId).Take(5).Take(6);
  208. var query = q.ToList();
  209. Assert.AreEqual(5, query.Count);
  210. Assert.AreEqual("ALFKI", query[0]);
  211. Assert.AreEqual("BLAUS", query[4]);
  212. }
  213. [Test]
  214. [Ignore("Multiple Takes (or Skips) not handled correctly")]
  215. public void CustomersChainedSkip()
  216. {
  217. var q = (from c in db.Customers select c.CustomerId).Skip(10).Skip(5);
  218. var query = q.ToList();
  219. Assert.AreEqual(query[0], "CONSH");
  220. Assert.AreEqual(76, query.Count);
  221. }
  222. [Test]
  223. [Ignore("Count with Skip or Take is incorrect (Skip / Take done on the query not the HQL, so get applied at the wrong point")]
  224. public void CountAfterTakeShouldReportTheCorrectNumber()
  225. {
  226. var users = db.Customers.Skip(3).Take(10);
  227. Assert.AreEqual(10, users.Count());
  228. }
  229. [Test]
  230. public void OrderedPagedProductsWithOuterProjection()
  231. {
  232. //NH-3108
  233. var inMemoryIds = db.Products.ToList()
  234. .OrderBy(p => p.ProductId)
  235. .Skip(10).Take(20)
  236. .Select(p => p.ProductId)
  237. .ToList();
  238. var ids = db.Products
  239. .OrderBy(p => p.ProductId)
  240. .Skip(10).Take(20)
  241. .Select(p => p.ProductId)
  242. .ToList();
  243. Assert.That(ids, Is.EqualTo(inMemoryIds));
  244. }
  245. [Test]
  246. public void OrderedPagedProductsWithInnerProjection()
  247. {
  248. //NH-3108 (not failing)
  249. var inMemoryIds = db.Products.ToList()
  250. .OrderBy(p => p.ProductId)
  251. .Select(p => p.ProductId)
  252. .Skip(10).Take(20)
  253. .ToList();
  254. var ids = db.Products
  255. .OrderBy(p => p.ProductId)
  256. .Select(p => p.ProductId)
  257. .Skip(10).Take(20)
  258. .ToList();
  259. Assert.That(ids, Is.EqualTo(inMemoryIds));
  260. }
  261. [Test]
  262. public void DescendingOrderedPagedProductsWithOuterProjection()
  263. {
  264. //NH-3108
  265. var inMemoryIds = db.Products.ToList()
  266. .OrderByDescending(p => p.ProductId)
  267. .Skip(10).Take(20)
  268. .Select(p => p.ProductId)
  269. .ToList();
  270. var ids = db.Products
  271. .OrderByDescending(p => p.ProductId)
  272. .Skip(10).Take(20)
  273. .Select(p => p.ProductId)
  274. .ToList();
  275. Assert.That(ids, Is.EqualTo(inMemoryIds));
  276. }
  277. [Test]
  278. public void DescendingOrderedPagedProductsWithInnerProjection()
  279. {
  280. //NH-3108 (not failing)
  281. var inMemoryIds = db.Products.ToList()
  282. .OrderByDescending(p => p.ProductId)
  283. .Select(p => p.ProductId)
  284. .Skip(10).Take(20)
  285. .ToList();
  286. var ids = db.Products
  287. .OrderByDescending(p => p.ProductId)
  288. .Select(p => p.ProductId)
  289. .Skip(10).Take(20)
  290. .ToList();
  291. Assert.That(ids, Is.EqualTo(inMemoryIds));
  292. }
  293. [Test]
  294. public void PagedProductsWithOuterWhereClause()
  295. {
  296. //NH-2588
  297. var inMemoryIds = db.Products.ToList()
  298. .OrderByDescending(x => x.ProductId)
  299. .Skip(10).Take(20)
  300. .Where(x => x.UnitsInStock > 0)
  301. .ToList();
  302. var ids = db.Products
  303. .OrderByDescending(x => x.ProductId)
  304. .Skip(10).Take(20)
  305. .Where(x => x.UnitsInStock > 0)
  306. .ToList();
  307. Assert.That(ids, Is.EqualTo(inMemoryIds));
  308. }
  309. [Test]
  310. public void PagedProductsWithOuterWhereClauseEquivalent()
  311. {
  312. //NH-2588
  313. var inMemoryIds = db.Products.ToList()
  314. .OrderByDescending(x => x.ProductId)
  315. .Skip(10).Take(20)
  316. .Where(x => x.UnitsInStock > 0)
  317. .ToList();
  318. var subquery = db.Products
  319. .OrderByDescending(x => x.ProductId)
  320. .Skip(10).Take(20);
  321. var ids = db.Products
  322. .Where(x => subquery.Contains(x))
  323. .Where(x => x.UnitsInStock > 0);
  324. Assert.That(ids, Is.EqualTo(inMemoryIds));
  325. }
  326. [Test]
  327. public void PagedProductsWithOuterWhereClauseAndProjection()
  328. {
  329. //NH-2588
  330. var inMemoryIds = db.Products.ToList()
  331. .OrderByDescending(x => x.ProductId)
  332. .Skip(10).Take(20)
  333. .Where(x => x.UnitsInStock > 0)
  334. .Select(x => x.ProductId)
  335. .ToList();
  336. var ids = db.Products
  337. .OrderByDescending(x => x.ProductId)
  338. .Skip(10).Take(20)
  339. .Where(x => x.UnitsInStock > 0)
  340. .Select(x => x.ProductId)
  341. .ToList();
  342. Assert.That(ids, Is.EqualTo(inMemoryIds));
  343. }
  344. [Test]
  345. public void PagedProductsWithOuterWhereClauseAndComplexProjection()
  346. {
  347. //NH-2588
  348. var inMemoryIds = db.Products.ToList()
  349. .OrderByDescending(x => x.ProductId)
  350. .Skip(10).Take(20)
  351. .Where(x => x.UnitsInStock > 0)
  352. .Select(x => new { x.ProductId })
  353. .ToList();
  354. var ids = db.Products
  355. .OrderByDescending(x => x.ProductId)
  356. .Skip(10).Take(20)
  357. .Where(x => x.UnitsInStock > 0)
  358. .Select(x => new { x.ProductId })
  359. .ToList();
  360. Assert.That(ids, Is.EqualTo(inMemoryIds));
  361. }
  362. }
  363. }