From 969f8ae5e2e729310454e7c473c05f7b038e657b Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Mon, 20 Feb 2012 19:25:19 -0800 Subject: [PATCH] SeasonProvider Added to handle ignoring of Seasons. --- .../JobTests/SeriesSearchJobTest.cs | 2 +- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 + .../ProviderTests/EpisodeProviderTest.cs | 187 ++--------------- .../ProviderTests/SeasonProviderTest.cs | 193 ++++++++++++++++++ .../Datastore/Migrations/Migration20120220.cs | 22 ++ NzbDrone.Core/Jobs/SeriesSearchJob.cs | 11 +- NzbDrone.Core/NzbDrone.Core.csproj | 3 + NzbDrone.Core/Providers/EpisodeProvider.cs | 16 +- NzbDrone.Core/Providers/SeasonProvider.cs | Bin 0 -> 7279 bytes NzbDrone.Core/Repository/Season.cs | 16 ++ 10 files changed, 265 insertions(+), 186 deletions(-) create mode 100644 NzbDrone.Core.Test/ProviderTests/SeasonProviderTest.cs create mode 100644 NzbDrone.Core/Datastore/Migrations/Migration20120220.cs create mode 100644 NzbDrone.Core/Providers/SeasonProvider.cs create mode 100644 NzbDrone.Core/Repository/Season.cs diff --git a/NzbDrone.Core.Test/JobTests/SeriesSearchJobTest.cs b/NzbDrone.Core.Test/JobTests/SeriesSearchJobTest.cs index 6c74b4faf..8b527ee20 100644 --- a/NzbDrone.Core.Test/JobTests/SeriesSearchJobTest.cs +++ b/NzbDrone.Core.Test/JobTests/SeriesSearchJobTest.cs @@ -26,7 +26,7 @@ public void SeriesSearch_success() Mocker.GetMock() .Setup(c => c.GetSeasons(1)).Returns(seasons); - Mocker.GetMock() + Mocker.GetMock() .Setup(c => c.IsIgnored(It.IsAny(), It.IsAny())).Returns(false); Mocker.GetMock() diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 2126da70d..c69605810 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -115,6 +115,7 @@ + diff --git a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs index 47ff0f5f7..3ee535711 100644 --- a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs @@ -439,7 +439,6 @@ public void RefreshEpisodeInfo_should_add_old_episodes_with_no_title() result.Should().HaveSameCount(fakeEpisodes.Episodes); } - [Test] public void RefreshEpisodeInfo_ignore_season_zero() { @@ -466,6 +465,10 @@ public void RefreshEpisodeInfo_ignore_season_zero() .Setup(c => c.GetSeries(seriesId, true)) .Returns(fakeEpisodes); + Mocker.GetMock() + .Setup(s => s.IsIgnored(seriesId, 0)) + .Returns(true); + //Act Mocker.Resolve().RefreshEpisodeInfo(fakeSeries); @@ -694,6 +697,10 @@ public void RefreshEpisodeInfo_should_ignore_new_episode_for_ignored_season() .Setup(c => c.GetSeries(seriesId, true)) .Returns(tvdbSeries); + Mocker.GetMock() + .Setup(s => s.IsIgnored(seriesId, It.IsAny())) + .Returns(true); + //Act Mocker.Resolve().RefreshEpisodeInfo(fakeSeries); @@ -704,180 +711,6 @@ public void RefreshEpisodeInfo_should_ignore_new_episode_for_ignored_season() result.Where(e => e.Ignored).Should().HaveCount(episodeCount); } - [Test] - public void IsSeasonIgnored_should_return_true_if_all_episodes_ignored() - { - WithRealDb(); - - var episodes = Builder.CreateListOfSize(4) - .All() - .With(c => c.Ignored = true) - .With(c => c.SeriesId = 10) - .With(c => c.SeasonNumber = 2) - .Build(); - - episodes.ToList().ForEach(c => Db.Insert(c)); - - //Act - var result = Mocker.Resolve().IsIgnored(10, 2); - - //Assert - result.Should().BeTrue(); - } - - [Test] - public void IsSeasonIgnored_should_return_false_if_none_of_episodes_are_ignored() - { - WithRealDb(); - - var episodes = Builder.CreateListOfSize(4) - .All() - .With(c => c.Ignored = false) - .With(c => c.SeriesId = 10) - .With(c => c.SeasonNumber = 2) - .Build(); - - episodes.ToList().ForEach(c => Db.Insert(c)); - - //Act - var result = Mocker.Resolve().IsIgnored(10, 2); - - //Assert - result.Should().BeFalse(); - } - - [Test] - public void IsSeasonIgnored_should_return_false_if_some_of_episodes_are_ignored() - { - WithRealDb(); - - var episodes = Builder.CreateListOfSize(4) - .All() - .With(c => c.SeriesId = 10) - .With(c => c.SeasonNumber = 2) - .With(c => c.Ignored = true) - .Build(); - - episodes[2].Ignored = false; - - - episodes.ToList().ForEach(c => Db.Insert(c)); - - //Act - var result = Mocker.Resolve().IsIgnored(10, 2); - - //Assert - result.Should().BeFalse(); - } - - [Test] - public void IsSeasonIgnored_should_return_false_if_zero_episodes_in_db_for_season() - { - WithRealDb(); - - var episodes = Builder.CreateListOfSize(4) - .All() - .With(c => c.SeriesId = 10) - .With(c => c.SeasonNumber = 3) - .With(c => c.Ignored = true) - .Build(); - - episodes.ToList().ForEach(c => Db.Insert(c)); - - //Act - var result = Mocker.Resolve().IsIgnored(10, 2); - - //Assert - result.Should().BeFalse(); - } - - [Test] - public void IsSeasonIgnored_should_return_true_if_zero_episodes_in_db_for_season_and_previous_is_ignored() - { - WithRealDb(); - - var episodes = Builder.CreateListOfSize(4) - .All() - .With(c => c.SeriesId = 10) - .With(c => c.SeasonNumber = 3) - .With(c => c.Ignored = true) - .Build(); - - episodes.ToList().ForEach(c => Db.Insert(c)); - - //Act - var result = Mocker.Resolve().IsIgnored(10, 4); - - //Assert - result.Should().BeTrue(); - } - - [Test] - public void IsSeasonIgnored_should_return_false_if_zero_episodes_in_db_for_season_and_previous_is_not_ignored() - { - WithRealDb(); - - var episodes = Builder.CreateListOfSize(4) - .All() - .With(c => c.SeriesId = 10) - .With(c => c.SeasonNumber = 3) - .With(c => c.Ignored = false) - .Build(); - - episodes.ToList().ForEach(c => Db.Insert(c)); - - //Act - var result = Mocker.Resolve().IsIgnored(10, 4); - - //Assert - result.Should().BeFalse(); - } - - [Test] - public void IsSeasonIgnored_should_return_false_if_zero_episodes_in_db_for_season_one() - { - WithRealDb(); - - //Act - var result = Mocker.Resolve().IsIgnored(10, 1); - - //Assert - result.Should().BeFalse(); - } - - [Test] - public void IsSeasonIgnored_should_return_true_if_zero_episodes_in_db_for_season_zero() - { - WithRealDb(); - - //Act - var result = Mocker.Resolve().IsIgnored(10, 0); - - //Assert - result.Should().BeTrue(); - } - - [Test] - public void IsSeasonIgnored_should_return_false_if_season_zero_is_not_ignored() - { - WithRealDb(); - - var episodes = Builder.CreateListOfSize(4) - .All() - .With(c => c.SeriesId = 10) - .With(c => c.SeasonNumber = 0) - .With(c => c.Ignored = false) - .Build(); - - episodes.ToList().ForEach(c => Db.Insert(c)); - - //Act - var result = Mocker.Resolve().IsIgnored(10, 0); - - //Assert - result.Should().BeFalse(); - } - [Test] [Explicit] public void Add_daily_show_episodes() @@ -1049,6 +882,10 @@ public void AddEpisode_episode_is_ignored_when_full_season_is_ignored() .With(e => e.Ignored = false) .Build(); + Mocker.GetMock() + .Setup(s => s.IsIgnored(newEpisode.SeriesId, newEpisode.SeasonNumber)) + .Returns(true); + //Act Mocker.Resolve().AddEpisode(newEpisode); diff --git a/NzbDrone.Core.Test/ProviderTests/SeasonProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/SeasonProviderTest.cs new file mode 100644 index 000000000..2a4401c22 --- /dev/null +++ b/NzbDrone.Core.Test/ProviderTests/SeasonProviderTest.cs @@ -0,0 +1,193 @@ +// ReSharper disable RedundantUsingDirective + +using System; +using System.Collections.Generic; +using System.Linq; + +using FizzWare.NBuilder; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Common; +using NzbDrone.Core.Model; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Providers.Core; +using NzbDrone.Core.Repository; +using NzbDrone.Core.Repository.Quality; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Test.Common.AutoMoq; +using PetaPoco; +using TvdbLib.Data; + +namespace NzbDrone.Core.Test.ProviderTests +{ + [TestFixture] + // ReSharper disable InconsistentNaming + public class SeasonProviderTest : CoreTest + { + [Test] + public void AddSeason_should_insert_season_to_database_with_ignored_false() + { + WithRealDb(); + + var seriesId = 10; + var seasonNumber = 50; + + //Act + Mocker.Resolve().Add(seriesId, seasonNumber); + + //Assert + var result = Db.Fetch(); + result.Should().HaveCount(1); + result.First().SeriesId.Should().Be(seriesId); + result.First().SeasonNumber.Should().Be(seasonNumber); + result.First().Ignored.Should().BeFalse(); + } + + [TestCase(true)] + [TestCase(false)] + public void AddSeason_should_insert_season_to_database_with_preset_ignored_status(bool isIgnored) + { + WithRealDb(); + + var seriesId = 10; + var seasonNumber = 50; + + //Act + Mocker.Resolve().Add(seriesId, seasonNumber, isIgnored); + + //Assert + var result = Db.Fetch(); + result.Should().HaveCount(1); + result.First().SeriesId.Should().Be(seriesId); + result.First().SeasonNumber.Should().Be(seasonNumber); + result.First().Ignored.Should().Be(isIgnored); + } + + [Test] + public void DeleteSeason_should_remove_season_from_database() + { + WithRealDb(); + + var fakeSeason = Builder.CreateNew().Build(); + + Db.Insert(fakeSeason); + + //Act + Mocker.Resolve().Delete(fakeSeason.SeriesId, fakeSeason.SeasonNumber); + + //Assert + var result = Db.Fetch(); + result.Should().BeEmpty(); + } + + [Test] + public void SetIgnore_should_update_ignored_status() + { + WithRealDb(); + + var fakeSeason = Builder.CreateNew() + .With(s => s.Ignored = false) + .Build(); + + var id = Db.Insert(fakeSeason); + + //Act + Mocker.Resolve().SetIgnore(fakeSeason.SeriesId, fakeSeason.SeasonNumber, true); + + //Assert + var result = Db.SingleOrDefault(id); + result.Ignored.Should().BeTrue(); + } + + [Test] + public void IsIgnored_should_return_ignored_status_of_season() + { + //Setup + var fakeSeason = Builder.CreateNew() + .With(s => s.Ignored = false) + .Build(); + + Db.Insert(fakeSeason); + + //Act + var result = Mocker.Resolve().IsIgnored(fakeSeason.SeriesId, fakeSeason.SeasonNumber); + + //Assert + result.Should().Be(fakeSeason.Ignored); + Db.Fetch().Count.Should().Be(1); + } + + [Test] + public void IsIgnored_should_return_true_if_not_in_db_and_is_season_zero() + { + //Setup + WithRealDb(); + + //Act + var result = Mocker.Resolve().IsIgnored(10, 0); + + //Assert + result.Should().BeTrue(); + Db.Fetch().Should().HaveCount(1); + } + + [Test] + public void IsIgnored_should_return_false_if_not_in_db_and_is_season_one() + { + //Setup + WithRealDb(); + + //Act + var result = Mocker.Resolve().IsIgnored(10, 1); + + //Assert + result.Should().BeFalse(); + Db.Fetch().Should().HaveCount(1); + } + + [Test] + public void IsIgnored_should_return_false_if_not_in_db_and_previous_season_is_not_ignored() + { + //Setup + WithRealDb(); + + var lastSeason = Builder.CreateNew() + .With(s => s.SeriesId = 10) + .With(s => s.SeasonNumber = 4) + .With(s => s.Ignored = true) + .Build(); + + Db.Insert(lastSeason); + + //Act + var result = Mocker.Resolve().IsIgnored(10, 5); + + //Assert + result.Should().BeFalse(); + Db.Fetch().Should().HaveCount(2); + } + + [Test] + public void IsIgnored_should_return_true_if_not_in_db_and_previous_season_is_ignored() + { + //Setup + WithRealDb(); + + var lastSeason = Builder.CreateNew() + .With(s => s.SeriesId = 10) + .With(s => s.SeasonNumber = 4) + .With(s => s.Ignored = true) + .Build(); + + Db.Insert(lastSeason); + + //Act + var result = Mocker.Resolve().IsIgnored(10, 5); + + //Assert + result.Should().BeTrue(); + Db.Fetch().Should().HaveCount(2); + } + } +} diff --git a/NzbDrone.Core/Datastore/Migrations/Migration20120220.cs b/NzbDrone.Core/Datastore/Migrations/Migration20120220.cs new file mode 100644 index 000000000..0d4d40de7 --- /dev/null +++ b/NzbDrone.Core/Datastore/Migrations/Migration20120220.cs @@ -0,0 +1,22 @@ +using System; +using System.Data; +using Migrator.Framework; + +namespace NzbDrone.Core.Datastore.Migrations +{ + + [Migration(20120220)] + public class Migration20120220 : NzbDroneMigration + { + protected override void MainDbUpgrade() + { + Database.AddTable("Seasons", new[] + { + new Column("SeasonId", DbType.Int32, ColumnProperty.PrimaryKeyWithIdentity), + new Column("SeriesId", DbType.Int32, ColumnProperty.NotNull), + new Column("SeasonNumber", DbType.Int32, ColumnProperty.NotNull), + new Column("Ignored", DbType.Boolean, ColumnProperty.NotNull) + }); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Jobs/SeriesSearchJob.cs b/NzbDrone.Core/Jobs/SeriesSearchJob.cs index 5e225f1dd..6886b4cb4 100644 --- a/NzbDrone.Core/Jobs/SeriesSearchJob.cs +++ b/NzbDrone.Core/Jobs/SeriesSearchJob.cs @@ -8,15 +8,16 @@ namespace NzbDrone.Core.Jobs { public class SeriesSearchJob : IJob { - private readonly EpisodeProvider _episodeProvider; private readonly SeasonSearchJob _seasonSearchJob; + private readonly SeasonProvider _seasonProvider; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public SeriesSearchJob(EpisodeProvider episodeProvider, SeasonSearchJob seasonSearchJob) + public SeriesSearchJob(SeasonSearchJob seasonSearchJob, + SeasonProvider seasonProvider) { - _episodeProvider = episodeProvider; _seasonSearchJob = seasonSearchJob; + _seasonProvider = seasonProvider; } public string Name @@ -35,12 +36,12 @@ public void Start(ProgressNotification notification, int targetId, int secondary throw new ArgumentOutOfRangeException("targetId"); Logger.Debug("Getting seasons from database for series: {0}", targetId); - var seasons = _episodeProvider.GetSeasons(targetId).Where(s => s > 0); + var seasons = _seasonProvider.GetSeasons(targetId).Where(s => s > 0); foreach (var season in seasons) { //Skip ignored seasons - if (_episodeProvider.IsIgnored(targetId, season)) + if (_seasonProvider.IsIgnored(targetId, season)) continue; _seasonSearchJob.Start(notification, targetId, season); diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 3b73de5e5..e9d984107 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -222,6 +222,7 @@ + @@ -269,6 +270,7 @@ + @@ -337,6 +339,7 @@ + diff --git a/NzbDrone.Core/Providers/EpisodeProvider.cs b/NzbDrone.Core/Providers/EpisodeProvider.cs index 0aceb7799..dbc157735 100644 --- a/NzbDrone.Core/Providers/EpisodeProvider.cs +++ b/NzbDrone.Core/Providers/EpisodeProvider.cs @@ -20,13 +20,16 @@ public class EpisodeProvider private static readonly Regex multiPartCleanupRegex = new Regex(@"\(\d+\)$", RegexOptions.Compiled); private readonly TvDbProvider _tvDbProvider; + private readonly SeasonProvider _seasonProvider; private readonly IDatabase _database; private readonly SeriesProvider _seriesProvider; [Inject] - public EpisodeProvider(IDatabase database, SeriesProvider seriesProvider, TvDbProvider tvDbProviderProvider) + public EpisodeProvider(IDatabase database, SeriesProvider seriesProvider, + TvDbProvider tvDbProviderProvider, SeasonProvider seasonProvider) { _tvDbProvider = tvDbProviderProvider; + _seasonProvider = seasonProvider; _database = database; _seriesProvider = seriesProvider; } @@ -38,8 +41,7 @@ public EpisodeProvider() public virtual void AddEpisode(Episode episode) { //If Season is ignored ignore this episode - if (IsIgnored(episode.SeriesId, episode.SeasonNumber)) - episode.Ignored = true; + episode.Ignored = _seasonProvider.IsIgnored(episode.SeriesId, episode.SeasonNumber); _database.Insert(episode); } @@ -312,7 +314,7 @@ public virtual void RefreshEpisodeInfo(Series series) //Else we need to check if this episode should be ignored based on IsIgnored rules else { - episodeToUpdate.Ignored = IsIgnored(series.SeriesId, episode.SeasonNumber); + episodeToUpdate.Ignored = _seasonProvider.IsIgnored(series.SeriesId, episode.SeasonNumber); } } else @@ -396,10 +398,14 @@ public virtual IList GetEpisodeNumbersBySeason(int seriesId, int seasonNumb return _database.Fetch("SELECT EpisodeNumber FROM Episodes WHERE SeriesId=@0 AND SeasonNumber=@1", seriesId, seasonNumber).OrderBy(c => c).ToList(); } - public virtual void SetSeasonIgnore(long seriesId, int seasonNumber, bool isIgnored) + public virtual void SetSeasonIgnore(int seriesId, int seasonNumber, bool isIgnored) { logger.Info("Setting ignore flag on Series:{0} Season:{1} to {2}", seriesId, seasonNumber, isIgnored); + //Set the SeasonIgnore + _seasonProvider.SetIgnore(seriesId, seasonNumber, isIgnored); + + //Ignore all the episodes in the season _database.Execute(@"UPDATE Episodes SET Ignored = @0 WHERE SeriesId = @1 AND SeasonNumber = @2 AND Ignored = @3", isIgnored, seriesId, seasonNumber, !isIgnored); diff --git a/NzbDrone.Core/Providers/SeasonProvider.cs b/NzbDrone.Core/Providers/SeasonProvider.cs new file mode 100644 index 0000000000000000000000000000000000000000..f80ba99f54af110c2da8bc65c98120fef39d48b4 GIT binary patch literal 7279 zcmeHJU2iHk5bblM{D)J3J_lD^%BC@FB;l?XwBst-+7ncac7UF@yx5 z!II^YN^PY|>j&8Nj>mJ(oEbaOT&!UBq7AOUJwHqKUb_PsMHm<^h3JqO~zdO zaX2@}XVZPaRT42ZdX5!pZCw;*6E1!ux8?g-uF97`7egfl(&h@gcQVB2$hQZKrRGMe zm%?C*hD~K4i;4LrT#WgmJ7k8@z5VRF4h}>V_zTYvyXpR^)gG4D5W?=Vl zmR>sz)4H_d;=(*}4Ig9|V4c^x3;3_UIkXet@he%Jx`W<@kf)(cb}SV$QG*ZxY7&_jxF$s&#Q8`_8q z+Pc0Vzs9r4apaVctlRE>645!SCACn+0Z zga%Kr_7--WqbtYEwb%OThtb1mpMjg40=S(FGX`B!2sC{xQMYPfJ2(fwHWl04ZHG86 zeY}|kf6TSHBAr|Vbp{u{uJCajy+V^Hu_uuaXo9CJUzl$$iGtZ^JQ~bl=+EZ2f(vh3m9bBTGrZ(HMKY@wCWb8L<^ek%{5`_T4T6{(jrrfswGi+!53qbv2O`R9>vr zR%gMM#W{M$AR!^&w0@ipZ|7EdRt$86jOI|*%uV5}tdX3x!~behoJd_?IjP{(U~gAn zZ0L-`Fq^6qxJz^q-f=n(d{JDV7pq7gRQy$FmCU#RTt=2s9Z88!f`D!h%Os+sRHONu zIm(Irlnm20?B|(1A;aUc+c1@tJ@HVgVMgkU@LmmZ$>@CkZ@W?szxrP-lUiiXF9dnbyz=3`;KZ5+z@&^tkJNy`-YHT3G#Z8!Ur+)7Tm zSHhGz>HhGz>HhGz>HhGz>Hh MGz>Hh{BIce3tV&B3jhEB literal 0 HcmV?d00001 diff --git a/NzbDrone.Core/Repository/Season.cs b/NzbDrone.Core/Repository/Season.cs new file mode 100644 index 000000000..8f9ce8f74 --- /dev/null +++ b/NzbDrone.Core/Repository/Season.cs @@ -0,0 +1,16 @@ +using System; +using NzbDrone.Core.Model; +using PetaPoco; + +namespace NzbDrone.Core.Repository +{ + [TableName("Seasons")] + [PrimaryKey("SeasonId", autoIncrement = true)] + public class Season + { + public int SeasonId { get; set; } + public int SeriesId { get; set; } + public int SeasonNumber { get; set; } + public Boolean Ignored { get; set; } + } +} \ No newline at end of file