diff --git a/AuroraSignal.test/CursorList.test.cs b/AuroraSignal.test/CursorList.test.cs index 9a51457..2faef15 100644 --- a/AuroraSignal.test/CursorList.test.cs +++ b/AuroraSignal.test/CursorList.test.cs @@ -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 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, 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]); + } + } } } diff --git a/AuroraSignal/Src/Cursor/Cursor.cs b/AuroraSignal/Src/Cursor/Cursor.cs index 2b93df9..91db9ea 100644 --- a/AuroraSignal/Src/Cursor/Cursor.cs +++ b/AuroraSignal/Src/Cursor/Cursor.cs @@ -28,7 +28,7 @@ namespace Aurora.Cursor this._pageSize = 10; this._sortDelgate = delegate (KeyValuePair 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> selection = tmpList.GetRange(startIdx, this._pageSize > tmpList.Count ? tmpList.Count : this._pageSize); + + List> selection = new List>(); + if (adjustedSize != 0) + { + selection = tmpList.GetRange(startIdx, adjustedSize); + } return new CursorResult { - 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) };