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 list = new CursorList(); 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 result = new Cursor(ref list) .WithSort(item => item.Value.Name, direction) .GetNextPage(); if (direction == Aurora.Cursor.SortDirection.Desc) { Assert.Collection(result.Result, item => item.Name.Equals("asdf"), item => item.Name.Equals("bsdf"), item => item.Name.Equals("csdf")); } else { Assert.Collection(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 list = new CursorList(); // 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 cursor = new Cursor(ref list); if (size < 0) { Assert.Throws(() => { cursor.WithSize(size).GetNextPage(); }); } else if (size > numOfItems) { CursorResult res = cursor.WithSize(size).GetNextPage(); Assert.Equal(res.Result.Count, numOfItems); } else if (size < numOfItems && size > 0) { CursorResult 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 list = new CursorList(); // 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 cursor = new Cursor(ref list); List pagedResults = new List(); while (pageToken != string.Empty) { CursorResult 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 cursorList = new CursorList(); // 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 cursor = new Cursor(ref cursorList); List pagedResults = new List(); while (pageToken != string.Empty) { CursorResult 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]); } } } }