This repository has been archived on 2021-03-06. You can view files and clone it, but cannot push or open issues or pull requests.
aurora-cradle-sharp/AuroraCradle.test/CursorList.test.cs
2021-03-05 19:09:42 -05:00

164 lines
5.5 KiB
C#

using Xunit;
using Aurora.Services.Signal;
using Aurora.Cursor;
using System.Collections.Generic;
using System.Linq;
namespace AuroraCradle.test
{
public class CursorListTest
{
[Theory()]
[InlineData(Aurora.Cursor.SortDirection.Asc)]
[InlineData(Aurora.Cursor.SortDirection.Desc)]
public void CursorListSortOnStringValue(Aurora.Cursor.SortDirection direction)
{
CursorList<Party> list = new CursorList<Party>();
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)
.GetNextPage();
if (direction == Aurora.Cursor.SortDirection.Desc)
{
Assert.Collection<Party>(result.Result,
item => item.Name.Equals("asdf"),
item => item.Name.Equals("bsdf"),
item => item.Name.Equals("csdf"));
}
else
{
Assert.Collection<Party>(result.Result,
item => item.Name.Equals("csdf"),
item => item.Name.Equals("bsdf"),
item => item.Name.Equals("asdf"));
}
}
[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);
}
}
[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, Aurora.Cursor.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]);
}
}
}
}