mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-16 02:02:55 -07:00
Added unit tests for the new pagination helper, got it working now
This commit is contained in:
parent
227ec0a6f8
commit
68ea440dbb
4 changed files with 138 additions and 10797 deletions
|
@ -1,17 +1,14 @@
|
|||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Ombi.Helpers.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class PaginationHelperTests
|
||||
{
|
||||
[TestCaseSource(nameof(TestData))]
|
||||
[Ignore("https://stackoverflow.com/questions/55710966/working-out-how-many-items-to-take-in-a-paginated-list")]
|
||||
public void TestPaginationPagesToLoad(int currentlyLoaded, int toLoad, int maxItemsPerPage, int[] expectedPages)
|
||||
[TestCaseSource(nameof(TestPageData))]
|
||||
public void TestPaginationPages(int currentlyLoaded, int toLoad, int maxItemsPerPage, int[] expectedPages)
|
||||
{
|
||||
var result = PaginationHelper.GetNextPages(currentlyLoaded, toLoad, maxItemsPerPage);
|
||||
var pages = result.Select(x => x.Page).ToArray();
|
||||
|
@ -21,21 +18,92 @@ namespace Ombi.Helpers.Tests
|
|||
{
|
||||
Assert.That(pages[i], Is.EqualTo(expectedPages[i]));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static IEnumerable<TestCaseData> TestData
|
||||
public static IEnumerable<TestCaseData> TestPageData
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new TestCaseData(0, 10, 20, new [] { 1 }).SetName("Load_First_Page");
|
||||
yield return new TestCaseData(20, 10, 20, new [] { 2 }).SetName("Load_Second_Page");
|
||||
yield return new TestCaseData(0, 20, 20, new [] { 2 }).SetName("Load_Full_First_Page_Should_Get_NextPage");
|
||||
yield return new TestCaseData(20, 20, 20, new [] { 3 }).SetName("Load_Full_Second_Page_Should_Get_Next_Page");
|
||||
yield return new TestCaseData(10, 20, 20, new [] { 1, 2 }).SetName("Load_Half_First_Page_And_Half_Second_Page");
|
||||
yield return new TestCaseData(19, 20, 20, new [] { 1, 2 }).SetName("Load_End_First_Page_And_Most_Second_Page");
|
||||
yield return new TestCaseData(0, 10, 20, new [] { 1 }).SetName("Pagination_Load_First_Page");
|
||||
yield return new TestCaseData(20, 10, 20, new [] { 2 }).SetName("Pagination_Load_Second_Page");
|
||||
yield return new TestCaseData(0, 20, 20, new [] { 1 }).SetName("Pagination_Load_Full_First_Page");
|
||||
yield return new TestCaseData(20, 20, 20, new [] { 2 }).SetName("Pagination_Load_Full_Second_Page");
|
||||
yield return new TestCaseData(10, 20, 20, new [] { 1, 2 }).SetName("Pagination_Load_Half_First_Page_And_Half_Second_Page");
|
||||
yield return new TestCaseData(19, 20, 20, new[] { 1, 2 }).SetName("Pagination_Load_End_First_Page_And_Most_Second_Page");
|
||||
yield return new TestCaseData(19, 40, 20, new[] { 1, 2, 3 }).SetName("Pagination_Load_End_First_Page_And_Most_Second_And_Third_Page");
|
||||
yield return new TestCaseData(10, 10, 20, new[] { 1 }).SetName("Pagination_Load_Half_First_Page");
|
||||
yield return new TestCaseData(10, 9, 20, new[] { 1 }).SetName("Pagination_Load_LessThan_Half_First_Page");
|
||||
yield return new TestCaseData(20, 10, 20, new[] { 2 }).SetName("Pagination_Load_Half_Second_Page");
|
||||
yield return new TestCaseData(20, 9, 20, new[] { 2 }).SetName("Pagination_Load_LessThan_Half_Second_Page");
|
||||
yield return new TestCaseData(30, 10, 20, new[] { 2 }).SetName("Pagination_Load_All_Second_Page_With_Half_Take");
|
||||
yield return new TestCaseData(49, 1, 50, new[] { 1 }).SetName("Pagination_Load_49_OutOf_50");
|
||||
}
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(CurrentPositionTestData))]
|
||||
public void TestCurrentPositionOfPagination(int currentlyLoaded, int toLoad, int maxItemsPerPage, int expectedTake, int expectedSkip)
|
||||
{
|
||||
var result = PaginationHelper.GetNextPages(currentlyLoaded, toLoad, maxItemsPerPage);
|
||||
|
||||
var first = result.FirstOrDefault();
|
||||
Assert.That(first.Take, Is.EqualTo(expectedTake));
|
||||
Assert.That(first.Skip, Is.EqualTo(expectedSkip));
|
||||
}
|
||||
public static IEnumerable<TestCaseData> CurrentPositionTestData
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new TestCaseData(0, 10, 20, 10, 0).SetName("PaginationPosition_Load_First_Half_Of_Page");
|
||||
yield return new TestCaseData(10, 10, 20, 10, 10).SetName("PaginationPosition_Load_EndHalf_First_Page");
|
||||
yield return new TestCaseData(19, 1, 20, 1, 19).SetName("PaginationPosition_Load_LastItem_Of_First_Page");
|
||||
yield return new TestCaseData(20, 20, 20, 20, 20).SetName("PaginationPosition_Load_Full_Second_Page");
|
||||
}
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(CurrentPositionMultiplePagesTestData))]
|
||||
public void TestCurrentPositionOfPaginationWithMultiplePages(int currentlyLoaded, int toLoad, int maxItemsPerPage, List<MultiplePagesTestData> data)
|
||||
{
|
||||
var result = PaginationHelper.GetNextPages(currentlyLoaded, toLoad, maxItemsPerPage);
|
||||
|
||||
foreach (var r in result)
|
||||
{
|
||||
// get result data for this page
|
||||
var expectedPage = data.FirstOrDefault(x => x.Page == r.Page);
|
||||
Assert.That(r.Take, Is.EqualTo(expectedPage.ExpectedTake));
|
||||
Assert.That(r.Skip, Is.EqualTo(expectedPage.ExpectedSkip));
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<TestCaseData> CurrentPositionMultiplePagesTestData
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new TestCaseData(10, 20, 20, new List<MultiplePagesTestData> { new MultiplePagesTestData(1, 10, 10), new MultiplePagesTestData(2, 10, 0) })
|
||||
.SetName("PaginationPosition_Load_SecondHalf_FirstPage_FirstHalf_SecondPage");
|
||||
yield return new TestCaseData(0, 40, 20, new List<MultiplePagesTestData> { new MultiplePagesTestData(1, 20, 0), new MultiplePagesTestData(2, 20, 0) })
|
||||
.SetName("PaginationPosition_Load_Full_First_And_SecondPage");
|
||||
yield return new TestCaseData(35, 15, 20, new List<MultiplePagesTestData> { new MultiplePagesTestData(2, 5, 15), new MultiplePagesTestData(3, 10, 0) })
|
||||
.SetName("PaginationPosition_Load_EndSecondPage_Beginning_ThirdPage");
|
||||
yield return new TestCaseData(18, 22, 20, new List<MultiplePagesTestData> { new MultiplePagesTestData(1, 2, 18), new MultiplePagesTestData(2, 20, 0) })
|
||||
.SetName("PaginationPosition_Load_EndFirstPage_Full_SecondPage");
|
||||
yield return new TestCaseData(38, 4, 20, new List<MultiplePagesTestData> { new MultiplePagesTestData(2, 2, 18), new MultiplePagesTestData(3, 20, 0) })
|
||||
.SetName("PaginationPosition_Load_EndSecondPage_Full_ThirdPage");
|
||||
}
|
||||
}
|
||||
|
||||
public class MultiplePagesTestData
|
||||
{
|
||||
public MultiplePagesTestData(int page, int take, int skip)
|
||||
{
|
||||
Page = page;
|
||||
ExpectedTake = take;
|
||||
ExpectedSkip = skip;
|
||||
}
|
||||
public int Page { get; set; }
|
||||
public int ExpectedTake { get; set; }
|
||||
public int ExpectedSkip { get; set; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
56
src/Ombi.Helpers/PaginationHelper.cs
Normal file
56
src/Ombi.Helpers/PaginationHelper.cs
Normal file
|
@ -0,0 +1,56 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class PaginationHelper
|
||||
{
|
||||
public static List<PagesToLoad> GetNextPages(int currentlyLoaded, int toTake, int maxItemsPerPage)
|
||||
{
|
||||
var result = new List<PagesToLoad>();
|
||||
|
||||
var firstPage = currentlyLoaded / maxItemsPerPage + 1;
|
||||
var startPos = currentlyLoaded % maxItemsPerPage + 1;
|
||||
|
||||
var lastItemIndex = currentlyLoaded + toTake - 1;
|
||||
var lastPage = lastItemIndex / maxItemsPerPage + 1;
|
||||
var stopPos = lastItemIndex % maxItemsPerPage + 1;
|
||||
|
||||
if (currentlyLoaded > maxItemsPerPage)
|
||||
{
|
||||
currentlyLoaded = currentlyLoaded - maxItemsPerPage;
|
||||
}
|
||||
|
||||
var page1 = new PagesToLoad {Page = firstPage, Skip = currentlyLoaded, Take = toTake};
|
||||
|
||||
if (toTake + startPos - 1 > maxItemsPerPage)
|
||||
{
|
||||
page1.Take = maxItemsPerPage - startPos + 1;
|
||||
result.Add(page1);
|
||||
|
||||
for (var i = firstPage + 1; i < lastPage; i++)
|
||||
{
|
||||
var nextPage = new PagesToLoad {Page = i, Skip = 0, Take = maxItemsPerPage};
|
||||
result.Add(nextPage);
|
||||
}
|
||||
|
||||
var pageN = new PagesToLoad {Page = lastPage, Skip = 0, Take = stopPos};
|
||||
result.Add(pageN);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(page1);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class PagesToLoad
|
||||
{
|
||||
public int Page { get; set; }
|
||||
public int Take { get; set; }
|
||||
public int Skip { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,75 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class PaginationHelper
|
||||
{
|
||||
public static List<PagesToLoad> GetNextPages(int currentlyLoaded, int toLoad, int maxItemsPerPage)
|
||||
{
|
||||
var pagesToLoad = new List<PagesToLoad>();
|
||||
|
||||
if (currentlyLoaded == maxItemsPerPage)
|
||||
{
|
||||
currentlyLoaded++;
|
||||
}
|
||||
|
||||
var a = currentlyLoaded / maxItemsPerPage;
|
||||
//var currentPage = Convert.ToInt32(Math.Round((decimal)((decimal)currentlyLoaded / (decimal)maxItemsPerPage), 2, MidpointRounding.AwayFromZero));
|
||||
var currentPage = Convert.ToInt32(Math.Ceiling((decimal)((decimal)currentlyLoaded / (decimal)maxItemsPerPage)));
|
||||
if (currentlyLoaded < maxItemsPerPage)
|
||||
{
|
||||
currentPage = 1;
|
||||
}
|
||||
|
||||
|
||||
var toBeLoaded = (currentlyLoaded + toLoad)+1;
|
||||
//var toBeLoadedPage = Convert.ToInt32(Math.Round((decimal)((decimal)toBeLoaded / (decimal)maxItemsPerPage), 2, MidpointRounding.AwayFromZero));
|
||||
var toBeLoadedPage = Convert.ToInt32(Math.Ceiling((decimal)((decimal)toBeLoaded / (decimal)maxItemsPerPage)));
|
||||
|
||||
|
||||
if (currentlyLoaded == 0)
|
||||
{
|
||||
// If we have not loaded any yet, then we should only load
|
||||
// the first page
|
||||
currentPage = toBeLoadedPage;
|
||||
}
|
||||
var extraPageNeeded = (toBeLoadedPage != currentPage);
|
||||
if(extraPageNeeded)
|
||||
{
|
||||
// Add the first page
|
||||
pagesToLoad.Add(new PagesToLoad
|
||||
{
|
||||
Page = currentPage,
|
||||
Skip = currentlyLoaded
|
||||
});
|
||||
// Add extra page
|
||||
pagesToLoad.Add(new PagesToLoad
|
||||
{
|
||||
Page = toBeLoadedPage,
|
||||
Skip = (currentlyLoaded + toLoad) - maxItemsPerPage,
|
||||
Take = toLoad
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
pagesToLoad.Add(new PagesToLoad
|
||||
{
|
||||
Page = currentPage,
|
||||
Skip = currentlyLoaded,
|
||||
Take = toLoad
|
||||
});
|
||||
}
|
||||
|
||||
return pagesToLoad;
|
||||
}
|
||||
}
|
||||
|
||||
public class PagesToLoad
|
||||
{
|
||||
public int Page { get; set; }
|
||||
public int Take { get; set; }
|
||||
public int Skip { get; set; }
|
||||
}
|
||||
}
|
10708
src/Ombi/ClientApp/package-lock.json
generated
10708
src/Ombi/ClientApp/package-lock.json
generated
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue