Adding cursor size tests

This commit is contained in:
Brandon Watson 2021-03-05 14:30:25 -05:00
parent b61ffde4c9
commit 24fd683cb7
3 changed files with 57 additions and 15 deletions

View File

@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Faker.Net" Version="1.4.108" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

View File

@ -2,6 +2,7 @@ using Xunit;
using Aurora;
using Aurora.Services.Signal;
using Aurora.Cursor;
using Faker;
namespace AuroraSignal.test
@ -14,9 +15,9 @@ namespace AuroraSignal.test
public void CursorListSortOnStringValue(SortDirection direction)
{
CursorList<Party> list = new CursorList<Party>();
list.Add(new Party() { Name = "asdf", Id = "1111" });
list.Add(new Party() { Name = "bsdf", Id = "2222" });
list.Add(new Party() { Name = "csdf", Id = "3333" });
list.Add(new Party() { Name = "asdf", Id = Faker.RandomNumber.Next().ToString() });
list.Add(new Party() { Name = "bsdf", Id = Faker.RandomNumber.Next().ToString() });
list.Add(new Party() { Name = "csdf", Id = Faker.RandomNumber.Next().ToString() });
CursorResult<Party> result = new Cursor<Party>(ref list)
.WithSort(item => item.Value.Name, direction)
@ -38,5 +39,47 @@ namespace AuroraSignal.test
}
}
[Theory()]
[InlineData(2)]
[InlineData(10)]
[InlineData(49)]
[InlineData(51)]
[InlineData(-1)]
public void CursorListSizeTest(int size)
{
int numOfItems = 50;
CursorList<Party> list = new CursorList<Party>();
// Add items to cursor list
for (int i = 0; i < numOfItems; i++)
{
list.Add(new Party()
{
Name = string.Join(" ",
Faker.Lorem.Words(2)),
Id = Faker.RandomNumber.Next().ToString()
});
}
Cursor<Party> cursor = new Cursor<Party>(ref list);
if (size < 0)
{
Assert.Throws<System.InvalidOperationException>(() =>
{
cursor.WithSize(size).GetNextPage();
});
}
else if (size > numOfItems)
{
CursorResult<Party> res = cursor.WithSize(size).GetNextPage();
Assert.Equal(res.Result.Count, numOfItems);
}
else if (size < numOfItems && size > 0)
{
CursorResult<Party> res = cursor.WithSize(size).GetNextPage();
Assert.Equal(res.Result.Count, size);
}
}
}
}

View File

@ -34,6 +34,10 @@ namespace Aurora.Cursor
public CursorResult<T> GetNextPage()
{
if (this._pageSize < 0)
{
throw new InvalidOperationException("Page Size must be greater than zero");
}
List<KeyValuePair<string, T>> tmpList;
// Sort reference list
@ -61,20 +65,14 @@ namespace Aurora.Cursor
int endIdx = startIdx + this._pageSize;
List<T> selection = new List<T>();
// Get page
for (int i = startIdx; i < tmpList.Count && i < endIdx; i++)
{
selection.Add(tmpList.ElementAt(i).Value);
}
List<KeyValuePair<string, T>> selection = tmpList.GetRange(startIdx, this._pageSize > tmpList.Count ? tmpList.Count : this._pageSize);
return new CursorResult<T>
{
NextPageToken = selection[selection.Count - 1].Id,
PrevPageToken = selection[0].Id,
NextPageToken = selection[selection.Count - 1].Key,
PrevPageToken = selection[0].Key,
Count = this._list.Count,
Result = selection
Result = selection.ConvertAll(item => item.Value)
};
}
@ -83,13 +81,13 @@ namespace Aurora.Cursor
throw new NotImplementedException();
}
public Cursor<T> WithNextPage(string nextPageToken)
public Cursor<T> WithNextPageToken(string nextPageToken)
{
this._nextPageToken = nextPageToken;
return this;
}
public Cursor<T> WithPreviousPage(string prevPageToken)
public Cursor<T> WithPreviousPageToken(string prevPageToken)
{
this._previousPageToken = prevPageToken;
return this;