Readded 0 cat to the end of the Newznab list

This commit is contained in:
Taloth Saldono 2020-11-14 22:33:03 +01:00 committed by Qstick
parent 2e4b168985
commit 40df88e37c
4 changed files with 194 additions and 13 deletions

View File

@ -209,7 +209,11 @@ namespace Lidarr.Http.ClientSchema
{
return fieldValue =>
{
if (fieldValue.GetType() == typeof(JArray))
if (fieldValue == null)
{
return Enumerable.Empty<int>();
}
else if (fieldValue.GetType() == typeof(JArray))
{
return ((JArray)fieldValue).Select(s => s.Value<int>());
}
@ -223,7 +227,11 @@ namespace Lidarr.Http.ClientSchema
{
return fieldValue =>
{
if (fieldValue.GetType() == typeof(JArray))
if (fieldValue == null)
{
return Enumerable.Empty<string>();
}
else if (fieldValue.GetType() == typeof(JArray))
{
return ((JArray)fieldValue).Select(s => s.Value<string>());
}

View File

@ -10,7 +10,10 @@ namespace NzbDrone.Core.Indexers.Newznab
public static List<FieldSelectOption> GetFieldSelectOptions(List<NewznabCategory> categories)
{
// Ignore categories not relevant for Lidarr
var ignoreCategories = new[] { 0, 1000, 2000, 4000, 5000, 6000, 7000 };
var ignoreCategories = new[] { 1000, 2000, 4000, 5000, 6000, 7000 };
// And maybe relevant for specific users
var unimportantCategories = new[] { 0 };
var result = new List<FieldSelectOption>();
@ -32,13 +35,8 @@ namespace NzbDrone.Core.Indexers.Newznab
});
}
foreach (var category in categories)
foreach (var category in categories.Where(cat => !ignoreCategories.Contains(cat.Id)).OrderBy(cat => unimportantCategories.Contains(cat.Id)).ThenBy(cat => cat.Id))
{
if (ignoreCategories.Contains(category.Id))
{
continue;
}
result.Add(new FieldSelectOption
{
Value = category.Id,
@ -48,7 +46,7 @@ namespace NzbDrone.Core.Indexers.Newznab
if (category.Subcategories != null)
{
foreach (var subcat in category.Subcategories)
foreach (var subcat in category.Subcategories.OrderBy(cat => cat.Id))
{
result.Add(new FieldSelectOption
{
@ -61,8 +59,6 @@ namespace NzbDrone.Core.Indexers.Newznab
}
}
result.Sort((l, r) => l.Value.CompareTo(r.Value));
return result;
}
}

View File

@ -1,5 +1,8 @@
using System.Linq;
using System.Linq;
using FluentAssertions;
using Lidarr.Api.V1.Indexers;
using Lidarr.Http.ClientSchema;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using NzbDrone.Core.ThingiProvider;
@ -18,5 +21,172 @@ namespace NzbDrone.Integration.Test.ApiTests
indexers.Should().NotContain(c => string.IsNullOrWhiteSpace(c.Name));
indexers.Where(c => c.ConfigContract == typeof(NullConfig).Name).Should().OnlyContain(c => c.EnableRss);
}
private IndexerResource GetNewznabSchemav1(string name = null)
{
var schema = Indexers.Schema().First(v => v.Implementation == "Newznab");
schema.Name = name;
schema.EnableRss = false;
schema.EnableAutomaticSearch = false;
schema.EnableInteractiveSearch = false;
return schema;
}
private Field GetCategoriesField(IndexerResource resource)
{
var field = resource.Fields.First(v => v.Name == "categories");
return field;
}
[Test]
public void v2_categories_should_be_array()
{
var schema = GetNewznabSchemav1();
var categoriesField = GetCategoriesField(schema);
categoriesField.Value.Should().BeOfType<JArray>();
}
[Test]
public void v3_categories_should_be_array()
{
var schema = GetNewznabSchemav1();
var categoriesField = GetCategoriesField(schema);
categoriesField.Value.Should().BeOfType<JArray>();
}
[Test]
public void v2_categories_should_accept_null()
{
var schema = GetNewznabSchemav1("Testv2null");
var categoriesField = GetCategoriesField(schema);
categoriesField.Value = null;
var result = Indexers.Post(schema);
var resultArray = GetCategoriesField(result).Value;
resultArray.Should().BeOfType<JArray>();
resultArray.As<JArray>().Should().BeEmpty();
}
[Test]
public void v2_categories_should_accept_emptystring()
{
var schema = GetNewznabSchemav1("Testv2emptystring");
var categoriesField = GetCategoriesField(schema);
categoriesField.Value = "";
var result = Indexers.Post(schema);
var resultArray = GetCategoriesField(result).Value;
resultArray.Should().BeOfType<JArray>();
resultArray.As<JArray>().Should().BeEmpty();
}
[Test]
public void v2_categories_should_accept_string()
{
var schema = GetNewznabSchemav1("Testv2string");
var categoriesField = GetCategoriesField(schema);
categoriesField.Value = "1000,1010";
var result = Indexers.Post(schema);
var resultArray = GetCategoriesField(result).Value;
resultArray.Should().BeOfType<JArray>();
resultArray.As<JArray>().ToObject<int[]>().Should().BeEquivalentTo(new[] { 1000, 1010 });
}
[Test]
public void v2_categories_should_accept_array()
{
var schema = GetNewznabSchemav1("Testv2array");
var categoriesField = GetCategoriesField(schema);
categoriesField.Value = new object[] { 1000, 1010 };
var result = Indexers.Post(schema);
var resultArray = GetCategoriesField(result).Value;
resultArray.Should().BeOfType<JArray>();
resultArray.As<JArray>().ToObject<int[]>().Should().BeEquivalentTo(new[] { 1000, 1010 });
}
[Test]
public void v3_categories_should_accept_null()
{
var schema = GetNewznabSchemav1("Testv3null");
var categoriesField = GetCategoriesField(schema);
categoriesField.Value = null;
var result = Indexers.Post(schema);
var resultArray = GetCategoriesField(result).Value;
resultArray.Should().BeOfType<JArray>();
resultArray.As<JArray>().Should().BeEmpty();
}
[Test]
public void v3_categories_should_accept_emptystring()
{
var schema = GetNewznabSchemav1("Testv3emptystring");
var categoriesField = GetCategoriesField(schema);
categoriesField.Value = "";
var result = Indexers.Post(schema);
var resultArray = GetCategoriesField(result).Value;
resultArray.Should().BeOfType<JArray>();
resultArray.As<JArray>().Should().BeEmpty();
}
[Test]
public void v3_categories_should_accept_string()
{
var schema = GetNewznabSchemav1("Testv3string");
var categoriesField = GetCategoriesField(schema);
categoriesField.Value = "1000,1010";
var result = Indexers.Post(schema);
var resultArray = GetCategoriesField(result).Value;
resultArray.Should().BeOfType<JArray>();
resultArray.As<JArray>().ToObject<int[]>().Should().BeEquivalentTo(new[] { 1000, 1010 });
}
[Test]
public void v3_categories_should_accept_array()
{
var schema = GetNewznabSchemav1("Testv3array");
var categoriesField = GetCategoriesField(schema);
categoriesField.Value = new object[] { 1000, 1010 };
var result = Indexers.Post(schema);
var resultArray = GetCategoriesField(result).Value;
resultArray.Should().BeOfType<JArray>();
resultArray.As<JArray>().ToObject<int[]>().Should().BeEquivalentTo(new[] { 1000, 1010 });
}
}
}

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using Lidarr.Api.V1.Indexers;
using RestSharp;
@ -9,5 +10,11 @@ namespace NzbDrone.Integration.Test.Client
: base(restClient, apiKey)
{
}
public List<IndexerResource> Schema()
{
var request = BuildRequest("/schema");
return Get<List<IndexerResource>>(request);
}
}
}