2013-03-28 22:07:09 +00:00
|
|
|
|
2012-08-29 15:34:51 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using FizzWare.NBuilder;
|
2013-04-11 15:28:08 +00:00
|
|
|
using Marr.Data;
|
2012-08-29 15:34:51 +00:00
|
|
|
using Moq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using NzbDrone.Common;
|
2013-03-01 07:03:41 +00:00
|
|
|
using NzbDrone.Core.MediaFiles;
|
2013-04-11 15:28:08 +00:00
|
|
|
using NzbDrone.Core.RootFolders;
|
2013-02-19 06:01:03 +00:00
|
|
|
using NzbDrone.Core.Tv;
|
2012-08-29 15:34:51 +00:00
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
using NzbDrone.Core.Providers;
|
2013-03-07 01:51:47 +00:00
|
|
|
|
2012-08-29 15:34:51 +00:00
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
using NzbDrone.Test.Common;
|
|
|
|
using NzbDrone.Test.Common.AutoMoq;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests
|
|
|
|
{
|
|
|
|
[TestFixture]
|
2013-02-17 05:44:06 +00:00
|
|
|
public class ProcessVideoFileFixture : CoreTest
|
2012-08-29 15:34:51 +00:00
|
|
|
{
|
|
|
|
Series fakeSeries;
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
|
|
|
fakeSeries = Builder<Series>.CreateNew()
|
2013-04-11 15:28:08 +00:00
|
|
|
.With(s => s.RootFolder = new LazyLoaded<RootFolder>(new RootFolder { Path = @"C:\Test\TV" }))
|
|
|
|
.With(s => s.FolderName = "30 Rock")
|
2012-08-29 15:34:51 +00:00
|
|
|
.Build();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WithOldWrite()
|
|
|
|
{
|
|
|
|
Mocker.GetMock<DiskProvider>()
|
|
|
|
.Setup(c => c.GetLastFileWrite(It.IsAny<String>()))
|
|
|
|
.Returns(DateTime.Now.AddDays(-5));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WithRecentWrite()
|
|
|
|
{
|
|
|
|
Mocker.GetMock<DiskProvider>()
|
|
|
|
.Setup(c => c.GetLastFileWrite(It.IsAny<String>()))
|
|
|
|
.Returns(DateTime.UtcNow);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WithValidSeries()
|
|
|
|
{
|
2013-02-19 06:56:02 +00:00
|
|
|
Mocker.GetMock<ISeriesRepository>()
|
2013-02-20 02:05:15 +00:00
|
|
|
.Setup(c => c.GetByTitle(It.IsAny<string>()))
|
2012-08-29 15:34:51 +00:00
|
|
|
.Returns(fakeSeries);
|
2012-10-20 06:46:36 +00:00
|
|
|
|
|
|
|
Mocker.GetMock<DiskProvider>()
|
|
|
|
.Setup(s => s.FolderExists(fakeSeries.Path))
|
|
|
|
.Returns(true);
|
2012-08-29 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void WithImportableFiles()
|
|
|
|
{
|
|
|
|
Mocker.GetMock<DiskScanProvider>()
|
|
|
|
.Setup(c => c.Scan(It.IsAny<Series>(), It.IsAny<string>()))
|
|
|
|
.Returns(Builder<EpisodeFile>.CreateListOfSize(1).Build().ToList());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WithLotsOfFreeDiskSpace()
|
|
|
|
{
|
2013-02-17 04:33:56 +00:00
|
|
|
Mocker.GetMock<DiskProvider>().Setup(s => s.FreeDiskSpace(It.IsAny<string>())).Returns(1000000000);
|
2012-08-29 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void WithImportedFile(string file)
|
|
|
|
{
|
|
|
|
var fakeEpisodeFile = Builder<EpisodeFile>.CreateNew()
|
2013-02-26 03:58:57 +00:00
|
|
|
.With(f => f.SeriesId = fakeSeries.Id)
|
2012-08-29 15:34:51 +00:00
|
|
|
.Build();
|
|
|
|
|
|
|
|
Mocker.GetMock<DiskScanProvider>().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<PostDownloadProvider>().ProcessVideoFile(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_continue_processing_if_not_fresh()
|
|
|
|
{
|
|
|
|
WithOldWrite();
|
|
|
|
|
|
|
|
var file = Path.Combine(TempFolder, "test.avi");
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2013-02-20 02:05:15 +00:00
|
|
|
Mocker.GetMock<ISeriesRepository>().Setup(s => s.GetByTitle(It.IsAny<String>())).Returns<Series>(null).Verifiable();
|
2012-08-29 15:34:51 +00:00
|
|
|
Mocker.Resolve<PostDownloadProvider>().ProcessVideoFile(file);
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2013-02-20 02:05:15 +00:00
|
|
|
Mocker.GetMock<ISeriesRepository>().Verify(s => s.GetByTitle(It.IsAny<String>()), Times.Once());
|
2012-08-29 15:34:51 +00:00
|
|
|
ExceptionVerification.IgnoreWarns();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_return_if_series_is_not_found()
|
|
|
|
{
|
|
|
|
WithOldWrite();
|
|
|
|
|
|
|
|
var file = Path.Combine(TempFolder, "test.avi");
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2013-02-20 02:05:15 +00:00
|
|
|
Mocker.GetMock<ISeriesRepository>().Setup(s => s.GetByTitle(It.IsAny<String>())).Returns<Series>(null);
|
2012-08-29 15:34:51 +00:00
|
|
|
Mocker.Resolve<PostDownloadProvider>().ProcessVideoFile(file);
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-10-20 01:42:42 +00:00
|
|
|
Mocker.GetMock<DiskProvider>().Verify(s => s.GetSize(It.IsAny<String>()), Times.Never());
|
2012-08-29 15:34:51 +00:00
|
|
|
ExceptionVerification.IgnoreWarns();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_move_file_if_imported()
|
|
|
|
{
|
|
|
|
WithLotsOfFreeDiskSpace();
|
|
|
|
WithOldWrite();
|
|
|
|
|
|
|
|
var file = Path.Combine(TempFolder, "test.avi");
|
|
|
|
|
|
|
|
WithValidSeries();
|
|
|
|
WithImportedFile(file);
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-08-29 15:34:51 +00:00
|
|
|
Mocker.Resolve<PostDownloadProvider>().ProcessVideoFile(file);
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2013-03-07 04:49:00 +00:00
|
|
|
Mocker.GetMock<IMoveEpisodeFiles>().Verify(s => s.MoveEpisodeFile(It.IsAny<EpisodeFile>(), true), Times.Once());
|
2012-08-29 15:34:51 +00:00
|
|
|
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<Series>.CreateNew()
|
|
|
|
.With(s => s.Title = "30 Rock")
|
2013-04-11 15:28:08 +00:00
|
|
|
.With(s => s.RootFolder = new LazyLoaded<RootFolder>(new RootFolder { Path = @"C:\Test\TV" }))
|
|
|
|
.With(s => s.FolderName = "30 Rock")
|
2012-08-29 15:34:51 +00:00
|
|
|
.Build();
|
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
Mocker.GetMock<ISeriesRepository>()
|
2013-02-20 02:05:15 +00:00
|
|
|
.Setup(c => c.GetByTitle("rock"))
|
2012-08-29 15:34:51 +00:00
|
|
|
.Returns(series);
|
|
|
|
|
|
|
|
Mocker.GetMock<DiskProvider>()
|
2012-10-20 01:42:42 +00:00
|
|
|
.Setup(s => s.GetSize(downloadName))
|
2012-08-29 15:34:51 +00:00
|
|
|
.Returns(10);
|
|
|
|
|
|
|
|
Mocker.GetMock<DiskProvider>()
|
2013-02-17 04:33:56 +00:00
|
|
|
.Setup(s => s.FreeDiskSpace(series.Path))
|
2012-08-29 15:34:51 +00:00
|
|
|
.Returns(9);
|
|
|
|
|
2012-10-20 06:46:36 +00:00
|
|
|
Mocker.GetMock<DiskProvider>()
|
|
|
|
.Setup(s => s.FolderExists(series.Path))
|
|
|
|
.Returns(true);
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-08-29 15:34:51 +00:00
|
|
|
Mocker.Resolve<PostDownloadProvider>().ProcessVideoFile(downloadName);
|
|
|
|
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-08-29 15:34:51 +00:00
|
|
|
Mocker.GetMock<DiskScanProvider>().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";
|
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
Mocker.GetMock<ISeriesRepository>()
|
2013-02-20 02:05:15 +00:00
|
|
|
.Setup(c => c.GetByTitle("rock"))
|
2012-08-29 15:34:51 +00:00
|
|
|
.Returns(fakeSeries);
|
|
|
|
|
|
|
|
Mocker.GetMock<DiskProvider>()
|
2012-10-20 01:42:42 +00:00
|
|
|
.Setup(s => s.GetSize(downloadName))
|
2012-08-29 15:34:51 +00:00
|
|
|
.Returns(8);
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-08-29 15:34:51 +00:00
|
|
|
Mocker.Resolve<PostDownloadProvider>().ProcessVideoFile(downloadName);
|
|
|
|
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-08-29 15:34:51 +00:00
|
|
|
Mocker.GetMock<DiskScanProvider>().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<DiskProvider>()
|
|
|
|
.Setup(s => s.GetDirectorySize(downloadName))
|
|
|
|
.Returns(10);
|
|
|
|
|
|
|
|
Mocker.GetMock<DiskProvider>()
|
2013-02-17 04:33:56 +00:00
|
|
|
.Setup(s => s.FreeDiskSpace(It.IsAny<string>()))
|
2012-08-29 15:34:51 +00:00
|
|
|
.Returns(10);
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-08-29 15:34:51 +00:00
|
|
|
Mocker.Resolve<PostDownloadProvider>().ProcessVideoFile(downloadName);
|
|
|
|
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-08-29 15:34:51 +00:00
|
|
|
Mocker.GetMock<DiskScanProvider>().Verify(c => c.ImportFile(fakeSeries, downloadName), Times.Once());
|
|
|
|
}
|
2012-10-20 06:46:36 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_return_if_series_Path_doesnt_exist()
|
|
|
|
{
|
|
|
|
var downloadName = @"C:\Test\Drop\30.Rock.S01E01.Pilot.mkv";
|
|
|
|
|
|
|
|
WithValidSeries();
|
|
|
|
|
|
|
|
Mocker.GetMock<DiskProvider>()
|
|
|
|
.Setup(s => s.FolderExists(fakeSeries.Path))
|
|
|
|
.Returns(false);
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-10-20 06:46:36 +00:00
|
|
|
Mocker.Resolve<PostDownloadProvider>().ProcessVideoFile(downloadName);
|
|
|
|
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-10-20 06:46:36 +00:00
|
|
|
ExceptionVerification.ExpectedWarns(1);
|
|
|
|
}
|
2012-10-20 08:01:47 +00:00
|
|
|
|
|
|
|
[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<DiskProvider>()
|
|
|
|
.Setup(s => s.IsFileLocked(It.Is<FileInfo>(f => f.FullName == downloadName)))
|
|
|
|
.Returns(true);
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-10-20 08:01:47 +00:00
|
|
|
Mocker.Resolve<PostDownloadProvider>().ProcessVideoFile(downloadName);
|
|
|
|
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
2012-10-20 08:01:47 +00:00
|
|
|
Mocker.GetMock<DiskScanProvider>().Verify(c => c.ImportFile(fakeSeries, downloadName), Times.Never());
|
|
|
|
}
|
2012-08-29 15:34:51 +00:00
|
|
|
}
|
|
|
|
}
|