sonarr-repo-only/NzbDrone.Core.Test/ProviderTests/SearchProviderTests/PerformSearchFixture.cs

283 lines
9.9 KiB
C#
Raw Normal View History

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.Indexer;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests
{
public class PerformSearchFixture : CoreTest
{
2011-11-18 06:23:11 +00:00
private const string SCENE_NAME = "Scene Name";
private const int SEASON_NUMBER = 5;
private const int EPISODE_NUMBER = 1;
2011-11-18 06:23:11 +00:00
private const int PARSE_RESULT_COUNT = 10;
private Mock<IndexerBase> _episodeIndexer1;
private Mock<IndexerBase> _episodeIndexer2;
private Mock<IndexerBase> _brokenIndexer;
private Mock<IndexerBase> _nullIndexer;
private List<IndexerBase> _indexers;
private Series _series;
2011-11-18 06:23:11 +00:00
private IList<Episode> _episodes;
[SetUp]
public void Setup()
{
2011-11-18 06:23:11 +00:00
var parseResults = Builder<EpisodeParseResult>.CreateListOfSize(PARSE_RESULT_COUNT)
.Build();
_series = Builder<Series>.CreateNew()
.Build();
2011-11-18 06:23:11 +00:00
_episodes = Builder<Episode>.CreateListOfSize(1)
.Build();
BuildIndexers(parseResults);
_indexers = new List<IndexerBase> { _episodeIndexer1.Object, _episodeIndexer2.Object };
Mocker.GetMock<IndexerProvider>()
.Setup(c => c.GetEnabledIndexers())
.Returns(_indexers);
}
2011-11-18 06:23:11 +00:00
private void BuildIndexers(IList<EpisodeParseResult> parseResults)
{
_episodeIndexer1 = new Mock<IndexerBase>();
_episodeIndexer1.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
2011-11-18 06:23:11 +00:00
.Returns(parseResults);
_episodeIndexer1.Setup(c => c.FetchDailyEpisode(It.IsAny<string>(), It.IsAny<DateTime>()))
.Returns(parseResults);
2011-11-18 06:23:11 +00:00
_episodeIndexer1.Setup(c => c.FetchSeason(It.IsAny<string>(), It.IsAny<int>()))
.Returns(parseResults);
_episodeIndexer1.Setup(c => c.FetchPartialSeason(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.Returns(parseResults);
2012-12-20 22:00:57 +00:00
_episodeIndexer1.Setup(s => s.Name).Returns("Episode Indexer 1");
_episodeIndexer2 = new Mock<IndexerBase>();
_episodeIndexer2.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
2011-11-18 06:23:11 +00:00
.Returns(parseResults);
_episodeIndexer2.Setup(c => c.FetchDailyEpisode(It.IsAny<string>(), It.IsAny<DateTime>()))
.Returns(parseResults);
2011-11-18 06:23:11 +00:00
_episodeIndexer2.Setup(c => c.FetchSeason(It.IsAny<string>(), It.IsAny<int>()))
.Returns(parseResults);
_episodeIndexer2.Setup(c => c.FetchPartialSeason(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.Returns(parseResults);
2012-12-20 22:00:57 +00:00
_episodeIndexer2.Setup(s => s.Name).Returns("Episode Indexer 2");
_brokenIndexer = new Mock<IndexerBase>();
_brokenIndexer.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
2011-11-18 03:25:16 +00:00
.Throws(new Exception());
2012-12-20 22:00:57 +00:00
_brokenIndexer.Setup(s => s.Name).Returns("Broken Indexer");
_nullIndexer = new Mock<IndexerBase>();
_nullIndexer.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
2011-11-18 06:23:11 +00:00
.Returns<List<EpisodeParseResult>>(null);
2012-12-20 22:00:57 +00:00
_nullIndexer.Setup(s => s.Name).Returns("Null Indexer");
}
2011-11-18 06:23:11 +00:00
private void WithTwoGoodOneBrokenIndexer()
{
_indexers = new List<IndexerBase> { _episodeIndexer1.Object, _brokenIndexer.Object, _episodeIndexer2.Object };
Mocker.GetMock<IndexerProvider>()
.Setup(c => c.GetEnabledIndexers())
.Returns(_indexers);
}
private void WithNullIndexers()
{
_indexers = new List<IndexerBase> { _nullIndexer.Object, _nullIndexer.Object };
Mocker.GetMock<IndexerProvider>()
.Setup(c => c.GetEnabledIndexers())
.Returns(_indexers);
}
private void WithSceneName()
{
Mocker.GetMock<SceneMappingProvider>()
2011-11-18 06:23:11 +00:00
.Setup(s => s.GetSceneName(_series.SeriesId)).Returns(SCENE_NAME);
}
2011-11-18 06:23:11 +00:00
private void With30Episodes()
{
_episodes = Builder<Episode>.CreateListOfSize(30)
.Build();
}
private void WithNullEpisodes()
{
_episodes = null;
}
2011-11-18 03:25:16 +00:00
private void VerifyFetchEpisode(Times times)
{
2011-11-18 06:23:11 +00:00
_episodeIndexer1.Verify(v => v.FetchEpisode(_series.Title, SEASON_NUMBER, It.IsAny<int>())
, times);
_episodeIndexer2.Verify(v => v.FetchEpisode(_series.Title, SEASON_NUMBER, It.IsAny<int>())
, times);
}
private void VerifyFetchDailyEpisode(Times times)
{
_episodeIndexer1.Verify(v => v.FetchDailyEpisode(_series.Title, It.IsAny<DateTime>())
, times);
_episodeIndexer2.Verify(v => v.FetchDailyEpisode(_series.Title, It.IsAny<DateTime>())
, times);
}
2011-11-18 06:23:11 +00:00
private void VerifyFetchEpisodeWithSceneName(Times times)
{
_episodeIndexer1.Verify(v => v.FetchEpisode(SCENE_NAME, SEASON_NUMBER, It.IsAny<int>())
2011-11-18 03:25:16 +00:00
, times);
2011-11-18 06:23:11 +00:00
_episodeIndexer2.Verify(v => v.FetchEpisode(SCENE_NAME, SEASON_NUMBER, It.IsAny<int>())
2011-11-18 03:25:16 +00:00
, times);
}
private void VerifyFetchEpisodeBrokenIndexer(Times times)
{
2011-11-18 06:23:11 +00:00
_brokenIndexer.Verify(v => v.FetchEpisode(It.IsAny<string>(), SEASON_NUMBER, It.IsAny<int>())
2011-11-18 03:25:16 +00:00
, times);
}
private void VerifyFetchPartialSeason(Times times)
{
2011-11-18 06:23:11 +00:00
_episodeIndexer1.Verify(v => v.FetchPartialSeason(_series.Title, SEASON_NUMBER, It.IsAny<int>())
2011-11-18 03:25:16 +00:00
, times);
2011-11-18 06:23:11 +00:00
_episodeIndexer2.Verify(v => v.FetchPartialSeason(_series.Title, SEASON_NUMBER, It.IsAny<int>())
2011-11-18 03:25:16 +00:00
, times);
}
private void VerifyFetchSeason(Times times)
{
2011-11-18 06:23:11 +00:00
_episodeIndexer1.Verify(v => v.FetchSeason(_series.Title, SEASON_NUMBER), times);
_episodeIndexer1.Verify(v => v.FetchSeason(_series.Title, SEASON_NUMBER), times);
2011-11-18 03:25:16 +00:00
}
[Test]
public void PerformSearch_should_search_all_enabled_providers()
{
//Act
var result = Mocker.Resolve<SearchProvider>().PerformEpisodeSearch(_series, SEASON_NUMBER, _episodes.First().EpisodeNumber);
//Assert
2011-11-18 03:25:16 +00:00
VerifyFetchEpisode(Times.Once());
2011-11-18 06:23:11 +00:00
result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
}
[Test]
2011-11-18 06:23:11 +00:00
public void broken_indexer_should_not_block_other_indexers()
{
//Setup
2011-11-18 06:23:11 +00:00
WithTwoGoodOneBrokenIndexer();
//Act
var result = Mocker.Resolve<SearchProvider>().PerformEpisodeSearch(_series, SEASON_NUMBER, EPISODE_NUMBER);
//Assert
2011-11-18 06:23:11 +00:00
result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
2011-11-18 03:25:16 +00:00
VerifyFetchEpisode(Times.Once());
VerifyFetchEpisodeBrokenIndexer(Times.Once());
2011-11-18 06:23:11 +00:00
Mocker.GetMock<SceneMappingProvider>().Verify(c => c.GetSceneName(_series.SeriesId),
Times.Once());
2011-11-18 06:23:11 +00:00
ExceptionVerification.ExpectedErrors(1);
}
[Test]
public void PerformSearch_for_episode_should_call_FetchEpisode()
{
//Act
var result = Mocker.Resolve<SearchProvider>().PerformEpisodeSearch(_series, SEASON_NUMBER, EPISODE_NUMBER);
//Assert
2011-11-18 06:23:11 +00:00
result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
2011-11-18 03:25:16 +00:00
VerifyFetchEpisode(Times.Once());
}
[Test]
public void PerformSearch_for_daily_episode_should_call_FetchEpisode()
{
//Setup
_series.IsDaily = true;
//Act
var result = Mocker.Resolve<SearchProvider>().PerformDailyEpisodeSearch(_series, _episodes.First());
//Assert
result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
VerifyFetchDailyEpisode(Times.Once());
}
[Test]
public void PerformSearch_for_partial_season_should_call_FetchPartialSeason()
{
2011-11-18 06:23:11 +00:00
With30Episodes();
//Act
var result = Mocker.Resolve<SearchProvider>().PerformPartialSeasonSearch(_series, SEASON_NUMBER, new List<int>{0, 1, 2, 3});
//Assert
result.Should().HaveCount(80);
2011-11-18 03:25:16 +00:00
VerifyFetchPartialSeason(Times.Exactly(4));
}
[Test]
public void PerformSearch_for_season_should_call_FetchSeason()
{
WithNullEpisodes();
//Act
var result = Mocker.Resolve<SearchProvider>().PerformSeasonSearch(_series, SEASON_NUMBER);
//Assert
result.Should().HaveCount(20);
2011-11-18 03:25:16 +00:00
VerifyFetchSeason(Times.Once());
}
[Test]
public void PerformSearch_should_return_empty_list_when_results_from_indexers_are_null()
{
//Setup
WithNullIndexers();
//Act
var result = Mocker.Resolve<SearchProvider>().PerformEpisodeSearch(_series, SEASON_NUMBER, EPISODE_NUMBER);
//Assert
result.Should().HaveCount(0);
ExceptionVerification.ExpectedErrors(2);
}
2011-11-18 06:23:11 +00:00
[Test]
public void should_use_scene_name_to_search_for_episode_when_avilable()
{
WithSceneName();
Mocker.Resolve<SearchProvider>().PerformEpisodeSearch(_series, SEASON_NUMBER, EPISODE_NUMBER);
2011-11-18 06:23:11 +00:00
VerifyFetchEpisodeWithSceneName(Times.Once());
}
}
}