From a853f80f9e8b544087c6dfe5f1685f6cdc051f7f Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 19 Jun 2011 13:43:33 -0700 Subject: [PATCH] Fixed season ignore --- NzbDrone.Core.Test/ImportNewSeriesJobTest.cs | 128 ++++++++++++++---- .../Providers/Jobs/ImportNewSeriesJob.cs | 14 +- NzbDrone.Core/Providers/MediaFileProvider.cs | 2 +- 3 files changed, 108 insertions(+), 36 deletions(-) diff --git a/NzbDrone.Core.Test/ImportNewSeriesJobTest.cs b/NzbDrone.Core.Test/ImportNewSeriesJobTest.cs index 488a3a0c9..0a0fe7597 100644 --- a/NzbDrone.Core.Test/ImportNewSeriesJobTest.cs +++ b/NzbDrone.Core.Test/ImportNewSeriesJobTest.cs @@ -26,8 +26,6 @@ namespace NzbDrone.Core.Test .AndTheNext(1).Has(s => s.SeriesId = 15) .Build(); - var episodes = Builder.CreateListOfSize(10).Build(); - var notification = new ProgressNotification("Test"); var mocker = new AutoMoqer(MockBehavior.Strict); @@ -39,23 +37,21 @@ namespace NzbDrone.Core.Test mocker.GetMock() .Setup(j => j.Start(notification, series[0].SeriesId)) - .Callback(() => series[0].LastDiskSync = DateTime.Now) - .AtMostOnce(); + .Callback(() => series[0].LastDiskSync = DateTime.Now); + mocker.GetMock() .Setup(j => j.Start(notification, series[1].SeriesId)) - .Callback(() => series[1].LastDiskSync = DateTime.Now) - .AtMostOnce(); + .Callback(() => series[1].LastDiskSync = DateTime.Now); + mocker.GetMock() .Setup(j => j.Start(notification, series[0].SeriesId)) - .Callback(() => series[0].LastInfoSync = DateTime.Now) - .AtMostOnce(); + .Callback(() => series[0].LastInfoSync = DateTime.Now); mocker.GetMock() .Setup(j => j.Start(notification, series[1].SeriesId)) - .Callback(() => series[1].LastInfoSync = DateTime.Now) - .AtMostOnce(); + .Callback(() => series[1].LastInfoSync = DateTime.Now); mocker.GetMock() .Setup(s => s.GetSeries(series[0].SeriesId)).Returns(series[0]); @@ -63,13 +59,6 @@ namespace NzbDrone.Core.Test mocker.GetMock() .Setup(s => s.GetSeries(series[1].SeriesId)).Returns(series[1]); - - mocker.GetMock() - .Setup(s => s.GetEpisodeBySeries(series[0].SeriesId)).Returns(episodes); - - mocker.GetMock() - .Setup(s => s.GetEpisodeBySeries(series[1].SeriesId)).Returns(episodes); - mocker.GetMock() .Setup(s => s.GetSeriesFiles(It.IsAny())).Returns(new List()); @@ -78,6 +67,13 @@ namespace NzbDrone.Core.Test //Assert mocker.VerifyAllMocks(); + + mocker.GetMock().Verify(j => j.Start(notification, series[0].SeriesId), Times.Once()); + mocker.GetMock().Verify(j => j.Start(notification, series[1].SeriesId), Times.Once()); + + mocker.GetMock().Verify(j => j.Start(notification, series[0].SeriesId), Times.Once()); + mocker.GetMock().Verify(j => j.Start(notification, series[1].SeriesId), Times.Once()); + } @@ -93,8 +89,6 @@ namespace NzbDrone.Core.Test .AndTheNext(1).Has(s => s.SeriesId = 15) .Build(); - var episodes = Builder.CreateListOfSize(10).Build(); - var notification = new ProgressNotification("Test"); var mocker = new AutoMoqer(MockBehavior.Strict); @@ -105,18 +99,15 @@ namespace NzbDrone.Core.Test mocker.GetMock() .Setup(j => j.Start(notification, series[0].SeriesId)) - .Callback(() => series[0].LastInfoSync = DateTime.Now) - .AtMostOnce(); + .Callback(() => series[0].LastInfoSync = DateTime.Now); mocker.GetMock() .Setup(j => j.Start(notification, series[1].SeriesId)) - .Throws(new InvalidOperationException()) - .AtMostOnce(); + .Throws(new InvalidOperationException()); mocker.GetMock() .Setup(j => j.Start(notification, series[0].SeriesId)) - .Callback(() => series[0].LastDiskSync = DateTime.Now) - .AtMostOnce(); + .Callback(() => series[0].LastDiskSync = DateTime.Now); mocker.GetMock() @@ -125,17 +116,98 @@ namespace NzbDrone.Core.Test mocker.GetMock() .Setup(s => s.GetSeriesFiles(It.IsAny())).Returns(new List()); - mocker.GetMock() - .Setup(s => s.GetEpisodeBySeries(It.IsAny())).Returns(episodes); - //Act mocker.Resolve().Start(notification, 0); //Assert mocker.VerifyAllMocks(); + + mocker.GetMock().Verify(j => j.Start(notification, series[0].SeriesId), Times.Once()); + mocker.GetMock().Verify(j => j.Start(notification, series[1].SeriesId), Times.Once()); + + mocker.GetMock().Verify(j => j.Start(notification, series[0].SeriesId), Times.Once()); + ExceptionVerification.ExcpectedErrors(1); + } + + + [Test] + public void AutoIgnoreSeason_new_series_should_not_ignore_any() + { + int seriesId = 12; + + var mocker = new AutoMoqer(MockBehavior.Strict); + mocker.GetMock() + .Setup(p => p.GetSeriesFiles(seriesId)) + .Returns(new List()); + + mocker.GetMock() + .Setup(p => p.GetSeasons(seriesId)) + .Returns(new List { 0, 1, 2, 3, 4 }); + + mocker.Resolve().AutoIgnoreSeasons(seriesId); + + + mocker.GetMock().Verify(p => p.SetSeasonIgnore(seriesId, It.IsAny(), It.IsAny()), Times.Never()); + } + + [Test] + public void AutoIgnoreSeason_existing_should_not_ignore_currentseason() + { + int seriesId = 12; + + var episodesFiles = Builder.CreateListOfSize(2) + .WhereAll().Have(e => e.SeriesId = seriesId) + .Build(); + + episodesFiles[0].SeasonNumber = 0; + episodesFiles[1].SeasonNumber = 1; + + var mocker = new AutoMoqer(MockBehavior.Strict); + + mocker.GetMock() + .Setup(p => p.GetSeriesFiles(seriesId)) + .Returns(episodesFiles); + + mocker.GetMock() + .Setup(p => p.GetSeasons(seriesId)) + .Returns(new List { 0, 1, 2 }); + + mocker.Resolve().AutoIgnoreSeasons(seriesId); + + mocker.GetMock().Verify(p => p.SetSeasonIgnore(seriesId, 2, It.IsAny()), Times.Never()); + } + + [Test] + public void AutoIgnoreSeason_existing_should_ignore_seasons_with_no_file() + { + int seriesId = 12; + + var episodesFiles = Builder.CreateListOfSize(2) + .WhereAll().Have(e => e.SeriesId = seriesId) + .Build(); + + episodesFiles[0].SeasonNumber = 1; + + var mocker = new AutoMoqer(); + + mocker.GetMock() + .Setup(p => p.GetSeriesFiles(seriesId)) + .Returns(episodesFiles); + + mocker.GetMock() + .Setup(p => p.GetSeasons(seriesId)) + .Returns(new List { 0, 1, 2 }); + + mocker.Resolve().AutoIgnoreSeasons(seriesId); + + mocker.GetMock().Verify(p => p.SetSeasonIgnore(seriesId, 0, true), Times.Once()); + mocker.GetMock().Verify(p => p.SetSeasonIgnore(seriesId, 1, true), Times.Never()); + mocker.GetMock().Verify(p => p.SetSeasonIgnore(seriesId, 2, It.IsAny()), Times.Never()); + } } + } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/Jobs/ImportNewSeriesJob.cs b/NzbDrone.Core/Providers/Jobs/ImportNewSeriesJob.cs index 8374f3a62..6d8c0d40f 100644 --- a/NzbDrone.Core/Providers/Jobs/ImportNewSeriesJob.cs +++ b/NzbDrone.Core/Providers/Jobs/ImportNewSeriesJob.cs @@ -71,7 +71,7 @@ namespace NzbDrone.Core.Providers.Jobs _diskScanJob.Start(notification, currentSeries.SeriesId); var updatedSeries = _seriesProvider.GetSeries(currentSeries.SeriesId); - AutoIgnoreSeasons(updatedSeries); + AutoIgnoreSeasons(updatedSeries.SeriesId); } catch (Exception e) @@ -84,20 +84,20 @@ namespace NzbDrone.Core.Providers.Jobs ScanSeries(notification); } - private void AutoIgnoreSeasons(Series updatedSeries) + public void AutoIgnoreSeasons(int seriesId) { - var episodeFiles = _mediaFileProvider.GetSeriesFiles(updatedSeries.SeriesId); - var episodes = _episodeProvider.GetEpisodeBySeries(updatedSeries.SeriesId); + var episodeFiles = _mediaFileProvider.GetSeriesFiles(seriesId); + if (episodeFiles.Count() != 0) { - var seasons = episodes.Select(c => c.SeasonNumber).Distinct(); + var seasons = _episodeProvider.GetSeasons(seriesId); var currentSeasons = seasons.Max(); foreach (var season in seasons) { - if (season != currentSeasons) + if (season != currentSeasons && !episodeFiles.Any(e => e.SeasonNumber == season)) { - + _episodeProvider.SetSeasonIgnore(seriesId, season, true); } } } diff --git a/NzbDrone.Core/Providers/MediaFileProvider.cs b/NzbDrone.Core/Providers/MediaFileProvider.cs index 9d936f2c6..8a25655df 100644 --- a/NzbDrone.Core/Providers/MediaFileProvider.cs +++ b/NzbDrone.Core/Providers/MediaFileProvider.cs @@ -204,7 +204,7 @@ namespace NzbDrone.Core.Providers return _database.Fetch(); } - public virtual List GetSeriesFiles(int seriesId) + public virtual IList GetSeriesFiles(int seriesId) { return _database.Fetch("WHERE seriesId= @0", seriesId); }