2011-06-22 08:44:57 +03:00
|
|
|
using System;
|
2013-03-04 02:18:43 +03:00
|
|
|
using System.Linq;
|
2011-06-22 08:44:57 +03:00
|
|
|
using FizzWare.NBuilder;
|
|
|
|
using FluentAssertions;
|
|
|
|
using Moq;
|
|
|
|
using NUnit.Framework;
|
2011-11-13 07:07:06 +03:00
|
|
|
using NzbDrone.Common;
|
2013-03-01 10:03:41 +03:00
|
|
|
using NzbDrone.Core.MediaFiles;
|
2013-04-15 04:41:39 +03:00
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-02-27 06:19:22 +03:00
|
|
|
using NzbDrone.Core.Qualities;
|
2013-02-19 09:01:03 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
2011-06-22 08:44:57 +03:00
|
|
|
using NzbDrone.Core.Providers;
|
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
|
2012-08-29 18:34:51 +03:00
|
|
|
namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
|
2011-06-22 08:44:57 +03:00
|
|
|
{
|
2013-04-01 09:22:16 +03:00
|
|
|
|
2013-04-15 08:27:32 +03:00
|
|
|
public class ImportFileFixture : CoreTest<DiskScanService>
|
2011-06-22 08:44:57 +03:00
|
|
|
{
|
2012-10-14 03:36:16 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
|
|
|
|
private long _fileSize = 80.Megabytes();
|
|
|
|
private Series _fakeSeries;
|
2013-05-13 03:36:23 +03:00
|
|
|
private Episode[] _fakeEpisodes;
|
2013-04-15 04:41:39 +03:00
|
|
|
private Episode _fakeEpisode;
|
2013-02-08 04:47:24 +03:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
_fakeSeries = Builder<Series>
|
2013-02-08 04:47:24 +03:00
|
|
|
.CreateNew()
|
|
|
|
.Build();
|
2012-10-20 04:42:42 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
_fakeEpisode = Builder<Episode>
|
|
|
|
.CreateNew()
|
2013-05-13 03:36:23 +03:00
|
|
|
.With(c => c.EpisodeFileId = 0)
|
2013-04-15 04:41:39 +03:00
|
|
|
.Build();
|
2011-06-22 08:44:57 +03:00
|
|
|
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
_fakeEpisodes = Builder<Episode>.CreateListOfSize(2)
|
|
|
|
.All()
|
|
|
|
.With(c => c.SeasonNumber = 3)
|
2013-05-13 03:36:23 +03:00
|
|
|
.With(c => c.EpisodeFileId = 1)
|
2013-04-15 04:41:39 +03:00
|
|
|
.With(e => e.EpisodeFile = new EpisodeFile())
|
2013-05-13 03:36:23 +03:00
|
|
|
.BuildList().ToArray();
|
2013-04-01 09:22:16 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenNewFile();
|
2011-06-22 08:44:57 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenVideoDuration(TimeSpan.FromMinutes(20));
|
2013-04-01 09:22:16 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenFileSize(_fileSize);
|
2011-06-22 08:44:57 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
private void GivenFileSize(long size)
|
2011-06-22 08:44:57 +03:00
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
_fileSize = size;
|
2011-06-22 08:44:57 +03:00
|
|
|
|
2013-05-11 02:53:50 +03:00
|
|
|
Mocker.GetMock<IDiskProvider>()
|
2013-05-29 07:10:23 +03:00
|
|
|
.Setup(d => d.GetFileSize(It.IsAny<String>()))
|
2013-04-15 04:41:39 +03:00
|
|
|
.Returns(size);
|
2011-06-22 08:44:57 +03:00
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
private void GivenVideoDuration(TimeSpan duration)
|
2011-06-22 08:44:57 +03:00
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
Mocker.GetMock<IVideoFileInfoReader>()
|
|
|
|
.Setup(d => d.GetRunTime(It.IsAny<String>()))
|
|
|
|
.Returns(duration);
|
2011-06-22 08:44:57 +03:00
|
|
|
}
|
|
|
|
|
2013-04-01 09:22:16 +03:00
|
|
|
|
2013-05-13 03:36:23 +03:00
|
|
|
private void GivenEpisodes(Episode[] episodes, QualityModel quality)
|
2011-06-22 08:44:57 +03:00
|
|
|
{
|
2013-05-13 03:36:23 +03:00
|
|
|
foreach (var episode in episodes)
|
|
|
|
{
|
|
|
|
if (episode.EpisodeFile == null)
|
|
|
|
{
|
|
|
|
episode.EpisodeFileId = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
episode.EpisodeFileId = episode.EpisodeFile.Value.Id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
Mocker.GetMock<IParsingService>()
|
|
|
|
.Setup(c => c.GetEpisodes(It.IsAny<string>(), It.IsAny<Series>()))
|
|
|
|
.Returns(new LocalEpisode
|
|
|
|
{
|
|
|
|
Episodes = episodes.ToList(),
|
|
|
|
Quality = quality
|
|
|
|
});
|
2011-06-22 08:44:57 +03:00
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
private void GivenNewFile()
|
2011-06-22 08:44:57 +03:00
|
|
|
{
|
2013-03-01 10:03:41 +03:00
|
|
|
Mocker.GetMock<IMediaFileService>()
|
2011-06-22 08:44:57 +03:00
|
|
|
.Setup(p => p.Exists(It.IsAny<String>()))
|
|
|
|
.Returns(false);
|
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
[Test]
|
|
|
|
public void import_new_file_should_succeed()
|
2011-08-26 09:23:21 +03:00
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenEpisodes(new[] { _fakeEpisode }, new QualityModel());
|
2011-08-26 09:23:21 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
|
|
|
VerifyFileImport(result);
|
|
|
|
}
|
2011-08-26 09:23:21 +03:00
|
|
|
|
2013-04-01 09:22:16 +03:00
|
|
|
|
2011-08-26 09:23:21 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
[Test]
|
|
|
|
public void import_new_file_with_same_quality_should_succeed()
|
|
|
|
{
|
|
|
|
_fakeEpisode.EpisodeFile = new EpisodeFile { Quality = new QualityModel(Quality.SDTV) };
|
2013-05-13 03:36:23 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenEpisodes(new[] { _fakeEpisode }, new QualityModel(Quality.SDTV));
|
2013-04-01 09:22:16 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
|
|
|
VerifyFileImport(result);
|
2011-08-26 09:23:21 +03:00
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
[Test]
|
|
|
|
public void import_new_file_with_better_quality_should_succeed()
|
2011-08-26 09:23:21 +03:00
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
_fakeEpisode.EpisodeFile = new EpisodeFile { Quality = new QualityModel(Quality.SDTV) };
|
2013-05-13 03:36:23 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenEpisodes(new[] { _fakeEpisode }, new QualityModel(Quality.HDTV1080p));
|
2011-08-26 09:23:21 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
|
|
|
VerifyFileImport(result);
|
2011-08-26 09:23:21 +03:00
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
[Test]
|
|
|
|
public void import_new_file_episode_has_better_quality_should_skip()
|
2011-08-26 09:23:21 +03:00
|
|
|
{
|
2013-05-13 03:36:23 +03:00
|
|
|
_fakeEpisode.EpisodeFile = new EpisodeFile { Quality = new QualityModel(Quality.HDTV1080p), Id = 1 };
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenEpisodes(new[] { _fakeEpisode }, new QualityModel(Quality.SDTV));
|
2011-08-26 09:23:21 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
2011-08-26 09:23:21 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
VerifySkipImport(result);
|
|
|
|
}
|
2011-08-26 09:23:21 +03:00
|
|
|
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
[Test]
|
|
|
|
public void import_unparsable_file_should_skip()
|
|
|
|
{
|
|
|
|
Mocker.GetMock<IParsingService>()
|
|
|
|
.Setup(c => c.GetEpisodes(It.IsAny<string>(), It.IsAny<Series>()))
|
|
|
|
.Returns<LocalEpisode>(null);
|
2013-04-01 09:22:16 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
2011-08-26 09:23:21 +03:00
|
|
|
|
2013-04-01 09:22:16 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
VerifySkipImport(result);
|
2011-08-26 09:23:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2013-04-15 04:41:39 +03:00
|
|
|
public void import_existing_file_should_skip()
|
2011-08-26 09:23:21 +03:00
|
|
|
{
|
2013-03-01 10:03:41 +03:00
|
|
|
Mocker.GetMock<IMediaFileService>()
|
2011-08-26 09:23:21 +03:00
|
|
|
.Setup(p => p.Exists(It.IsAny<String>()))
|
2013-04-15 04:41:39 +03:00
|
|
|
.Returns(true);
|
2011-08-26 09:23:21 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
2013-04-01 09:22:16 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
VerifySkipImport(result);
|
2011-08-26 09:23:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2013-04-15 04:41:39 +03:00
|
|
|
public void import_file_with_no_episode_in_db_should_skip()
|
2011-08-26 09:23:21 +03:00
|
|
|
{
|
2013-05-13 03:36:23 +03:00
|
|
|
GivenEpisodes(new Episode[0], new QualityModel());
|
2011-08-26 09:23:21 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
2011-08-26 09:23:21 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
VerifySkipImport(result);
|
2011-08-26 09:23:21 +03:00
|
|
|
}
|
|
|
|
|
2012-10-17 10:39:06 +03:00
|
|
|
[Test]
|
2013-04-15 04:41:39 +03:00
|
|
|
public void import_new_multi_part_file_episode_with_better_quality_than_existing()
|
2012-10-17 10:39:06 +03:00
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
_fakeEpisodes[0].EpisodeFile = new EpisodeFile();
|
|
|
|
_fakeEpisodes[1].EpisodeFile = new EpisodeFile();
|
2013-04-01 09:22:16 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
_fakeEpisodes[0].EpisodeFile = new EpisodeFile { Quality = new QualityModel(Quality.SDTV) };
|
|
|
|
_fakeEpisodes[1].EpisodeFile = new EpisodeFile { Quality = new QualityModel(Quality.SDTV) };
|
2012-10-21 03:19:30 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenEpisodes(_fakeEpisodes, new QualityModel(Quality.HDTV1080p));
|
2012-10-17 10:39:06 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
2012-10-17 10:39:06 +03:00
|
|
|
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
VerifyFileImport(result);
|
2012-10-17 10:39:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2013-04-15 04:41:39 +03:00
|
|
|
public void skip_import_new_multi_part_file_episode_existing_has_better_quality()
|
2012-10-17 10:39:06 +03:00
|
|
|
{
|
2013-05-13 03:36:23 +03:00
|
|
|
_fakeEpisodes[0].EpisodeFile = new EpisodeFile { Quality = new QualityModel(Quality.HDTV1080p), Id = 1 };
|
|
|
|
_fakeEpisodes[1].EpisodeFile = new EpisodeFile { Quality = new QualityModel(Quality.HDTV1080p), Id = 1 };
|
2012-10-17 10:39:06 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenEpisodes(_fakeEpisodes, new QualityModel(Quality.SDTV));
|
2012-10-17 10:39:06 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
2012-10-21 03:19:30 +03:00
|
|
|
|
2012-10-17 10:39:06 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
VerifySkipImport(result);
|
2012-10-17 10:39:06 +03:00
|
|
|
}
|
2013-01-21 03:35:42 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
|
2013-01-21 03:35:42 +03:00
|
|
|
[Test]
|
2013-04-15 04:41:39 +03:00
|
|
|
public void should_skip_if_file_size_is_under_70MB_and_runTime_under_3_minutes()
|
2013-01-21 03:35:42 +03:00
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenFileSize(50.Megabytes());
|
|
|
|
GivenVideoDuration(TimeSpan.FromMinutes(1));
|
2013-01-21 03:35:42 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenEpisodes(new[] { _fakeEpisode }, new QualityModel(Quality.HDTV1080p));
|
2013-01-21 03:35:42 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
2013-01-21 03:35:42 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
VerifySkipImport(result);
|
2013-01-21 03:35:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2013-01-26 10:18:42 +03:00
|
|
|
public void should_import_if_file_size_is_under_70MB_but_runTime_over_3_minutes()
|
2013-01-21 03:35:42 +03:00
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenFileSize(50.Megabytes());
|
|
|
|
GivenVideoDuration(TimeSpan.FromMinutes(20));
|
2013-01-21 03:35:42 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenEpisodes(new[] { _fakeEpisode }, new QualityModel(Quality.HDTV1080p));
|
2013-01-21 03:35:42 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
2013-01-21 03:35:42 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
VerifyFileImport(result);
|
2013-05-11 02:53:50 +03:00
|
|
|
Mocker.GetMock<IDiskProvider>().Verify(p => p.DeleteFile(It.IsAny<string>()), Times.Never());
|
2013-01-21 03:35:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2013-01-26 10:18:42 +03:00
|
|
|
public void should_import_if_file_size_is_over_70MB_but_runTime_under_3_minutes()
|
2013-01-21 03:35:42 +03:00
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenFileSize(100.Megabytes());
|
|
|
|
GivenVideoDuration(TimeSpan.FromMinutes(1));
|
2013-01-21 03:35:42 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenEpisodes(new[] { _fakeEpisode }, new QualityModel(Quality.HDTV1080p));
|
2013-01-21 03:35:42 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
2013-01-21 03:35:42 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
VerifyFileImport(result);
|
2013-01-21 03:35:42 +03:00
|
|
|
}
|
|
|
|
|
2013-02-08 04:47:24 +03:00
|
|
|
[Test]
|
|
|
|
public void should_import_special_even_if_file_size_is_under_70MB_and_runTime_under_3_minutes()
|
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenFileSize(10.Megabytes());
|
|
|
|
GivenVideoDuration(TimeSpan.FromMinutes(1));
|
2013-02-08 04:47:24 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
_fakeEpisode.SeasonNumber = 0;
|
2013-02-08 04:47:24 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenEpisodes(new[] { _fakeEpisode }, new QualityModel(Quality.HDTV1080p));
|
2013-02-08 04:47:24 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
2013-02-08 04:47:24 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
VerifyFileImport(result);
|
2013-02-08 04:47:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2013-04-15 04:41:39 +03:00
|
|
|
public void should_skip_if_daily_series_with_file_size_is_under_70MB_and_runTime_under_3_minutes()
|
2013-02-08 04:47:24 +03:00
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenFileSize(10.Megabytes());
|
|
|
|
GivenVideoDuration(TimeSpan.FromMinutes(1));
|
2013-02-08 04:47:24 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
_fakeEpisode.SeasonNumber = 0;
|
|
|
|
_fakeSeries.SeriesType = SeriesTypes.Daily;
|
2013-02-08 04:47:24 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
GivenEpisodes(new[] { _fakeEpisode }, new QualityModel(Quality.HDTV1080p));
|
2013-02-08 04:47:24 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
var result = Subject.ImportFile(_fakeSeries, "file.ext");
|
2013-02-08 04:47:24 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
VerifySkipImport(result);
|
2013-02-08 04:47:24 +03:00
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
private void VerifyFileImport(EpisodeFile result)
|
2013-01-21 03:35:42 +03:00
|
|
|
{
|
|
|
|
result.Should().NotBeNull();
|
2013-04-15 04:41:39 +03:00
|
|
|
result.SeriesId.Should().Be(_fakeSeries.Id);
|
|
|
|
result.Size.Should().Be(_fileSize);
|
2013-01-21 03:35:42 +03:00
|
|
|
result.DateAdded.Should().HaveDay(DateTime.Now.Day);
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
Mocker.GetMock<IMediaFileService>().Verify(c => c.Add(result), Times.Once());
|
2013-01-21 03:35:42 +03:00
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
private void VerifySkipImport(EpisodeFile result)
|
2013-01-21 03:35:42 +03:00
|
|
|
{
|
|
|
|
result.Should().BeNull();
|
2013-03-01 10:03:41 +03:00
|
|
|
Mocker.GetMock<IMediaFileService>().Verify(p => p.Add(It.IsAny<EpisodeFile>()), Times.Never());
|
2013-01-21 03:35:42 +03:00
|
|
|
}
|
2011-06-22 08:44:57 +03:00
|
|
|
}
|
|
|
|
}
|