From cc8c70d7f5c571fab0426da0b85fe8919aa25464 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 1 Dec 2011 11:05:17 -0800 Subject: [PATCH] Fixed some bugs, cleaned up some code. --- ...isodeProviderTest_DeleteInvalidEpisodes.cs | 10 +- ...deProviderTest_GetEpisodesByParseResult.cs | 215 +++++++++--------- NzbDrone.Core/Providers/EpisodeProvider.cs | 49 ++-- 3 files changed, 136 insertions(+), 138 deletions(-) diff --git a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest_DeleteInvalidEpisodes.cs b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest_DeleteInvalidEpisodes.cs index 7a84b87f3..a0e976afd 100644 --- a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest_DeleteInvalidEpisodes.cs +++ b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest_DeleteInvalidEpisodes.cs @@ -51,7 +51,7 @@ namespace NzbDrone.Core.Test.ProviderTests db.Insert(fakeEpisode); //Act - mocker.Resolve().DeleteInvalidEpisodes(fakeSeries, tvDbSeries); + mocker.Resolve().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries); //Assert var result = db.Fetch(); @@ -91,7 +91,7 @@ namespace NzbDrone.Core.Test.ProviderTests db.Insert(fakeEpisode); //Act - mocker.Resolve().DeleteInvalidEpisodes(fakeSeries, tvDbSeries); + mocker.Resolve().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries); //Assert var result = db.Fetch(); @@ -131,7 +131,7 @@ namespace NzbDrone.Core.Test.ProviderTests db.Insert(fakeEpisode); //Act - mocker.Resolve().DeleteInvalidEpisodes(fakeSeries, tvDbSeries); + mocker.Resolve().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries); //Assert var result = db.Fetch(); @@ -173,7 +173,7 @@ namespace NzbDrone.Core.Test.ProviderTests db.Insert(fakeEpisode); //Act - mocker.Resolve().DeleteInvalidEpisodes(fakeSeries, tvDbSeries); + mocker.Resolve().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries); //Assert var result = db.Fetch(); @@ -230,7 +230,7 @@ namespace NzbDrone.Core.Test.ProviderTests db.Insert(otherFakeEpisode); //Act - mocker.Resolve().DeleteInvalidEpisodes(fakeSeries, tvDbSeries); + mocker.Resolve().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries); //Assert var result = db.Fetch(); diff --git a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest_GetEpisodesByParseResult.cs b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest_GetEpisodesByParseResult.cs index cb59121d1..52183b8ac 100644 --- a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest_GetEpisodesByParseResult.cs +++ b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest_GetEpisodesByParseResult.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Linq; - using FizzWare.NBuilder; using FluentAssertions; using Moq; @@ -21,24 +20,21 @@ namespace NzbDrone.Core.Test.ProviderTests // ReSharper disable InconsistentNaming public class EpisodeProviderTest_GetEpisodesByParseResult : CoreTest { - [Test] public void Single_GetSeason_Episode_Exists() { - var db = TestDbHelper.GetEmptyDatabase(); - var mocker = new AutoMoqer(); - mocker.SetConstant(db); + WithRealDb(); var fakeEpisode = Builder.CreateNew() - .With(e => e.SeriesId = 1) - .With(e => e.SeasonNumber = 2) - .With(e => e.EpisodeNumber = 10) - .Build(); + .With(e => e.SeriesId = 1) + .With(e => e.SeasonNumber = 2) + .With(e => e.EpisodeNumber = 10) + .Build(); var fakeSeries = Builder.CreateNew().Build(); - db.Insert(fakeEpisode); - db.Insert(fakeSeries); + Db.Insert(fakeEpisode); + Db.Insert(fakeSeries); var parseResult = new EpisodeParseResult { @@ -47,7 +43,7 @@ namespace NzbDrone.Core.Test.ProviderTests EpisodeNumbers = new List { 10 } }; - var ep = mocker.Resolve().GetEpisodesByParseResult(parseResult); + var ep = Mocker.Resolve().GetEpisodesByParseResult(parseResult); ep.Should().HaveCount(1); parseResult.EpisodeTitle.Should().Be(fakeEpisode.Title); @@ -57,85 +53,79 @@ namespace NzbDrone.Core.Test.ProviderTests [Test] public void Single_GetSeason_Episode_Doesnt_exists_should_not_add() { - var mocker = new AutoMoqer(); - var db = TestDbHelper.GetEmptyDatabase(); - mocker.SetConstant(db); + WithRealDb(); var fakeSeries = Builder.CreateNew().Build(); var parseResult = new EpisodeParseResult - { - Series = fakeSeries, - SeasonNumber = 2, - EpisodeNumbers = new List { 10 } - }; + { + Series = fakeSeries, + SeasonNumber = 2, + EpisodeNumbers = new List { 10 } + }; - var ep = mocker.Resolve().GetEpisodesByParseResult(parseResult); + var ep = Mocker.Resolve().GetEpisodesByParseResult(parseResult); ep.Should().BeEmpty(); - db.Fetch().Should().HaveCount(0); + Db.Fetch().Should().HaveCount(0); } [Test] public void Single_GetSeason_Episode_Doesnt_exists_should_add() { - var mocker = new AutoMoqer(); - var db = TestDbHelper.GetEmptyDatabase(); - mocker.SetConstant(db); + WithRealDb(); var fakeSeries = Builder.CreateNew().Build(); var parseResult = new EpisodeParseResult - { - Series = fakeSeries, - SeasonNumber = 2, - EpisodeNumbers = new List { 10 } - }; + { + Series = fakeSeries, + SeasonNumber = 2, + EpisodeNumbers = new List { 10 } + }; - var ep = mocker.Resolve().GetEpisodesByParseResult(parseResult, true); + var ep = Mocker.Resolve().GetEpisodesByParseResult(parseResult, true); ep.Should().HaveCount(1); - db.Fetch().Should().HaveCount(1); + Db.Fetch().Should().HaveCount(1); } [Test] public void Multi_GetSeason_Episode_Exists() { - var db = TestDbHelper.GetEmptyDatabase(); - var mocker = new AutoMoqer(); - mocker.SetConstant(db); + WithRealDb(); var fakeEpisode = Builder.CreateNew() - .With(e => e.SeriesId = 1) - .With(e => e.SeasonNumber = 2) - .With(e => e.EpisodeNumber = 10) - .Build(); + .With(e => e.SeriesId = 1) + .With(e => e.SeasonNumber = 2) + .With(e => e.EpisodeNumber = 10) + .Build(); var fakeEpisode2 = Builder.CreateNew() - .With(e => e.SeriesId = 1) - .With(e => e.SeasonNumber = 2) - .With(e => e.EpisodeNumber = 11) - .Build(); + .With(e => e.SeriesId = 1) + .With(e => e.SeasonNumber = 2) + .With(e => e.EpisodeNumber = 11) + .Build(); var fakeSeries = Builder.CreateNew().Build(); - db.Insert(fakeEpisode); - db.Insert(fakeEpisode2); - db.Insert(fakeSeries); + Db.Insert(fakeEpisode); + Db.Insert(fakeEpisode2); + Db.Insert(fakeSeries); var parseResult = new EpisodeParseResult - { - Series = fakeSeries, - SeasonNumber = 2, - EpisodeNumbers = new List { 10, 11 } - }; + { + Series = fakeSeries, + SeasonNumber = 2, + EpisodeNumbers = new List { 10, 11 } + }; - var ep = mocker.Resolve().GetEpisodesByParseResult(parseResult); + var ep = Mocker.Resolve().GetEpisodesByParseResult(parseResult); ep.Should().HaveCount(2); - db.Fetch().Should().HaveCount(2); + Db.Fetch().Should().HaveCount(2); ep.First().ShouldHave().AllPropertiesBut(e => e.Series); parseResult.EpisodeTitle.Should().Be(fakeEpisode.Title); } @@ -143,94 +133,86 @@ namespace NzbDrone.Core.Test.ProviderTests [Test] public void Multi_GetSeason_Episode_Doesnt_exists_should_not_add() { - var mocker = new AutoMoqer(); - var db = TestDbHelper.GetEmptyDatabase(); - mocker.SetConstant(db); + WithRealDb(); var fakeSeries = Builder.CreateNew().Build(); var parseResult = new EpisodeParseResult - { - Series = fakeSeries, - SeasonNumber = 2, - EpisodeNumbers = new List { 10, 11 } - }; + { + Series = fakeSeries, + SeasonNumber = 2, + EpisodeNumbers = new List { 10, 11 } + }; - var ep = mocker.Resolve().GetEpisodesByParseResult(parseResult); + var ep = Mocker.Resolve().GetEpisodesByParseResult(parseResult); ep.Should().BeEmpty(); - db.Fetch().Should().HaveCount(0); + Db.Fetch().Should().HaveCount(0); } [Test] public void Multi_GetSeason_Episode_Doesnt_exists_should_add() { - var mocker = new AutoMoqer(); - var db = TestDbHelper.GetEmptyDatabase(); - mocker.SetConstant(db); + WithRealDb(); var fakeSeries = Builder.CreateNew().Build(); var parseResult = new EpisodeParseResult - { - Series = fakeSeries, - SeasonNumber = 2, - EpisodeNumbers = new List { 10, 11 } - }; + { + Series = fakeSeries, + SeasonNumber = 2, + EpisodeNumbers = new List { 10, 11 } + }; - var ep = mocker.Resolve().GetEpisodesByParseResult(parseResult, true); + var ep = Mocker.Resolve().GetEpisodesByParseResult(parseResult, true); ep.Should().HaveCount(2); - db.Fetch().Should().HaveCount(2); + Db.Fetch().Should().HaveCount(2); } [Test] public void Get_Episode_Zero_Doesnt_Exist_Should_add_ignored() { - var mocker = new AutoMoqer(); - var db = TestDbHelper.GetEmptyDatabase(); - mocker.SetConstant(db); + WithRealDb(); var fakeSeries = Builder.CreateNew().Build(); var parseResult = new EpisodeParseResult - { - Series = fakeSeries, - SeasonNumber = 2, - EpisodeNumbers = new List { 0 } - }; + { + Series = fakeSeries, + SeasonNumber = 2, + EpisodeNumbers = new List { 0 } + }; - var ep = mocker.Resolve().GetEpisodesByParseResult(parseResult, true); + var ep = Mocker.Resolve().GetEpisodesByParseResult(parseResult, true); ep.Should().HaveCount(1); - db.Fetch().Should().HaveCount(1); + Db.Fetch().Should().HaveCount(1); ep.First().Ignored.Should().BeTrue(); } [Test] public void Get_Multi_Episode_Zero_Doesnt_Exist_Should_not_add_ignored() { - var mocker = new AutoMoqer(); - var db = TestDbHelper.GetEmptyDatabase(); - mocker.SetConstant(db); + WithRealDb(); var fakeSeries = Builder.CreateNew().Build(); var parseResult = new EpisodeParseResult - { - Series = fakeSeries, - SeasonNumber = 2, - EpisodeNumbers = new List { 0, 1 } - }; + { + Series = fakeSeries, + SeasonNumber = 2, + EpisodeNumbers = new List { 0, 1 } + }; - var ep = mocker.Resolve().GetEpisodesByParseResult(parseResult, true); + var ep = Mocker.Resolve().GetEpisodesByParseResult(parseResult, true); ep.Should().HaveCount(2); - db.Fetch().Should().HaveCount(2); + Db.Fetch().Should().HaveCount(2); ep.First().Ignored.Should().BeFalse(); } @@ -238,9 +220,8 @@ namespace NzbDrone.Core.Test.ProviderTests [Description("GetEpisodeParseResult should return empty list if episode list is null")] public void GetEpisodeParseResult_should_return_empty_list_if_episode_list_is_null() { - var mocker = new AutoMoqer(); //Act - var episodes = mocker.Resolve().GetEpisodesByParseResult(new EpisodeParseResult()); + var episodes = Mocker.Resolve().GetEpisodesByParseResult(new EpisodeParseResult()); //Assert episodes.Should().NotBeNull(); episodes.Should().BeEmpty(); @@ -250,9 +231,9 @@ namespace NzbDrone.Core.Test.ProviderTests [Description("GetEpisodeParseResult should return empty list if episode list is empty")] public void GetEpisodeParseResult_should_return_empty_list_if_episode_list_is_empty() { - var mocker = new AutoMoqer(); //Act - var episodes = mocker.Resolve().GetEpisodesByParseResult(new EpisodeParseResult{EpisodeNumbers = new List()}); + var episodes = + Mocker.Resolve().GetEpisodesByParseResult(new EpisodeParseResult { EpisodeNumbers = new List() }); //Assert episodes.Should().NotBeNull(); episodes.Should().BeEmpty(); @@ -272,18 +253,22 @@ namespace NzbDrone.Core.Test.ProviderTests .With(s => s.SeriesId = 1) .Build(); - Mocker.GetMock().Setup(s => s.Fetch(It.IsAny(), It.IsAny())) - .Returns(fakeEpisode); - + Mocker.GetMock().Setup( + s => + s.Fetch(It.IsAny(), + It.IsAny())) + .Returns(fakeEpisode); + //Act var episodes = Mocker.Resolve() - .GetEpisodesByParseResult(new EpisodeParseResult { AirDate = DateTime.Today, Series = fakeSeries }, true); - + .GetEpisodesByParseResult(new EpisodeParseResult { AirDate = DateTime.Today, Series = fakeSeries }, + true); + //Assert episodes.Should().HaveCount(1); episodes.First().AirDate.Should().Be(DateTime.Today); - Mocker.GetMock().Verify(v=> v.Insert(It.IsAny()), Times.Never()); + Mocker.GetMock().Verify(v => v.Insert(It.IsAny()), Times.Never()); } [Test] @@ -298,7 +283,8 @@ namespace NzbDrone.Core.Test.ProviderTests //Act var episodes = Mocker.Resolve() - .GetEpisodesByParseResult(new EpisodeParseResult { AirDate = DateTime.Today, Series = fakeSeries }, true); + .GetEpisodesByParseResult(new EpisodeParseResult { AirDate = DateTime.Today, Series = fakeSeries }, + true); //Assert episodes.Should().HaveCount(1); @@ -308,5 +294,24 @@ namespace NzbDrone.Core.Test.ProviderTests episodesInDb.Should().HaveCount(1); } + + [Test] + public void GetEpisodeParseResult_get_daily_should_not_add_new_episode_when_auto_add_is_false() + { + //Setup + WithRealDb(); + + var fakeSeries = Builder.CreateNew() + .With(s => s.SeriesId = 1) + .Build(); + + //Act + var episodes = Mocker.Resolve() + .GetEpisodesByParseResult(new EpisodeParseResult { AirDate = DateTime.Today, Series = fakeSeries }, false); + + //Assert + episodes.Should().BeEmpty(); + Db.Fetch().Should().BeEmpty(); + } } -} \ No newline at end of file +} diff --git a/NzbDrone.Core/Providers/EpisodeProvider.cs b/NzbDrone.Core/Providers/EpisodeProvider.cs index 73b3f8168..ab9747f82 100644 --- a/NzbDrone.Core/Providers/EpisodeProvider.cs +++ b/NzbDrone.Core/Providers/EpisodeProvider.cs @@ -129,17 +129,16 @@ namespace NzbDrone.Core.Providers { var episodeInfo = GetEpisode(parseResult.Series.SeriesId, parseResult.AirDate.Value); - //if still null we should add the temp episode if (episodeInfo == null && autoAddNew) { Logger.Debug("Episode {0} doesn't exist in db. adding it now.", parseResult); episodeInfo = new Episode - { - SeriesId = parseResult.Series.SeriesId, - AirDate = parseResult.AirDate.Value, - Title = "TBD", - Overview = String.Empty - }; + { + SeriesId = parseResult.Series.SeriesId, + AirDate = parseResult.AirDate.Value, + Title = "TBD", + Overview = String.Empty + }; var episodesInSeries = GetEpisodeBySeries(parseResult.Series.SeriesId); @@ -151,18 +150,19 @@ namespace NzbDrone.Core.Providers //Find the latest episode number var maxEpisodeNumber = episodesInSeries - .Where(w => w.SeasonNumber == episodeInfo.SeasonNumber) - .Select(s => s.EpisodeNumber).MaxOrDefault(); + .Where(w => w.SeasonNumber == episodeInfo.SeasonNumber) + .Select(s => s.EpisodeNumber).MaxOrDefault(); //Set the episode number to max + 1 episodeInfo.EpisodeNumber = maxEpisodeNumber + 1; AddEpisode(episodeInfo); } + if (episodeInfo != null) + { + result.Add(episodeInfo); + } - //Add to Result and Return (There will only be one episode to return) - //TODO: This should not add if episode is still null (When doesn't exist and autoadd is false.) - result.Add(episodeInfo); return result; } @@ -254,13 +254,6 @@ namespace NzbDrone.Core.Providers { try { - //DateTime throws an error in SQLServer per message below: - //SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. - //So lets hack it so it works for SQLServer (as well as SQLite), perhaps we can find a better solution - //Todo: Fix this hack - if (episode.FirstAired < new DateTime(1753, 1, 1)) - episode.FirstAired = new DateTime(1753, 1, 1); - Logger.Trace("Updating info for [{0}] - S{1}E{2}", tvDbSeriesInfo.SeriesName, episode.SeasonNumber, episode.EpisodeNumber); //first check using tvdbId, this should cover cases when and episode number in a season is changed @@ -280,13 +273,15 @@ namespace NzbDrone.Core.Providers //If it is Episode Zero Ignore it, since it is new if (episode.EpisodeNumber == 0) + { episodeToUpdate.Ignored = true; - + } //Else we need to check if this episode should be ignored based on IsIgnored rules else + { episodeToUpdate.Ignored = IsIgnored(series.SeriesId, episode.SeasonNumber); + } } - else { updateList.Add(episodeToUpdate); @@ -301,7 +296,6 @@ namespace NzbDrone.Core.Providers if (episode.FirstAired.Year > 1900) episodeToUpdate.AirDate = episode.FirstAired.Date; - else episodeToUpdate.AirDate = null; @@ -321,8 +315,8 @@ namespace NzbDrone.Core.Providers Logger.Info("Finished episode refresh for series: {0}. Successful: {1} - Failed: {2} ", tvDbSeriesInfo.SeriesName, successCount, failCount); - //DeleteInvalidEpisodes - DeleteInvalidEpisodes(series, tvDbSeriesInfo); + //DeleteEpisodesNotInTvdb + DeleteEpisodesNotInTvdb(series, tvDbSeriesInfo); } public virtual void UpdateEpisode(Episode episode) @@ -426,9 +420,9 @@ namespace NzbDrone.Core.Providers return episode; } - public virtual void DeleteInvalidEpisodes(Series series, TvdbSeries tvDbSeriesInfo) + public virtual void DeleteEpisodesNotInTvdb(Series series, TvdbSeries tvDbSeriesInfo) { - Logger.Info("Starting deletion of invalid episode for series: {0}", series.Title.WithDefault(series.SeriesId)); + Logger.Trace("Starting deletion of episodes that no longer exist in TVDB: {0}", series.Title.WithDefault(series.SeriesId)); //Delete Episodes not matching TvDbIds for this series var tvDbIds = tvDbSeriesInfo.Episodes.Select(e => e.Id); @@ -437,10 +431,9 @@ namespace NzbDrone.Core.Providers var tvDbIdQuery = String.Format("DELETE FROM Episodes WHERE SeriesId = {0} AND TvDbEpisodeId > 0 AND TvDbEpisodeId NOT IN ({1})", series.SeriesId, tvDbIdString); - Logger.Trace("Deleting invalid episodes by TvDbId for {0}", series.SeriesId); _database.Execute(tvDbIdQuery); - Logger.Trace("Finished deleting invalid episodes for {0}", series.SeriesId); + Logger.Trace("Deleted episodes that no longer exist in TVDB for {0}", series.SeriesId); } public virtual void SetPostDownloadStatus(List episodeIds, PostDownloadStatusType postDownloadStatus)