Adding pagination tests

This commit is contained in:
Brandon Watson 2021-03-05 16:13:25 -05:00
parent 24fd683cb7
commit 4a4cef8dd7
2 changed files with 98 additions and 7 deletions

View File

@ -1,8 +1,11 @@
using Xunit;
using Aurora;
using Aurora.Services.Signal;
using Aurora.Cursor;
using Faker;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AuroraSignal.test
@ -81,5 +84,84 @@ namespace AuroraSignal.test
Assert.Equal(res.Result.Count, size);
}
}
[Theory()]
[InlineData(2)]
[InlineData(10)]
[InlineData(3)]
public void CursorPaginationTest(int pageSize)
{
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()
});
}
string pageToken = null;
Cursor<Party> cursor = new Cursor<Party>(ref list);
List<Party> pagedResults = new List<Party>();
while (pageToken != string.Empty)
{
CursorResult<Party> res = cursor
.WithSize(pageSize)
.WithNextPageToken(pageToken)
.GetNextPage();
pagedResults.AddRange(res.Result);
pageToken = res.NextPageToken;
}
Assert.Equal(pagedResults.Count, numOfItems);
}
[Fact()]
public void CursorPaginationWithSortTest()
{
int numOfItems = 50;
CursorList<Party> cursorList = new CursorList<Party>();
// Add items to cursor list
for (int i = 0; i < numOfItems; i++)
{
cursorList.Add(new Party()
{
Name = string.Join(" ",
Faker.Lorem.Words(2)),
Id = Faker.RandomNumber.Next().ToString()
});
}
var orderedList = cursorList.ToList().OrderBy(item => item.Value.Name).ToList().ConvertAll(item => item.Value);
string pageToken = null;
Cursor<Party> cursor = new Cursor<Party>(ref cursorList);
List<Party> pagedResults = new List<Party>();
while (pageToken != string.Empty)
{
CursorResult<Party> res = cursor
.WithSize(10)
.WithSort(item => item.Value.Name, SortDirection.Asc)
.WithNextPageToken(pageToken)
.GetNextPage();
pagedResults.AddRange(res.Result);
pageToken = res.NextPageToken;
}
var list = cursorList.ToList();
for (int i = 0; i < orderedList.Count; i++)
{
Assert.Equal(orderedList[i], pagedResults[i]);
}
}
}
}

View File

@ -28,7 +28,7 @@ namespace Aurora.Cursor
this._pageSize = 10;
this._sortDelgate = delegate (KeyValuePair<string, T> item)
{
return item.Value.Id;
return item.Key;
};
}
@ -60,17 +60,26 @@ namespace Aurora.Cursor
if (!string.IsNullOrEmpty(this._nextPageToken))
{
// TODO find another way to index into the list that's not a regular array search
startIdx = tmpList.FindIndex(item => item.Key == this._nextPageToken);
startIdx = tmpList.FindIndex(item => item.Key == this._nextPageToken) + 1;
}
int endIdx = startIdx + this._pageSize;
int adjustedSize = this._pageSize;
if (startIdx + this._pageSize > tmpList.Count)
{
adjustedSize = this._pageSize - ((startIdx + _pageSize) - tmpList.Count);
}
List<KeyValuePair<string, T>> selection = tmpList.GetRange(startIdx, this._pageSize > tmpList.Count ? tmpList.Count : this._pageSize);
List<KeyValuePair<string, T>> selection = new List<KeyValuePair<string, T>>();
if (adjustedSize != 0)
{
selection = tmpList.GetRange(startIdx, adjustedSize);
}
return new CursorResult<T>
{
NextPageToken = selection[selection.Count - 1].Key,
PrevPageToken = selection[0].Key,
NextPageToken = this._pageSize == selection.Count ? selection[selection.Count - 1].Key : string.Empty,
PrevPageToken = string.Empty,
Count = this._list.Count,
Result = selection.ConvertAll(item => item.Value)
};