diff --git a/src/Jackett.Common/Indexers/BaseIndexer.cs b/src/Jackett.Common/Indexers/BaseIndexer.cs index 192819c87..978da4b4f 100644 --- a/src/Jackett.Common/Indexers/BaseIndexer.cs +++ b/src/Jackett.Common/Indexers/BaseIndexer.cs @@ -583,9 +583,11 @@ namespace Jackett.Common.Indexers protected void AddCategoryMapping(string trackerCategory, TorznabCategory newznabCategory, string trackerCategoryDesc = null) => TorznabCaps.Categories.AddCategoryMapping(trackerCategory, newznabCategory, trackerCategoryDesc); + // TODO: remove this method ? protected void AddCategoryMapping(int trackerCategory, TorznabCategory newznabCategory, string trackerCategoryDesc = null) => AddCategoryMapping(trackerCategory.ToString(), newznabCategory, trackerCategoryDesc); + // TODO: remove this method and use AddCategoryMapping instead. this method doesn't allow to create custom cats protected void AddMultiCategoryMapping(TorznabCategory newznabCategory, params int[] trackerCategories) { foreach (var trackerCat in trackerCategories) diff --git a/src/Jackett.Test/Common/Indexers/BaseWebIndexerTests.cs b/src/Jackett.Test/Common/Indexers/BaseWebIndexerTests.cs new file mode 100644 index 000000000..926f04dfe --- /dev/null +++ b/src/Jackett.Test/Common/Indexers/BaseWebIndexerTests.cs @@ -0,0 +1,104 @@ +using System.Linq; +using Jackett.Common.Models; +using Jackett.Test.TestHelpers; +using NUnit.Framework; +using Assert = NUnit.Framework.Assert; + +namespace Jackett.Test.Common.Indexers +{ + [TestFixture] + public class BaseWebIndexerTests + { + [Test] + public void TestConstructor() + { + var indexer = new TestWebIndexer(); + var caps = indexer.TorznabCaps; + + Assert.True(caps.SearchAvailable); + Assert.IsEmpty(caps.TvSearchParams); + Assert.False(caps.TvSearchAvailable); + Assert.False(caps.TvSearchSeasonAvailable); + Assert.False(caps.TvSearchEpAvailable); + Assert.False(caps.TvSearchImdbAvailable); + Assert.False(caps.TvSearchTvdbAvailable); + Assert.False(caps.TvSearchTvRageAvailable); + Assert.IsEmpty(caps.MovieSearchParams); + Assert.False(caps.MovieSearchAvailable); + Assert.False(caps.MovieSearchImdbAvailable); + Assert.False(caps.MovieSearchTmdbAvailable); + Assert.IsEmpty(caps.MusicSearchParams); + Assert.False(caps.MusicSearchAvailable); + Assert.False(caps.MusicSearchAlbumAvailable); + Assert.False(caps.MusicSearchArtistAvailable); + Assert.False(caps.MusicSearchLabelAvailable); + Assert.False(caps.MusicSearchYearAvailable); + Assert.IsEmpty(caps.BookSearchParams); + Assert.False(caps.BookSearchAvailable); + Assert.False(caps.BookSearchTitleAvailable); + Assert.False(caps.BookSearchAuthorAvailable); + Assert.AreEqual(0, caps.Categories.GetTorznabCategories().Count); + } + + [Test] + public void TestAddCategoryMapping() + { + var indexer = new TestWebIndexer(); + + // you can find more complex tests in TorznabCapabilitiesCategoriesTests.cs + indexer._AddCategoryMapping("11", TorznabCatType.MoviesSD, "MoviesSD"); + Assert.AreEqual(2, indexer.TorznabCaps.Categories.GetTorznabCategories().Count); + indexer._AddCategoryMapping(14, TorznabCatType.MoviesHD); + Assert.AreEqual(3, indexer.TorznabCaps.Categories.GetTorznabCategories().Count); + } + + [Test] + public void TestAddMultiCategoryMapping() + { + var indexer = new TestWebIndexer(); + + indexer._AddMultiCategoryMapping(TorznabCatType.MoviesHD,19, 18); + Assert.AreEqual(1, indexer.TorznabCaps.Categories.GetTorznabCategories().Count); + } + + [Test] + public void TestMapTorznabCapsToTrackers() + { + var indexer = new TestWebIndexer(); + indexer.AddTestCategories(); + + var query = new TorznabQuery // int category with subcategories (parent cat) + { + Categories = new [] { TorznabCatType.Movies.ID } + }; + var trackerCats = indexer._MapTorznabCapsToTrackers(query); + Assert.AreEqual(2, trackerCats.Count); + Assert.AreEqual("1", trackerCats[0]); // Movies + Assert.AreEqual("mov_sd", trackerCats[1]); // Movies SD + } + + [Test] + public void TestMapTrackerCatToNewznab() + { + var indexer = new TestWebIndexer(); + indexer.AddTestCategories(); + + // TODO: this is wrong, custom cat 100001 doesn't exists (it's not defined by us) + var torznabCats = indexer._MapTrackerCatToNewznab("1").ToList(); + Assert.AreEqual(2, torznabCats.Count); + Assert.AreEqual(2000, torznabCats[0]); + Assert.AreEqual(100001, torznabCats[1]); + } + + [Test] + public void TestMapTrackerCatDescToNewznab() + { + var indexer = new TestWebIndexer(); + indexer.AddTestCategories(); + + var torznabCats = indexer._MapTrackerCatDescToNewznab("Console/Wii_c").ToList(); + Assert.AreEqual(1, torznabCats.Count); + Assert.AreEqual(1030, torznabCats[0]); + } + } +} diff --git a/src/Jackett.Test/Torznab/TorznabTests.cs b/src/Jackett.Test/Common/Indexers/CardigannIndexerTests.cs similarity index 59% rename from src/Jackett.Test/Torznab/TorznabTests.cs rename to src/Jackett.Test/Common/Indexers/CardigannIndexerTests.cs index 109668c26..ac2dd9681 100644 --- a/src/Jackett.Test/Torznab/TorznabTests.cs +++ b/src/Jackett.Test/Common/Indexers/CardigannIndexerTests.cs @@ -1,123 +1,14 @@ -using System; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Jackett.Common.Indexers; using Jackett.Common.Models; -using Jackett.Common.Models.IndexerConfig; -using Newtonsoft.Json.Linq; using NUnit.Framework; -namespace Jackett.Test.Torznab +namespace Jackett.Test.Common.Indexers { - // TODO: this cass is temporary. We have categories functionality spread across a lot of classes and we - // need to make sure we are not losing features in the refactor. - [TestFixture] - public class TorznabTests: BaseWebIndexer + public class CardigannIndexerTests { - public TorznabTests(): - base(id: "test_id", - name: "test_name", - description: "test_description", - link: "https://test.link/", - caps: new TorznabCapabilities(), - client: null, - configService: null, - logger: null, - configData: new ConfigurationData(), - p: null) - { - } - - public override Task ApplyConfiguration(JToken configJson) => throw new NotImplementedException(); - protected override Task> PerformQuery(TorznabQuery query) => throw new NotImplementedException(); - - [Test] - public void TestCSharpTorznabCategories() - { - Assert.True(TorznabCaps.SearchAvailable); - Assert.IsEmpty(TorznabCaps.TvSearchParams); - Assert.False(TorznabCaps.TvSearchAvailable); - Assert.False(TorznabCaps.TvSearchSeasonAvailable); - Assert.False(TorznabCaps.TvSearchEpAvailable); - Assert.False(TorznabCaps.TvSearchImdbAvailable); - Assert.False(TorznabCaps.TvSearchTvdbAvailable); - Assert.False(TorznabCaps.TvSearchTvRageAvailable); - Assert.IsEmpty(TorznabCaps.MovieSearchParams); - Assert.False(TorznabCaps.MovieSearchAvailable); - Assert.False(TorznabCaps.MovieSearchImdbAvailable); - Assert.False(TorznabCaps.MovieSearchTmdbAvailable); - Assert.IsEmpty(TorznabCaps.MusicSearchParams); - Assert.False(TorznabCaps.MusicSearchAvailable); - Assert.False(TorznabCaps.MusicSearchAlbumAvailable); - Assert.False(TorznabCaps.MusicSearchArtistAvailable); - Assert.False(TorznabCaps.MusicSearchLabelAvailable); - Assert.False(TorznabCaps.MusicSearchYearAvailable); - Assert.IsEmpty(TorznabCaps.BookSearchParams); - Assert.False(TorznabCaps.BookSearchAvailable); - Assert.False(TorznabCaps.BookSearchTitleAvailable); - Assert.False(TorznabCaps.BookSearchAuthorAvailable); - Assert.AreEqual(0, TorznabCaps.Categories.GetTorznabCategories().Count); - - // simple category tests - AddCategoryMapping("1", TorznabCatType.Movies); - AddCategoryMapping("mov_sd", TorznabCatType.MoviesSD); - AddCategoryMapping("33", TorznabCatType.BooksComics); - AddCategoryMapping("44", TorznabCatType.ConsoleXBox, "Console/Xbox_c"); - AddCategoryMapping("con_wii", TorznabCatType.ConsoleWii, "Console/Wii_c"); - AddCategoryMapping("45", TorznabCatType.ConsoleXBox, "Console/Xbox_c2"); - - var query = new TorznabQuery // int category with subcategories (parent cat) - { - Categories = new [] { TorznabCatType.Movies.ID } - }; - var trackerCats = MapTorznabCapsToTrackers(query); - Assert.AreEqual(2, trackerCats.Count); - Assert.AreEqual("1", trackerCats[0]); // Movies - Assert.AreEqual("mov_sd", trackerCats[1]); // Movies SD - - // TODO: this is wrong, custom cat 100001 doesn't exists (it's not defined by us) - var torznabCats = MapTrackerCatToNewznab("1").ToList(); - Assert.AreEqual(2, torznabCats.Count); - Assert.AreEqual(2000, torznabCats[0]); - Assert.AreEqual(100001, torznabCats[1]); - - torznabCats = MapTrackerCatDescToNewznab("Console/Wii_c").ToList(); - Assert.AreEqual(1, torznabCats.Count); - Assert.AreEqual(1030, torznabCats[0]); - - // TODO: test AddMultiCategoryMapping - // TODO: add duplicates: different trackerCat but same newznabCat - // TODO: duplicates are not working well because we keep 2 internal lists with categories. One is deduplicated - // and the other doesn't - - // test Jackett UI categories (internal JSON) - var dto = new Jackett.Common.Models.DTO.Indexer(this); - var dtoCaps = dto.caps.ToList(); - Assert.AreEqual(7, dtoCaps.Count); - Assert.AreEqual("100044", dtoCaps[0].ID); - Assert.AreEqual("100045", dtoCaps[1].ID); - Assert.AreEqual("1030", dtoCaps[2].ID); - Assert.AreEqual("1040", dtoCaps[3].ID); - Assert.AreEqual("2000", dtoCaps[4].ID); - Assert.AreEqual("2030", dtoCaps[5].ID); - Assert.AreEqual("7030", dtoCaps[6].ID); - - // test Torznab caps (XML) => more in Common.Model.TorznabCapabilitiesTests - var xDocument = TorznabCaps.GetXDocument(); - var xDoumentCategories = xDocument.Root?.Element("categories")?.Elements("category").ToList(); - Assert.AreEqual(7, xDoumentCategories?.Count); - Assert.AreEqual("100044", xDoumentCategories?[0].Attribute("id")?.Value); - Assert.AreEqual("100045", xDoumentCategories?[1].Attribute("id")?.Value); - Assert.AreEqual("1030", xDoumentCategories?[2].Attribute("id")?.Value); - Assert.AreEqual("1040", xDoumentCategories?[3].Attribute("id")?.Value); - Assert.AreEqual("2000", xDoumentCategories?[4].Attribute("id")?.Value); // Movies - Assert.AreEqual("2030", xDoumentCategories?[5].Attribute("id")?.Value); - Assert.AreEqual("7030", xDoumentCategories?[6].Attribute("id")?.Value); - Assert.AreEqual(9, xDoumentCategories?[4]?.Elements("subcat").ToList().Count); // Movies - } - + // TODO: split this test into smaller tests [Test] public void TestCardigannTorznabCategories() { diff --git a/src/Jackett.Test/Common/Models/DTO/IndexerTests.cs b/src/Jackett.Test/Common/Models/DTO/IndexerTests.cs new file mode 100644 index 000000000..7ef66a1b3 --- /dev/null +++ b/src/Jackett.Test/Common/Models/DTO/IndexerTests.cs @@ -0,0 +1,53 @@ +using System.Linq; +using Jackett.Common.Models.DTO; +using Jackett.Test.TestHelpers; +using NUnit.Framework; +using Assert = NUnit.Framework.Assert; + +namespace Jackett.Test.Common.Models.DTO +{ + [TestFixture] + public class IndexerTests + { + [Test] + public void TestConstructor() + { + var indexer = new TestWebIndexer(); + + var dto = new Indexer(indexer); + Assert.AreEqual("test_id", dto.id); + Assert.AreEqual("test_name", dto.name); + Assert.AreEqual("test_description", dto.description); + Assert.AreEqual("private", dto.type); + Assert.False(dto.configured); + Assert.AreEqual("https://test.link/", dto.site_link); + Assert.AreEqual(2, dto.alternativesitelinks.ToList().Count); + Assert.AreEqual("en-us", dto.language); + Assert.AreEqual("", dto.last_error); + Assert.False(dto.potatoenabled); + Assert.AreEqual(0, dto.caps.ToList().Count); + } + + [Test] + public void TestConstructorWithCategories() + { + var indexer = new TestWebIndexer(); + indexer.AddTestCategories(); + + // test Jackett UI categories (internal JSON) + var dto = new Indexer(indexer); + var dtoCaps = dto.caps.ToList(); + Assert.AreEqual(7, dtoCaps.Count); + Assert.AreEqual("100044", dtoCaps[0].ID); + Assert.AreEqual("100045", dtoCaps[1].ID); + Assert.AreEqual("1030", dtoCaps[2].ID); + Assert.AreEqual("1040", dtoCaps[3].ID); + Assert.AreEqual("2000", dtoCaps[4].ID); + Assert.AreEqual("2030", dtoCaps[5].ID); + Assert.AreEqual("7030", dtoCaps[6].ID); + + // movies categories enable potato search + Assert.True(dto.potatoenabled); + } + } +} diff --git a/src/Jackett.Test/Common/Models/TorznabCapabilitiesCategoriesTests.cs b/src/Jackett.Test/Common/Models/TorznabCapabilitiesCategoriesTests.cs index 57c6e8915..48a5761a1 100644 --- a/src/Jackett.Test/Common/Models/TorznabCapabilitiesCategoriesTests.cs +++ b/src/Jackett.Test/Common/Models/TorznabCapabilitiesCategoriesTests.cs @@ -1,10 +1,15 @@ using System.Linq; using Jackett.Common.Models; +using Jackett.Test.TestHelpers; using NUnit.Framework; using Assert = NUnit.Framework.Assert; namespace Jackett.Test.Common.Models { + // TODO: add duplicates: different trackerCat but same newznabCat + // TODO: duplicates are not working well because we keep 2 internal lists with categories. One is de-duplicated + // and the other doesn't + [TestFixture] public class TorznabCapabilitiesCategoriesTests { @@ -220,12 +225,7 @@ namespace Jackett.Test.Common.Models private static TorznabCapabilitiesCategories CreateTestDataset() { var tcc = new TorznabCapabilitiesCategories(); - tcc.AddCategoryMapping("1", TorznabCatType.Movies); - tcc.AddCategoryMapping("mov_sd", TorznabCatType.MoviesSD); - tcc.AddCategoryMapping("33", TorznabCatType.BooksComics); - tcc.AddCategoryMapping("44", TorznabCatType.ConsoleXBox, "Console/Xbox_c"); - tcc.AddCategoryMapping("con_wii", TorznabCatType.ConsoleWii, "Console/Wii_c"); - tcc.AddCategoryMapping("45", TorznabCatType.ConsoleXBox, "Console/Xbox_c2"); + TestCategories.AddTestCategories(tcc); return tcc; } } diff --git a/src/Jackett.Test/Common/Models/TorznabCapabilitiesTests.cs b/src/Jackett.Test/Common/Models/TorznabCapabilitiesTests.cs index 01f75173a..36f03e09d 100644 --- a/src/Jackett.Test/Common/Models/TorznabCapabilitiesTests.cs +++ b/src/Jackett.Test/Common/Models/TorznabCapabilitiesTests.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using Jackett.Common.Models; +using Jackett.Test.TestHelpers; using NUnit.Framework; using Assert = NUnit.Framework.Assert; @@ -353,19 +354,19 @@ namespace Jackett.Test.Common.Models SearchAvailable = false }; xDocument = torznabCaps.GetXDocument(); - var xDoumentSearching = xDocument.Root?.Element("searching"); - Assert.AreEqual("no", xDoumentSearching?.Element("search")?.Attribute("available")?.Value); - Assert.AreEqual("q", xDoumentSearching?.Element("search")?.Attribute("supportedParams")?.Value); - Assert.AreEqual("no", xDoumentSearching?.Element("tv-search")?.Attribute("available")?.Value); - Assert.AreEqual("q", xDoumentSearching?.Element("tv-search")?.Attribute("supportedParams")?.Value); - Assert.AreEqual("no", xDoumentSearching?.Element("movie-search")?.Attribute("available")?.Value); - Assert.AreEqual("q", xDoumentSearching?.Element("movie-search")?.Attribute("supportedParams")?.Value); - Assert.AreEqual("no", xDoumentSearching?.Element("music-search")?.Attribute("available")?.Value); - Assert.AreEqual("q", xDoumentSearching?.Element("music-search")?.Attribute("supportedParams")?.Value); - Assert.AreEqual("no", xDoumentSearching?.Element("audio-search")?.Attribute("available")?.Value); - Assert.AreEqual("q", xDoumentSearching?.Element("audio-search")?.Attribute("supportedParams")?.Value); - Assert.AreEqual("no", xDoumentSearching?.Element("book-search")?.Attribute("available")?.Value); - Assert.AreEqual("q", xDoumentSearching?.Element("book-search")?.Attribute("supportedParams")?.Value); + var xDocumentSearching = xDocument.Root?.Element("searching"); + Assert.AreEqual("no", xDocumentSearching?.Element("search")?.Attribute("available")?.Value); + Assert.AreEqual("q", xDocumentSearching?.Element("search")?.Attribute("supportedParams")?.Value); + Assert.AreEqual("no", xDocumentSearching?.Element("tv-search")?.Attribute("available")?.Value); + Assert.AreEqual("q", xDocumentSearching?.Element("tv-search")?.Attribute("supportedParams")?.Value); + Assert.AreEqual("no", xDocumentSearching?.Element("movie-search")?.Attribute("available")?.Value); + Assert.AreEqual("q", xDocumentSearching?.Element("movie-search")?.Attribute("supportedParams")?.Value); + Assert.AreEqual("no", xDocumentSearching?.Element("music-search")?.Attribute("available")?.Value); + Assert.AreEqual("q", xDocumentSearching?.Element("music-search")?.Attribute("supportedParams")?.Value); + Assert.AreEqual("no", xDocumentSearching?.Element("audio-search")?.Attribute("available")?.Value); + Assert.AreEqual("q", xDocumentSearching?.Element("audio-search")?.Attribute("supportedParams")?.Value); + Assert.AreEqual("no", xDocumentSearching?.Element("book-search")?.Attribute("available")?.Value); + Assert.AreEqual("q", xDocumentSearching?.Element("book-search")?.Attribute("supportedParams")?.Value); // test all features enabled torznabCaps = new TorznabCapabilities @@ -389,55 +390,75 @@ namespace Jackett.Test.Common.Models }, }; xDocument = torznabCaps.GetXDocument(); - xDoumentSearching = xDocument.Root?.Element("searching"); - Assert.AreEqual("yes", xDoumentSearching?.Element("search")?.Attribute("available")?.Value); - Assert.AreEqual("q", xDoumentSearching?.Element("search")?.Attribute("supportedParams")?.Value); - Assert.AreEqual("yes", xDoumentSearching?.Element("tv-search")?.Attribute("available")?.Value); - Assert.AreEqual("q,season,ep,tvdbid,rid", xDoumentSearching?.Element("tv-search")?.Attribute("supportedParams")?.Value); - Assert.AreEqual("yes", xDoumentSearching?.Element("movie-search")?.Attribute("available")?.Value); - Assert.AreEqual("q,imdbid,tmdbid", xDoumentSearching?.Element("movie-search")?.Attribute("supportedParams")?.Value); - Assert.AreEqual("yes", xDoumentSearching?.Element("music-search")?.Attribute("available")?.Value); - Assert.AreEqual("q,album,artist,label,year", xDoumentSearching?.Element("music-search")?.Attribute("supportedParams")?.Value); - Assert.AreEqual("yes", xDoumentSearching?.Element("audio-search")?.Attribute("available")?.Value); - Assert.AreEqual("q,album,artist,label,year", xDoumentSearching?.Element("audio-search")?.Attribute("supportedParams")?.Value); - Assert.AreEqual("yes", xDoumentSearching?.Element("book-search")?.Attribute("available")?.Value); - Assert.AreEqual("q,title,author", xDoumentSearching?.Element("book-search")?.Attribute("supportedParams")?.Value); + xDocumentSearching = xDocument.Root?.Element("searching"); + Assert.AreEqual("yes", xDocumentSearching?.Element("search")?.Attribute("available")?.Value); + Assert.AreEqual("q", xDocumentSearching?.Element("search")?.Attribute("supportedParams")?.Value); + Assert.AreEqual("yes", xDocumentSearching?.Element("tv-search")?.Attribute("available")?.Value); + Assert.AreEqual("q,season,ep,tvdbid,rid", xDocumentSearching?.Element("tv-search")?.Attribute("supportedParams")?.Value); + Assert.AreEqual("yes", xDocumentSearching?.Element("movie-search")?.Attribute("available")?.Value); + Assert.AreEqual("q,imdbid,tmdbid", xDocumentSearching?.Element("movie-search")?.Attribute("supportedParams")?.Value); + Assert.AreEqual("yes", xDocumentSearching?.Element("music-search")?.Attribute("available")?.Value); + Assert.AreEqual("q,album,artist,label,year", xDocumentSearching?.Element("music-search")?.Attribute("supportedParams")?.Value); + Assert.AreEqual("yes", xDocumentSearching?.Element("audio-search")?.Attribute("available")?.Value); + Assert.AreEqual("q,album,artist,label,year", xDocumentSearching?.Element("audio-search")?.Attribute("supportedParams")?.Value); + Assert.AreEqual("yes", xDocumentSearching?.Element("book-search")?.Attribute("available")?.Value); + Assert.AreEqual("q,title,author", xDocumentSearching?.Element("book-search")?.Attribute("supportedParams")?.Value); // test categories torznabCaps = new TorznabCapabilities(); torznabCaps.Categories.AddCategoryMapping("c1", TorznabCatType.MoviesSD); // child category xDocument = torznabCaps.GetXDocument(); - var xDoumentCategories = xDocument.Root?.Element("categories")?.Elements("category").ToList(); - Assert.AreEqual(1, xDoumentCategories?.Count); - Assert.AreEqual(TorznabCatType.MoviesSD.ID.ToString(), xDoumentCategories?.First().Attribute("id")?.Value); - Assert.AreEqual(TorznabCatType.MoviesSD.Name, xDoumentCategories?.First().Attribute("name")?.Value); + var xDocumentCategories = xDocument.Root?.Element("categories")?.Elements("category").ToList(); + Assert.AreEqual(1, xDocumentCategories?.Count); + Assert.AreEqual(TorznabCatType.MoviesSD.ID.ToString(), xDocumentCategories?.First().Attribute("id")?.Value); + Assert.AreEqual(TorznabCatType.MoviesSD.Name, xDocumentCategories?.First().Attribute("name")?.Value); // TODO: child category is duplicated. should we add just parent and child without other subcats? torznabCaps = new TorznabCapabilities(); torznabCaps.Categories.AddCategoryMapping("c1", TorznabCatType.Movies); // parent and child category torznabCaps.Categories.AddCategoryMapping("c2", TorznabCatType.MoviesSD); xDocument = torznabCaps.GetXDocument(); - xDoumentCategories = xDocument.Root?.Element("categories")?.Elements("category").ToList(); - Assert.AreEqual(2, xDoumentCategories?.Count); - Assert.AreEqual(TorznabCatType.Movies.ID.ToString(), xDoumentCategories?.First().Attribute("id")?.Value); - Assert.AreEqual(TorznabCatType.Movies.Name, xDoumentCategories?.First().Attribute("name")?.Value); - Assert.AreEqual(TorznabCatType.MoviesSD.ID.ToString(), xDoumentCategories?[1].Attribute("id")?.Value); - Assert.AreEqual(TorznabCatType.MoviesSD.Name, xDoumentCategories?[1].Attribute("name")?.Value); - var xDoumentSubCategories = xDoumentCategories?.First()?.Elements("subcat").ToList(); - Assert.AreEqual(9, xDoumentSubCategories?.Count); - Assert.AreEqual(TorznabCatType.MoviesForeign.ID.ToString(), xDoumentSubCategories?.First().Attribute("id")?.Value); - Assert.AreEqual(TorznabCatType.MoviesForeign.Name, xDoumentSubCategories?.First().Attribute("name")?.Value); + xDocumentCategories = xDocument.Root?.Element("categories")?.Elements("category").ToList(); + Assert.AreEqual(2, xDocumentCategories?.Count); + Assert.AreEqual(TorznabCatType.Movies.ID.ToString(), xDocumentCategories?.First().Attribute("id")?.Value); + Assert.AreEqual(TorznabCatType.Movies.Name, xDocumentCategories?.First().Attribute("name")?.Value); + Assert.AreEqual(TorznabCatType.MoviesSD.ID.ToString(), xDocumentCategories?[1].Attribute("id")?.Value); + Assert.AreEqual(TorznabCatType.MoviesSD.Name, xDocumentCategories?[1].Attribute("name")?.Value); + var xDocumentSubCategories = xDocumentCategories?.First()?.Elements("subcat").ToList(); + Assert.AreEqual(9, xDocumentSubCategories?.Count); + Assert.AreEqual(TorznabCatType.MoviesForeign.ID.ToString(), xDocumentSubCategories?.First().Attribute("id")?.Value); + Assert.AreEqual(TorznabCatType.MoviesForeign.Name, xDocumentSubCategories?.First().Attribute("name")?.Value); torznabCaps = new TorznabCapabilities(); torznabCaps.Categories.AddCategoryMapping("c1", new TorznabCategory(100001, "CustomCat")); // custom category torznabCaps.Categories.AddCategoryMapping("c2", TorznabCatType.MoviesSD); xDocument = torznabCaps.GetXDocument(); - xDoumentCategories = xDocument.Root?.Element("categories")?.Elements("category").ToList(); - Assert.AreEqual(2, xDoumentCategories?.Count); - Assert.AreEqual("100001", xDoumentCategories?[0].Attribute("id")?.Value); // custom cats are first in the list - Assert.AreEqual("CustomCat", xDoumentCategories?[0].Attribute("name")?.Value); - Assert.AreEqual(TorznabCatType.MoviesSD.ID.ToString(), xDoumentCategories?[1].Attribute("id")?.Value); - Assert.AreEqual(TorznabCatType.MoviesSD.Name, xDoumentCategories?[1].Attribute("name")?.Value); + xDocumentCategories = xDocument.Root?.Element("categories")?.Elements("category").ToList(); + Assert.AreEqual(2, xDocumentCategories?.Count); + Assert.AreEqual("100001", xDocumentCategories?[0].Attribute("id")?.Value); // custom cats are first in the list + Assert.AreEqual("CustomCat", xDocumentCategories?[0].Attribute("name")?.Value); + Assert.AreEqual(TorznabCatType.MoviesSD.ID.ToString(), xDocumentCategories?[1].Attribute("id")?.Value); + Assert.AreEqual(TorznabCatType.MoviesSD.Name, xDocumentCategories?[1].Attribute("name")?.Value); + } + + [Test] + public void TestTorznabCapsCategories() + { + var torznabCaps = new TorznabCapabilities(); + TestCategories.AddTestCategories(torznabCaps.Categories); + + // test Torznab caps (XML) => more in Common.Model.TorznabCapabilitiesTests + var xDocument = torznabCaps.GetXDocument(); + var xDocumentCategories = xDocument.Root?.Element("categories")?.Elements("category").ToList(); + Assert.AreEqual(7, xDocumentCategories?.Count); + Assert.AreEqual("100044", xDocumentCategories?[0].Attribute("id")?.Value); + Assert.AreEqual("100045", xDocumentCategories?[1].Attribute("id")?.Value); + Assert.AreEqual("1030", xDocumentCategories?[2].Attribute("id")?.Value); + Assert.AreEqual("1040", xDocumentCategories?[3].Attribute("id")?.Value); + Assert.AreEqual("2000", xDocumentCategories?[4].Attribute("id")?.Value); // Movies + Assert.AreEqual("2030", xDocumentCategories?[5].Attribute("id")?.Value); + Assert.AreEqual("7030", xDocumentCategories?[6].Attribute("id")?.Value); + Assert.AreEqual(9, xDocumentCategories?[4]?.Elements("subcat").ToList().Count); // Movies } [Test] diff --git a/src/Jackett.Test/Common/Utils/ServerUtilTests.cs b/src/Jackett.Test/Common/Utils/ServerUtilTests.cs index 045b84607..65dfc1976 100644 --- a/src/Jackett.Test/Common/Utils/ServerUtilTests.cs +++ b/src/Jackett.Test/Common/Utils/ServerUtilTests.cs @@ -1,5 +1,6 @@ using Jackett.Common.Utils; using Jackett.Common.Utils.Clients; +using Jackett.Test.TestHelpers; using NUnit.Framework; namespace Jackett.Test.Common.Utils diff --git a/src/Jackett.Test/Common/Utils/TvCategoryParserTests.cs b/src/Jackett.Test/Common/Utils/TvCategoryParserTests.cs index e55e0d5cb..582c38271 100644 --- a/src/Jackett.Test/Common/Utils/TvCategoryParserTests.cs +++ b/src/Jackett.Test/Common/Utils/TvCategoryParserTests.cs @@ -1,4 +1,5 @@ using Jackett.Common.Utils; +using Jackett.Test.TestHelpers; using NUnit.Framework; namespace Jackett.Test.Common.Utils diff --git a/src/Jackett.Test/Server/Services/ProtectionServiceTests.cs b/src/Jackett.Test/Server/Services/ProtectionServiceTests.cs index 35c5f53c7..77bb83b76 100644 --- a/src/Jackett.Test/Server/Services/ProtectionServiceTests.cs +++ b/src/Jackett.Test/Server/Services/ProtectionServiceTests.cs @@ -1,6 +1,7 @@ using Autofac; using Jackett.Common.Models.Config; using Jackett.Common.Services.Interfaces; +using Jackett.Test.TestHelpers; using NUnit.Framework; namespace Jackett.Test.Server.Services diff --git a/src/Jackett.Test/TestBase.cs b/src/Jackett.Test/TestHelpers/TestBase.cs similarity index 81% rename from src/Jackett.Test/TestBase.cs rename to src/Jackett.Test/TestHelpers/TestBase.cs index 481a133ce..b406c5e53 100644 --- a/src/Jackett.Test/TestBase.cs +++ b/src/Jackett.Test/TestHelpers/TestBase.cs @@ -1,6 +1,6 @@ using NUnit.Framework; -namespace Jackett.Test +namespace Jackett.Test.TestHelpers { internal abstract class TestBase { diff --git a/src/Jackett.Test/TestHelpers/TestCategories.cs b/src/Jackett.Test/TestHelpers/TestCategories.cs new file mode 100644 index 000000000..2b96490f4 --- /dev/null +++ b/src/Jackett.Test/TestHelpers/TestCategories.cs @@ -0,0 +1,22 @@ +using Jackett.Common.Models; + +namespace Jackett.Test.TestHelpers +{ + public static class TestCategories + { + public static void AddTestCategories(TorznabCapabilitiesCategories tcc) + { + // these categories are chosen to test all kind of category types: + // - with integer and string id + // - with and without description + // - parent and child categories + // - custom categories are not added automatically but they are created from other categories automatically + tcc.AddCategoryMapping("1", TorznabCatType.Movies); + tcc.AddCategoryMapping("mov_sd", TorznabCatType.MoviesSD); + tcc.AddCategoryMapping("33", TorznabCatType.BooksComics); + tcc.AddCategoryMapping("44", TorznabCatType.ConsoleXBox, "Console/Xbox_c"); + tcc.AddCategoryMapping("con_wii", TorznabCatType.ConsoleWii, "Console/Wii_c"); + tcc.AddCategoryMapping("45", TorznabCatType.ConsoleXBox, "Console/Xbox_c2"); + } + } +} diff --git a/src/Jackett.Test/TestIIndexerManagerServiceHelper.cs b/src/Jackett.Test/TestHelpers/TestIIndexerManagerServiceHelper.cs similarity index 96% rename from src/Jackett.Test/TestIIndexerManagerServiceHelper.cs rename to src/Jackett.Test/TestHelpers/TestIIndexerManagerServiceHelper.cs index 73d0ea56a..aee453ca5 100644 --- a/src/Jackett.Test/TestIIndexerManagerServiceHelper.cs +++ b/src/Jackett.Test/TestHelpers/TestIIndexerManagerServiceHelper.cs @@ -5,7 +5,7 @@ using Jackett.Common.Indexers; using Jackett.Common.Services.Interfaces; using Newtonsoft.Json.Linq; -namespace Jackett.Test +namespace Jackett.Test.TestHelpers { internal class TestIndexerManagerServiceHelper : IIndexerManagerService { diff --git a/src/Jackett.Test/TestUtil.cs b/src/Jackett.Test/TestHelpers/TestUtil.cs similarity index 98% rename from src/Jackett.Test/TestUtil.cs rename to src/Jackett.Test/TestHelpers/TestUtil.cs index 9a18e7c8a..80e1506e1 100644 --- a/src/Jackett.Test/TestUtil.cs +++ b/src/Jackett.Test/TestHelpers/TestUtil.cs @@ -9,7 +9,7 @@ using Jackett.Common.Utils.Clients; using Microsoft.AspNetCore.DataProtection; using NLog; -namespace Jackett.Test +namespace Jackett.Test.TestHelpers { internal static class TestUtil { diff --git a/src/Jackett.Test/TestWebClient.cs b/src/Jackett.Test/TestHelpers/TestWebClient.cs similarity index 96% rename from src/Jackett.Test/TestWebClient.cs rename to src/Jackett.Test/TestHelpers/TestWebClient.cs index fe691a337..2bb7a413b 100644 --- a/src/Jackett.Test/TestWebClient.cs +++ b/src/Jackett.Test/TestHelpers/TestWebClient.cs @@ -7,7 +7,7 @@ using Jackett.Common.Services.Interfaces; using Jackett.Common.Utils.Clients; using NLog; -namespace Jackett.Test +namespace Jackett.Test.TestHelpers { // Currently not used in any Unit tests. Leaving it for potential future testing purposes. diff --git a/src/Jackett.Test/TestHelpers/TestWebIndexer.cs b/src/Jackett.Test/TestHelpers/TestWebIndexer.cs new file mode 100644 index 000000000..adf235c6b --- /dev/null +++ b/src/Jackett.Test/TestHelpers/TestWebIndexer.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Jackett.Common.Indexers; +using Jackett.Common.Models; +using Jackett.Common.Models.IndexerConfig; +using Newtonsoft.Json.Linq; + +namespace Jackett.Test.TestHelpers +{ + public class TestWebIndexer: BaseWebIndexer + { + public TestWebIndexer(): + base(id: "test_id", + name: "test_name", + description: "test_description", + link: "https://test.link/", + caps: new TorznabCapabilities(), + client: null, + configService: null, + logger: null, + configData: new ConfigurationData(), + p: null) + { + Encoding = Encoding.UTF8; + Language = "en-us"; + Type = "private"; + } + + public override string[] AlternativeSiteLinks { get; protected set; } = { + "https://test.link/", + "https://alternative-test.link/" + }; + + public override string[] LegacySiteLinks { get; protected set; } = { + "https://legacy-test.link/" + }; + + public override Task ApplyConfiguration(JToken configJson) => + throw new NotImplementedException(); + protected override Task> PerformQuery(TorznabQuery query) => + throw new NotImplementedException(); + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // public methods to test private methods + public void _AddCategoryMapping(string trackerCategory, TorznabCategory newznabCategory, string trackerCategoryDesc = null) => + AddCategoryMapping(trackerCategory, newznabCategory, trackerCategoryDesc); + + public void _AddCategoryMapping(int trackerCategory, TorznabCategory newznabCategory, string trackerCategoryDesc = null) => + AddCategoryMapping(trackerCategory, newznabCategory, trackerCategoryDesc); + + public void _AddMultiCategoryMapping(TorznabCategory newznabCategory, params int[] trackerCategories) => + AddMultiCategoryMapping(newznabCategory, trackerCategories); + + public List _MapTorznabCapsToTrackers(TorznabQuery query, bool mapChildrenCatsToParent = false) => + MapTorznabCapsToTrackers(query, mapChildrenCatsToParent); + + public ICollection _MapTrackerCatToNewznab(string input) => + MapTrackerCatToNewznab(input); + + public ICollection _MapTrackerCatDescToNewznab(string input) => + MapTrackerCatDescToNewznab(input); + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // public methods to load sample datasets + public void AddTestCategories() + { + TestCategories.AddTestCategories(TorznabCaps.Categories); + } + } +} diff --git a/src/Jackett.Test/WebUtilityHelpersTests.cs b/src/Jackett.Test/TestHelpers/WebUtilityHelpersTests.cs similarity index 99% rename from src/Jackett.Test/WebUtilityHelpersTests.cs rename to src/Jackett.Test/TestHelpers/WebUtilityHelpersTests.cs index e449a8ed9..b819295b6 100644 --- a/src/Jackett.Test/WebUtilityHelpersTests.cs +++ b/src/Jackett.Test/TestHelpers/WebUtilityHelpersTests.cs @@ -3,7 +3,7 @@ using System.Web; using Jackett.Common.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace Jackett.Test +namespace Jackett.Test.TestHelpers { [TestClass] public class WebUtilityHelpersTests