|
| 1 | +using System.Linq.Expressions; |
1 | 2 | using GreenDonut.Data.TestContext;
|
2 | 3 | using Microsoft.EntityFrameworkCore;
|
3 | 4 | using Microsoft.Extensions.DependencyInjection;
|
@@ -178,6 +179,45 @@ await CreateSnapshot()
|
178 | 179 | .MatchMarkdownAsync();
|
179 | 180 | }
|
180 | 181 |
|
| 182 | + [Fact] |
| 183 | + public async Task Paging_WithChildCollectionProjectionExpression_First_5() |
| 184 | + { |
| 185 | + // Arrange |
| 186 | + var connectionString = CreateConnectionString(); |
| 187 | + await SeedAsync(connectionString); |
| 188 | + using var capture = new CapturePagingQueryInterceptor(); |
| 189 | + |
| 190 | + // Act |
| 191 | + await using var context = new CatalogContext(connectionString); |
| 192 | + |
| 193 | + var pagingArgs = new PagingArguments |
| 194 | + { |
| 195 | + First = 5 |
| 196 | + }; |
| 197 | + |
| 198 | + var result = await context.Brands |
| 199 | + .Select(BrandWithProductsDto.Projection) |
| 200 | + .OrderBy(t => t.Name) |
| 201 | + .ThenBy(t => t.Id) |
| 202 | + .ToPageAsync(pagingArgs); |
| 203 | + |
| 204 | + // Assert |
| 205 | + await CreateSnapshot() |
| 206 | + .AddQueries(capture.Queries) |
| 207 | + .Add( |
| 208 | + new |
| 209 | + { |
| 210 | + result.HasNextPage, |
| 211 | + result.HasPreviousPage, |
| 212 | + First = result.First?.Id, |
| 213 | + FirstCursor = result.First is not null ? result.CreateCursor(result.First) : null, |
| 214 | + Last = result.Last?.Id, |
| 215 | + LastCursor = result.Last is not null ? result.CreateCursor(result.Last) : null |
| 216 | + }) |
| 217 | + .Add(result.Items) |
| 218 | + .MatchMarkdownAsync(); |
| 219 | + } |
| 220 | + |
181 | 221 | [Fact]
|
182 | 222 | public async Task BatchPaging_First_5()
|
183 | 223 | {
|
@@ -351,6 +391,37 @@ public BrandDto(int id, string name)
|
351 | 391 | public string Name { get; }
|
352 | 392 | }
|
353 | 393 |
|
| 394 | + public class BrandWithProductsDto |
| 395 | + { |
| 396 | + public required int Id { get; init; } |
| 397 | + |
| 398 | + public required string Name { get; init; } |
| 399 | + |
| 400 | + public required IReadOnlyCollection<ProductDto> Products { get; init; } |
| 401 | + |
| 402 | + public static Expression<Func<Brand, BrandWithProductsDto>> Projection |
| 403 | + => brand => new BrandWithProductsDto |
| 404 | + { |
| 405 | + Id = brand.Id, |
| 406 | + Name = brand.Name, |
| 407 | + Products = brand.Products.AsQueryable().Select(ProductDto.Projection).ToList() |
| 408 | + }; |
| 409 | + } |
| 410 | + |
| 411 | + public class ProductDto |
| 412 | + { |
| 413 | + public required int Id { get; init; } |
| 414 | + |
| 415 | + public required string Name { get; init; } |
| 416 | + |
| 417 | + public static Expression<Func<Product, ProductDto>> Projection |
| 418 | + => product => new ProductDto |
| 419 | + { |
| 420 | + Id = product.Id, |
| 421 | + Name = product.Name |
| 422 | + }; |
| 423 | + } |
| 424 | + |
354 | 425 | public class ProductsByBrandDataLoader : StatefulBatchDataLoader<int, Page<Product>>
|
355 | 426 | {
|
356 | 427 | private readonly IServiceProvider _services;
|
|
0 commit comments