From 45304b1bbc89e4101aa702d7e1599fb360f085f6 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sun, 2 Feb 2014 00:38:19 +0100 Subject: [PATCH] Solved error in quality comparison for HistoryService --- .../HistorySpecificationFixture.cs | 12 ++-- .../HistoryTests/HistoryServiceFixture.cs | 59 +++++++++++++++++++ .../NzbDrone.Core.Test.csproj | 1 + .../RssSync/HistorySpecification.cs | 2 +- src/NzbDrone.Core/History/HistoryService.cs | 9 ++- 5 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs diff --git a/src/NzbDrone.Core.Test/DecisionEngineTests/HistorySpecificationFixture.cs b/src/NzbDrone.Core.Test/DecisionEngineTests/HistorySpecificationFixture.cs index 4013271c9..54c8ff6e0 100644 --- a/src/NzbDrone.Core.Test/DecisionEngineTests/HistorySpecificationFixture.cs +++ b/src/NzbDrone.Core.Test/DecisionEngineTests/HistorySpecificationFixture.cs @@ -62,9 +62,9 @@ public void Setup() _upgradableQuality = new QualityModel(Quality.SDTV, false); _notupgradableQuality = new QualityModel(Quality.HDTV1080p, true); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(1)).Returns(_notupgradableQuality); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(2)).Returns(_notupgradableQuality); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(3)).Returns(null); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(It.IsAny(), 1)).Returns(_notupgradableQuality); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(It.IsAny(), 2)).Returns(_notupgradableQuality); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(It.IsAny(), 3)).Returns(null); Mocker.GetMock() .Setup(c => c.GetDownloadClient()).Returns(Mocker.GetMock().Object); @@ -72,12 +72,12 @@ public void Setup() private void WithFirstReportUpgradable() { - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(1)).Returns(_upgradableQuality); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(It.IsAny(), 1)).Returns(_upgradableQuality); } private void WithSecondReportUpgradable() { - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(2)).Returns(_upgradableQuality); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(It.IsAny(), 2)).Returns(_upgradableQuality); } private void GivenSabnzbdDownloadClient() @@ -134,7 +134,7 @@ public void should_not_be_upgradable_if_episode_is_of_same_quality_as_existing() _parseResultSingle.ParsedEpisodeInfo.Quality = new QualityModel(Quality.WEBDL1080p, false); _upgradableQuality = new QualityModel(Quality.WEBDL1080p, false); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(1)).Returns(_upgradableQuality); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(It.IsAny(), 1)).Returns(_upgradableQuality); _upgradeHistory.IsSatisfiedBy(_parseResultSingle, null).Should().BeFalse(); } diff --git a/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs b/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs new file mode 100644 index 000000000..717f9f915 --- /dev/null +++ b/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs @@ -0,0 +1,59 @@ +using NUnit.Framework; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Core.History; +using NzbDrone.Core.Qualities; +using System.Collections.Generic; +using NzbDrone.Core.Test.Qualities; +using FluentAssertions; + +namespace NzbDrone.Core.Test.HistoryTests +{ + public class HistoryServiceFixture : CoreTest + { + private QualityProfile _profile; + private QualityProfile _profileCustom; + + [SetUp] + public void Setup() + { + _profile = new QualityProfile { Cutoff = Quality.WEBDL720p, Items = QualityFixture.GetDefaultQualities() }; + _profileCustom = new QualityProfile { Cutoff = Quality.WEBDL720p, Items = QualityFixture.GetDefaultQualities(Quality.DVD) }; + } + + [Test] + public void should_return_null_if_no_history() + { + Mocker.GetMock() + .Setup(v => v.GetBestQualityInHistory(2)) + .Returns(new List()); + + var quality = Subject.GetBestQualityInHistory(_profile, 2); + + quality.Should().BeNull(); + } + + [Test] + public void should_return_best_quality() + { + Mocker.GetMock() + .Setup(v => v.GetBestQualityInHistory(2)) + .Returns(new List { new QualityModel(Quality.DVD), new QualityModel(Quality.Bluray1080p) }); + + var quality = Subject.GetBestQualityInHistory(_profile, 2); + + quality.Should().Be(new QualityModel(Quality.Bluray1080p)); + } + + [Test] + public void should_return_best_quality_with_custom_order() + { + Mocker.GetMock() + .Setup(v => v.GetBestQualityInHistory(2)) + .Returns(new List { new QualityModel(Quality.DVD), new QualityModel(Quality.Bluray1080p) }); + + var quality = Subject.GetBestQualityInHistory(_profileCustom, 2); + + quality.Should().Be(new QualityModel(Quality.DVD)); + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 3e406aef7..568e6feb6 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -130,6 +130,7 @@ + diff --git a/src/NzbDrone.Core/DecisionEngine/Specifications/RssSync/HistorySpecification.cs b/src/NzbDrone.Core/DecisionEngine/Specifications/RssSync/HistorySpecification.cs index a4c3b3745..808bba106 100644 --- a/src/NzbDrone.Core/DecisionEngine/Specifications/RssSync/HistorySpecification.cs +++ b/src/NzbDrone.Core/DecisionEngine/Specifications/RssSync/HistorySpecification.cs @@ -59,7 +59,7 @@ public virtual bool IsSatisfiedBy(RemoteEpisode subject, SearchCriteriaBase sear foreach (var episode in subject.Episodes) { - var bestQualityInHistory = _historyService.GetBestQualityInHistory(episode.Id); + var bestQualityInHistory = _historyService.GetBestQualityInHistory(subject.Series.QualityProfile, episode.Id); if (bestQualityInHistory != null) { _logger.Trace("Comparing history quality with report. History is {0}", bestQualityInHistory); diff --git a/src/NzbDrone.Core/History/HistoryService.cs b/src/NzbDrone.Core/History/HistoryService.cs index 07e32b824..4b3282b8f 100644 --- a/src/NzbDrone.Core/History/HistoryService.cs +++ b/src/NzbDrone.Core/History/HistoryService.cs @@ -16,7 +16,7 @@ public interface IHistoryService List All(); void Purge(); void Trim(); - QualityModel GetBestQualityInHistory(int episodeId); + QualityModel GetBestQualityInHistory(QualityProfile qualityProfile, int episodeId); PagingSpec Paged(PagingSpec pagingSpec); List BetweenDates(DateTime startDate, DateTime endDate, HistoryEventType eventType); List Failed(); @@ -81,9 +81,12 @@ public virtual void Trim() _historyRepository.Trim(); } - public QualityModel GetBestQualityInHistory(int episodeId) + public QualityModel GetBestQualityInHistory(QualityProfile qualityProfile, int episodeId) { - return _historyRepository.GetBestQualityInHistory(episodeId).OrderByDescending(q => q).FirstOrDefault(); + var comparer = new QualityModelComparer(qualityProfile); + return _historyRepository.GetBestQualityInHistory(episodeId) + .OrderByDescending(q => q, comparer) + .FirstOrDefault(); } public void Handle(EpisodeGrabbedEvent message)