using System; using System.Collections.Generic; using System.Linq; using FizzWare.NBuilder; using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; using NzbDrone.Core.Providers.DecisionEngine; using NzbDrone.Core.Providers.Indexer; using NzbDrone.Core.Repository; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests { public class SearchFixture : CoreTest { private const string SCENE_NAME = "Scene Name"; private const int SEASON_NUMBER = 5; private const int PARSE_RESULT_COUNT = 10; private Mock _episodeIndexer1; private Mock _episodeIndexer2; private Mock _brokenIndexer; private Mock _nullIndexer; private List _indexers; private Series _series; private IList _episodes; [SetUp] public void Setup() { var parseResults = Builder.CreateListOfSize(PARSE_RESULT_COUNT) .Build(); _series = Builder.CreateNew() .Build(); _episodes = Builder.CreateListOfSize(1) .Build(); BuildIndexers(parseResults); _indexers = new List { _episodeIndexer1.Object, _episodeIndexer2.Object }; Mocker.GetMock() .Setup(c => c.GetEnabledIndexers()) .Returns(_indexers); } private void BuildIndexers(IList parseResults) { _episodeIndexer1 = new Mock(); _episodeIndexer1.Setup(c => c.FetchEpisode(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(parseResults); _episodeIndexer1.Setup(c => c.FetchSeason(It.IsAny(), It.IsAny())) .Returns(parseResults); _episodeIndexer1.Setup(c => c.FetchPartialSeason(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(parseResults); _episodeIndexer2 = new Mock(); _episodeIndexer2.Setup(c => c.FetchEpisode(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(parseResults); _episodeIndexer2.Setup(c => c.FetchSeason(It.IsAny(), It.IsAny())) .Returns(parseResults); _episodeIndexer2.Setup(c => c.FetchPartialSeason(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(parseResults); _brokenIndexer = new Mock(); _brokenIndexer.Setup(c => c.FetchEpisode(It.IsAny(), It.IsAny(), It.IsAny())) .Throws(new Exception()); _nullIndexer = new Mock(); _nullIndexer.Setup(c => c.FetchEpisode(It.IsAny(), It.IsAny(), It.IsAny())) .Returns>(null); } private void WithTwoGoodOneBrokenIndexer() { _indexers = new List { _episodeIndexer1.Object, _brokenIndexer.Object, _episodeIndexer2.Object }; Mocker.GetMock() .Setup(c => c.GetEnabledIndexers()) .Returns(_indexers); } private void WithNullIndexers() { _indexers = new List { _nullIndexer.Object, _nullIndexer.Object }; Mocker.GetMock() .Setup(c => c.GetEnabledIndexers()) .Returns(_indexers); } private void WithSceneName() { Mocker.GetMock() .Setup(s => s.GetSceneName(_series.SeriesId)).Returns(SCENE_NAME); } private void With30Episodes() { _episodes = Builder.CreateListOfSize(30) .Build(); } private void WithNullEpisodes() { _episodes = null; } private void VerifyFetchEpisode(Times times) { _episodeIndexer1.Verify(v => v.FetchEpisode(_series.Title, SEASON_NUMBER, It.IsAny()) , times); _episodeIndexer2.Verify(v => v.FetchEpisode(_series.Title, SEASON_NUMBER, It.IsAny()) , times); } private void VerifyFetchEpisodeWithSceneName(Times times) { _episodeIndexer1.Verify(v => v.FetchEpisode(SCENE_NAME, SEASON_NUMBER, It.IsAny()) , times); _episodeIndexer2.Verify(v => v.FetchEpisode(SCENE_NAME, SEASON_NUMBER, It.IsAny()) , times); } private void VerifyFetchEpisodeBrokenIndexer(Times times) { _brokenIndexer.Verify(v => v.FetchEpisode(It.IsAny(), SEASON_NUMBER, It.IsAny()) , times); } private void VerifyFetchPartialSeason(Times times) { _episodeIndexer1.Verify(v => v.FetchPartialSeason(_series.Title, SEASON_NUMBER, It.IsAny()) , times); _episodeIndexer2.Verify(v => v.FetchPartialSeason(_series.Title, SEASON_NUMBER, It.IsAny()) , times); } private void VerifyFetchSeason(Times times) { _episodeIndexer1.Verify(v => v.FetchSeason(_series.Title, SEASON_NUMBER), times); _episodeIndexer1.Verify(v => v.FetchSeason(_series.Title, SEASON_NUMBER), times); } [Test] public void SeasonSearch_should_skip_daily_series() { //Setup _series.IsDaily = true; Mocker.GetMock().Setup(s => s.GetSeries(1)).Returns(_series); //Act var result = Mocker.Resolve().SeasonSearch(MockNotification, _series.SeriesId, 1); //Assert result.Should().BeEmpty(); } [Test] public void PartialSeasonSearch_should_skip_daily_series() { //Setup _series.IsDaily = true; Mocker.GetMock().Setup(s => s.GetSeries(1)).Returns(_series); //Act var result = Mocker.Resolve().PartialSeasonSearch(MockNotification, _series.SeriesId, 1); //Assert result.Should().BeEmpty(); } [Test] public void EpisodeSearch_should_skip_if_air_date_is_null_and_is_a_daily_series() { //Setup _series.IsDaily = true; var episode = _episodes.First(); episode.AirDate = null; episode.Series = _series; Mocker.GetMock().Setup(s => s.IsSatisfiedBy(It.IsAny())) .Returns(true); Mocker.GetMock().Setup(s => s.GetEpisode(episode.EpisodeId)) .Returns(episode); //Act var result = Mocker.Resolve().EpisodeSearch(MockNotification, episode.EpisodeId); //Assert result.Should().BeFalse(); ExceptionVerification.ExpectedWarns(1); } [Test] public void EpisodeSearch_should_skip_if_sceneNumbering_is_invalid_and_should_use_sceneNumbering() { //Setup _series.UseSceneNumbering = true; var episode = _episodes.First(); episode.SceneSeasonNumber = 0; episode.SceneEpisodeNumber = 0; episode.Series = _series; Mocker.GetMock().Setup(s => s.IsSatisfiedBy(It.IsAny())) .Returns(true); Mocker.GetMock().Setup(s => s.GetEpisode(episode.EpisodeId)) .Returns(episode); //Act var result = Mocker.Resolve().EpisodeSearch(MockNotification, episode.EpisodeId); //Assert result.Should().BeFalse(); ExceptionVerification.ExpectedWarns(1); } } }