using System; using System.Collections.Generic; using System.IO; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Common; using NzbDrone.Core.Configuration; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles.Commands; using NzbDrone.Core.MediaFiles.EpisodeImport; using NzbDrone.Core.Parser; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Tv; using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.MediaFileTests { [TestFixture] public class DropFolderImportServiceFixture : CoreTest { private EpisodeFile _fakeEpisodeFile; private string[] _subFolders = new[] { "c:\\root\\foldername" }; private string[] _videoFiles = new[] { "c:\\root\\foldername\\video.ext" }; [SetUp] public void Setup() { _fakeEpisodeFile = Builder.CreateNew().Build(); Mocker.GetMock().Setup(c => c.GetVideoFiles(It.IsAny(), It.IsAny())) .Returns(_videoFiles); Mocker.GetMock().Setup(c => c.GetDirectories(It.IsAny())) .Returns(_subFolders); Mocker.GetMock().Setup(c => c.FolderExists(It.IsAny())) .Returns(true); Mocker.GetMock().SetupGet(c => c.DownloadedEpisodesFolder) .Returns("c:\\drop\\"); Mocker.GetMock() .Setup(s => s.Import(It.IsAny>(), true)) .Returns(new List()); } private void GivenValidSeries() { Mocker.GetMock() .Setup(s => s.GetSeries(It.IsAny())) .Returns(Builder.CreateNew().Build()); } [Test] public void should_search_for_series_using_folder_name() { Subject.Execute(new DownloadedEpisodesScanCommand()); Mocker.GetMock().Verify(c => c.GetSeries("foldername"), Times.Once()); } [Test] public void should_skip_import_if_dropfolder_doesnt_exist() { Mocker.GetMock().Setup(c => c.FolderExists(It.IsAny())).Returns(false); Subject.Execute(new DownloadedEpisodesScanCommand()); Mocker.GetMock().Verify(c => c.GetDirectories(It.IsAny()), Times.Never()); Mocker.GetMock().Verify(c => c.GetFiles(It.IsAny(), It.IsAny()), Times.Never()); ExceptionVerification.ExpectedWarns(1); } [Test] public void should_skip_if_file_is_in_use_by_another_process() { Mocker.GetMock().Setup(c => c.IsFileLocked(It.IsAny())) .Returns(true); Subject.Execute(new DownloadedEpisodesScanCommand()); VerifyNoImport(); } [Test] public void should_not_import_if_folder_is_a_series_path() { Mocker.GetMock() .Setup(s => s.SeriesPathExists(It.IsAny())) .Returns(true); Mocker.GetMock() .Setup(c => c.GetVideoFiles(It.IsAny(), It.IsAny())) .Returns(new string[0]); Subject.Execute(new DownloadedEpisodesScanCommand()); Mocker.GetMock() .Verify(v => v.GetSeries(It.IsAny()), Times.Never()); } [Test] public void should_not_delete_folder_if_no_files_were_imported() { Mocker.GetMock() .Setup(s => s.Import(It.IsAny>(), false)) .Returns(new List()); Subject.Execute(new DownloadedEpisodesScanCommand()); Mocker.GetMock() .Verify(v => v.GetFolderSize(It.IsAny()), Times.Never()); } [Test] public void should_delete_folder_if_files_were_imported() { GivenValidSeries(); var localEpisode = new LocalEpisode(); var imported = new List(); imported.Add(new ImportDecision(localEpisode)); Mocker.GetMock() .Setup(s => s.GetImportDecisions(It.IsAny>(), It.IsAny(), true)) .Returns(imported); Mocker.GetMock() .Setup(s => s.Import(It.IsAny>(), true)) .Returns(imported); Subject.Execute(new DownloadedEpisodesScanCommand()); Mocker.GetMock() .Verify(v => v.DeleteFolder(It.IsAny(), true), Times.Once()); } private void VerifyNoImport() { Mocker.GetMock().Verify(c => c.Import(It.IsAny>(), true), Times.Never()); } private void VerifyImport() { Mocker.GetMock().Verify(c => c.Import(It.IsAny>(), true), Times.Once()); } } }