Lidarr/NzbDrone.Core.Test/ProviderTests/PostDownloadProviderTests/DropFolderImportServiceFixt...

132 lines
4.0 KiB
C#
Raw Normal View History

using System;
using System.IO;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
2013-05-16 00:25:51 +00:00
using NzbDrone.Core.Configuration;
using NzbDrone.Core.MediaFiles;
2013-04-17 23:32:53 +00:00
using NzbDrone.Core.Parser;
using NzbDrone.Core.Tv;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests
{
[TestFixture]
2013-05-16 00:10:48 +00:00
public class DropFolderImportServiceFixture : CoreTest<DownloadedEpisodesImportService>
{
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<EpisodeFile>.CreateNew().Build();
2013-04-15 05:27:32 +00:00
Mocker.GetMock<IDiskScanService>().Setup(c => c.GetVideoFiles(It.IsAny<string>(), It.IsAny<bool>()))
.Returns(_videoFiles);
2013-05-10 23:53:50 +00:00
Mocker.GetMock<IDiskProvider>().Setup(c => c.GetDirectories(It.IsAny<string>()))
.Returns(_subFolders);
2013-05-16 00:25:51 +00:00
Mocker.GetMock<IConfigService>().SetupGet(c => c.DownloadedEpisodesFolder)
.Returns("c:\\drop\\");
}
private void WithOldWrite()
{
2013-05-10 23:53:50 +00:00
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetLastFolderWrite(It.IsAny<String>()))
.Returns(DateTime.Now.AddDays(-5));
}
private void WithRecentFolderWrite()
{
2013-05-10 23:53:50 +00:00
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetLastFolderWrite(It.IsAny<String>()))
.Returns(DateTime.UtcNow);
2013-04-17 23:32:53 +00:00
2013-05-10 23:53:50 +00:00
Mocker.GetMock<IDiskProvider>()
2013-04-17 23:32:53 +00:00
.Setup(c => c.GetLastFileWrite(It.IsAny<String>()))
.Returns(DateTime.UtcNow);
}
[Test]
public void should_import_file()
{
2013-05-16 00:25:51 +00:00
Subject.ProcessDownloadedEpiosdesFolder();
VerifyImport();
}
[Test]
public void should_skip_if_folder_is_too_fresh()
{
WithRecentFolderWrite();
2013-05-16 00:25:51 +00:00
Subject.ProcessDownloadedEpiosdesFolder();
VerifyNoImport();
}
[Test]
public void should_search_for_series_using_folder_name()
{
WithOldWrite();
2013-05-16 00:25:51 +00:00
Subject.ProcessDownloadedEpiosdesFolder();
2013-04-17 23:32:53 +00:00
Mocker.GetMock<IParsingService>().Verify(c => c.GetSeries("foldername"), Times.Once());
}
[Test]
public void all_imported_files_should_be_moved()
{
2013-04-15 05:27:32 +00:00
Mocker.GetMock<IDiskScanService>().Setup(c => c.ImportFile(It.IsAny<Series>(), It.IsAny<string>()))
.Returns(_fakeEpisodeFile);
2013-05-16 00:25:51 +00:00
Subject.ProcessDownloadedEpiosdesFolder();
Mocker.GetMock<IMoveEpisodeFiles>().Verify(c => c.MoveEpisodeFile(_fakeEpisodeFile, true), Times.Once());
}
[Test]
public void should_not_attempt_move_if_nothing_is_imported()
{
2013-04-15 05:27:32 +00:00
Mocker.GetMock<IDiskScanService>().Setup(c => c.ImportFile(It.IsAny<Series>(), It.IsAny<string>()))
.Returns<EpisodeFile>(null);
2013-05-16 00:25:51 +00:00
Subject.ProcessDownloadedEpiosdesFolder();
Mocker.GetMock<IMoveEpisodeFiles>().Verify(c => c.MoveEpisodeFile(It.IsAny<EpisodeFile>(), It.IsAny<bool>()), Times.Never());
}
[Test]
public void should_skip_if_folder_is_in_use_by_another_process()
{
2013-05-10 23:53:50 +00:00
Mocker.GetMock<IDiskProvider>().Setup(c => c.IsFileLocked(It.IsAny<FileInfo>()))
.Returns(true);
2013-05-16 00:25:51 +00:00
Subject.ProcessDownloadedEpiosdesFolder();
VerifyNoImport();
}
private void VerifyNoImport()
{
2013-04-15 05:27:32 +00:00
Mocker.GetMock<IDiskScanService>().Verify(c => c.ImportFile(It.IsAny<Series>(), It.IsAny<string>()),
Times.Never());
}
private void VerifyImport()
{
2013-04-15 05:27:32 +00:00
Mocker.GetMock<IDiskScanService>().Verify(c => c.ImportFile(It.IsAny<Series>(), It.IsAny<string>()),
Times.Once());
}
}
}