Cleanup orphaned seasons when deleting episodes

This commit is contained in:
Mark McDowall 2013-08-28 18:43:45 -07:00
parent 1f90d546d4
commit 9e5353aacc
6 changed files with 120 additions and 5 deletions

View File

@ -186,6 +186,7 @@
<Compile Include="ProviderTests\DiskProviderTests\ArchiveProviderFixture.cs" />
<Compile Include="MediaFileTests\DropFolderImportServiceFixture.cs" />
<Compile Include="SeriesStatsTests\SeriesStatisticsFixture.cs" />
<Compile Include="TvTests\SeasonServiceTests\HandleEpisodeInfoDeletedEventFixture.cs" />
<Compile Include="TvTests\SeasonServiceTests\SetSeasonPassFixture.cs" />
<Compile Include="TvTests\SeasonServiceTests\SetMonitoredFixture.cs" />
<Compile Include="TvTests\SeriesRepositoryTests\QualityProfileRepositoryFixture.cs" />

View File

@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
using NzbDrone.Core.Tv.Events;
namespace NzbDrone.Core.Test.TvTests.SeasonServiceTests
{
[TestFixture]
public class HandleEpisodeInfoDeletedEventFixture : CoreTest<SeasonService>
{
private List<Season> _seasons;
private List<Episode> _episodes;
[SetUp]
public void Setup()
{
_seasons = Builder<Season>
.CreateListOfSize(1)
.All()
.With(s => s.SeriesId = 1)
.Build()
.ToList();
_episodes = Builder<Episode>
.CreateListOfSize(1)
.All()
.With(e => e.SeasonNumber = _seasons.First().SeasonNumber)
.With(s => s.SeriesId = _seasons.First().SeasonNumber)
.Build()
.ToList();
Mocker.GetMock<ISeasonRepository>()
.Setup(s => s.GetSeasonBySeries(It.IsAny<int>()))
.Returns(_seasons);
Mocker.GetMock<IEpisodeService>()
.Setup(s => s.GetEpisodesBySeason(It.IsAny<int>(), _seasons.First().SeasonNumber))
.Returns(_episodes);
}
private void GivenAbandonedSeason()
{
Mocker.GetMock<IEpisodeService>()
.Setup(s => s.GetEpisodesBySeason(It.IsAny<int>(), _seasons.First().SeasonNumber))
.Returns(new List<Episode>());
}
[Test]
public void should_not_delete_when_season_is_still_valid()
{
Subject.Handle(new EpisodeInfoDeletedEvent(_episodes));
Mocker.GetMock<ISeasonRepository>()
.Verify(v => v.Delete(It.IsAny<Season>()), Times.Never());
}
[Test]
public void should_delete_season_if_no_episodes_exist_in_that_season()
{
GivenAbandonedSeason();
Subject.Handle(new EpisodeInfoDeletedEvent(_episodes));
Mocker.GetMock<ISeasonRepository>()
.Verify(v => v.Delete(It.IsAny<Season>()), Times.Once());
}
[Test]
public void should_only_delete_a_season_once()
{
_episodes = Builder<Episode>
.CreateListOfSize(5)
.All()
.With(e => e.SeasonNumber = _seasons.First().SeasonNumber)
.With(s => s.SeriesId = _seasons.First().SeasonNumber)
.Build()
.ToList();
GivenAbandonedSeason();
Subject.Handle(new EpisodeInfoDeletedEvent(_episodes));
Mocker.GetMock<ISeasonRepository>()
.Verify(v => v.Delete(It.IsAny<Season>()), Times.Once());
}
}
}

View File

@ -350,6 +350,7 @@
<Compile Include="Rest\RestException.cs" />
<Compile Include="SeriesStats\SeriesStatisticsService.cs" />
<Compile Include="Tv\EpisodeService.cs" />
<Compile Include="Tv\Events\EpisodeInfoDeletedEvent.cs" />
<Compile Include="Tv\Events\EpisodeInfoUpdatedEvent.cs" />
<Compile Include="Tv\Events\EpisodeInfoAddedEvent.cs" />
<Compile Include="Tv\Events\SeriesAddedEvent.cs" />

View File

@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using NzbDrone.Common.Messaging;
namespace NzbDrone.Core.Tv.Events
{
public class EpisodeInfoDeletedEvent : IEvent
{
public ReadOnlyCollection<Episode> Episodes { get; private set; }
public EpisodeInfoDeletedEvent(IList<Episode> episodes)
{
Episodes = new ReadOnlyCollection<Episode>(episodes);
}
}
}

View File

@ -34,7 +34,7 @@ namespace NzbDrone.Core.Tv
var successCount = 0;
var failCount = 0;
var existinEpisodes = _episodeService.GetEpisodeBySeries(series.Id);
var existingEpisodes = _episodeService.GetEpisodeBySeries(series.Id);
var seasons = _seasonService.GetSeasonsBySeries(series.Id);
var updateList = new List<Episode>();
@ -44,11 +44,11 @@ namespace NzbDrone.Core.Tv
{
try
{
var episodeToUpdate = existinEpisodes.SingleOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber);
var episodeToUpdate = existingEpisodes.SingleOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber);
if (episodeToUpdate != null)
{
existinEpisodes.Remove(episodeToUpdate);
existingEpisodes.Remove(episodeToUpdate);
updateList.Add(episodeToUpdate);
}
else
@ -82,11 +82,10 @@ namespace NzbDrone.Core.Tv
AdjustMultiEpisodeAirTime(series, allEpisodes);
_episodeService.DeleteMany(existinEpisodes);
_episodeService.DeleteMany(existingEpisodes);
_episodeService.UpdateMany(updateList);
_episodeService.InsertMany(newList);
if (newList.Any())
{
_messageAggregator.PublishEvent(new EpisodeInfoAddedEvent(newList, series));
@ -97,6 +96,11 @@ namespace NzbDrone.Core.Tv
_messageAggregator.PublishEvent(new EpisodeInfoUpdatedEvent(updateList));
}
if (existingEpisodes.Any())
{
_messageAggregator.PublishEvent(new EpisodeInfoDeletedEvent(updateList));
}
if (failCount != 0)
{
_logger.Info("Finished episode refresh for series: {0}. Successful: {1} - Failed: {2} ",

Binary file not shown.