Skip to content

Commit

Permalink
core: rearrange test classes (#10023)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngosang committed Oct 31, 2020
1 parent 3d27e18 commit 15ea7ed
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 169 deletions.
2 changes: 2 additions & 0 deletions src/Jackett.Common/Indexers/BaseIndexer.cs
Expand Up @@ -583,9 +583,11 @@ private async Task DoFollowIfRedirect(WebResult incomingResponse, string referre
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)
Expand Down
104 changes: 104 additions & 0 deletions 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]);
}
}
}
@@ -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<IndexerConfigurationStatus> ApplyConfiguration(JToken configJson) => throw new NotImplementedException();
protected override Task<IEnumerable<ReleaseInfo>> 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()
{
Expand Down
53 changes: 53 additions & 0 deletions 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);
}
}
}
@@ -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
{
Expand Down Expand Up @@ -220,12 +225,7 @@ public void TestConcat()
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;
}
}
Expand Down

0 comments on commit 15ea7ed

Please sign in to comment.