From 23f8f534fc0e73c8c1fc7545ac51ad2f7cd772e4 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 6 Oct 2012 22:30:12 -0700 Subject: [PATCH] TVDB numbering confusion fixed Fixed: Issue when episode numbers changed on TheTVDB causing episode files were linked incorrectly --- .../ProviderTests/EpisodeProviderTest.cs | 103 +++++++++++++++++- NzbDrone.Core/Providers/EpisodeProvider.cs | 10 ++ 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs index 66db8435f..9870924f1 100644 --- a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs @@ -630,7 +630,16 @@ public void existing_episodes_keep_their_episodeId_file_id() var currentEpisodes = new List(); foreach (var tvDbEpisode in tvdbSeries.Episodes) { - currentEpisodes.Add(new Episode { TvDbEpisodeId = tvDbEpisode.Id, EpisodeId = 99, EpisodeFileId = 69, Ignored = true, Series = fakeSeries }); + currentEpisodes.Add(new Episode + { + TvDbEpisodeId = tvDbEpisode.Id, + EpisodeId = 99, + EpisodeFileId = 69, + Ignored = true, + Series = fakeSeries, + EpisodeNumber = tvDbEpisode.EpisodeNumber, + SeasonNumber = tvDbEpisode.SeasonNumber + }); } Mocker.GetMock(MockBehavior.Strict) @@ -657,6 +666,98 @@ public void existing_episodes_keep_their_episodeId_file_id() updatedEpisodes.Should().OnlyContain(c => c.Ignored == true); } + [Test] + public void existing_episodes_remote_their_episodeId_file_id_when_episode_number_doesnt_match_tvdbid() + { + const int seriesId = 71663; + var tvdbSeries = Builder.CreateNew() + .With(c => c.Episodes = new List(Builder.CreateListOfSize(5).Build()) + ).With(c => c.Id = seriesId).Build(); + + var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); + + var currentEpisodes = new List(); + foreach (var tvDbEpisode in tvdbSeries.Episodes) + { + currentEpisodes.Add(new Episode + { + TvDbEpisodeId = tvDbEpisode.Id, + EpisodeId = 99, + EpisodeFileId = 69, + Ignored = true, + Series = fakeSeries, + EpisodeNumber = tvDbEpisode.EpisodeNumber + 1, + SeasonNumber = tvDbEpisode.SeasonNumber + }); + } + + Mocker.GetMock(MockBehavior.Strict) + .Setup(c => c.GetSeries(seriesId, true, false)) + .Returns(tvdbSeries); + + var updatedEpisodes = new List(); + + Mocker.GetMock() + .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) + .Returns(currentEpisodes); + + Mocker.GetMock() + .Setup(c => c.UpdateMany(It.IsAny>())) + .Callback>(ep => updatedEpisodes = ep.ToList()); + + //Act + Mocker.Resolve().RefreshEpisodeInfo(fakeSeries); + + //Assert + updatedEpisodes.Should().OnlyContain(c => c.EpisodeFileId == 0); + } + + [Test] + public void existing_episodes_remote_their_episodeId_file_id_when_season_number_doesnt_match_tvdbid() + { + const int seriesId = 71663; + var tvdbSeries = Builder.CreateNew() + .With(c => c.Episodes = new List(Builder.CreateListOfSize(5).Build()) + ).With(c => c.Id = seriesId).Build(); + + var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); + + var currentEpisodes = new List(); + foreach (var tvDbEpisode in tvdbSeries.Episodes) + { + currentEpisodes.Add(new Episode + { + TvDbEpisodeId = tvDbEpisode.Id, + EpisodeId = 99, + EpisodeFileId = 69, + Ignored = true, + Series = fakeSeries, + EpisodeNumber = tvDbEpisode.EpisodeNumber, + SeasonNumber = tvDbEpisode.SeasonNumber + 1 + }); + } + + Mocker.GetMock(MockBehavior.Strict) + .Setup(c => c.GetSeries(seriesId, true, false)) + .Returns(tvdbSeries); + + var updatedEpisodes = new List(); + + Mocker.GetMock() + .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) + .Returns(currentEpisodes); + + Mocker.GetMock() + .Setup(c => c.UpdateMany(It.IsAny>())) + .Callback>(ep => updatedEpisodes = ep.ToList()); + + //Act + Mocker.Resolve().RefreshEpisodeInfo(fakeSeries); + + //Assert + updatedEpisodes.Should().OnlyContain(c => c.EpisodeFileId == 0); + } + [Test] public void RefreshEpisodeInfo_should_ignore_new_episode_for_ignored_season() { diff --git a/NzbDrone.Core/Providers/EpisodeProvider.cs b/NzbDrone.Core/Providers/EpisodeProvider.cs index 90a4e0d39..78e23961d 100644 --- a/NzbDrone.Core/Providers/EpisodeProvider.cs +++ b/NzbDrone.Core/Providers/EpisodeProvider.cs @@ -322,6 +322,16 @@ public virtual void RefreshEpisodeInfo(Series series) updateList.Add(episodeToUpdate); } + if ((episodeToUpdate.EpisodeNumber != episode.EpisodeNumber || + episodeToUpdate.SeasonNumber != episode.SeasonNumber) && + episodeToUpdate.EpisodeFileId > 0) + { + logger.Info("Unlinking episode file because TheTVDB changed the epsiode number..."); + + _database.Delete(episodeToUpdate.EpisodeFileId); + episodeToUpdate.EpisodeFileId = 0; + } + episodeToUpdate.SeriesId = series.SeriesId; episodeToUpdate.TvDbEpisodeId = episode.Id; episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;