aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBond_009 <bond.009@outlook.com>2021-01-24 11:43:05 +0100
committerBond_009 <bond.009@outlook.com>2021-01-24 11:43:05 +0100
commitd24e7f60c754620f88faa294af5bf0309b59c785 (patch)
tree0efa874d3075ec012fc63ff8c301e58c9012bdb6
parentb4d04f9ca5345c87d44993fbbb9e211c1dbf2dcd (diff)
Fix GetOrderBy and add tests
-rw-r--r--Jellyfin.Api/Helpers/RequestHelpers.cs19
-rw-r--r--tests/Jellyfin.Api.Tests/Helpers/RequestHelpersTests.cs59
2 files changed, 70 insertions, 8 deletions
diff --git a/Jellyfin.Api/Helpers/RequestHelpers.cs b/Jellyfin.Api/Helpers/RequestHelpers.cs
index 056ad83da..59cb8ea2c 100644
--- a/Jellyfin.Api/Helpers/RequestHelpers.cs
+++ b/Jellyfin.Api/Helpers/RequestHelpers.cs
@@ -25,22 +25,25 @@ namespace Jellyfin.Api.Helpers
/// <param name="sortBy">Sort By. Comma delimited string.</param>
/// <param name="requestedSortOrder">Sort Order. Comma delimited string.</param>
/// <returns>Order By.</returns>
- public static ValueTuple<string, SortOrder>[] GetOrderBy(IReadOnlyList<string> sortBy, IReadOnlyList<SortOrder> requestedSortOrder)
+ public static (string, SortOrder)[] GetOrderBy(IReadOnlyList<string> sortBy, IReadOnlyList<SortOrder> requestedSortOrder)
{
if (sortBy.Count == 0)
{
return Array.Empty<ValueTuple<string, SortOrder>>();
}
- var result = new ValueTuple<string, SortOrder>[sortBy.Count];
- for (var i = 0; i < sortBy.Count; i++)
+ var result = new (string, SortOrder)[sortBy.Count];
+ var i = 0;
+ // Add elements which have a SortOrder specified
+ for (; i < requestedSortOrder.Count; i++)
{
- var sortOrderIndex = requestedSortOrder.Count > i ? i : 0;
+ result[i] = (sortBy[i], requestedSortOrder[i]);
+ }
- var sortOrder = requestedSortOrder.Count > sortOrderIndex
- ? requestedSortOrder[sortOrderIndex]
- : SortOrder.Ascending;
- result[i] = new ValueTuple<string, SortOrder>(sortBy[i], sortOrder);
+ // Add remaining elements with the default SortOrder
+ for (; i < sortBy.Count; i++)
+ {
+ result[i] = (sortBy[i], SortOrder.Ascending);
}
return result;
diff --git a/tests/Jellyfin.Api.Tests/Helpers/RequestHelpersTests.cs b/tests/Jellyfin.Api.Tests/Helpers/RequestHelpersTests.cs
new file mode 100644
index 000000000..944472c68
--- /dev/null
+++ b/tests/Jellyfin.Api.Tests/Helpers/RequestHelpersTests.cs
@@ -0,0 +1,59 @@
+using System;
+using System.Collections.Generic;
+using Jellyfin.Api.Helpers;
+using Jellyfin.Data.Enums;
+using Xunit;
+
+namespace Jellyfin.Api.Tests.Helpers
+{
+ public class RequestHelpersTests
+ {
+ [Theory]
+ [MemberData(nameof(GetOrderBy_Success_TestData))]
+ public void GetOrderBy_Success(IReadOnlyList<string> sortBy, IReadOnlyList<SortOrder> requestedSortOrder, (string, SortOrder)[] expected)
+ {
+ Assert.Equal(expected, RequestHelpers.GetOrderBy(sortBy, requestedSortOrder));
+ }
+
+ public static IEnumerable<object[]> GetOrderBy_Success_TestData()
+ {
+ yield return new object[]
+ {
+ Array.Empty<string>(),
+ Array.Empty<SortOrder>(),
+ Array.Empty<(string, SortOrder)>()
+ };
+ yield return new object[]
+ {
+ new string[]
+ {
+ "IsFavoriteOrLiked",
+ "Random"
+ },
+ Array.Empty<SortOrder>(),
+ new (string, SortOrder)[]
+ {
+ ("IsFavoriteOrLiked", SortOrder.Ascending),
+ ("Random", SortOrder.Ascending),
+ }
+ };
+ yield return new object[]
+ {
+ new string[]
+ {
+ "SortName",
+ "ProductionYear"
+ },
+ new SortOrder[]
+ {
+ SortOrder.Descending
+ },
+ new (string, SortOrder)[]
+ {
+ ("SortName", SortOrder.Descending),
+ ("ProductionYear", SortOrder.Ascending),
+ }
+ };
+ }
+ }
+}