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

https://github.com/RogerKratz/nhibernate-core · C# · 387 lines · 299 code · 61 blank · 27 comment · 6 complexity · a7ba237d39423c7fd2a7a461e998482f MD5 · raw file

  1. //------------------------------------------------------------------------------
  2. // <auto-generated>
  3. // This code was generated by AsyncGenerator.
  4. //
  5. // Changes to this file may cause incorrect behavior and will be lost if
  6. // the code is regenerated.
  7. // </auto-generated>
  8. //------------------------------------------------------------------------------
  9. using System.Linq;
  10. using NHibernate.Cfg;
  11. using NHibernate.Dialect;
  12. using NUnit.Framework;
  13. using NHibernate.Linq;
  14. namespace NHibernate.Test.Linq
  15. {
  16. using System.Threading.Tasks;
  17. [TestFixture]
  18. public class PagingTestsAsync : LinqTestCase
  19. {
  20. [Test]
  21. public async Task PageBetweenProjectionsAsync()
  22. {
  23. // NH-3326
  24. var list = await (db.Products
  25. .Select(p => new { p.ProductId, p.Name })
  26. .Skip(5).Take(10)
  27. .Select(a => new { a.Name, a.ProductId })
  28. .ToListAsync());
  29. Assert.That(list, Has.Count.EqualTo(10));
  30. }
  31. [Test]
  32. public async Task PageBetweenProjectionsReturningNestedAnonymousAsync()
  33. {
  34. // The important part in this query is that the outer select
  35. // grabs the entire element from the inner select, plus more.
  36. // NH-3326
  37. var list = await (db.Products
  38. .Select(p => new { p.ProductId, p.Name })
  39. .Skip(5).Take(10)
  40. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  41. .ToListAsync());
  42. Assert.That(list, Has.Count.EqualTo(10));
  43. }
  44. [Test]
  45. public async Task PageBetweenProjectionsReturningNestedClassAsync()
  46. {
  47. // NH-3326
  48. var list = await (db.Products
  49. .Select(p => new ProductProjection { ProductId = p.ProductId, Name = p.Name })
  50. .Skip(5).Take(10)
  51. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  52. .ToListAsync());
  53. Assert.That(list, Has.Count.EqualTo(10));
  54. }
  55. [Test]
  56. public async Task PageBetweenProjectionsReturningOrderedNestedAnonymousAsync()
  57. {
  58. // Variation of NH-3326 with order
  59. var list = await (db.Products
  60. .Select(p => new { p.ProductId, p.Name })
  61. .OrderBy(x => x.ProductId)
  62. .Skip(5).Take(10)
  63. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  64. .ToListAsync());
  65. Assert.That(list, Has.Count.EqualTo(10));
  66. }
  67. [Test]
  68. public async Task PageBetweenProjectionsReturningOrderedNestedClassAsync()
  69. {
  70. // Variation of NH-3326 with order
  71. var list = await (db.Products
  72. .Select(p => new ProductProjection { ProductId = p.ProductId, Name = p.Name })
  73. .OrderBy(x => x.ProductId)
  74. .Skip(5).Take(10)
  75. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  76. .ToListAsync());
  77. Assert.That(list, Has.Count.EqualTo(10));
  78. }
  79. [Test]
  80. public async Task PageBetweenProjectionsReturningOrderedConstrainedNestedAnonymousAsync()
  81. {
  82. // Variation of NH-3326 with where
  83. var list = await (db.Products
  84. .Select(p => new { p.ProductId, p.Name })
  85. .Where(p => p.ProductId > 0)
  86. .OrderBy(x => x.ProductId)
  87. .Skip(5).Take(10)
  88. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  89. .ToListAsync());
  90. Assert.That(list, Has.Count.EqualTo(10));
  91. }
  92. [Test]
  93. public async Task PageBetweenProjectionsReturningOrderedConstrainedNestedClassAsync()
  94. {
  95. // Variation of NH-3326 with where
  96. var list = await (db.Products
  97. .Select(p => new ProductProjection { ProductId = p.ProductId, Name = p.Name })
  98. .Where(p => p.ProductId > 0)
  99. .OrderBy(x => x.ProductId)
  100. .Skip(5).Take(10)
  101. .Select(a => new { ExpandedElement = a, a.Name, a.ProductId })
  102. .ToListAsync());
  103. Assert.That(list, Has.Count.EqualTo(10));
  104. }
  105. [Test]
  106. public async Task Customers1to5Async()
  107. {
  108. var q = (from c in db.Customers select c.CustomerId).Take(5);
  109. var query = await (q.ToListAsync());
  110. Assert.AreEqual(5, query.Count);
  111. }
  112. [Test]
  113. public async Task Customers11to20Async()
  114. {
  115. var query = await ((from c in db.Customers
  116. orderby c.CustomerId
  117. select c.CustomerId).Skip(10).Take(10).ToListAsync());
  118. Assert.AreEqual(query[0], "BSBEV");
  119. Assert.AreEqual(10, query.Count);
  120. }
  121. [Test]
  122. public async Task Customers11to20And21to30ShouldNoCacheQueryAsync()
  123. {
  124. var query = await ((from c in db.Customers
  125. orderby c.CustomerId
  126. select c.CustomerId).Skip(10).Take(10).ToListAsync());
  127. Assert.AreEqual(query[0], "BSBEV");
  128. Assert.AreEqual(10, query.Count);
  129. query = await ((from c in db.Customers
  130. orderby c.CustomerId
  131. select c.CustomerId).Skip(20).Take(10).ToListAsync());
  132. Assert.AreNotEqual(query[0], "BSBEV");
  133. Assert.AreEqual(10, query.Count);
  134. query = await ((from c in db.Customers
  135. orderby c.CustomerId
  136. select c.CustomerId).Skip(10).Take(20).ToListAsync());
  137. Assert.AreEqual(query[0], "BSBEV");
  138. Assert.AreEqual(20, query.Count);
  139. }
  140. [Test]
  141. public async Task OrderedPagedProductsWithOuterProjectionAsync()
  142. {
  143. //NH-3108
  144. var inMemoryIds = (await (db.Products.ToListAsync()))
  145. .OrderBy(p => p.ProductId)
  146. .Skip(10).Take(20)
  147. .Select(p => p.ProductId)
  148. .ToList();
  149. var ids = await (db.Products
  150. .OrderBy(p => p.ProductId)
  151. .Skip(10).Take(20)
  152. .Select(p => p.ProductId)
  153. .ToListAsync());
  154. Assert.That(ids, Is.EqualTo(inMemoryIds));
  155. }
  156. [Test]
  157. public async Task OrderedPagedProductsWithInnerProjectionAsync()
  158. {
  159. //NH-3108 (not failing)
  160. var inMemoryIds = (await (db.Products.ToListAsync()))
  161. .OrderBy(p => p.ProductId)
  162. .Select(p => p.ProductId)
  163. .Skip(10).Take(20)
  164. .ToList();
  165. var ids = await (db.Products
  166. .OrderBy(p => p.ProductId)
  167. .Select(p => p.ProductId)
  168. .Skip(10).Take(20)
  169. .ToListAsync());
  170. Assert.That(ids, Is.EqualTo(inMemoryIds));
  171. }
  172. [Test]
  173. public async Task DescendingOrderedPagedProductsWithOuterProjectionAsync()
  174. {
  175. //NH-3108
  176. var inMemoryIds = (await (db.Products.ToListAsync()))
  177. .OrderByDescending(p => p.ProductId)
  178. .Skip(10).Take(20)
  179. .Select(p => p.ProductId)
  180. .ToList();
  181. var ids = await (db.Products
  182. .OrderByDescending(p => p.ProductId)
  183. .Skip(10).Take(20)
  184. .Select(p => p.ProductId)
  185. .ToListAsync());
  186. Assert.That(ids, Is.EqualTo(inMemoryIds));
  187. }
  188. [Test]
  189. public async Task DescendingOrderedPagedProductsWithInnerProjectionAsync()
  190. {
  191. //NH-3108 (not failing)
  192. var inMemoryIds = (await (db.Products.ToListAsync()))
  193. .OrderByDescending(p => p.ProductId)
  194. .Select(p => p.ProductId)
  195. .Skip(10).Take(20)
  196. .ToList();
  197. var ids = await (db.Products
  198. .OrderByDescending(p => p.ProductId)
  199. .Select(p => p.ProductId)
  200. .Skip(10).Take(20)
  201. .ToListAsync());
  202. Assert.That(ids, Is.EqualTo(inMemoryIds));
  203. }
  204. [Test]
  205. public async Task PagedProductsWithOuterWhereClauseAsync()
  206. {
  207. if (Dialect is MySQLDialect)
  208. Assert.Ignore("MySQL does not support LIMIT in subqueries.");
  209. //NH-2588
  210. var inMemoryIds = (await (db.Products.ToListAsync()))
  211. .OrderByDescending(x => x.ProductId)
  212. .Skip(10).Take(20)
  213. .Where(x => x.UnitsInStock > 0)
  214. .ToList();
  215. var ids = await (db.Products
  216. .OrderByDescending(x => x.ProductId)
  217. .Skip(10).Take(20)
  218. .Where(x => x.UnitsInStock > 0)
  219. .ToListAsync());
  220. Assert.That(ids, Is.EqualTo(inMemoryIds));
  221. }
  222. [Test]
  223. public async Task PagedProductsWithOuterWhereClauseResortAsync()
  224. {
  225. if (Dialect is MySQLDialect)
  226. Assert.Ignore("MySQL does not support LIMIT in subqueries.");
  227. //NH-2588
  228. var inMemoryIds = (await (db.Products.ToListAsync()))
  229. .OrderByDescending(x => x.ProductId)
  230. .Skip(10).Take(20)
  231. .Where(x => x.UnitsInStock > 0)
  232. .OrderBy(x => x.Name)
  233. .ToList();
  234. var ids = await (db.Products
  235. .OrderByDescending(x => x.ProductId)
  236. .Skip(10).Take(20)
  237. .Where(x => x.UnitsInStock > 0)
  238. .OrderBy(x => x.Name)
  239. .ToListAsync());
  240. Assert.That(ids, Is.EqualTo(inMemoryIds));
  241. }
  242. [Test]
  243. public async Task PagedProductsWithInnerAndOuterWhereClausesAsync()
  244. {
  245. if (Dialect is MySQLDialect)
  246. Assert.Ignore("MySQL does not support LIMIT in subqueries.");
  247. //NH-2588
  248. var inMemoryIds = (await (db.Products.ToListAsync()))
  249. .Where(x => x.UnitsInStock < 100)
  250. .OrderByDescending(x => x.ProductId)
  251. .Skip(10).Take(20)
  252. .Where(x => x.UnitsInStock > 0)
  253. .OrderBy(x => x.Name)
  254. .ToList();
  255. var ids = await (db.Products
  256. .Where(x => x.UnitsInStock < 100)
  257. .OrderByDescending(x => x.ProductId)
  258. .Skip(10).Take(20)
  259. .Where(x => x.UnitsInStock > 0)
  260. .OrderBy(x => x.Name)
  261. .ToListAsync());
  262. Assert.That(ids, Is.EqualTo(inMemoryIds));
  263. }
  264. [Test]
  265. public async Task PagedProductsWithOuterWhereClauseEquivalentAsync()
  266. {
  267. if (Dialect is MySQLDialect)
  268. Assert.Ignore("MySQL does not support LIMIT in subqueries.");
  269. //NH-2588
  270. var inMemoryIds = (await (db.Products.ToListAsync()))
  271. .OrderByDescending(x => x.ProductId)
  272. .Skip(10).Take(20)
  273. .Where(x => x.UnitsInStock > 0)
  274. .ToList();
  275. var subquery = db.Products
  276. .OrderByDescending(x => x.ProductId)
  277. .Skip(10).Take(20);
  278. var ids = await (db.Products
  279. .Where(x => subquery.Contains(x))
  280. .Where(x => x.UnitsInStock > 0)
  281. .OrderByDescending(x => x.ProductId)
  282. .ToListAsync());
  283. Assert.That(ids, Is.EqualTo(inMemoryIds));
  284. }
  285. [Test]
  286. public async Task PagedProductsWithOuterWhereClauseAndProjectionAsync()
  287. {
  288. if (Dialect is MySQLDialect)
  289. Assert.Ignore("MySQL does not support LIMIT in subqueries.");
  290. //NH-2588
  291. var inMemoryIds = (await (db.Products.ToListAsync()))
  292. .OrderByDescending(x => x.ProductId)
  293. .Skip(10).Take(20)
  294. .Where(x => x.UnitsInStock > 0)
  295. .Select(x => x.ProductId)
  296. .ToList();
  297. var ids = await (db.Products
  298. .OrderByDescending(x => x.ProductId)
  299. .Skip(10).Take(20)
  300. .Where(x => x.UnitsInStock > 0)
  301. .Select(x => x.ProductId)
  302. .ToListAsync());
  303. Assert.That(ids, Is.EqualTo(inMemoryIds));
  304. }
  305. [Test]
  306. public async Task PagedProductsWithOuterWhereClauseAndComplexProjectionAsync()
  307. {
  308. if (Dialect is MySQLDialect)
  309. Assert.Ignore("MySQL does not support LIMIT in subqueries.");
  310. //NH-2588
  311. var inMemoryIds = (await (db.Products.ToListAsync()))
  312. .OrderByDescending(x => x.ProductId)
  313. .Skip(10).Take(20)
  314. .Where(x => x.UnitsInStock > 0)
  315. .Select(x => new { x.ProductId })
  316. .ToList();
  317. var ids = await (db.Products
  318. .OrderByDescending(x => x.ProductId)
  319. .Skip(10).Take(20)
  320. .Where(x => x.UnitsInStock > 0)
  321. .Select(x => new { x.ProductId })
  322. .ToListAsync());
  323. Assert.That(ids, Is.EqualTo(inMemoryIds));
  324. }
  325. }
  326. }