2011-06-21 08:44:01 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2011-12-14 09:42:24 +03:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2011-06-21 08:44:01 +03:00
|
|
|
using FizzWare.NBuilder;
|
2011-12-14 09:42:24 +03:00
|
|
|
using FluentAssertions;
|
2011-06-21 08:44:01 +03:00
|
|
|
using Moq;
|
|
|
|
using NUnit.Framework;
|
2011-11-13 07:07:06 +03:00
|
|
|
using NzbDrone.Common;
|
2011-06-21 08:44:01 +03:00
|
|
|
using NzbDrone.Core.Providers;
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-12-14 09:42:24 +03:00
|
|
|
using NzbDrone.Core.Repository.Quality;
|
2011-06-21 08:44:01 +03:00
|
|
|
using NzbDrone.Core.Test.Framework;
|
2011-10-24 08:54:09 +03:00
|
|
|
using NzbDrone.Test.Common;
|
2011-11-14 03:22:18 +03:00
|
|
|
using NzbDrone.Test.Common.AutoMoq;
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-10-21 02:42:17 +03:00
|
|
|
namespace NzbDrone.Core.Test.ProviderTests
|
2011-06-21 08:44:01 +03:00
|
|
|
{
|
|
|
|
// ReSharper disable InconsistentNaming
|
2011-11-13 10:27:16 +03:00
|
|
|
public class DiskScanProviderTest : CoreTest
|
2011-06-21 08:44:01 +03:00
|
|
|
{
|
|
|
|
[Test]
|
2011-10-23 02:28:57 +03:00
|
|
|
public void scan_series_should_update_the_last_scan_date()
|
2011-06-21 08:44:01 +03:00
|
|
|
{
|
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
|
|
|
|
Mocker.GetMock<SeriesProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(c => c.UpdateSeries(It.Is<Series>(s => s.LastDiskSync != null))).Verifiable();
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<EpisodeProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(c => c.GetEpisodeBySeries(It.IsAny<long>()))
|
|
|
|
.Returns(new List<Episode> { new Episode() });
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<DiskProvider>()
|
2011-10-23 03:13:49 +03:00
|
|
|
.Setup(c => c.FolderExists(It.IsAny<string>()))
|
|
|
|
.Returns(true);
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<MediaFileProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(c => c.GetSeriesFiles(It.IsAny<int>()))
|
|
|
|
.Returns(new List<EpisodeFile>());
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.Resolve<DiskScanProvider>().Scan(new Series());
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.VerifyAllMocks();
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-06-22 04:12:20 +03:00
|
|
|
}
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-06-22 08:44:57 +03:00
|
|
|
[Test]
|
|
|
|
public void cleanup_should_skip_existing_files()
|
2011-06-21 08:44:01 +03:00
|
|
|
{
|
2011-12-15 07:15:53 +03:00
|
|
|
WithStrictMocker();
|
2011-06-22 08:44:57 +03:00
|
|
|
var episodes = Builder<EpisodeFile>.CreateListOfSize(10).Build();
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<DiskProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(e => e.FileExists(It.IsAny<String>()))
|
|
|
|
.Returns(true);
|
2011-06-21 08:44:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
//Act
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.Resolve<DiskScanProvider>().CleanUp(episodes);
|
2011-06-21 08:44:01 +03:00
|
|
|
|
|
|
|
//Assert
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.VerifyAllMocks();
|
2011-06-21 08:44:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2011-06-22 08:44:57 +03:00
|
|
|
public void cleanup_should_delete_none_existing_files()
|
2011-06-21 08:44:01 +03:00
|
|
|
{
|
2011-12-15 07:15:53 +03:00
|
|
|
WithStrictMocker();
|
2011-06-22 08:44:57 +03:00
|
|
|
var episodes = Builder<EpisodeFile>.CreateListOfSize(10).Build();
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<DiskProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(e => e.FileExists(It.IsAny<String>()))
|
|
|
|
.Returns(false);
|
2011-06-22 04:12:20 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<EpisodeProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(e => e.GetEpisodesByFileId(It.IsAny<int>()))
|
|
|
|
.Returns(new List<Episode>());
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<MediaFileProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(e => e.Delete(It.IsAny<int>()));
|
2011-06-21 08:44:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
//Act
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.Resolve<DiskScanProvider>().CleanUp(episodes);
|
2011-06-21 08:44:01 +03:00
|
|
|
|
|
|
|
//Assert
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.VerifyAllMocks();
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<EpisodeProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Verify(e => e.GetEpisodesByFileId(It.IsAny<int>()), Times.Exactly(10));
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<MediaFileProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Verify(e => e.Delete(It.IsAny<int>()), Times.Exactly(10));
|
2011-06-21 08:44:01 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2011-06-22 08:44:57 +03:00
|
|
|
public void cleanup_should_delete_none_existing_files_remove_links_to_episodes()
|
2011-06-21 08:44:01 +03:00
|
|
|
{
|
2011-12-15 07:15:53 +03:00
|
|
|
WithStrictMocker();
|
2011-06-22 08:44:57 +03:00
|
|
|
var episodes = Builder<EpisodeFile>.CreateListOfSize(10).Build();
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<DiskProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(e => e.FileExists(It.IsAny<String>()))
|
|
|
|
.Returns(false);
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<EpisodeProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(e => e.GetEpisodesByFileId(It.IsAny<int>()))
|
|
|
|
.Returns(new List<Episode> { new Episode { EpisodeFileId = 10 }, new Episode { EpisodeFileId = 10 } });
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<EpisodeProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(e => e.UpdateEpisode(It.IsAny<Episode>()));
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<MediaFileProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(e => e.Delete(It.IsAny<int>()));
|
2011-06-22 04:12:20 +03:00
|
|
|
|
2012-02-28 09:18:11 +03:00
|
|
|
Mocker.GetMock<ConfigProvider>()
|
|
|
|
.SetupGet(s => s.AutoIgnorePreviouslyDownloadedEpisodes)
|
|
|
|
.Returns(true);
|
2011-06-21 08:44:01 +03:00
|
|
|
|
|
|
|
//Act
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.Resolve<DiskScanProvider>().CleanUp(episodes);
|
2011-06-21 08:44:01 +03:00
|
|
|
|
|
|
|
//Assert
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.VerifyAllMocks();
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<EpisodeProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Verify(e => e.GetEpisodesByFileId(It.IsAny<int>()), Times.Exactly(10));
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<EpisodeProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Verify(e => e.UpdateEpisode(It.Is<Episode>(g=>g.EpisodeFileId == 0)), Times.Exactly(20));
|
2011-06-21 09:34:45 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<MediaFileProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Verify(e => e.Delete(It.IsAny<int>()), Times.Exactly(10));
|
2011-06-21 08:44:01 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<MediaFileProvider>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Verify(e => e.Delete(It.IsAny<int>()), Times.Exactly(10));
|
2011-06-21 08:44:01 +03:00
|
|
|
|
|
|
|
}
|
2011-06-22 04:12:20 +03:00
|
|
|
|
2011-10-22 22:03:54 +03:00
|
|
|
[Test]
|
|
|
|
public void scan_series_should_log_warning_if_path_doesnt_exist_on_disk()
|
|
|
|
{
|
|
|
|
//Setup
|
2011-12-15 07:15:53 +03:00
|
|
|
WithStrictMocker();
|
2011-10-22 22:03:54 +03:00
|
|
|
|
|
|
|
var series = Builder<Series>.CreateNew()
|
|
|
|
.With(s => s.Path = @"C:\Test\TV\SeriesName\")
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<MediaFileProvider>()
|
2012-01-17 07:05:36 +03:00
|
|
|
.Setup(c => c.CleanUpDatabase());
|
|
|
|
|
2011-06-22 04:12:20 +03:00
|
|
|
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.GetMock<DiskProvider>()
|
2011-10-22 22:03:54 +03:00
|
|
|
.Setup(c => c.FolderExists(series.Path))
|
|
|
|
.Returns(false);
|
2011-06-22 04:12:20 +03:00
|
|
|
|
2011-10-22 22:03:54 +03:00
|
|
|
//Act
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.Resolve<DiskScanProvider>().Scan(series, series.Path);
|
2011-10-22 22:03:54 +03:00
|
|
|
|
|
|
|
//Assert
|
2011-12-15 07:15:53 +03:00
|
|
|
Mocker.VerifyAllMocks();
|
2011-12-20 03:58:26 +03:00
|
|
|
ExceptionVerification.ExpectedWarns(1);
|
2011-10-22 22:03:54 +03:00
|
|
|
}
|
2011-12-14 09:42:24 +03:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void move_should_not_move_file_if_source_and_destination_are_the_same_path()
|
|
|
|
{
|
|
|
|
var fakeSeries = Builder<Series>.CreateNew()
|
|
|
|
.With(s => s.SeriesId = 5)
|
|
|
|
.With(s => s.Title = "30 Rock")
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
var fakeEpisode = Builder<Episode>.CreateListOfSize(1)
|
|
|
|
.All()
|
|
|
|
.With(e => e.SeriesId = fakeSeries.SeriesId)
|
|
|
|
.With(e => e.SeasonNumber = 1)
|
|
|
|
.With(e => e.EpisodeNumber = 1)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
const string filename = @"30 Rock - S01E01 - TBD";
|
|
|
|
var fi = new FileInfo(Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", filename + ".avi"));
|
|
|
|
|
|
|
|
var file = Builder<EpisodeFile>.CreateNew()
|
|
|
|
.With(f => f.SeriesId = fakeSeries.SeriesId)
|
|
|
|
.With(f => f.Path = fi.FullName)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
Mocker.GetMock<SeriesProvider>()
|
|
|
|
.Setup(e => e.GetSeries(fakeSeries.SeriesId))
|
|
|
|
.Returns(fakeSeries);
|
|
|
|
|
|
|
|
Mocker.GetMock<EpisodeProvider>()
|
|
|
|
.Setup(e => e.GetEpisodesByFileId(file.EpisodeFileId))
|
|
|
|
.Returns(fakeEpisode);
|
|
|
|
|
|
|
|
Mocker.GetMock<MediaFileProvider>()
|
2012-01-22 22:25:59 +03:00
|
|
|
.Setup(e => e.GetNewFilename(fakeEpisode, fakeSeries.Title, It.IsAny<QualityTypes>(), It.IsAny<bool>()))
|
2011-12-14 09:42:24 +03:00
|
|
|
.Returns(filename);
|
|
|
|
|
|
|
|
Mocker.GetMock<MediaFileProvider>()
|
|
|
|
.Setup(e => e.CalculateFilePath(It.IsAny<Series>(), fakeEpisode.First().SeasonNumber, filename, ".avi"))
|
|
|
|
.Returns(fi);
|
|
|
|
|
|
|
|
//Act
|
|
|
|
var result = Mocker.Resolve<DiskScanProvider>().MoveEpisodeFile(file, false);
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
result.Should().BeFalse();
|
|
|
|
}
|
2012-04-18 00:44:20 +03:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void CleanUpDropFolder_should_do_nothing_if_no_files_are_found()
|
|
|
|
{
|
|
|
|
//Setup
|
|
|
|
var folder = @"C:\Test\DropDir\The Office";
|
|
|
|
|
|
|
|
Mocker.GetMock<DiskProvider>().Setup(s => s.GetFiles(folder, SearchOption.AllDirectories))
|
|
|
|
.Returns(new string[0]);
|
|
|
|
|
|
|
|
//Act
|
|
|
|
Mocker.Resolve<DiskScanProvider>().CleanUpDropFolder(folder);
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
Mocker.GetMock<MediaFileProvider>().Verify(v => v.GetFileByPath(It.IsAny<string>()), Times.Never());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void CleanUpDropFolder_should_do_nothing_if_no_conflicting_files_are_found()
|
|
|
|
{
|
|
|
|
//Setup
|
|
|
|
var folder = @"C:\Test\DropDir\The Office";
|
|
|
|
var filename = Path.Combine(folder, "NotAProblem.avi");
|
|
|
|
|
|
|
|
var episodeFile = Builder<EpisodeFile>.CreateNew()
|
|
|
|
.With(f => f.Path = filename.NormalizePath())
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
Mocker.GetMock<DiskProvider>().Setup(s => s.GetFiles(folder, SearchOption.AllDirectories))
|
|
|
|
.Returns(new string[] { filename });
|
|
|
|
|
|
|
|
Mocker.GetMock<MediaFileProvider>().Setup(s => s.GetFileByPath(filename))
|
|
|
|
.Returns(() => null);
|
|
|
|
|
|
|
|
//Act
|
|
|
|
Mocker.Resolve<DiskScanProvider>().CleanUpDropFolder(folder);
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
Mocker.GetMock<MediaFileProvider>().Verify(v => v.GetFileByPath(filename), Times.Once());
|
|
|
|
Mocker.GetMock<SeriesProvider>().Verify(v => v.GetSeries(It.IsAny<int>()), Times.Never());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void CleanUpDropFolder_should_move_file_if_a_conflict_is_found()
|
|
|
|
{
|
|
|
|
//Setup
|
|
|
|
var folder = @"C:\Test\DropDir\The Office";
|
|
|
|
var filename = Path.Combine(folder, "Problem.avi");
|
|
|
|
var seriesId = 12345;
|
|
|
|
var newFilename = "S01E01 - Title";
|
|
|
|
var newFilePath = @"C:\Test\TV\The Office\Season 01\S01E01 - Title.avi";
|
|
|
|
|
|
|
|
var episodeFile = Builder<EpisodeFile>.CreateNew()
|
|
|
|
.With(f => f.Path = filename.NormalizePath())
|
|
|
|
.With(f => f.SeriesId = seriesId)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
var series = Builder<Series>.CreateNew()
|
|
|
|
.With(s => s.SeriesId = seriesId)
|
|
|
|
.With(s => s.Title = "The Office")
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
var episode = Builder<Episode>.CreateListOfSize(1)
|
|
|
|
.All()
|
|
|
|
.With(e => e.SeriesId = seriesId)
|
|
|
|
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
Mocker.GetMock<MediaFileProvider>().Setup(v => v.GetFileByPath(filename))
|
|
|
|
.Returns(() => null);
|
|
|
|
|
|
|
|
Mocker.GetMock<DiskProvider>().Setup(s => s.GetFiles(folder, SearchOption.AllDirectories))
|
|
|
|
.Returns(new string[] { filename });
|
|
|
|
|
|
|
|
Mocker.GetMock<MediaFileProvider>().Setup(s => s.GetFileByPath(filename))
|
|
|
|
.Returns(episodeFile);
|
|
|
|
|
|
|
|
Mocker.GetMock<SeriesProvider>().Setup(s => s.GetSeries(It.IsAny<int>()))
|
|
|
|
.Returns(series);
|
|
|
|
|
|
|
|
Mocker.GetMock<EpisodeProvider>().Setup(s => s.GetEpisodesByFileId(episodeFile.EpisodeFileId))
|
|
|
|
.Returns(episode);
|
|
|
|
|
|
|
|
Mocker.GetMock<MediaFileProvider>().Setup(s => s.GetNewFilename(It.IsAny<IList<Episode>>(), series.Title, QualityTypes.Unknown, false))
|
|
|
|
.Returns(newFilename);
|
|
|
|
|
|
|
|
Mocker.GetMock<MediaFileProvider>().Setup(s => s.CalculateFilePath(It.IsAny<Series>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>()))
|
|
|
|
.Returns(new FileInfo(newFilePath));
|
|
|
|
|
|
|
|
Mocker.GetMock<DiskProvider>().Setup(s => s.MoveFile(episodeFile.Path, newFilePath));
|
|
|
|
|
|
|
|
//Act
|
|
|
|
Mocker.Resolve<DiskScanProvider>().CleanUpDropFolder(folder);
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
Mocker.GetMock<MediaFileProvider>().Verify(v => v.GetFileByPath(filename), Times.Once());
|
|
|
|
Mocker.GetMock<DiskProvider>().Verify(v => v.MoveFile(filename.NormalizePath(), newFilePath), Times.Once());
|
|
|
|
}
|
2011-06-21 08:44:01 +03:00
|
|
|
}
|
|
|
|
}
|