diff --git a/AuroraSignal.test/AuroraSignal.test.csproj b/AuroraSignal.test/AuroraSignal.test.csproj index e343af3..2f7facf 100644 --- a/AuroraSignal.test/AuroraSignal.test.csproj +++ b/AuroraSignal.test/AuroraSignal.test.csproj @@ -7,6 +7,7 @@ + diff --git a/AuroraSignal.test/CursorList.test.cs b/AuroraSignal.test/CursorList.test.cs index 7780280..9a51457 100644 --- a/AuroraSignal.test/CursorList.test.cs +++ b/AuroraSignal.test/CursorList.test.cs @@ -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 list = new CursorList(); - 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 result = new Cursor(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 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); + } + } } } diff --git a/AuroraSignal/Src/Cursor/Cursor.cs b/AuroraSignal/Src/Cursor/Cursor.cs index b740951..2b93df9 100644 --- a/AuroraSignal/Src/Cursor/Cursor.cs +++ b/AuroraSignal/Src/Cursor/Cursor.cs @@ -34,6 +34,10 @@ namespace Aurora.Cursor public CursorResult GetNextPage() { + if (this._pageSize < 0) + { + throw new InvalidOperationException("Page Size must be greater than zero"); + } List> tmpList; // Sort reference list @@ -61,20 +65,14 @@ namespace Aurora.Cursor int endIdx = startIdx + this._pageSize; - List selection = new List(); - - // Get page - for (int i = startIdx; i < tmpList.Count && i < endIdx; i++) - { - selection.Add(tmpList.ElementAt(i).Value); - } + List> selection = tmpList.GetRange(startIdx, this._pageSize > tmpList.Count ? tmpList.Count : this._pageSize); return new CursorResult { - 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 WithNextPage(string nextPageToken) + public Cursor WithNextPageToken(string nextPageToken) { this._nextPageToken = nextPageToken; return this; } - public Cursor WithPreviousPage(string prevPageToken) + public Cursor WithPreviousPageToken(string prevPageToken) { this._previousPageToken = prevPageToken; return this;