From 5a812801b7fb92423140c260e7a6f4294bd01d05 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 26 May 2011 20:07:32 -0700 Subject: [PATCH] EpisodeSearchJob bug fixes/tests --- NzbDrone.Core.Test/EpisodeSearchJobTest.cs | 153 ++++++++++++++++++ NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 + NzbDrone.Core/Providers/InventoryProvider.cs | 11 +- .../Providers/Jobs/EpisodeSearchJob.cs | 2 +- 4 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 NzbDrone.Core.Test/EpisodeSearchJobTest.cs diff --git a/NzbDrone.Core.Test/EpisodeSearchJobTest.cs b/NzbDrone.Core.Test/EpisodeSearchJobTest.cs new file mode 100644 index 000000000..4bc9bb0f3 --- /dev/null +++ b/NzbDrone.Core.Test/EpisodeSearchJobTest.cs @@ -0,0 +1,153 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using AutoMoq; +using FizzWare.NBuilder; +using Gallio.Framework; +using MbUnit.Framework; +using MbUnit.Framework.ContractVerifiers; +using Moq; +using NzbDrone.Core.Model; +using NzbDrone.Core.Model.Notification; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Providers.Jobs; +using NzbDrone.Core.Repository; +using NzbDrone.Core.Repository.Quality; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test +{ + [TestFixture] + // ReSharper disable InconsistentNaming + public class EpisodeSearchJobTest : TestBase + { + [Test] + public void ParseResult_should_return_after_match() + { + var parseResults = Builder.CreateListOfSize(5) + .Build(); + + var episode = Builder.CreateNew().Build(); + + var mocker = new AutoMoqer(MockBehavior.Strict); + + mocker.GetMock() + .Setup(c => c.IsNeeded(It.IsAny())).Returns(true) + .AtMostOnce(); + + mocker.GetMock() + .Setup(c => c.DownloadReport(It.IsAny())).Returns(true) + .AtMostOnce(); + + mocker.Resolve().ProcessResults(new ProgressNotification("Test"), episode, parseResults); + + + mocker.VerifyAllMocks(); + } + + + [Test] + public void higher_quality_should_be_called_first() + { + var parseResults = Builder.CreateListOfSize(2) + .WhereTheFirst(1).Has(c => c.Quality = QualityTypes.Bluray1080p) + .AndTheNext(1).Has(c => c.Quality = QualityTypes.DVD) + .Build(); + + var episode = Builder.CreateNew().Build(); + + var mocker = new AutoMoqer(MockBehavior.Strict); + + mocker.GetMock() + .Setup(c => c.IsNeeded(parseResults[0])).Returns(true) + .AtMostOnce(); + + mocker.GetMock() + .Setup(c => c.DownloadReport(parseResults[0])).Returns(true) + .AtMostOnce(); + + mocker.Resolve().ProcessResults(new ProgressNotification("Test"), episode, parseResults); + + + mocker.VerifyAllMocks(); + } + + + [Test] + public void when_same_quality_proper_should_be_called_first() + { + var parseResults = Builder.CreateListOfSize(20) + .WhereAll().Have(c => c.Quality = QualityTypes.DVD) + .And(c => c.Proper = false) + .WhereRandom(1).Has(c => c.Proper = true) + .Build(); + + Assert.Count(1, parseResults.Where(c => c.Proper)); + + var episode = Builder.CreateNew().Build(); + + var mocker = new AutoMoqer(MockBehavior.Strict); + + mocker.GetMock() + .Setup(c => c.IsNeeded(It.Is(p => p.Proper))).Returns(true) + .AtMostOnce(); + + mocker.GetMock() + .Setup(c => c.DownloadReport(It.Is(p => p.Proper))).Returns(true) + .AtMostOnce(); + + mocker.Resolve().ProcessResults(new ProgressNotification("Test"), episode, parseResults); + + + mocker.VerifyAllMocks(); + } + + + [Test] + public void when_not_needed_should_check_the_rest() + { + var parseResults = Builder.CreateListOfSize(4) + .Build(); + + var episode = Builder.CreateNew().Build(); + + var mocker = new AutoMoqer(MockBehavior.Strict); + + mocker.GetMock() + .Setup(c => c.IsNeeded(It.IsAny())).Returns(false); + + mocker.Resolve().ProcessResults(new ProgressNotification("Test"), episode, parseResults); + + + mocker.VerifyAllMocks(); + mocker.GetMock().Verify(c => c.IsNeeded(It.IsAny()), Times.Exactly(4)); + + ExceptionVerification.ExcpectedWarns(1); + } + + + [Test] + public void failed_is_neede_should_check_the_rest() + { + var parseResults = Builder.CreateListOfSize(4) + .Build(); + + var episode = Builder.CreateNew().Build(); + + var mocker = new AutoMoqer(MockBehavior.Strict); + + mocker.GetMock() + .Setup(c => c.IsNeeded(It.IsAny())).Throws(new Exception()); + + mocker.Resolve().ProcessResults(new ProgressNotification("Test"), episode, parseResults); + + + mocker.VerifyAllMocks(); + mocker.GetMock().Verify(c => c.IsNeeded(It.IsAny()), Times.Exactly(4)); + + ExceptionVerification.ExcpectedErrors(4); + ExceptionVerification.ExcpectedWarns(1); + } + } +} diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 0a5ffe855..1f5e05cf4 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -86,6 +86,7 @@ + diff --git a/NzbDrone.Core/Providers/InventoryProvider.cs b/NzbDrone.Core/Providers/InventoryProvider.cs index bcab33f26..804e1a84f 100644 --- a/NzbDrone.Core/Providers/InventoryProvider.cs +++ b/NzbDrone.Core/Providers/InventoryProvider.cs @@ -12,20 +12,23 @@ namespace NzbDrone.Core.Providers private readonly SeasonProvider _seasonProvider; private readonly EpisodeProvider _episodeProvider; private readonly HistoryProvider _historyProvider; - private readonly SabProvider _sabProvider; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public InventoryProvider(SeriesProvider seriesProvider, SeasonProvider seasonProvider, EpisodeProvider episodeProvider, HistoryProvider historyProvider, SabProvider sabProvider) + public InventoryProvider(SeriesProvider seriesProvider, SeasonProvider seasonProvider, EpisodeProvider episodeProvider, HistoryProvider historyProvider) { _seriesProvider = seriesProvider; _seasonProvider = seasonProvider; _episodeProvider = episodeProvider; _historyProvider = historyProvider; - _sabProvider = sabProvider; } - internal bool IsNeeded(EpisodeParseResult parseResult) + public InventoryProvider() + { + + } + + public virtual bool IsNeeded(EpisodeParseResult parseResult) { var series = _seriesProvider.FindSeries(parseResult.CleanTitle); diff --git a/NzbDrone.Core/Providers/Jobs/EpisodeSearchJob.cs b/NzbDrone.Core/Providers/Jobs/EpisodeSearchJob.cs index d9c93acd3..e08219d5a 100644 --- a/NzbDrone.Core/Providers/Jobs/EpisodeSearchJob.cs +++ b/NzbDrone.Core/Providers/Jobs/EpisodeSearchJob.cs @@ -64,7 +64,7 @@ namespace NzbDrone.Core.Providers.Jobs public void ProcessResults(ProgressNotification notification, Episode episode, IEnumerable reports) { - foreach (var episodeParseResult in reports.OrderBy(c => c.Quality).ThenBy(c => c.Proper)) + foreach (var episodeParseResult in reports.OrderByDescending(c => c.Quality).ThenByDescending(c => c.Proper)) { try {