Fixed a bunch of specs that handled propers and cutoffs

This commit is contained in:
Mark McDowall 2013-09-10 22:45:08 -07:00
parent edfd527901
commit fc43db164b
8 changed files with 235 additions and 64 deletions

View File

@ -11,24 +11,38 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
public class CutoffSpecificationFixture : CoreTest<QualityUpgradableSpecification> public class CutoffSpecificationFixture : CoreTest<QualityUpgradableSpecification>
{ {
[Test] [Test]
public void IsUpgradePossible_should_return_true_if_current_episode_is_less_than_cutoff() public void should_return_true_if_current_episode_is_less_than_cutoff()
{ {
Subject.CutoffNotMet(new QualityProfile { Cutoff = Quality.Bluray1080p }, Subject.CutoffNotMet(new QualityProfile { Cutoff = Quality.Bluray1080p },
new QualityModel(Quality.DVD, true)).Should().BeTrue(); new QualityModel(Quality.DVD, true)).Should().BeTrue();
} }
[Test] [Test]
public void IsUpgradePossible_should_return_false_if_current_episode_is_equal_to_cutoff() public void should_return_false_if_current_episode_is_equal_to_cutoff()
{ {
Subject.CutoffNotMet(new QualityProfile { Cutoff = Quality.HDTV720p }, Subject.CutoffNotMet(new QualityProfile { Cutoff = Quality.HDTV720p },
new QualityModel(Quality.HDTV720p, true)).Should().BeFalse(); new QualityModel(Quality.HDTV720p, true)).Should().BeFalse();
} }
[Test] [Test]
public void IsUpgradePossible_should_return_false_if_current_episode_is_greater_than_cutoff() public void should_return_false_if_current_episode_is_greater_than_cutoff()
{ {
Subject.CutoffNotMet(new QualityProfile { Cutoff = Quality.HDTV720p }, Subject.CutoffNotMet(new QualityProfile { Cutoff = Quality.HDTV720p },
new QualityModel(Quality.Bluray1080p, true)).Should().BeFalse(); new QualityModel(Quality.Bluray1080p, true)).Should().BeFalse();
} }
[Test]
public void should_return_true_when_new_episode_is_proper_but_existing_is_not()
{
Subject.CutoffNotMet(new QualityProfile { Cutoff = Quality.HDTV720p },
new QualityModel(Quality.HDTV720p, false), new QualityModel(Quality.HDTV720p, true)).Should().BeTrue();
}
[Test]
public void should_return_false_if_cutoff_is_met_and_quality_is_higher()
{
Subject.CutoffNotMet(new QualityProfile { Cutoff = Quality.HDTV720p },
new QualityModel(Quality.HDTV720p, true), new QualityModel(Quality.Bluray1080p, true)).Should().BeFalse();
}
} }
} }

View File

