2011-11-18 05:36:53 +03:00
|
|
|
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 09:23:11 +03: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;
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
private List<IndexerBase> _indexers;
|
|
|
|
|
|
|
|
private Series _series;
|
2011-11-18 09:23:11 +03:00
|
|
|
private IList<Episode> _episodes;
|
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
2011-11-18 09:23:11 +03:00
|
|
|
var parseResults = Builder<EpisodeParseResult>.CreateListOfSize(PARSE_RESULT_COUNT)
|
2011-11-18 05:36:53 +03:00
|
|
|
.Build();
|
|
|
|
|
|
|
|
_series = Builder<Series>.CreateNew()
|
|
|
|
.Build();
|
|
|
|
|
2011-11-18 09:23:11 +03: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 05:36:53 +03:00
|
|
|
}
|
|
|
|
|
2011-11-18 09:23:11 +03:00
|
|
|
private void BuildIndexers(IList<EpisodeParseResult> parseResults)
|
2011-11-18 05:36:53 +03:00
|
|
|
{
|
|
|
|
_episodeIndexer1 = new Mock<IndexerBase>();
|
|
|
|
_episodeIndexer1.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
|
2011-11-18 09:23:11 +03: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);
|
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
_episodeIndexer2 = new Mock<IndexerBase>();
|
|
|
|
_episodeIndexer2.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
|
2011-11-18 09:23:11 +03: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);
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
_brokenIndexer = new Mock<IndexerBase>();
|
|
|
|
_brokenIndexer.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
|
2011-11-18 06:25:16 +03:00
|
|
|
.Throws(new Exception());
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
_nullIndexer = new Mock<IndexerBase>();
|
|
|
|
_nullIndexer.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
|
2011-11-18 09:23:11 +03:00
|
|
|
.Returns<List<EpisodeParseResult>>(null);
|
2011-11-18 05:36:53 +03:00
|
|
|
}
|
|
|
|
|
2011-11-18 09:23:11 +03:00
|
|
|
private void WithTwoGoodOneBrokenIndexer()
|
2011-11-18 05:36:53 +03:00
|
|
|
{
|
|
|
|
_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 09:23:11 +03:00
|
|
|
.Setup(s => s.GetSceneName(_series.SeriesId)).Returns(SCENE_NAME);
|
2011-11-18 05:36:53 +03:00
|
|
|
}
|
|
|
|
|
2011-11-18 09:23:11 +03:00
|
|
|
private void With30Episodes()
|
2011-11-18 05:36:53 +03:00
|
|
|
{
|
|
|
|
_episodes = Builder<Episode>.CreateListOfSize(30)
|
|
|
|
.Build();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WithNullEpisodes()
|
|
|
|
{
|
|
|
|
_episodes = null;
|
|
|
|
}
|
|
|
|
|
2011-11-18 06:25:16 +03:00
|
|
|
private void VerifyFetchEpisode(Times times)
|
|
|
|
{
|
2011-11-18 09:23:11 +03: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 06:25:16 +03:00
|
|
|
, times);
|
|
|
|
|
2011-11-18 09:23:11 +03:00
|
|
|
_episodeIndexer2.Verify(v => v.FetchEpisode(SCENE_NAME, SEASON_NUMBER, It.IsAny<int>())
|
2011-11-18 06:25:16 +03:00
|
|
|
, times);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void VerifyFetchEpisodeBrokenIndexer(Times times)
|
|
|
|
{
|
2011-11-18 09:23:11 +03:00
|
|
|
_brokenIndexer.Verify(v => v.FetchEpisode(It.IsAny<string>(), SEASON_NUMBER, It.IsAny<int>())
|
2011-11-18 06:25:16 +03:00
|
|
|
, times);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void VerifyFetchPartialSeason(Times times)
|
|
|
|
{
|
2011-11-18 09:23:11 +03:00
|
|
|
_episodeIndexer1.Verify(v => v.FetchPartialSeason(_series.Title, SEASON_NUMBER, It.IsAny<int>())
|
2011-11-18 06:25:16 +03:00
|
|
|
, times);
|
|
|
|
|
2011-11-18 09:23:11 +03:00
|
|
|
_episodeIndexer2.Verify(v => v.FetchPartialSeason(_series.Title, SEASON_NUMBER, It.IsAny<int>())
|
2011-11-18 06:25:16 +03:00
|
|
|
, times);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void VerifyFetchSeason(Times times)
|
|
|
|
{
|
2011-11-18 09:23:11 +03:00
|
|
|
_episodeIndexer1.Verify(v => v.FetchSeason(_series.Title, SEASON_NUMBER), times);
|
|
|
|
_episodeIndexer1.Verify(v => v.FetchSeason(_series.Title, SEASON_NUMBER), times);
|
2011-11-18 06:25:16 +03:00
|
|
|
}
|
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
[Test]
|
|
|
|
public void PerformSearch_should_search_all_enabled_providers()
|
|
|
|
{
|
|
|
|
//Act
|
2011-11-18 09:23:11 +03:00
|
|
|
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Assert
|
2011-11-18 06:25:16 +03:00
|
|
|
VerifyFetchEpisode(Times.Once());
|
2011-11-18 09:23:11 +03:00
|
|
|
result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
|
2011-11-18 05:36:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void PerformSearch_should_look_for_scene_name_to_search()
|
|
|
|
{
|
|
|
|
WithSceneName();
|
|
|
|
|
|
|
|
//Act
|
2011-11-18 09:23:11 +03:00
|
|
|
Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, 1, _episodes);
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Assert
|
2011-11-18 09:23:11 +03:00
|
|
|
Mocker.GetMock<SceneMappingProvider>().Verify(c => c.GetSceneName(_series.SeriesId),
|
2011-11-18 05:36:53 +03:00
|
|
|
Times.Once());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2011-11-18 09:23:11 +03:00
|
|
|
public void broken_indexer_should_not_block_other_indexers()
|
2011-11-18 05:36:53 +03:00
|
|
|
{
|
|
|
|
//Setup
|
2011-11-18 09:23:11 +03:00
|
|
|
WithTwoGoodOneBrokenIndexer();
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Act
|
2011-11-18 09:23:11 +03:00
|
|
|
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Assert
|
2011-11-18 09:23:11 +03:00
|
|
|
result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
|
|
|
|
|
2011-11-18 06:25:16 +03:00
|
|
|
VerifyFetchEpisode(Times.Once());
|
|
|
|
VerifyFetchEpisodeBrokenIndexer(Times.Once());
|
2011-11-18 05:36:53 +03:00
|
|
|
|
2011-11-18 09:23:11 +03:00
|
|
|
Mocker.GetMock<SceneMappingProvider>().Verify(c => c.GetSceneName(_series.SeriesId),
|
2011-11-18 05:36:53 +03:00
|
|
|
Times.Once());
|
2011-11-18 09:23:11 +03:00
|
|
|
|
|
|
|
ExceptionVerification.ExcpectedErrors(1);
|
2011-11-18 05:36:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void PerformSearch_for_episode_should_call_FetchEpisode()
|
|
|
|
{
|
|
|
|
//Act
|
2011-11-18 09:23:11 +03:00
|
|
|
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Assert
|
2011-11-18 09:23:11 +03:00
|
|
|
result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
|
2011-11-18 05:36:53 +03:00
|
|
|
|
2011-11-18 06:25:16 +03:00
|
|
|
VerifyFetchEpisode(Times.Once());
|
2011-11-18 05:36:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void PerformSearch_for_partial_season_should_call_FetchPartialSeason()
|
|
|
|
{
|
2011-11-18 09:23:11 +03:00
|
|
|
With30Episodes();
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Act
|
2011-11-18 09:23:11 +03:00
|
|
|
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
result.Should().HaveCount(80);
|
2011-11-18 06:25:16 +03:00
|
|
|
VerifyFetchPartialSeason(Times.Exactly(4));
|
2011-11-18 05:36:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void PerformSearch_for_season_should_call_FetchSeason()
|
|
|
|
{
|
|
|
|
WithNullEpisodes();
|
|
|
|
|
|
|
|
//Act
|
2011-11-18 09:23:11 +03:00
|
|
|
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
result.Should().HaveCount(20);
|
2011-11-18 06:25:16 +03:00
|
|
|
VerifyFetchSeason(Times.Once());
|
2011-11-18 05:36:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void PerformSearch_should_return_empty_list_when_results_from_indexers_are_null()
|
|
|
|
{
|
|
|
|
//Setup
|
|
|
|
WithNullIndexers();
|
|
|
|
|
|
|
|
//Act
|
2011-11-18 09:23:11 +03:00
|
|
|
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
result.Should().HaveCount(0);
|
2011-11-18 09:23:11 +03:00
|
|
|
ExceptionVerification.ExcpectedErrors(2);
|
2011-11-18 05:36:53 +03:00
|
|
|
}
|
2011-11-18 09:23:11 +03: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());
|
|
|
|
}
|
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
}
|
|
|
|
}
|