From 3e7ef408ee1d9c8c5923d0b54899aca3f3542dcd Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 11 Jul 2013 22:57:24 -0700 Subject: [PATCH] Added tests for ImportApprovedEpisodes --- .../DropFolderImportServiceFixture.cs | 2 +- .../ImportApprovedEpisodesFixture.cs | 130 ++++++++++++++++++ NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 3 +- NzbDrone.Core/MediaFiles/DiskScanService.cs | 2 +- .../EpisodeImport/ImportApprovedEpisodes.cs | 17 +-- 5 files changed, 143 insertions(+), 11 deletions(-) rename NzbDrone.Core.Test/{ProviderTests/PostDownloadProviderTests => MediaFileTests}/DropFolderImportServiceFixture.cs (97%) create mode 100644 NzbDrone.Core.Test/MediaFileTests/ImportApprovedEpisodesFixture.cs diff --git a/NzbDrone.Core.Test/ProviderTests/PostDownloadProviderTests/DropFolderImportServiceFixture.cs b/NzbDrone.Core.Test/MediaFileTests/DropFolderImportServiceFixture.cs similarity index 97% rename from NzbDrone.Core.Test/ProviderTests/PostDownloadProviderTests/DropFolderImportServiceFixture.cs rename to NzbDrone.Core.Test/MediaFileTests/DropFolderImportServiceFixture.cs index 380311fa6..e96ddfd78 100644 --- a/NzbDrone.Core.Test/ProviderTests/PostDownloadProviderTests/DropFolderImportServiceFixture.cs +++ b/NzbDrone.Core.Test/MediaFileTests/DropFolderImportServiceFixture.cs @@ -13,7 +13,7 @@ using NzbDrone.Core.Tv; using NzbDrone.Core.Test.Framework; -namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests +namespace NzbDrone.Core.Test.MediaFileTests { [TestFixture] public class DropFolderImportServiceFixture : CoreTest diff --git a/NzbDrone.Core.Test/MediaFileTests/ImportApprovedEpisodesFixture.cs b/NzbDrone.Core.Test/MediaFileTests/ImportApprovedEpisodesFixture.cs new file mode 100644 index 000000000..95b3c11f6 --- /dev/null +++ b/NzbDrone.Core.Test/MediaFileTests/ImportApprovedEpisodesFixture.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Common; +using NzbDrone.Common.Messaging; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.MediaFiles; +using NzbDrone.Core.MediaFiles.EpisodeImport; +using NzbDrone.Core.MediaFiles.Events; +using NzbDrone.Core.Parser; +using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.Qualities; +using NzbDrone.Core.Tv; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.MediaFileTests +{ + [TestFixture] + public class ImportApprovedEpisodesFixture : CoreTest + { + private List _rejectedDecisions; + private List _approvedDecisions; + + [SetUp] + public void Setup() + { + _rejectedDecisions = new List(); + _approvedDecisions = new List(); + + var series = Builder.CreateNew() + .Build(); + + var episodes = Builder.CreateListOfSize(5) + .Build(); + + _rejectedDecisions.Add(new ImportDecision(new LocalEpisode(), "Rejected!")); + _rejectedDecisions.Add(new ImportDecision(new LocalEpisode(), "Rejected!")); + _rejectedDecisions.Add(new ImportDecision(new LocalEpisode(), "Rejected!")); + + foreach (var episode in episodes) + { + _approvedDecisions.Add(new ImportDecision + ( + new LocalEpisode + { + Series = series, + Episodes = new List {episode}, + Path = @"C:\Test\TV\30 Rock\30 Rock - S01E01 - Pilit.avi", + Quality = new QualityModel(Quality.Bluray720p) + + })); + } + } + + [Test] + public void should_return_empty_list_if_there_are_no_approved_decisions() + { + Subject.Import(_rejectedDecisions).Should().BeEmpty(); + } + + [Test] + public void should_import_each_approved() + { + Subject.Import(_approvedDecisions).Should().HaveCount(5); + } + + [Test] + public void should_only_import_approved() + { + var all = new List(); + all.AddRange(_rejectedDecisions); + all.AddRange(_approvedDecisions); + + Subject.Import(all).Should().HaveCount(5); + } + + [Test] + public void should_only_import_each_episode_once() + { + var all = new List(); + all.AddRange(_approvedDecisions); + all.Add(new ImportDecision(_approvedDecisions.First().LocalEpisode)); + + Subject.Import(all).Should().HaveCount(5); + } + + [Test] + public void should_move_new_downloads() + { + Subject.Import(new List {_approvedDecisions.First()}, true); + + Mocker.GetMock() + .Verify(v => v.MoveEpisodeFile(It.IsAny(), _approvedDecisions.First().LocalEpisode), + Times.Once()); + } + + [Test] + public void should_publish_EpisodeImportedEvent_for_new_downloads() + { + Subject.Import(new List { _approvedDecisions.First() }, true); + + Mocker.GetMock() + .Verify(v => v.PublishEvent(It.IsAny()), Times.Once()); + } + + [Test] + public void should_not_move_existing_files() + { + Subject.Import(new List { _approvedDecisions.First() }); + + Mocker.GetMock() + .Verify(v => v.MoveEpisodeFile(It.IsAny(), _approvedDecisions.First().LocalEpisode), + Times.Never()); + } + + [Test] + public void should_not_trigger_EpisodeImportedEvent_for_existing_files() + { + Subject.Import(new List { _approvedDecisions.First() }); + + Mocker.GetMock() + .Verify(v => v.PublishEvent(It.IsAny()), Times.Never()); + } + } +} \ 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 0faf48d9c..c8cb159d3 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -147,6 +147,7 @@ + @@ -193,7 +194,7 @@ - + diff --git a/NzbDrone.Core/MediaFiles/DiskScanService.cs b/NzbDrone.Core/MediaFiles/DiskScanService.cs index deb8a6ba9..39e29d9f6 100644 --- a/NzbDrone.Core/MediaFiles/DiskScanService.cs +++ b/NzbDrone.Core/MediaFiles/DiskScanService.cs @@ -48,7 +48,7 @@ private void Scan(Series series) if (!_diskProvider.FolderExists(series.Path)) { - Logger.Trace("Series folder doesn't exist: {0}", series.Path); + Logger.Debug("Series folder doesn't exist: {0}", series.Path); return; } diff --git a/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs b/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs index 946f8b5c5..798e686dd 100644 --- a/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs +++ b/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs @@ -18,14 +18,14 @@ public interface IImportApprovedEpisodes public class ImportApprovedEpisodes : IImportApprovedEpisodes { private readonly IMoveEpisodeFiles _episodeFileMover; - private readonly MediaFileService _mediaFileService; - private readonly DiskProvider _diskProvider; + private readonly IMediaFileService _mediaFileService; + private readonly IDiskProvider _diskProvider; private readonly IMessageAggregator _messageAggregator; private readonly Logger _logger; public ImportApprovedEpisodes(IMoveEpisodeFiles episodeFileMover, - MediaFileService mediaFileService, - DiskProvider diskProvider, + IMediaFileService mediaFileService, + IDiskProvider diskProvider, IMessageAggregator messageAggregator, Logger logger) { @@ -38,12 +38,12 @@ public ImportApprovedEpisodes(IMoveEpisodeFiles episodeFileMover, public List Import(List decisions, bool newDownload = false) { - var qualifiedReports = GetQualifiedReports(decisions); + var qualifiedImports = GetQualifiedImports(decisions); var imported = new List(); - foreach (var report in qualifiedReports) + foreach (var importDecision in qualifiedImports) { - var localEpisode = report.LocalEpisode; + var localEpisode = importDecision.LocalEpisode; try { @@ -73,6 +73,7 @@ public List Import(List decisions, bool newDownl } _mediaFileService.Add(episodeFile); + imported.Add(importDecision); } catch (Exception e) { @@ -83,7 +84,7 @@ public List Import(List decisions, bool newDownl return imported; } - private List GetQualifiedReports(List decisions) + private List GetQualifiedImports(List decisions) { return decisions.Where(c => c.Approved) .OrderByDescending(c => c.LocalEpisode.Quality)