diff --git a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTests/EpisodeProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTests/EpisodeProviderTest.cs index 3362b3e7f..8b4aae5c8 100644 --- a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTests/EpisodeProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTests/EpisodeProviderTest.cs @@ -120,7 +120,6 @@ namespace NzbDrone.Core.Test.ProviderTests.EpisodeProviderTests //Act Mocker.Resolve().GetEpisode(1); } - [Test] public void GetEpisodesBySeason_success() @@ -181,7 +180,6 @@ namespace NzbDrone.Core.Test.ProviderTests.EpisodeProviderTests actualCount.Should().Be(episodeCount); } - [Test] public void RefreshEpisodeInfo_should_set_older_than_1900_to_null() { @@ -1518,5 +1516,26 @@ namespace NzbDrone.Core.Test.ProviderTests.EpisodeProviderTests result.Where(e => e.Ignored).Should().HaveCount(episodeCount - 1); result.Single(e => e.SeasonNumber == 1).Ignored.Should().BeFalse(); } + + [Test] + public void GetEpisode_with_EpisodeFile_should_have_quality_set_properly() + { + WithRealDb(); + + var fakeSeries = Builder.CreateNew().Build(); + var fakeFile = Builder.CreateNew().With(f => f.EpisodeFileId).With(c => c.Quality = QualityTypes.WEBDL1080p).Build(); + var fakeEpisodes = Builder.CreateListOfSize(5) + .All().With(e => e.SeriesId = 1).TheFirst(1).With(e => e.EpisodeFileId = 1).With(e => e.EpisodeFile = fakeFile).Build(); + + Db.Insert(fakeSeries); + Db.InsertMany(fakeEpisodes); + Db.Insert(fakeFile); + + //Act + var episode = Mocker.Resolve().GetEpisode(1); + + //Assert + episode.EpisodeFile.Quality.Should().Be(QualityTypes.WEBDL1080p); + } } } diff --git a/NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs index 5a70735d1..7a9cac64f 100644 --- a/NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs @@ -158,41 +158,72 @@ namespace NzbDrone.Core.Test.ProviderTests var episodes = Builder.CreateListOfSize(10).Build(); var historyEpisode = episodes[6]; - var history0 = Builder.CreateNew() - .With(h => h.Quality = QualityTypes.DVD) - .With(h => h.IsProper = true) - .With(h => h.EpisodeId = historyEpisode.EpisodeId) - .Build(); + var history = Builder + .CreateListOfSize(5) + .All() + .With(h => h.EpisodeId = historyEpisode.EpisodeId) + .With(h => h.SeriesId = historyEpisode.SeriesId) + .TheFirst(1) + .With(h => h.Quality = QualityTypes.DVD) + .With(h => h.IsProper = true) + .TheNext(1) + .With(h => h.Quality = QualityTypes.Bluray720p) + .With(h => h.IsProper = false) + .TheNext(1) + .With(h => h.Quality = QualityTypes.Bluray720p) + .With(h => h.IsProper = true) + .TheNext(1) + .With(h => h.Quality = QualityTypes.Bluray720p) + .With(h => h.IsProper = false) + .TheNext(1) + .With(h => h.Quality = QualityTypes.SDTV) + .With(h => h.IsProper = true) + .Build(); + + Db.InsertMany(history); + Db.InsertMany(episodes); - var history1 = Builder.CreateNew() - .With(h => h.Quality = QualityTypes.Bluray720p) - .With(h => h.IsProper = false) - .With(h => h.EpisodeId = historyEpisode.EpisodeId) - .Build(); + //Act + var result = Mocker.Resolve() + .GetBestQualityInHistory(historyEpisode.SeriesId, historyEpisode.SeasonNumber, historyEpisode.EpisodeNumber); - var history2 = Builder.CreateNew() - .With(h => h.Quality = QualityTypes.Bluray720p) - .With(h => h.IsProper = true) - .With(h => h.EpisodeId = historyEpisode.EpisodeId) - .Build(); + //Assert + result.Should().NotBeNull(); + result.Quality.Should().Be(QualityTypes.Bluray720p); + result.Proper.Should().BeTrue(); + } - var history3 = Builder.CreateNew() - .With(h => h.Quality = QualityTypes.Bluray720p) - .With(h => h.IsProper = false) - .With(h => h.EpisodeId = historyEpisode.EpisodeId) - .Build(); + [Test] + public void GetBestQualityInHistory_should_return_highest_weighted_result() + { + WithRealDb(); - var history4 = Builder.CreateNew() - .With(h => h.Quality = QualityTypes.SDTV) - .With(h => h.IsProper = true) - .With(h => h.EpisodeId = historyEpisode.EpisodeId) - .Build(); + var episodes = Builder.CreateListOfSize(10).Build(); + var historyEpisode = episodes[6]; - Db.Insert(history0); - Db.Insert(history1); - Db.Insert(history2); - Db.Insert(history2); - Db.Insert(history4); + var history = Builder + .CreateListOfSize(5) + .All() + .With(h => h.EpisodeId = historyEpisode.EpisodeId) + .With(h => h.SeriesId = historyEpisode.SeriesId) + .TheFirst(1) + .With(h => h.Quality = QualityTypes.DVD) + .With(h => h.IsProper = true) + .TheNext(1) + .With(h => h.Quality = QualityTypes.WEBDL720p) + .With(h => h.IsProper = false) + .TheNext(1) + .With(h => h.Quality = QualityTypes.WEBDL720p) + .With(h => h.IsProper = true) + .TheNext(1) + .With(h => h.Quality = QualityTypes.WEBDL1080p) + .With(h => h.IsProper = false) + .TheNext(1) + .With(h => h.Quality = QualityTypes.SDTV) + .With(h => h.IsProper = true) + .Build(); + + Db.InsertMany(history); Db.InsertMany(episodes); //Act @@ -201,8 +232,8 @@ namespace NzbDrone.Core.Test.ProviderTests //Assert result.Should().NotBeNull(); - result.Quality.Should().Be(QualityTypes.Bluray720p); - result.Proper.Should().BeTrue(); + result.Quality.Should().Be(QualityTypes.WEBDL1080p); + result.Proper.Should().BeFalse(); } [Test] diff --git a/NzbDrone.Core/Providers/HistoryProvider.cs b/NzbDrone.Core/Providers/HistoryProvider.cs index 2fd9a944c..7f72d08c3 100644 --- a/NzbDrone.Core/Providers/HistoryProvider.cs +++ b/NzbDrone.Core/Providers/HistoryProvider.cs @@ -60,17 +60,16 @@ namespace NzbDrone.Core.Providers public virtual QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber) { - var quality = _database.SingleOrDefault(@"SELECT TOP 1 History.Quality , History.IsProper FROM History - INNER JOIN Episodes ON History.EpisodeId = Episodes.EpisodeId - WHERE Episodes.seriesId = @0 AND + var quality = _database.Fetch(@"SELECT History.Quality , History.IsProper as Proper FROM History + INNER JOIN Episodes ON History.EpisodeId = Episodes.EpisodeId + WHERE Episodes.seriesId = @0 AND Episodes.SeasonNumber = @1 AND - Episodes.EpisodeNumber = @2 - ORDER BY History.Quality DESC, History.IsProper DESC" + Episodes.EpisodeNumber = @2" , seriesId, seasonNumber, episodeNumber); - if (quality == null) return null; + var best = quality.OrderByDescending(q => q).FirstOrDefault(); - return new QualityModel((QualityTypes)quality.Quality, quality.IsProper); + return best; } public virtual void Delete(int historyId)