1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-30 20:05:41 +00:00
Radarr/NzbDrone.Core.Test/ProviderTests/SearchProviderTests/PerformSearchFixture.cs

265 lines
8.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 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.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);
_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.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);
_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());
_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);
}
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 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
2011-11-18 06:23:11 +00:00
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
//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]
public void PerformSearch_should_look_for_scene_name_to_search()
{
WithSceneName();
//Act
2011-11-18 06:23:11 +00:00
Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, 1, _episodes);
//Assert
2011-11-18 06:23:11 +00:00
Mocker.GetMock<SceneMappingProvider>().Verify(c => c.GetSceneName(_series.SeriesId),
Times.Once());
}
[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
2011-11-18 06:23:11 +00:00
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
//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.ExcpectedErrors(1);
}
[Test]
public void PerformSearch_for_episode_should_call_FetchEpisode()
{
//Act
2011-11-18 06:23:11 +00:00
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
//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_partial_season_should_call_FetchPartialSeason()
{
2011-11-18 06:23:11 +00:00
With30Episodes();
//Act
2011-11-18 06:23:11 +00:00
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
//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
2011-11-18 06:23:11 +00:00
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
//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
2011-11-18 06:23:11 +00:00
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
//Assert
result.Should().HaveCount(0);
2011-11-18 06:23:11 +00:00
ExceptionVerification.ExcpectedErrors(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>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
VerifyFetchEpisodeWithSceneName(Times.Once());
}
}
}