From 8f87f06ac6ebb5b81df21760663a1bd810803e54 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 13 Feb 2014 20:02:58 +0100 Subject: [PATCH] Processed PR Comments. Added tests for EpisodesWithCutoffUnmet. --- src/NzbDrone.Api/Wanted/CutoffModule.cs | 10 +- src/NzbDrone.Api/Wanted/MissingModule.cs | 8 +- .../NzbDrone.Core.Test.csproj | 1 + .../EpisodesWhereCutoffUnmetFixture.cs | 168 ++++++++++++++++++ .../EpisodesWithoutFilesFixture.cs | 41 ++++- src/NzbDrone.Core/Tv/EpisodeRepository.cs | 8 +- src/NzbDrone.Core/Tv/EpisodeService.cs | 10 +- .../Radio/RadioButtonCollectionView.js | 3 +- 8 files changed, 227 insertions(+), 22 deletions(-) create mode 100644 src/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWhereCutoffUnmetFixture.cs diff --git a/src/NzbDrone.Api/Wanted/CutoffModule.cs b/src/NzbDrone.Api/Wanted/CutoffModule.cs index b68dd5f10..130feb308 100644 --- a/src/NzbDrone.Api/Wanted/CutoffModule.cs +++ b/src/NzbDrone.Api/Wanted/CutoffModule.cs @@ -29,14 +29,18 @@ namespace NzbDrone.Api.Wanted SortKey = pagingResource.SortKey, SortDirection = pagingResource.SortDirection }; - + if (pagingResource.FilterKey == "monitored" && pagingResource.FilterValue == "false") + { pagingSpec.FilterExpression = v => v.Monitored == false || v.Series.Monitored == false; + } else + { pagingSpec.FilterExpression = v => v.Monitored == true && v.Series.Monitored == true; + } + + PagingResource resource = ApplyToPage(_episodeService.EpisodesWhereCutoffUnmet, pagingSpec); - PagingResource resource = ApplyToPage(_episodeService.GetCutoffUnmetEpisodes, pagingSpec); - resource.Records = resource.Records.LoadSubtype(e => e.SeriesId, _seriesRepository).ToList(); return resource; diff --git a/src/NzbDrone.Api/Wanted/MissingModule.cs b/src/NzbDrone.Api/Wanted/MissingModule.cs index 4a5c34a90..dd4d97f69 100644 --- a/src/NzbDrone.Api/Wanted/MissingModule.cs +++ b/src/NzbDrone.Api/Wanted/MissingModule.cs @@ -28,13 +28,17 @@ namespace NzbDrone.Api.Wanted SortKey = pagingResource.SortKey, SortDirection = pagingResource.SortDirection }; - + if (pagingResource.FilterKey == "monitored" && pagingResource.FilterValue == "false") + { pagingSpec.FilterExpression = v => v.Monitored == false || v.Series.Monitored == false; + } else + { pagingSpec.FilterExpression = v => v.Monitored == true && v.Series.Monitored == true; + } - PagingResource resource = ApplyToPage(v => _episodeService.GetMissingEpisodes(v), pagingSpec); + PagingResource resource = ApplyToPage(v => _episodeService.EpisodesWithoutFiles(v), pagingSpec); resource.Records = resource.Records.LoadSubtype(e => e.SeriesId, _seriesRepository).ToList(); diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index fe5a842f8..15b3c68ab 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -211,6 +211,7 @@ + diff --git a/src/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWhereCutoffUnmetFixture.cs b/src/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWhereCutoffUnmetFixture.cs new file mode 100644 index 000000000..9a8695887 --- /dev/null +++ b/src/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWhereCutoffUnmetFixture.cs @@ -0,0 +1,168 @@ +using System; +using FizzWare.NBuilder; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Core.Tv; +using NzbDrone.Core.Qualities; +using NzbDrone.Core.MediaFiles; + +namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests +{ + [TestFixture] + public class EpisodesWhereCutoffUnmetFixture : DbTest + { + private Series _monitoredSeries; + private Series _unmonitoredSeries; + private PagingSpec _pagingSpec; + + [SetUp] + public void Setup() + { + var qualityProfile = new QualityProfile + { + Cutoff = Quality.WEBDL720p, + Items = Qualities.QualityFixture.GetDefaultQualities() + }; + + _monitoredSeries = Builder.CreateNew() + .With(s => s.Id = 0) + .With(s => s.TvRageId = RandomNumber) + .With(s => s.Runtime = 30) + .With(s => s.Monitored = true) + .With(s => s.TitleSlug = "Title3") + .With(s => s.QualityProfile = qualityProfile) + .Build(); + + _unmonitoredSeries = Builder.CreateNew() + .With(s => s.Id = 0) + .With(s => s.TvdbId = RandomNumber) + .With(s => s.Runtime = 30) + .With(s => s.Monitored = false) + .With(s => s.TitleSlug = "Title2") + .With(s => s.QualityProfile = qualityProfile) + .Build(); + + _monitoredSeries.Id = Db.Insert(_monitoredSeries).Id; + _unmonitoredSeries.Id = Db.Insert(_unmonitoredSeries).Id; + + _pagingSpec = new PagingSpec + { + Page = 1, + PageSize = 10, + SortKey = "AirDate", + SortDirection = SortDirection.Ascending + }; + + var qualityMet = new EpisodeFile { Path = "a", Quality = new QualityModel { Quality = Quality.WEBDL720p } }; + var qualityUnmet = new EpisodeFile { Path = "b", Quality = new QualityModel { Quality = Quality.WEBDL480p } }; + + MediaFileRepository fileRepository = Mocker.Resolve(); + + qualityMet = fileRepository.Insert(qualityMet); + qualityUnmet = fileRepository.Insert(qualityUnmet); + + var monitoredSeriesEpisodes = Builder.CreateListOfSize(3) + .All() + .With(e => e.Id = 0) + .With(e => e.SeriesId = _monitoredSeries.Id) + .With(e => e.AirDateUtc = DateTime.Now.AddDays(-5)) + .With(e => e.Monitored = true) + .With(e => e.EpisodeFileId = qualityUnmet.Id) + .TheFirst(1) + .With(e => e.Monitored = false) + .With(e => e.EpisodeFileId = qualityMet.Id) + .TheLast(1) + .With(e => e.SeasonNumber = 0) + .Build(); + + var unmonitoredSeriesEpisodes = Builder.CreateListOfSize(3) + .All() + .With(e => e.Id = 0) + .With(e => e.SeriesId = _unmonitoredSeries.Id) + .With(e => e.AirDateUtc = DateTime.Now.AddDays(-5)) + .With(e => e.Monitored = true) + .With(e => e.EpisodeFileId = qualityUnmet.Id) + .TheFirst(1) + .With(e => e.Monitored = false) + .With(e => e.EpisodeFileId = qualityMet.Id) + .TheLast(1) + .With(e => e.SeasonNumber = 0) + .Build(); + + + var unairedEpisodes = Builder.CreateListOfSize(1) + .All() + .With(e => e.Id = 0) + .With(e => e.SeriesId = _monitoredSeries.Id) + .With(e => e.AirDateUtc = DateTime.Now.AddDays(5)) + .With(e => e.Monitored = true) + .With(e => e.EpisodeFileId = qualityUnmet.Id) + .Build(); + + Db.InsertMany(monitoredSeriesEpisodes); + Db.InsertMany(unmonitoredSeriesEpisodes); + Db.InsertMany(unairedEpisodes); + } + + private void GivenMonitoredFilterExpression() + { + _pagingSpec.FilterExpression = e => e.Monitored == true && e.Series.Monitored == true; + } + + private void GivenUnmonitoredFilterExpression() + { + _pagingSpec.FilterExpression = e => e.Monitored == false || e.Series.Monitored == false; + } + + [Test] + public void should_get_monitored_episodes() + { + GivenMonitoredFilterExpression(); + + var episodes = Subject.EpisodesWhereCutoffUnmet(_pagingSpec, false); + + episodes.Should().HaveCount(1); + } + + [Test] + [Ignore("Specials not implemented")] + public void should_get_episode_including_specials() + { + var episodes = Subject.EpisodesWhereCutoffUnmet(_pagingSpec, true); + + episodes.Should().HaveCount(2); + } + + [Test] + public void should_not_include_unmonitored_episodes() + { + GivenMonitoredFilterExpression(); + + var episodes = Subject.EpisodesWhereCutoffUnmet(_pagingSpec, false); + + episodes.Should().NotContain(e => e.Monitored == false); + } + + [Test] + public void should_not_contain_unmonitored_series() + { + GivenMonitoredFilterExpression(); + + var episodes = Subject.EpisodesWhereCutoffUnmet(_pagingSpec, false); + + episodes.Should().NotContain(e => e.SeriesId == _unmonitoredSeries.Id); + } + + [Test] + public void should_not_include_cutoff_met_episodes() + { + GivenMonitoredFilterExpression(); + + var episodes = Subject.EpisodesWhereCutoffUnmet(_pagingSpec, false); + + episodes.Should().NotContain(e => e.EpisodeFile.Value.Quality.Quality == Quality.WEBDL720p); + } + } +} diff --git a/src/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWithoutFilesFixture.cs b/src/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWithoutFilesFixture.cs index 50c4df605..5f7afc669 100644 --- a/src/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWithoutFilesFixture.cs +++ b/src/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWithoutFilesFixture.cs @@ -72,14 +72,37 @@ namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests .Build(); + var unairedEpisodes = Builder.CreateListOfSize(1) + .All() + .With(e => e.Id = 0) + .With(e => e.SeriesId = _monitoredSeries.Id) + .With(e => e.EpisodeFileId = 0) + .With(e => e.AirDateUtc = DateTime.Now.AddDays(5)) + .With(e => e.Monitored = true) + .Build(); + + Db.InsertMany(monitoredSeriesEpisodes); Db.InsertMany(unmonitoredSeriesEpisodes); + Db.InsertMany(unairedEpisodes); + } + + private void GivenMonitoredFilterExpression() + { + _pagingSpec.FilterExpression = e => e.Monitored == true && e.Series.Monitored == true; + } + + private void GivenUnmonitoredFilterExpression() + { + _pagingSpec.FilterExpression = e => e.Monitored == false || e.Series.Monitored == false; } [Test] public void should_get_monitored_episodes() { - var episodes = Subject.GetMissingEpisodes(_pagingSpec, false); + GivenMonitoredFilterExpression(); + + var episodes = Subject.EpisodesWithoutFiles(_pagingSpec, false); episodes.Records.Should().HaveCount(1); } @@ -88,7 +111,7 @@ namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests [Ignore("Specials not implemented")] public void should_get_episode_including_specials() { - var episodes = Subject.GetMissingEpisodes(_pagingSpec, true); + var episodes = Subject.EpisodesWithoutFiles(_pagingSpec, true); episodes.Records.Should().HaveCount(2); } @@ -96,7 +119,9 @@ namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests [Test] public void should_not_include_unmonitored_episodes() { - var episodes = Subject.GetMissingEpisodes(_pagingSpec, false); + GivenMonitoredFilterExpression(); + + var episodes = Subject.EpisodesWithoutFiles(_pagingSpec, false); episodes.Records.Should().NotContain(e => e.Monitored == false); } @@ -104,17 +129,19 @@ namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests [Test] public void should_not_contain_unmonitored_series() { - var episodes = Subject.GetMissingEpisodes(_pagingSpec, false); + GivenMonitoredFilterExpression(); + + var episodes = Subject.EpisodesWithoutFiles(_pagingSpec, false); episodes.Records.Should().NotContain(e => e.SeriesId == _unmonitoredSeries.Id); } [Test] - public void should_have_count_of_one() + public void should_not_return_unaired() { - var episodes = Subject.GetMissingEpisodes(_pagingSpec, false); + var episodes = Subject.EpisodesWithoutFiles(_pagingSpec, false); - episodes.TotalRecords.Should().Be(1); + episodes.TotalRecords.Should().Be(4); } } } diff --git a/src/NzbDrone.Core/Tv/EpisodeRepository.cs b/src/NzbDrone.Core/Tv/EpisodeRepository.cs index d33f173ca..32a9840da 100644 --- a/src/NzbDrone.Core/Tv/EpisodeRepository.cs +++ b/src/NzbDrone.Core/Tv/EpisodeRepository.cs @@ -17,8 +17,8 @@ namespace NzbDrone.Core.Tv List GetEpisodes(int seriesId); List GetEpisodes(int seriesId, int seasonNumber); List GetEpisodeByFileId(int fileId); - PagingSpec GetMissingEpisodes(PagingSpec pagingSpec, bool includeSpecials); - List GetCutoffUnmetEpisodes(PagingSpec pagingSpec, bool includeSpecials); + PagingSpec EpisodesWithoutFiles(PagingSpec pagingSpec, bool includeSpecials); + List EpisodesWhereCutoffUnmet(PagingSpec pagingSpec, bool includeSpecials); Episode FindEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber); List EpisodesBetweenDates(DateTime startDate, DateTime endDate); void SetMonitoredFlat(Episode episode, bool monitored); @@ -82,7 +82,7 @@ namespace NzbDrone.Core.Tv return Query.Where(e => e.EpisodeFileId == fileId).ToList(); } - public PagingSpec GetMissingEpisodes(PagingSpec pagingSpec, bool includeSpecials) + public PagingSpec EpisodesWithoutFiles(PagingSpec pagingSpec, bool includeSpecials) { var currentTime = DateTime.UtcNow; var startingSeasonNumber = 1; @@ -98,7 +98,7 @@ namespace NzbDrone.Core.Tv return pagingSpec; } - public List GetCutoffUnmetEpisodes(PagingSpec pagingSpec, bool includeSpecials) + public List EpisodesWhereCutoffUnmet(PagingSpec pagingSpec, bool includeSpecials) { var currentTime = DateTime.UtcNow; var startingSeasonNumber = 1; diff --git a/src/NzbDrone.Core/Tv/EpisodeService.cs b/src/NzbDrone.Core/Tv/EpisodeService.cs index 82dcb84f6..f974f5867 100644 --- a/src/NzbDrone.Core/Tv/EpisodeService.cs +++ b/src/NzbDrone.Core/Tv/EpisodeService.cs @@ -21,8 +21,8 @@ namespace NzbDrone.Core.Tv Episode FindEpisode(int seriesId, String date); List GetEpisodeBySeries(int seriesId); List GetEpisodesBySeason(int seriesId, int seasonNumber); - PagingSpec GetMissingEpisodes(PagingSpec pagingSpec); - PagingSpec GetCutoffUnmetEpisodes(PagingSpec pagingSpec); + PagingSpec EpisodesWithoutFiles(PagingSpec pagingSpec); + PagingSpec EpisodesWhereCutoffUnmet(PagingSpec pagingSpec); List GetEpisodesByFileId(int episodeFileId); void UpdateEpisode(Episode episode); void SetEpisodeMonitored(int episodeId, bool monitored); @@ -109,12 +109,12 @@ namespace NzbDrone.Core.Tv public PagingSpec EpisodesWithoutFiles(PagingSpec pagingSpec) { - var episodeResult = _episodeRepository.GetMissingEpisodes(pagingSpec, false); + var episodeResult = _episodeRepository.EpisodesWithoutFiles(pagingSpec, false); return episodeResult; } - public PagingSpec GetCutoffUnmetEpisodes(PagingSpec pagingSpec) + public PagingSpec EpisodesWhereCutoffUnmet(PagingSpec pagingSpec) { var allSpec = new PagingSpec { @@ -123,7 +123,7 @@ namespace NzbDrone.Core.Tv FilterExpression = pagingSpec.FilterExpression }; - var allItems = _episodeRepository.GetCutoffUnmetEpisodes(allSpec, false); + var allItems = _episodeRepository.EpisodesWhereCutoffUnmet(allSpec, false); var qualityProfileComparers = _qualityProfileRepository.All().ToDictionary(v => v.Id, v => new { Profile = v, Comparer = new QualityModelComparer(v) }); diff --git a/src/UI/Shared/Toolbar/Radio/RadioButtonCollectionView.js b/src/UI/Shared/Toolbar/Radio/RadioButtonCollectionView.js index e3793b341..c6b66abc8 100644 --- a/src/UI/Shared/Toolbar/Radio/RadioButtonCollectionView.js +++ b/src/UI/Shared/Toolbar/Radio/RadioButtonCollectionView.js @@ -22,8 +22,9 @@ define( setActive: function () { var storedKey = this.menu.defaultAction; - if (this.menu.storeState) + if (this.menu.storeState) { storedKey = Config.getValue(this.menu.menuKey, storedKey); + } if (!storedKey) return;