diff --git a/src/NzbDrone.Core.Test/IndexerSearchTests/ReleaseSearchServiceFixture.cs b/src/NzbDrone.Core.Test/IndexerSearchTests/ReleaseSearchServiceFixture.cs new file mode 100644 index 000000000..d253e9157 --- /dev/null +++ b/src/NzbDrone.Core.Test/IndexerSearchTests/ReleaseSearchServiceFixture.cs @@ -0,0 +1,161 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Core.DecisionEngine; +using NzbDrone.Core.Indexers; +using NzbDrone.Core.IndexerSearch; +using NzbDrone.Core.IndexerSearch.Definitions; +using NzbDrone.Core.Movies; +using NzbDrone.Core.Movies.Translations; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.IndexerSearchTests +{ + public class ReleaseSearchServiceFixture : CoreTest + { + private Mock _mockIndexer; + private Movie _movie; + + [SetUp] + public void SetUp() + { + _mockIndexer = Mocker.GetMock(); + _mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition { Id = 1 }); + _mockIndexer.SetupGet(s => s.SupportsSearch).Returns(true); + + Mocker.GetMock() + .Setup(s => s.AutomaticSearchEnabled(true)) + .Returns(new List { _mockIndexer.Object }); + + Mocker.GetMock() + .Setup(s => s.GetSearchDecision(It.IsAny>(), It.IsAny())) + .Returns(new List()); + + _movie = Builder.CreateNew() + .With(v => v.Monitored = true) + .Build(); + + Mocker.GetMock() + .Setup(v => v.GetMovie(_movie.Id)) + .Returns(_movie); + + Mocker.GetMock() + .Setup(s => s.GetAllTranslationsForMovie(It.IsAny())) + .Returns(new List()); + } + + private List WatchForSearchCriteria() + { + var result = new List(); + + _mockIndexer.Setup(v => v.Fetch(It.IsAny())) + .Callback(s => result.Add(s)) + .Returns(new List()); + + return result; + } + + [Test] + public void Tags_IndexerTags_MovieNoTags_IndexerNotIncluded() + { + _mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition + { + Id = 1, + Tags = new HashSet { 3 } + }); + + var allCriteria = WatchForSearchCriteria(); + + Subject.MovieSearch(_movie, true, false); + + var criteria = allCriteria.OfType().ToList(); + + criteria.Count.Should().Be(0); + } + + [Test] + public void Tags_IndexerNoTags_MovieTags_IndexerIncluded() + { + _mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition + { + Id = 1 + }); + + _movie = Builder.CreateNew() + .With(v => v.Monitored = true) + .With(v => v.Tags = new HashSet { 3 }) + .Build(); + + Mocker.GetMock() + .Setup(v => v.GetMovie(_movie.Id)) + .Returns(_movie); + + var allCriteria = WatchForSearchCriteria(); + + Subject.MovieSearch(_movie, true, false); + + var criteria = allCriteria.OfType().ToList(); + + criteria.Count.Should().Be(1); + } + + [Test] + public void Tags_IndexerAndMovieTagsMatch_IndexerIncluded() + { + _mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition + { + Id = 1, + Tags = new HashSet { 1, 2, 3 } + }); + + _movie = Builder.CreateNew() + .With(v => v.Monitored = true) + .With(v => v.Tags = new HashSet { 3, 4, 5 }) + .Build(); + + Mocker.GetMock() + .Setup(v => v.GetMovie(_movie.Id)) + .Returns(_movie); + + var allCriteria = WatchForSearchCriteria(); + + Subject.MovieSearch(_movie, true, false); + + var criteria = allCriteria.OfType().ToList(); + + criteria.Count.Should().Be(1); + } + + [Test] + public void Tags_IndexerAndMovieTagsMismatch_IndexerNotIncluded() + { + _mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition + { + Id = 1, + Tags = new HashSet { 1, 2, 3 } + }); + + _movie = Builder.CreateNew() + .With(v => v.Monitored = true) + .With(v => v.Tags = new HashSet { 4, 5, 6 }) + .Build(); + + Mocker.GetMock() + .Setup(v => v.GetMovie(_movie.Id)) + .Returns(_movie); + + var allCriteria = WatchForSearchCriteria(); + + Subject.MovieSearch(_movie, true, false); + + var criteria = allCriteria.OfType().ToList(); + + criteria.Count.Should().Be(0); + } + } +} diff --git a/src/NzbDrone.Core/IndexerSearch/MoviesSearchService.cs b/src/NzbDrone.Core/IndexerSearch/MoviesSearchService.cs index bc7e2c946..ae0e82348 100644 --- a/src/NzbDrone.Core/IndexerSearch/MoviesSearchService.cs +++ b/src/NzbDrone.Core/IndexerSearch/MoviesSearchService.cs @@ -16,21 +16,21 @@ public class MovieSearchService : IExecute, IExecute movies, bool userInvokedSearch) try { - decisions = _nzbSearchService.MovieSearch(movieId.Key, userInvokedSearch, false); + decisions = _releaseSearchService.MovieSearch(movieId.Key, userInvokedSearch, false); } catch (Exception ex) { diff --git a/src/NzbDrone.Core/IndexerSearch/NzbSearchService.cs b/src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs similarity index 97% rename from src/NzbDrone.Core/IndexerSearch/NzbSearchService.cs rename to src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs index f4ed2715e..7ce8ddc66 100644 --- a/src/NzbDrone.Core/IndexerSearch/NzbSearchService.cs +++ b/src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs @@ -16,13 +16,13 @@ namespace NzbDrone.Core.IndexerSearch { - public interface ISearchForNzb + public interface ISearchForReleases { List MovieSearch(int movieId, bool userInvokedSearch, bool interactiveSearch); List MovieSearch(Movie movie, bool userInvokedSearch, bool interactiveSearch); } - public class NzbSearchService : ISearchForNzb + public class ReleaseSearchService : ISearchForReleases { private readonly IIndexerFactory _indexerFactory; private readonly IMakeDownloadDecision _makeDownloadDecision; @@ -31,7 +31,7 @@ public class NzbSearchService : ISearchForNzb private readonly IProfileService _profileService; private readonly Logger _logger; - public NzbSearchService(IIndexerFactory indexerFactory, + public ReleaseSearchService(IIndexerFactory indexerFactory, IMakeDownloadDecision makeDownloadDecision, IMovieService movieService, IMovieTranslationService movieTranslationService, diff --git a/src/Radarr.Api.V3/Indexers/ReleaseController.cs b/src/Radarr.Api.V3/Indexers/ReleaseController.cs index 73c41f7e4..520c3d8fd 100644 --- a/src/Radarr.Api.V3/Indexers/ReleaseController.cs +++ b/src/Radarr.Api.V3/Indexers/ReleaseController.cs @@ -22,7 +22,7 @@ namespace Radarr.Api.V3.Indexers public class ReleaseController : ReleaseControllerBase { private readonly IFetchAndParseRss _rssFetcherAndParser; - private readonly ISearchForNzb _nzbSearchService; + private readonly ISearchForReleases _releaseSearchService; private readonly IMakeDownloadDecision _downloadDecisionMaker; private readonly IPrioritizeDownloadDecision _prioritizeDownloadDecision; private readonly IDownloadService _downloadService; @@ -32,7 +32,7 @@ public class ReleaseController : ReleaseControllerBase private readonly ICached _remoteMovieCache; public ReleaseController(IFetchAndParseRss rssFetcherAndParser, - ISearchForNzb nzbSearchService, + ISearchForReleases releaseSearchService, IMakeDownloadDecision downloadDecisionMaker, IPrioritizeDownloadDecision prioritizeDownloadDecision, IDownloadService downloadService, @@ -43,7 +43,7 @@ public ReleaseController(IFetchAndParseRss rssFetcherAndParser, : base(qualityProfileService) { _rssFetcherAndParser = rssFetcherAndParser; - _nzbSearchService = nzbSearchService; + _releaseSearchService = releaseSearchService; _downloadDecisionMaker = downloadDecisionMaker; _prioritizeDownloadDecision = prioritizeDownloadDecision; _downloadService = downloadService; @@ -110,7 +110,7 @@ private List GetMovieReleases(int movieId) { try { - var decisions = _nzbSearchService.MovieSearch(movieId, true, true); + var decisions = _releaseSearchService.MovieSearch(movieId, true, true); var prioritizedDecisions = _prioritizeDownloadDecision.PrioritizeDecisionsForMovies(decisions); return MapDecisions(prioritizedDecisions);