@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.DecisionEngine.Specifications;
using NzbDrone.Core.DecisionEngine.Specifications.RssSync;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.Tv;
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.DecisionEngineTests.RssSync
{
[TestFixture]
public class ProperSpecificationFixture : CoreTest<ProperSpecification>
{
private RemoteEpisode _parseResultMulti;
private RemoteEpisode _parseResultSingle;
private EpisodeFile _firstFile;
private EpisodeFile _secondFile;
[SetUp]
public void Setup()
{
Mocker.Resolve<QualityUpgradableSpecification>();
_firstFile = new EpisodeFile { Quality = new QualityModel(Quality.Bluray1080p, false), DateAdded = DateTime.Now };
_secondFile = new EpisodeFile { Quality = new QualityModel(Quality.Bluray1080p, false), DateAdded = DateTime.Now };
var singleEpisodeList = new List<Episode> { new Episode { EpisodeFile = _firstFile, EpisodeFileId = 1 }, new Episode { EpisodeFile = null } };
var doubleEpisodeList = new List<Episode> { new Episode { EpisodeFile = _firstFile, EpisodeFileId = 1 }, new Episode { EpisodeFile = _secondFile, EpisodeFileId = 1 }, new Episode { EpisodeFile = null } };
var fakeSeries = Builder<Series>.CreateNew()
.With(c => c.QualityProfile = new QualityProfile { Cutoff = Quality.Bluray1080p })
.Build();
_parseResultMulti = new RemoteEpisode
{
Series = fakeSeries,
ParsedEpisodeInfo = new ParsedEpisodeInfo { Quality = new QualityModel(Quality.DVD, true) },
Episodes = doubleEpisodeList
};
_parseResultSingle = new RemoteEpisode
{
Series = fakeSeries,
ParsedEpisodeInfo = new ParsedEpisodeInfo { Quality = new QualityModel(Quality.DVD, true) },
Episodes = singleEpisodeList
};
}
private void WithFirstFileUpgradable()
{
_firstFile.Quality = new QualityModel(Quality.SDTV);
}
private void GivenAutoDownloadPropers()
{
Mocker.GetMock<IConfigService>()
.Setup(s => s.AutoDownloadPropers)
.Returns(true);
}
[Test]
public void should_return_false_when_episodeFile_was_added_more_than_7_days_ago()
{
_firstFile.Quality.Quality = Quality.DVD;
_firstFile.DateAdded = DateTime.Today.AddDays(-30);
Subject.IsSatisfiedBy(_parseResultSingle, null).Should().BeFalse();
}
[Test]
public void should_return_false_when_first_episodeFile_was_added_more_than_7_days_ago()
{
_firstFile.Quality.Quality = Quality.DVD;
_secondFile.Quality.Quality = Quality.DVD;
_firstFile.DateAdded = DateTime.Today.AddDays(-30);
Subject.IsSatisfiedBy(_parseResultMulti, null).Should().BeFalse();
}
[Test]
public void should_return_false_when_second_episodeFile_was_added_more_than_7_days_ago()
{
_firstFile.Quality.Quality = Quality.DVD;
_secondFile.Quality.Quality = Quality.DVD;
_secondFile.DateAdded = DateTime.Today.AddDays(-30);
Subject.IsSatisfiedBy(_parseResultMulti, null).Should().BeFalse();
}
[Test]
public void should_return_true_when_episodeFile_was_added_more_than_7_days_ago_but_proper_is_for_better_quality()
{
WithFirstFileUpgradable();
_firstFile.DateAdded = DateTime.Today.AddDays(-30);
Subject.IsSatisfiedBy(_parseResultSingle, null).Should().BeTrue();
}
[Test]
public void should_return_true_when_episodeFile_was_added_more_than_7_days_ago_but_is_for_search()
{
WithFirstFileUpgradable();
_firstFile.DateAdded = DateTime.Today.AddDays(-30);
Subject.IsSatisfiedBy(_parseResultSingle, new SingleEpisodeSearchCriteria()).Should().BeTrue();
}
[Test]
public void should_return_false_when_proper_but_auto_download_propers_is_false()
{
_firstFile.Quality.Quality = Quality.DVD;
_firstFile.DateAdded = DateTime.Today;
Subject.IsSatisfiedBy(_parseResultSingle, null).Should().BeFalse();
}
[Test]
public void should_return_true_when_episodeFile_was_added_today()
{
GivenAutoDownloadPropers();
_firstFile.Quality.Quality = Quality.DVD;
_firstFile.DateAdded = DateTime.Today;
Subject.IsSatisfiedBy(_parseResultSingle, null).Should().BeTrue();
}
}
}

View File

