Skip to content

Commit

Permalink
Merge pull request #96 from wemogy/fix/pagination
Browse files Browse the repository at this point in the history
Fixed Pagination support for InMemory database
  • Loading branch information
SebastianKuesters authored Jun 6, 2023
2 parents b7b8baa + 9245351 commit f30808b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,50 @@ public async Task QueryAsync_ShouldRespectTakeCount()
// Assert
queriedUser.Should().HaveCount(queryParameters.Take.Value);
}

[Fact]
public async Task QueryAsync_LambdaShouldRespectTakeCount()
{
// Arrange
var paginationParameters = new PaginationParameters(
0,
5);
await ResetAsync();
var users = User.Faker.Generate(10);
foreach (var user in users)
{
await MicrosoftUserRepository.CreateAsync(user);
}

// Act
var queriedUser = await MicrosoftUserRepository.QueryAsync(
x => true,
paginationParameters);

// Assert
queriedUser.Should().HaveCount(paginationParameters.Take);
}

[Fact]
public async Task QueryAsync_LambdaShouldRespectSkipCount()
{
// Arrange
var paginationParameters = new PaginationParameters(
2,
10);
await ResetAsync();
var users = User.Faker.Generate(10);
foreach (var user in users)
{
await MicrosoftUserRepository.CreateAsync(user);
}

// Act
var queriedUser = await MicrosoftUserRepository.QueryAsync(
x => true,
paginationParameters);

// Assert
queriedUser.Should().HaveCount(users.Count - paginationParameters.Skip);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public async Task<List<TEntity>> QueryAsync(

await IterateAsync(
predicate,
paginationParameters,
entities.Add,
cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,27 @@ public async Task IterateAsync(
CancellationToken cancellationToken)
{
var compiledPredicate = predicate.CompileFast();
var skipped = 0;
var taken = 0;

foreach (var entityPartition in EntityPartitions)
{
var queryable = entityPartition.Value.Where(compiledPredicate);
if (paginationParameters != null)
{
queryable = queryable.Skip(paginationParameters.Skip).Take(paginationParameters.Take);
}

foreach (var entity in queryable)
var entities = queryable.ToList();
foreach (var entity in entities)
{
if (paginationParameters != null && paginationParameters.Skip > skipped++)
{
continue;
}

await callback(entity.Clone());

if (paginationParameters != null && paginationParameters.Take <= ++taken)
{
return;
}
}
}
}
Expand Down

0 comments on commit f30808b

Please sign in to comment.