From 5aa02eb15c9c57953b5f8c68b59cc7058c2ff3cc Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 23 May 2017 22:04:56 -0700 Subject: [PATCH] Cleanup/fix EpisodeMonitoredService Fixed: Unmonitor episodes when the season is unmonitored when adding the series Fixes #1852 --- .../SetEpisodeMontitoredFixture.cs | 40 +++++++++++++++++++ .../Tv/EpisodeMonitoredService.cs | 30 +++++--------- 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/NzbDrone.Core.Test/TvTests/EpisodeMonitoredServiceTests/SetEpisodeMontitoredFixture.cs b/src/NzbDrone.Core.Test/TvTests/EpisodeMonitoredServiceTests/SetEpisodeMontitoredFixture.cs index 058a09b86..0cf020314 100644 --- a/src/NzbDrone.Core.Test/TvTests/EpisodeMonitoredServiceTests/SetEpisodeMontitoredFixture.cs +++ b/src/NzbDrone.Core.Test/TvTests/EpisodeMonitoredServiceTests/SetEpisodeMontitoredFixture.cs @@ -194,6 +194,46 @@ public void should_ignore_episodes_when_season_is_not_monitored() .Verify(v => v.UpdateEpisodes(It.Is>(l => l.All(e => !e.Monitored)))); } + [Test] + public void should_should_not_monitor_episodes_if_season_is_not_monitored() + { + _series = Builder.CreateNew() + .With(s => s.Seasons = Builder.CreateListOfSize(2) + .TheFirst(1) + .With(n => n.Monitored = true) + .TheLast(1) + .With(n => n.Monitored = false) + .Build() + .ToList()) + .Build(); + + var episodes = Builder.CreateListOfSize(10) + .All() + .With(e => e.Monitored = true) + .With(e => e.EpisodeFileId = 0) + .With(e => e.AirDateUtc = DateTime.UtcNow.AddDays(-7)) + .TheFirst(5) + .With(e => e.SeasonNumber = 1) + .TheLast(5) + .With(e => e.SeasonNumber = 2) + .BuildList(); + + Mocker.GetMock() + .Setup(s => s.GetEpisodeBySeries(It.IsAny())) + .Returns(episodes); + + Subject.SetEpisodeMonitoredStatus(_series, new MonitoringOptions + { + IgnoreEpisodesWithFiles = true, + IgnoreEpisodesWithoutFiles = false + }); + + VerifyMonitored(e => e.SeasonNumber == 1); + VerifyNotMonitored(e => e.SeasonNumber == 2); + VerifySeasonMonitored(s => s.SeasonNumber == 1); + VerifySeasonNotMonitored(s => s.SeasonNumber == 2); + } + private void VerifyMonitored(Func predicate) { Mocker.GetMock() diff --git a/src/NzbDrone.Core/Tv/EpisodeMonitoredService.cs b/src/NzbDrone.Core/Tv/EpisodeMonitoredService.cs index b15c130be..f186ade10 100644 --- a/src/NzbDrone.Core/Tv/EpisodeMonitoredService.cs +++ b/src/NzbDrone.Core/Tv/EpisodeMonitoredService.cs @@ -34,10 +34,9 @@ public void SetEpisodeMonitoredStatus(Series series, MonitoringOptions monitorin if (monitoringOptions.IgnoreEpisodesWithFiles) { - _logger.Debug("Ignoring Episodes with Files"); + _logger.Debug("Unmonitoring Episodes with Files"); ToggleEpisodesMonitoredState(episodes.Where(e => e.HasFile), false); } - else { _logger.Debug("Monitoring Episodes with Files"); @@ -46,10 +45,9 @@ public void SetEpisodeMonitoredStatus(Series series, MonitoringOptions monitorin if (monitoringOptions.IgnoreEpisodesWithoutFiles) { - _logger.Debug("Ignoring Episodes without Files"); + _logger.Debug("Unmonitoring Episodes without Files"); ToggleEpisodesMonitoredState(episodes.Where(e => !e.HasFile && e.AirDateUtc.HasValue && e.AirDateUtc.Value.Before(DateTime.UtcNow)), false); } - else { _logger.Debug("Monitoring Episodes without Files"); @@ -62,31 +60,21 @@ public void SetEpisodeMonitoredStatus(Series series, MonitoringOptions monitorin { var season = s; - if (season.Monitored) + // If the season is unmonitored we should unmonitor all episodes in that season + + if (!season.Monitored) { - if (!monitoringOptions.IgnoreEpisodesWithFiles && !monitoringOptions.IgnoreEpisodesWithoutFiles) - { - ToggleEpisodesMonitoredState(episodes.Where(e => e.SeasonNumber == season.SeasonNumber), true); - } + _logger.Debug("Unmonitoring all episodes in season {0}", season.SeasonNumber); + ToggleEpisodesMonitoredState(episodes.Where(e => e.SeasonNumber == season.SeasonNumber), false); } - else - { - if (!monitoringOptions.IgnoreEpisodesWithFiles && !monitoringOptions.IgnoreEpisodesWithoutFiles) - { - ToggleEpisodesMonitoredState(episodes.Where(e => e.SeasonNumber == season.SeasonNumber), false); - } - - else if (season.SeasonNumber == 0) - { - ToggleEpisodesMonitoredState(episodes.Where(e => e.SeasonNumber == season.SeasonNumber), false); - } - } + // If the season is not the latest season and all it's episodes are unmonitored the season will be unmonitored if (season.SeasonNumber < lastSeason) { if (episodes.Where(e => e.SeasonNumber == season.SeasonNumber).All(e => !e.Monitored)) { + _logger.Debug("Unmonitoring season {0} because all episodes are not monitored", season.SeasonNumber); season.Monitored = false; } }