Skip to content

Commit

Permalink
Write pagination tests
Browse files Browse the repository at this point in the history
  • Loading branch information
turecross321 committed Sep 22, 2024
1 parent d1b8fc2 commit 2e13bcc
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 3 deletions.
160 changes: 160 additions & 0 deletions SoundShapesServer.Tests/Tests/PaginationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using SoundShapesServer.Extensions;
using SoundShapesServer.Tests.Server;
using SoundShapesServer.Types;
using SoundShapesServer.Types.Database;

namespace SoundShapesServer.Tests.Tests;

public class PaginationTests : ServerTest
{
[Test]
public void TakeWorks()
{
using SSTestContext context = this.GetServer();
const int itemsToCreate = 150;

for (int i = 0; i < itemsToCreate; i++)
{
context.CreateUser();
}

IQueryable<DbUser> users = context.Database.GetUsers();

PaginatedDbList<DbUser, Guid> paginated = users.PaginateWithGuidId(new PageData
{
Skip = 0,
Take = itemsToCreate,
MinimumCreationDate = null,
MaximumCreationDate = null,
ExcludeIds = []
});

// Limit works
Assert.That(paginated.Items.Count(), Is.EqualTo(PaginatedDbList<DbUser, Guid>.MaxItems));

paginated = users.PaginateWithGuidId(new PageData
{
Skip = 0,
Take = 1,
MinimumCreationDate = null,
MaximumCreationDate = null,
ExcludeIds = []
});

Assert.Multiple(() =>
{
// It takes the right amount of items
Assert.That(paginated.Items.Count(), Is.EqualTo(1));
// TotalItems works correctly
Assert.That(paginated.TotalItems, Is.EqualTo(itemsToCreate));
});
}

[Test]
public void SkipWorks()
{
using SSTestContext context = this.GetServer();

DbUser firstUser = context.CreateUser();
const int itemsToCreate = 10;

for (int i = 0; i < itemsToCreate; i++)
{
context.CreateUser();
}

IQueryable<DbUser> users = context.Database.GetUsers();

PaginatedDbList<DbUser, Guid> paginated = users.PaginateWithGuidId(new PageData
{
Skip = 1,
Take = itemsToCreate,
MinimumCreationDate = null,
MaximumCreationDate = null,
ExcludeIds = []
});

Assert.That(paginated.Items.First().Id, Is.Not.EqualTo(firstUser.Id));
}

[Test]
public void ExcludeIdsWorks()
{
using SSTestContext context = this.GetServer();

DbUser firstUser = context.CreateUser();
const int itemsToCreate = 10;

for (int i = 0; i < itemsToCreate; i++)
{
context.CreateUser();
}

IQueryable<DbUser> users = context.Database.GetUsers();

PaginatedDbList<DbUser, Guid> paginated = users.PaginateWithGuidId(new PageData
{
Skip = 0,
Take = itemsToCreate,
MinimumCreationDate = null,
MaximumCreationDate = null,
ExcludeIds = [firstUser.Id.ToString()]
});

Assert.That(paginated.Items.First().Id, Is.Not.EqualTo(firstUser.Id));
}

[Test]
public void DateFilterWorks()
{
using SSTestContext context = this.GetServer();

const int oldItemAmount = 5;
const int newItemAmount = 10;

DateTime oldDate = context.Time.Now;
for (int i = 0; i < oldItemAmount; i++)
{
context.CreateUser();
}

context.Time.Now = context.Time.Now.AddYears(1);
DateTime newDate = context.Time.Now;
for (int i = 0; i < newItemAmount; i++)
{
context.CreateUser();
}

IQueryable<DbUser> users = context.Database.GetUsers();

PaginatedDbList<DbUser, Guid> paginated = users.PaginateWithGuidId(new PageData
{
Skip = 0,
Take = 100,
MinimumCreationDate = newDate,
MaximumCreationDate = null,
ExcludeIds = []
});

Assert.That(paginated.TotalItems, Is.EqualTo(newItemAmount));
foreach (DbUser user in paginated.Items)
{
Assert.That(user.CreationDate, Is.EqualTo(newDate));
}

paginated = users.PaginateWithGuidId(new PageData
{
Skip = 0,
Take = 100,
MinimumCreationDate = null,
MaximumCreationDate = oldDate,
ExcludeIds = []
});

Assert.That(paginated.TotalItems, Is.EqualTo(oldItemAmount));
foreach (DbUser user in paginated.Items)
{
Assert.That(user.CreationDate, Is.EqualTo(oldDate));
}
}
}
5 changes: 5 additions & 0 deletions SoundShapesServer/Database/GameDatabaseContext.Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@ public DbUser SetUserAuthorizationSettings(DbUser user, ApiAuthorizationSettings
SaveChanges();
return user;
}

public IQueryable<DbUser> GetUsers()
{
return Users;
}
}
6 changes: 3 additions & 3 deletions SoundShapesServer/Types/PaginatedDbList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public static PaginatedDbList<TDb, TDbId> FromQueryable(IQueryable<TDb> items, P
items = items.Where(i => i.CreationDate >= pageData.MinimumCreationDate);

if (pageData.MaximumCreationDate != null)
items = items.Where(i => i.CreationDate < pageData.MaximumCreationDate);
items = items.Where(i => i.CreationDate <= pageData.MaximumCreationDate);

// Filter out items that have excluded ids
items = pageData.ExcludeIds.Select(id => ParseId<TDbId>(id))
items = pageData.ExcludeIds.Select(ParseId<TDbId>)
.Aggregate(items, (current, parsedId) =>
current.Where(i => !i.Id!.Equals((TDbId)parsedId)));

Expand Down Expand Up @@ -48,7 +48,7 @@ private static object ParseId<TId>(string id)
throw new ArgumentOutOfRangeException(nameof(id), "Tried to parse unsupported id!");
}

private const int MaxItems = 100;
public const int MaxItems = 100;

public required IEnumerable<TDb> Items { get; init; }
public required int TotalItems { get; init; }
Expand Down

0 comments on commit 2e13bcc

Please sign in to comment.