@ -124,52 +124,5 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
_parseResultSingle.ParsedEpisodeInfo.Quality = new QualityModel(Quality.WEBDL1080p, false); _parseResultSingle.ParsedEpisodeInfo.Quality = new QualityModel(Quality.WEBDL1080p, false);
_upgradeDisk.IsSatisfiedBy(_parseResultSingle, null).Should().BeFalse(); _upgradeDisk.IsSatisfiedBy(_parseResultSingle, null).Should().BeFalse();
} }
[Test]
public void should_return_false_when_episodeFile_was_added_more_than_7_days_ago()
{
_firstFile.Quality.Quality = Quality.DVD;
_firstFile.DateAdded = DateTime.Today.AddDays(-30);
_upgradeDisk.IsSatisfiedBy(_parseResultSingle, null).Should().BeFalse();
}
[Test]
public void should_return_false_when_first_episodeFile_was_added_more_than_7_days_ago()
{
_firstFile.Quality.Quality = Quality.DVD;
_secondFile.Quality.Quality = Quality.DVD;
_firstFile.DateAdded = DateTime.Today.AddDays(-30);
_upgradeDisk.IsSatisfiedBy(_parseResultMulti, null).Should().BeFalse();
}
[Test]
public void should_return_false_when_second_episodeFile_was_added_more_than_7_days_ago()
{
_firstFile.Quality.Quality = Quality.DVD;
_secondFile.Quality.Quality = Quality.DVD;
_secondFile.DateAdded = DateTime.Today.AddDays(-30);
_upgradeDisk.IsSatisfiedBy(_parseResultMulti, null).Should().BeFalse();
}
[Test]
public void should_return_true_when_episodeFile_was_added_more_than_7_days_ago_but_proper_is_for_better_quality()
{
WithFirstFileUpgradable();
_firstFile.DateAdded = DateTime.Today.AddDays(-30);
_upgradeDisk.IsSatisfiedBy(_parseResultSingle, null).Should().BeTrue();
}
[Test]
public void should_return_true_when_episodeFile_was_added_more_than_7_days_ago_but_is_for_search()
{
WithFirstFileUpgradable();
_firstFile.DateAdded = DateTime.Today.AddDays(-30);
_upgradeDisk.IsSatisfiedBy(_parseResultSingle, new SingleEpisodeSearchCriteria()).Should().BeTrue();
}
} }
} }

View File

@ -112,6 +112,7 @@
<Compile Include="Datastore\SQLiteMigrationHelperTests\DuplicateFixture.cs" /> <Compile Include="Datastore\SQLiteMigrationHelperTests\DuplicateFixture.cs" />
<Compile Include="DecisionEngineTests\CutoffSpecificationFixture.cs" /> <Compile Include="DecisionEngineTests\CutoffSpecificationFixture.cs" />
<Compile Include="DecisionEngineTests\NotRestrictedReleaseSpecificationFixture.cs" /> <Compile Include="DecisionEngineTests\NotRestrictedReleaseSpecificationFixture.cs" />
<Compile Include="DecisionEngineTests\RssSync\ProperSpecificationFixture.cs" />
<Compile Include="Download\DownloadApprovedReportsTests\DownloadApprovedFixture.cs" /> <Compile Include="Download\DownloadApprovedReportsTests\DownloadApprovedFixture.cs" />
<Compile Include="Download\DownloadApprovedReportsTests\GetQualifiedReportsFixture.cs" /> <Compile Include="Download\DownloadApprovedReportsTests\GetQualifiedReportsFixture.cs" />
<Compile Include="Download\DownloadClientTests\BlackholeProviderFixture.cs" /> <Compile Include="Download\DownloadClientTests\BlackholeProviderFixture.cs" />

View File

