You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Radarr/src/NzbDrone.Core.Test/DecisionEngineTests/DownloadDecisionMakerFixtur...

302 lines
12 KiB

using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
using NzbDrone.Test.Common;
using FizzWare.NBuilder;
namespace NzbDrone.Core.Test.DecisionEngineTests
{
[TestFixture]
public class DownloadDecisionMakerFixture : CoreTest<DownloadDecisionMaker>
{
private List<ReleaseInfo> _reports;
private RemoteMovie _remoteEpisode;
private MappingResult _mappingResult;
private Mock<IDecisionEngineSpecification> _pass1;
private Mock<IDecisionEngineSpecification> _pass2;
private Mock<IDecisionEngineSpecification> _pass3;
private Mock<IDecisionEngineSpecification> _fail1;
private Mock<IDecisionEngineSpecification> _fail2;
private Mock<IDecisionEngineSpecification> _fail3;
[SetUp]
public void Setup()
{
_pass1 = new Mock<IDecisionEngineSpecification>();
_pass2 = new Mock<IDecisionEngineSpecification>();
_pass3 = new Mock<IDecisionEngineSpecification>();
_fail1 = new Mock<IDecisionEngineSpecification>();
_fail2 = new Mock<IDecisionEngineSpecification>();
_fail3 = new Mock<IDecisionEngineSpecification>();
_pass1.Setup(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null)).Returns(Decision.Accept);
_pass2.Setup(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null)).Returns(Decision.Accept);
_pass3.Setup(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null)).Returns(Decision.Accept);
_fail1.Setup(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null)).Returns(Decision.Reject("fail1"));
_fail2.Setup(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null)).Returns(Decision.Reject("fail2"));
_fail3.Setup(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null)).Returns(Decision.Reject("fail3"));
_reports = new List<ReleaseInfo> { new ReleaseInfo { Title = "Trolls.2016.720p.WEB-DL.DD5.1.H264-FGT" } };
_remoteEpisode = new RemoteMovie {
Movie = new Movie(),
ParsedMovieInfo = new ParsedMovieInfo()
};
_mappingResult = new MappingResult {Movie = new Movie(), MappingResultType = MappingResultType.Success};
_mappingResult.RemoteMovie = _remoteEpisode;
Mocker.GetMock<IParsingService>()
.Setup(c => c.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), It.IsAny<SearchCriteriaBase>())).Returns(_mappingResult);
}
private void GivenSpecifications(params Mock<IDecisionEngineSpecification>[] mocks)
{
Mocker.SetConstant<IEnumerable<IDecisionEngineSpecification>>(mocks.Select(c => c.Object));
}
[Test]
public void should_call_all_specifications()
{
GivenSpecifications(_pass1, _pass2, _pass3, _fail1, _fail2, _fail3);
Subject.GetRssDecision(_reports).ToList();
_fail1.Verify(c => c.IsSatisfiedBy(_remoteEpisode, null), Times.Once());
_fail2.Verify(c => c.IsSatisfiedBy(_remoteEpisode, null), Times.Once());
_fail3.Verify(c => c.IsSatisfiedBy(_remoteEpisode, null), Times.Once());
_pass1.Verify(c => c.IsSatisfiedBy(_remoteEpisode, null), Times.Once());
_pass2.Verify(c => c.IsSatisfiedBy(_remoteEpisode, null), Times.Once());
_pass3.Verify(c => c.IsSatisfiedBy(_remoteEpisode, null), Times.Once());
}
[Test]
public void should_return_rejected_if_single_specs_fail()
{
GivenSpecifications(_fail1);
var result = Subject.GetRssDecision(_reports);
result.Single().Approved.Should().BeFalse();
}
[Test]
public void should_return_rejected_if_one_of_specs_fail()
{
GivenSpecifications(_pass1, _fail1, _pass2, _pass3);
var result = Subject.GetRssDecision(_reports);
result.Single().Approved.Should().BeFalse();
}
[Test]
public void should_return_pass_if_all_specs_pass()
{
GivenSpecifications(_pass1, _pass2, _pass3);
var result = Subject.GetRssDecision(_reports);
result.Single().Approved.Should().BeTrue();
}
[Test]
public void should_have_same_number_of_rejections_as_specs_that_failed()
{
GivenSpecifications(_pass1, _pass2, _pass3, _fail1, _fail2, _fail3);
var result = Subject.GetRssDecision(_reports);
result.Single().Rejections.Should().HaveCount(3);
}
[Test]
public void should_not_attempt_to_map_episode_if_not_parsable()
{
GivenSpecifications(_pass1, _pass2, _pass3);
_reports[0].Title = "Not parsable";
_mappingResult.MappingResultType = MappingResultType.NotParsable;
var results = Subject.GetRssDecision(_reports).ToList();
Mocker.GetMock<IParsingService>().Verify(c => c.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), It.IsAny<SearchCriteriaBase>()), Times.Never());
_pass1.Verify(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null), Times.Never());
_pass2.Verify(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null), Times.Never());
_pass3.Verify(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null), Times.Never());
results.Should().NotBeEmpty();
}
[Test]
public void should_not_attempt_to_map_episode_series_title_is_blank()
{
GivenSpecifications(_pass1, _pass2, _pass3);
_reports[0].Title = "1937 - Snow White and the Seven Dwarves";
_mappingResult.MappingResultType = MappingResultType.NotParsable;
var results = Subject.GetRssDecision(_reports).ToList();
Mocker.GetMock<IParsingService>().Verify(c => c.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), It.IsAny<SearchCriteriaBase>()), Times.Never());
_pass1.Verify(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null), Times.Never());
_pass2.Verify(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null), Times.Never());
_pass3.Verify(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null), Times.Never());
results.Should().NotBeEmpty();
}
[Test]
public void should_not_attempt_to_make_decision_if_series_is_unknown()
{
GivenSpecifications(_pass1, _pass2, _pass3);
_remoteEpisode.Movie = null;
_mappingResult.MappingResultType = MappingResultType.TitleNotFound;
Subject.GetRssDecision(_reports);
_pass1.Verify(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null), Times.Never());
_pass2.Verify(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null), Times.Never());
_pass3.Verify(c => c.IsSatisfiedBy(It.IsAny<RemoteMovie>(), null), Times.Never());
}
[Test]
public void broken_report_shouldnt_blowup_the_process()
{
GivenSpecifications(_pass1);
Mocker.GetMock<IParsingService>().Setup(c => c.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), It.IsAny<SearchCriteriaBase>()))
.Throws<TestException>();
_reports = new List<ReleaseInfo>
{
new ReleaseInfo{Title = "Trolls.2016.720p.WEB-DL.DD5.1.H264-FGT"},
new ReleaseInfo{Title = "Trolls.2016.720p.WEB-DL.DD5.1.H264-FGT"},
new ReleaseInfo{Title = "Trolls.2016.720p.WEB-DL.DD5.1.H264-FGT"}
};
Subject.GetRssDecision(_reports);
Mocker.GetMock<IParsingService>().Verify(c => c.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), It.IsAny<SearchCriteriaBase>()), Times.Exactly(_reports.Count));
ExceptionVerification.ExpectedErrors(3);
}
[Test]
public void should_return_unknown_series_rejection_if_series_is_unknown()
{
GivenSpecifications(_pass1, _pass2, _pass3);
_remoteEpisode.Movie = null;
var result = Subject.GetRssDecision(_reports);
result.Should().HaveCount(1);
}
[Test]
[Ignore( "Series")]
public void should_only_include_reports_for_requested_episodes()
{
var series = Builder<Series>.CreateNew().Build();
var episodes = Builder<Episode>.CreateListOfSize(2)
.All()
.With(v => v.SeriesId, series.Id)
.With(v => v.Series, series)
.With(v => v.SeasonNumber, 1)
.With(v => v.SceneSeasonNumber, 2)
.BuildList();
var criteria = new SeasonSearchCriteria { Episodes = episodes.Take(1).ToList(), SeasonNumber = 1 };
var reports = episodes.Select(v =>
new ReleaseInfo()
{
Title = string.Format("{0}.S{1:00}E{2:00}.720p.WEB-DL-DRONE", series.Title, v.SceneSeasonNumber, v.SceneEpisodeNumber)
}).ToList();
Mocker.GetMock<IParsingService>()
.Setup(v => v.Map(It.IsAny<ParsedEpisodeInfo>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<SearchCriteriaBase>()))
.Returns<ParsedEpisodeInfo, int, int, SearchCriteriaBase>((p,tvdbid,tvrageid,c) =>
new RemoteEpisode
{
DownloadAllowed = true,
ParsedEpisodeInfo = p,
Series = series,
Episodes = episodes.Where(v => v.SceneEpisodeNumber == p.EpisodeNumbers.First()).ToList()
});
Mocker.SetConstant<IEnumerable<IDecisionEngineSpecification>>(new List<IDecisionEngineSpecification>
{
Mocker.Resolve<NzbDrone.Core.DecisionEngine.Specifications.Search.EpisodeRequestedSpecification>()
});
var decisions = Subject.GetSearchDecision(reports, criteria);
var approvedDecisions = decisions.Where(v => v.Approved).ToList();
approvedDecisions.Count.Should().Be(1);
}
[Test]
public void should_not_allow_download_if_series_is_unknown()
{
GivenSpecifications(_pass1, _pass2, _pass3);
_remoteEpisode.Movie = null;
_mappingResult.MappingResultType = MappingResultType.TitleNotFound;
var result = Subject.GetRssDecision(_reports);
result.Should().HaveCount(1);
result.First().RemoteMovie.DownloadAllowed.Should().BeFalse();
}
[Test]
[Ignore("Series")]
public void should_not_allow_download_if_no_episodes_found()
{
GivenSpecifications(_pass1, _pass2, _pass3);
_remoteEpisode.Movie = null;
var result = Subject.GetRssDecision(_reports);
result.Should().HaveCount(1);
result.First().RemoteMovie.DownloadAllowed.Should().BeFalse();
}
[Test]
public void should_return_a_decision_when_exception_is_caught()
{
GivenSpecifications(_pass1);
Mocker.GetMock<IParsingService>().Setup(c => c.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), It.IsAny<SearchCriteriaBase>()))
.Throws<TestException>();
_reports = new List<ReleaseInfo>
{
new ReleaseInfo{Title = "Trolls.2016.720p.WEB-DL.DD5.1.H264-FGT"},
};
Subject.GetRssDecision(_reports).Should().HaveCount(1);
ExceptionVerification.ExpectedErrors(1);
}
}
}