From 849beef5a24e0f89aba731f3bf1076b9b7616bd2 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 19 May 2011 21:59:35 -0700 Subject: [PATCH] Added inventory provider with basic tests --- NzbDrone.Core.Test/InventoryProviderTest.cs | 182 ++++++++++++++++++ NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 + NzbDrone.Core/Model/EpisodeParseResult.cs | 4 +- NzbDrone.Core/NzbDrone.Core.csproj | 2 +- .../Providers/Indexer/IndexerBase.cs | 1 + ...NeededProvider.cs => InventoryProvider.cs} | 22 ++- 6 files changed, 200 insertions(+), 12 deletions(-) create mode 100644 NzbDrone.Core.Test/InventoryProviderTest.cs rename NzbDrone.Core/Providers/Indexer/{IsNeededProvider.cs => InventoryProvider.cs} (88%) diff --git a/NzbDrone.Core.Test/InventoryProviderTest.cs b/NzbDrone.Core.Test/InventoryProviderTest.cs new file mode 100644 index 000000000..8e3ac069f --- /dev/null +++ b/NzbDrone.Core.Test/InventoryProviderTest.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.ServiceModel.Syndication; +using AutoMoq; +using FizzWare.NBuilder; +using MbUnit.Framework; +using Moq; +using NzbDrone.Core.Model; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Providers.Core; +using NzbDrone.Core.Providers.ExternalNotification; +using NzbDrone.Core.Providers.Indexer; +using NzbDrone.Core.Repository; +using NzbDrone.Core.Repository.Quality; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test +{ + [TestFixture] + // ReSharper disable InconsistentNaming + public class InventoryProviderTest : TestBase + { + [Test] + public void not_monitored_series_should_be_skipped() + { + var parseResult = new EpisodeParseResult() + { + CleanTitle = "Title", + EpisodeTitle = "EpisodeTitle", + Language = LanguageType.English, + Proper = true, + Quality = QualityTypes.Bluray720, + Episodes = new List { 3 }, + SeasonNumber = 12 + }; + + var series = Builder.CreateNew() + .With(c => c.Monitored = false) + .With(d => d.CleanTitle = parseResult.CleanTitle) + .Build(); + + var mocker = new AutoMoqer(MockBehavior.Strict); + + mocker.GetMock() + .Setup(p => p.FindSeries(It.IsAny())) + .Returns(series); + + //Act + var result = mocker.Resolve().IsNeeded(parseResult); + + //Assert + Assert.IsFalse(result); + } + + + [Test] + public void no_db_series_should_be_skipped() + { + var parseResult = new EpisodeParseResult() + { + CleanTitle = "Title", + EpisodeTitle = "EpisodeTitle", + Language = LanguageType.English, + Proper = true, + Quality = QualityTypes.Bluray720, + Episodes = new List { 3 }, + SeasonNumber = 12 + }; + + var mocker = new AutoMoqer(MockBehavior.Strict); + + mocker.GetMock() + .Setup(p => p.FindSeries(It.IsAny())) + .Returns(null); + + //Act + var result = mocker.Resolve().IsNeeded(parseResult); + + //Assert + Assert.IsFalse(result); + } + + [Test] + public void unwannted_quality_should_be_skipped() + { + var parseResult = new EpisodeParseResult() + { + CleanTitle = "Title", + EpisodeTitle = "EpisodeTitle", + Language = LanguageType.English, + Proper = true, + Quality = QualityTypes.Bluray720, + Episodes = new List { 3 }, + SeasonNumber = 12 + }; + + var series = Builder.CreateNew() + .With(c => c.Monitored = true) + .With(d => d.CleanTitle = parseResult.CleanTitle) + .Build(); + + var mocker = new AutoMoqer(MockBehavior.Strict); + mocker.GetMock() + .Setup(p => p.FindSeries(It.IsAny())) + .Returns(series); + + mocker.GetMock() + .Setup(p => p.QualityWanted(series.SeriesId, parseResult.Quality)) + .Returns(false); + + + //Act + var result = mocker.Resolve().IsNeeded(parseResult); + + //Assert + Assert.IsFalse(result); + } + + [Test] + public void unwannted_file_should_be_skipped() + { + + var parseResult = new EpisodeParseResult() + { + CleanTitle = "Title", + EpisodeTitle = "EpisodeTitle", + Language = LanguageType.English, + Proper = true, + Quality = QualityTypes.Bluray720, + Episodes = new List { 3 }, + SeasonNumber = 12 + }; + + var series = Builder.CreateNew() + .With(c => c.Monitored = true) + .With(d => d.CleanTitle = parseResult.CleanTitle) + .Build(); + + var episode = Builder.CreateNew() + .With(c => c.EpisodeNumber = parseResult.Episodes[0]) + .With(c => c.SeasonNumber = parseResult.SeasonNumber) + .Build(); + + + + var mocker = new AutoMoqer(MockBehavior.Strict); + + mocker.GetMock() + .Setup(p => p.FindSeries(It.IsAny())) + .Returns(series); + + mocker.GetMock() + .Setup(p => p.GetEpisode(episode.SeriesId, episode.SeasonNumber, episode.EpisodeNumber)) + .Returns(episode); + + + mocker.GetMock() + .Setup(p => p.QualityWanted(series.SeriesId, parseResult.Quality)) + .Returns(true); + + mocker.GetMock() + .Setup(p => p.IsIgnored(series.SeriesId, parseResult.SeasonNumber)) + .Returns(false); + + mocker.GetMock() + .Setup(p => p.IsNeeded(parseResult, episode)) + .Returns(false); + + //Act + var result = mocker.Resolve().IsNeeded(parseResult); + + //Assert + Assert.IsFalse(result); + } + } + + + + +} \ No newline at end of file diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index a15bb3b77..18c77b923 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -85,6 +85,7 @@ + diff --git a/NzbDrone.Core/Model/EpisodeParseResult.cs b/NzbDrone.Core/Model/EpisodeParseResult.cs index 6ea931ae3..4bbbf891a 100644 --- a/NzbDrone.Core/Model/EpisodeParseResult.cs +++ b/NzbDrone.Core/Model/EpisodeParseResult.cs @@ -9,7 +9,7 @@ namespace NzbDrone.Core.Model internal string CleanTitle { get; set; } internal int SeasonNumber { get; set; } - + internal List Episodes { get; set; } internal string EpisodeTitle { get; set; } @@ -22,6 +22,8 @@ namespace NzbDrone.Core.Model public LanguageType Language { get; set; } + public string NzbUrl { get; set; } + public override string ToString() { if (Episodes == null) diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 4403b5977..d52027eeb 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -172,7 +172,7 @@ - + diff --git a/NzbDrone.Core/Providers/Indexer/IndexerBase.cs b/NzbDrone.Core/Providers/Indexer/IndexerBase.cs index fa9bf7b2d..6e197b6c7 100644 --- a/NzbDrone.Core/Providers/Indexer/IndexerBase.cs +++ b/NzbDrone.Core/Providers/Indexer/IndexerBase.cs @@ -76,6 +76,7 @@ namespace NzbDrone.Core.Providers.Indexer var parsedEpisode = ParseFeed(item); if (parsedEpisode != null) { + parsedEpisode.NzbUrl = NzbDownloadUrl(item); result.Add(parsedEpisode); } } diff --git a/NzbDrone.Core/Providers/Indexer/IsNeededProvider.cs b/NzbDrone.Core/Providers/Indexer/InventoryProvider.cs similarity index 88% rename from NzbDrone.Core/Providers/Indexer/IsNeededProvider.cs rename to NzbDrone.Core/Providers/Indexer/InventoryProvider.cs index f9068c6cd..0e883ca97 100644 --- a/NzbDrone.Core/Providers/Indexer/IsNeededProvider.cs +++ b/NzbDrone.Core/Providers/Indexer/InventoryProvider.cs @@ -1,17 +1,12 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; using NLog; -using NzbDrone.Core.Helpers; using NzbDrone.Core.Model; using NzbDrone.Core.Repository; -using System.ServiceModel.Syndication; namespace NzbDrone.Core.Providers.Indexer { - public class IsNeededProvider + public class InventoryProvider { private readonly SeriesProvider _seriesProvider; private readonly SeasonProvider _seasonProvider; @@ -21,7 +16,7 @@ namespace NzbDrone.Core.Providers.Indexer private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public IsNeededProvider(SeriesProvider seriesProvider, SeasonProvider seasonProvider, EpisodeProvider episodeProvider, HistoryProvider historyProvider, SabProvider sabProvider) + public InventoryProvider(SeriesProvider seriesProvider, SeasonProvider seasonProvider, EpisodeProvider episodeProvider, HistoryProvider historyProvider, SabProvider sabProvider) { _seriesProvider = seriesProvider; _seasonProvider = seasonProvider; @@ -30,9 +25,16 @@ namespace NzbDrone.Core.Providers.Indexer _sabProvider = sabProvider; } - - internal bool IsNeeded(EpisodeParseResult parseResult, Series series) + internal bool IsNeeded(EpisodeParseResult parseResult) { + var series = _seriesProvider.FindSeries(parseResult.CleanTitle); + + if (series == null) + { + Logger.Trace("{0} is not mapped to any series in DB. skipping", parseResult.CleanTitle); + return false; + } + foreach (var episodeNumber in parseResult.Episodes) { //Todo: How to handle full season files? Currently the episode list is completely empty for these releases @@ -87,7 +89,7 @@ namespace NzbDrone.Core.Providers.Indexer Logger.Debug("Episode {0} is not needed. skipping.", parseResult); return false; } - + if (_historyProvider.Exists(episodeInfo.EpisodeId, parseResult.Quality, parseResult.Proper)) { Logger.Debug("Episode {0} is in history. skipping.", parseResult);