// ReSharper disable InconsistentNaming using System; using System.Collections.Generic; using System.IO; using System.Linq; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Common; using NzbDrone.Core.Tv; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; using NzbDrone.Core.Repository; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; using NzbDrone.Test.Common.AutoMoq; namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests { [TestFixture] public class ProcessVideoFileFixture : CoreTest { Series fakeSeries; [SetUp] public void Setup() { fakeSeries = Builder.CreateNew() .With(s => s.Path = @"C:\Test\TV\30 Rock") .Build(); } private void WithOldWrite() { Mocker.GetMock() .Setup(c => c.GetLastFileWrite(It.IsAny())) .Returns(DateTime.Now.AddDays(-5)); } private void WithRecentWrite() { Mocker.GetMock() .Setup(c => c.GetLastFileWrite(It.IsAny())) .Returns(DateTime.UtcNow); } private void WithValidSeries() { Mocker.GetMock() .Setup(c => c.GetByTitle(It.IsAny())) .Returns(fakeSeries); Mocker.GetMock() .Setup(s => s.FolderExists(fakeSeries.Path)) .Returns(true); } private void WithImportableFiles() { Mocker.GetMock() .Setup(c => c.Scan(It.IsAny(), It.IsAny())) .Returns(Builder.CreateListOfSize(1).Build().ToList()); } private void WithLotsOfFreeDiskSpace() { Mocker.GetMock().Setup(s => s.FreeDiskSpace(It.IsAny())).Returns(1000000000); } private void WithImportedFile(string file) { var fakeEpisodeFile = Builder.CreateNew() .With(f => f.SeriesId = fakeSeries.SeriesId) .Build(); Mocker.GetMock().Setup(s => s.ImportFile(fakeSeries, file)).Returns(fakeEpisodeFile); } [Test] public void should_skip_if_and_too_fresh() { WithStrictMocker(); WithRecentWrite(); var file = Path.Combine(TempFolder, "test.avi"); Mocker.Resolve().ProcessVideoFile(file); } [Test] public void should_continue_processing_if_not_fresh() { WithOldWrite(); var file = Path.Combine(TempFolder, "test.avi"); //Act Mocker.GetMock().Setup(s => s.GetByTitle(It.IsAny())).Returns(null).Verifiable(); Mocker.Resolve().ProcessVideoFile(file); //Assert Mocker.GetMock().Verify(s => s.GetByTitle(It.IsAny()), Times.Once()); ExceptionVerification.IgnoreWarns(); } [Test] public void should_return_if_series_is_not_found() { WithOldWrite(); var file = Path.Combine(TempFolder, "test.avi"); //Act Mocker.GetMock().Setup(s => s.GetByTitle(It.IsAny())).Returns(null); Mocker.Resolve().ProcessVideoFile(file); //Assert Mocker.GetMock().Verify(s => s.GetSize(It.IsAny()), Times.Never()); ExceptionVerification.IgnoreWarns(); } [Test] public void should_move_file_if_imported() { WithLotsOfFreeDiskSpace(); WithOldWrite(); var file = Path.Combine(TempFolder, "test.avi"); WithValidSeries(); WithImportedFile(file); //Act Mocker.Resolve().ProcessVideoFile(file); //Assert Mocker.GetMock().Verify(s => s.MoveEpisodeFile(It.IsAny(), true), Times.Once()); ExceptionVerification.IgnoreWarns(); } [Test] public void should_logError_and_return_if_size_exceeds_free_space() { var downloadName = @"C:\Test\Drop\30.Rock.S01E01.Pilot.mkv"; var series = Builder.CreateNew() .With(s => s.Title = "30 Rock") .With(s => s.Path = @"C:\Test\TV\30 Rock") .Build(); Mocker.GetMock() .Setup(c => c.GetByTitle("rock")) .Returns(series); Mocker.GetMock() .Setup(s => s.GetSize(downloadName)) .Returns(10); Mocker.GetMock() .Setup(s => s.FreeDiskSpace(series.Path)) .Returns(9); Mocker.GetMock() .Setup(s => s.FolderExists(series.Path)) .Returns(true); //Act Mocker.Resolve().ProcessVideoFile(downloadName); //Assert Mocker.GetMock().Verify(c => c.ImportFile(series, downloadName), Times.Never()); ExceptionVerification.ExpectedErrors(1); } [Test] public void should_process_if_free_disk_space_exceeds_size() { WithLotsOfFreeDiskSpace(); WithValidSeries(); var downloadName = @"C:\Test\Drop\30.Rock.S01E01.Pilot.mkv"; Mocker.GetMock() .Setup(c => c.GetByTitle("rock")) .Returns(fakeSeries); Mocker.GetMock() .Setup(s => s.GetSize(downloadName)) .Returns(8); //Act Mocker.Resolve().ProcessVideoFile(downloadName); //Assert Mocker.GetMock().Verify(c => c.ImportFile(fakeSeries, downloadName), Times.Once()); } [Test] public void should_process_if_free_disk_space_equals_size() { var downloadName = @"C:\Test\Drop\30.Rock.S01E01.Pilot.mkv"; WithValidSeries(); Mocker.GetMock() .Setup(s => s.GetDirectorySize(downloadName)) .Returns(10); Mocker.GetMock() .Setup(s => s.FreeDiskSpace(It.IsAny())) .Returns(10); //Act Mocker.Resolve().ProcessVideoFile(downloadName); //Assert Mocker.GetMock().Verify(c => c.ImportFile(fakeSeries, downloadName), Times.Once()); } [Test] public void should_return_if_series_Path_doesnt_exist() { var downloadName = @"C:\Test\Drop\30.Rock.S01E01.Pilot.mkv"; WithValidSeries(); Mocker.GetMock() .Setup(s => s.FolderExists(fakeSeries.Path)) .Returns(false); //Act Mocker.Resolve().ProcessVideoFile(downloadName); //Assert ExceptionVerification.ExpectedWarns(1); } [Test] public void should_skip_if_file_is_in_use_by_another_process() { var downloadName = @"C:\Test\Drop\30.Rock.S01E01.Pilot.mkv"; WithValidSeries(); Mocker.GetMock() .Setup(s => s.IsFileLocked(It.Is(f => f.FullName == downloadName))) .Returns(true); //Act Mocker.Resolve().ProcessVideoFile(downloadName); //Assert Mocker.GetMock().Verify(c => c.ImportFile(fakeSeries, downloadName), Times.Never()); } } }