@ -10,16 +10,15 @@ namespace NzbDrone.Core.DecisionEngine
{ {
bool IsUpgradable(QualityModel currentQuality, QualityModel newQuality = null); bool IsUpgradable(QualityModel currentQuality, QualityModel newQuality = null);
bool CutoffNotMet(QualityProfile profile, QualityModel currentQuality, QualityModel newQuality = null); bool CutoffNotMet(QualityProfile profile, QualityModel currentQuality, QualityModel newQuality = null);
bool IsProperUpgrade(QualityModel currentQuality, QualityModel newQuality);
} }
public class QualityUpgradableSpecification : IQualityUpgradableSpecification public class QualityUpgradableSpecification : IQualityUpgradableSpecification
{ {
private readonly IConfigService _configService;
private readonly Logger _logger; private readonly Logger _logger;
public QualityUpgradableSpecification(IConfigService configService, Logger logger) public QualityUpgradableSpecification(Logger logger)
{ {
_configService = configService;
_logger = logger; _logger = logger;
} }
@ -33,9 +32,8 @@ namespace NzbDrone.Core.DecisionEngine
return false; return false;
} }
if (currentQuality.Quality == newQuality.Quality && newQuality.Proper && _configService.AutoDownloadPropers) if (IsProperUpgrade(currentQuality, newQuality))
{ {
_logger.Trace("Upgrading existing item to proper.");
return true; return true;
} }
} }
@ -47,11 +45,27 @@ namespace NzbDrone.Core.DecisionEngine
{ {
if (currentQuality.Quality >= profile.Cutoff) if (currentQuality.Quality >= profile.Cutoff)
{ {
if (newQuality != null && IsProperUpgrade(currentQuality, newQuality))
{
return true;
}
_logger.Trace("Existing item meets cut-off. skipping."); _logger.Trace("Existing item meets cut-off. skipping.");
return false; return false;
} }
return true; return true;
} }
public bool IsProperUpgrade(QualityModel currentQuality, QualityModel newQuality)
{
if (currentQuality.Quality == newQuality.Quality && newQuality > currentQuality)
{
_logger.Trace("New quality is a proper for existing quality");
return true;
}
return false;
}
} }
} }

View File

@ -0,0 +1,59 @@
using System;
using System.Linq;
using NLog;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.DecisionEngine.Specifications.RssSync
{
public class ProperSpecification : IDecisionEngineSpecification
{
private readonly QualityUpgradableSpecification _qualityUpgradableSpecification;
private readonly IConfigService _configService;
private readonly Logger _logger;
public ProperSpecification(QualityUpgradableSpecification qualityUpgradableSpecification, IConfigService configService, Logger logger)
{
_qualityUpgradableSpecification = qualityUpgradableSpecification;
_configService = configService;
_logger = logger;
}
public string RejectionReason
{
get
{
return "Proper for old episode";
}
}
public virtual bool IsSatisfiedBy(RemoteEpisode subject, SearchCriteriaBase searchCriteria)
{
if (searchCriteria != null)
{
return true;
}
foreach (var file in subject.Episodes.Where(c => c.EpisodeFileId != 0).Select(c => c.EpisodeFile.Value))
{
if (_qualityUpgradableSpecification.IsProperUpgrade(file.Quality, subject.ParsedEpisodeInfo.Quality))
{
if (file.DateAdded < DateTime.Today.AddDays(-7))
{
_logger.Trace("Proper for old file, skipping: {0}", subject);
return false;
}
if (!_configService.AutoDownloadPropers)
{
_logger.Trace("Auto downloading of propers is disabled");
return false;
}
}
}
return true;
}
}
}

View File

@ -35,15 +35,6 @@ namespace NzbDrone.Core.DecisionEngine.Specifications
{ {
return false; return false;
} }
if (searchCriteria == null &&
subject.ParsedEpisodeInfo.Quality.Quality == file.Quality.Quality &&
subject.ParsedEpisodeInfo.Quality.Proper &&
file.DateAdded < DateTime.Today.AddDays(-7))
{
_logger.Trace("Proper for old file, skipping: {0}", subject);
return false;
}
} }
return true; return true;

View File

@ -187,6 +187,7 @@
<Compile Include="DecisionEngine\IDecisionEngineSpecification.cs" /> <Compile Include="DecisionEngine\IDecisionEngineSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\CutoffSpecification.cs" /> <Compile Include="DecisionEngine\Specifications\CutoffSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\NotRestrictedReleaseSpecification.cs" /> <Compile Include="DecisionEngine\Specifications\NotRestrictedReleaseSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\RssSync\ProperSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\Search\SeasonMatchSpecification.cs" /> <Compile Include="DecisionEngine\Specifications\Search\SeasonMatchSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\Search\DailyEpisodeMatchSpecification.cs" /> <Compile Include="DecisionEngine\Specifications\Search\DailyEpisodeMatchSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\AcceptableSizeSpecification.cs" /> <Compile Include="DecisionEngine\Specifications\AcceptableSizeSpecification.cs" />