mirror of https://github.com/Sonarr/Sonarr
DeleteInvalidEpisodes Deletes by TvDbId only, skipping any manually added episodes (TvDbEpisodeId is 0 or null)
This commit is contained in:
parent
71e1827ef8
commit
79472964ed
|
@ -22,7 +22,7 @@ namespace NzbDrone.Core.Test
|
|||
public class EpisodeProviderTest_DeleteInvalidEpisodes : TestBase
|
||||
{
|
||||
[Test]
|
||||
public void Delete_None()
|
||||
public void Delete_None_Valid_TvDbEpisodeId()
|
||||
{
|
||||
//Setup
|
||||
const int seriesId = 71663;
|
||||
|
@ -42,8 +42,87 @@ namespace NzbDrone.Core.Test
|
|||
|
||||
var fakeEpisode = Builder<Episode>.CreateNew()
|
||||
.With(e => e.SeriesId = seriesId)
|
||||
.With(e => e.SeasonNumber = 20)
|
||||
.With(e => e.EpisodeNumber = 20)
|
||||
.With(e => e.TvDbEpisodeId = tvDbSeries.Episodes.First().Id)
|
||||
.Build();
|
||||
|
||||
var mocker = new AutoMoqer();
|
||||
|
||||
var db = MockLib.GetEmptyDatabase();
|
||||
mocker.SetConstant(db);
|
||||
|
||||
db.Insert(fakeSeries);
|
||||
db.Insert(fakeEpisode);
|
||||
|
||||
//Act
|
||||
mocker.Resolve<EpisodeProvider>().DeleteInvalidEpisodes(fakeSeries, tvDbSeries);
|
||||
|
||||
//Assert
|
||||
var result = db.Fetch<Episode>();
|
||||
result.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Delete_None_TvDbEpisodeId_is_zero()
|
||||
{
|
||||
//Setup
|
||||
const int seriesId = 71663;
|
||||
const int episodeCount = 10;
|
||||
|
||||
var tvDbSeries = Builder<TvdbSeries>.CreateNew().With(
|
||||
c => c.Episodes =
|
||||
new List<TvdbEpisode>(Builder<TvdbEpisode>.CreateListOfSize(episodeCount).
|
||||
WhereAll()
|
||||
.Have(l => l.Language = new TvdbLanguage(0, "eng", "a"))
|
||||
.Build())
|
||||
).With(c => c.Id = seriesId).Build();
|
||||
|
||||
var fakeSeries = Builder<Series>.CreateNew()
|
||||
.With(c => c.SeriesId = seriesId)
|
||||
.Build();
|
||||
|
||||
var fakeEpisode = Builder<Episode>.CreateNew()
|
||||
.With(e => e.SeriesId = seriesId)
|
||||
.With(e => e.TvDbEpisodeId = 0)
|
||||
.Build();
|
||||
|
||||
var mocker = new AutoMoqer();
|
||||
|
||||
var db = MockLib.GetEmptyDatabase();
|
||||
mocker.SetConstant(db);
|
||||
|
||||
db.Insert(fakeSeries);
|
||||
db.Insert(fakeEpisode);
|
||||
|
||||
//Act
|
||||
mocker.Resolve<EpisodeProvider>().DeleteInvalidEpisodes(fakeSeries, tvDbSeries);
|
||||
|
||||
//Assert
|
||||
var result = db.Fetch<Episode>();
|
||||
result.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Delete_None_TvDbEpisodeId_is_null()
|
||||
{
|
||||
//Setup
|
||||
const int seriesId = 71663;
|
||||
const int episodeCount = 10;
|
||||
|
||||
var tvDbSeries = Builder<TvdbSeries>.CreateNew().With(
|
||||
c => c.Episodes =
|
||||
new List<TvdbEpisode>(Builder<TvdbEpisode>.CreateListOfSize(episodeCount).
|
||||
WhereAll()
|
||||
.Have(l => l.Language = new TvdbLanguage(0, "eng", "a"))
|
||||
.Build())
|
||||
).With(c => c.Id = seriesId).Build();
|
||||
|
||||
var fakeSeries = Builder<Series>.CreateNew()
|
||||
.With(c => c.SeriesId = seriesId)
|
||||
.Build();
|
||||
|
||||
var fakeEpisode = Builder<Episode>.CreateNew()
|
||||
.With(e => e.SeriesId = seriesId)
|
||||
.With(e => e.TvDbEpisodeId = null)
|
||||
.Build();
|
||||
|
||||
var mocker = new AutoMoqer();
|
||||
|
@ -104,109 +183,6 @@ namespace NzbDrone.Core.Test
|
|||
result.Should().HaveCount(0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Delete_EpisodeNumber()
|
||||
{
|
||||
//Setup
|
||||
const int seriesId = 71663;
|
||||
const int episodeCount = 10;
|
||||
|
||||
var tvDbSeries = Builder<TvdbSeries>.CreateNew().With(
|
||||
c => c.Episodes =
|
||||
new List<TvdbEpisode>(Builder<TvdbEpisode>.CreateListOfSize(episodeCount).
|
||||
WhereAll()
|
||||
.Have(l => l.Language = new TvdbLanguage(0, "eng", "a"))
|
||||
.Build())
|
||||
).With(c => c.Id = seriesId).Build();
|
||||
|
||||
var fakeSeries = Builder<Series>.CreateNew()
|
||||
.With(c => c.SeriesId = seriesId)
|
||||
.Build();
|
||||
|
||||
var fakeEpisode = Builder<Episode>.CreateNew()
|
||||
.With(e => e.SeriesId = seriesId)
|
||||
.With(e => e.SeasonNumber = 1)
|
||||
.With(e => e.EpisodeNumber = 20)
|
||||
.With(e => e.TvDbEpisodeId = 1)
|
||||
.Build();
|
||||
|
||||
var mocker = new AutoMoqer();
|
||||
|
||||
var db = MockLib.GetEmptyDatabase();
|
||||
mocker.SetConstant(db);
|
||||
|
||||
db.Insert(fakeSeries);
|
||||
db.Insert(fakeEpisode);
|
||||
|
||||
//Act
|
||||
mocker.Resolve<EpisodeProvider>().DeleteInvalidEpisodes(fakeSeries, tvDbSeries);
|
||||
|
||||
//Assert
|
||||
var result = db.Fetch<Episode>();
|
||||
result.Should().HaveCount(0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Delete_Both()
|
||||
{
|
||||
//Setup
|
||||
const int seriesId = 71663;
|
||||
const int episodeCount = 10;
|
||||
|
||||
var tvDbSeries = Builder<TvdbSeries>.CreateNew().With(
|
||||
c => c.Episodes =
|
||||
new List<TvdbEpisode>(Builder<TvdbEpisode>.CreateListOfSize(episodeCount).
|
||||
WhereAll()
|
||||
.Have(l => l.Language = new TvdbLanguage(0, "eng", "a"))
|
||||
.Build())
|
||||
).With(c => c.Id = seriesId).Build();
|
||||
|
||||
var fakeSeries = Builder<Series>.CreateNew()
|
||||
.With(c => c.SeriesId = seriesId)
|
||||
.Build();
|
||||
|
||||
var fakeEpisode1 = Builder<Episode>.CreateNew()
|
||||
.With(e => e.SeriesId = seriesId)
|
||||
.With(e => e.SeasonNumber = 1)
|
||||
.With(e => e.EpisodeNumber = 20)
|
||||
.With(e => e.TvDbEpisodeId = 1)
|
||||
.Build();
|
||||
|
||||
var fakeEpisode2 = Builder<Episode>.CreateNew()
|
||||
.With(e => e.SeriesId = seriesId)
|
||||
.With(e => e.SeasonNumber = 1)
|
||||
.With(e => e.EpisodeNumber = 1)
|
||||
.With(e => e.TvDbEpisodeId = 300)
|
||||
.Build();
|
||||
|
||||
//This should not be deleted
|
||||
var fakeEpisode3 = Builder<Episode>.CreateNew()
|
||||
.With(e => e.SeriesId = seriesId)
|
||||
.With(e => e.SeasonNumber = 1)
|
||||
.With(e => e.EpisodeNumber = 1)
|
||||
.With(e => e.TvDbEpisodeId = 1)
|
||||
.With(e => e.Title = "Not Deleted")
|
||||
.Build();
|
||||
|
||||
var mocker = new AutoMoqer();
|
||||
|
||||
var db = MockLib.GetEmptyDatabase();
|
||||
mocker.SetConstant(db);
|
||||
|
||||
db.Insert(fakeSeries);
|
||||
db.Insert(fakeEpisode1);
|
||||
db.Insert(fakeEpisode2);
|
||||
db.Insert(fakeEpisode3);
|
||||
|
||||
//Act
|
||||
mocker.Resolve<EpisodeProvider>().DeleteInvalidEpisodes(fakeSeries, tvDbSeries);
|
||||
|
||||
//Assert
|
||||
var result = db.Fetch<Episode>();
|
||||
result.Should().HaveCount(1);
|
||||
result.First().Title.Should().Be("Not Deleted");
|
||||
}
|
||||
|
||||
//Other series, by season/episode + by tvdbid
|
||||
[Test]
|
||||
public void Delete_TvDbId_multiple_series()
|
||||
|
@ -263,61 +239,5 @@ namespace NzbDrone.Core.Test
|
|||
var result = db.Fetch<Episode>();
|
||||
result.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Delete_EpisodeNumber_multiple_series()
|
||||
{
|
||||
//Setup
|
||||
const int seriesId = 71663;
|
||||
const int episodeCount = 10;
|
||||
|
||||
var tvDbSeries = Builder<TvdbSeries>.CreateNew().With(
|
||||
c => c.Episodes =
|
||||
new List<TvdbEpisode>(Builder<TvdbEpisode>.CreateListOfSize(episodeCount).
|
||||
WhereAll()
|
||||
.Have(l => l.Language = new TvdbLanguage(0, "eng", "a"))
|
||||
.Build())
|
||||
).With(c => c.Id = seriesId).Build();
|
||||
|
||||
var fakeSeries = Builder<Series>.CreateNew()
|
||||
.With(c => c.SeriesId = seriesId)
|
||||
.Build();
|
||||
|
||||
var fakeEpisode = Builder<Episode>.CreateNew()
|
||||
.With(e => e.SeriesId = seriesId)
|
||||
.With(e => e.SeasonNumber = 1)
|
||||
.With(e => e.EpisodeNumber = 20)
|
||||
.With(e => e.TvDbEpisodeId = 1)
|
||||
.Build();
|
||||
|
||||
//Other Series
|
||||
var otherFakeSeries = Builder<Series>.CreateNew()
|
||||
.With(c => c.SeriesId = 12345)
|
||||
.Build();
|
||||
|
||||
var otherFakeEpisode = Builder<Episode>.CreateNew()
|
||||
.With(e => e.SeriesId = 12345)
|
||||
.With(e => e.SeasonNumber = 1)
|
||||
.With(e => e.EpisodeNumber = 4)
|
||||
.With(e => e.TvDbEpisodeId = 2)
|
||||
.Build();
|
||||
|
||||
var mocker = new AutoMoqer();
|
||||
|
||||
var db = MockLib.GetEmptyDatabase();
|
||||
mocker.SetConstant(db);
|
||||
|
||||
db.Insert(fakeSeries);
|
||||
db.Insert(fakeEpisode);
|
||||
db.Insert(otherFakeSeries);
|
||||
db.Insert(otherFakeEpisode);
|
||||
|
||||
//Act
|
||||
mocker.Resolve<EpisodeProvider>().DeleteInvalidEpisodes(fakeSeries, tvDbSeries);
|
||||
|
||||
//Assert
|
||||
var result = db.Fetch<Episode>();
|
||||
result.Should().HaveCount(1);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -373,22 +373,6 @@ namespace NzbDrone.Core.Providers
|
|||
{
|
||||
Logger.Info("Starting deletion of invalid episode for series: {0}", series.Title.WithDefault(series.SeriesId));
|
||||
|
||||
var seasons = tvDbSeriesInfo.Episodes.Select(e => e.SeasonNumber).Distinct();
|
||||
|
||||
foreach (var s in seasons)
|
||||
{
|
||||
//Avoiding accessing modified closure
|
||||
var season = s;
|
||||
Logger.Trace("Processing invalid episodes for {0}, Season: {1}", series.SeriesId, season);
|
||||
|
||||
var episodesInSeason = tvDbSeriesInfo.Episodes.Where(e => e.SeasonNumber == season).Select(e => e.EpisodeNumber);
|
||||
var episodesString = String.Join(", ", episodesInSeason);
|
||||
var seasonQuery = String.Format("DELETE FROM Episodes WHERE SeriesId = {0} AND SeasonNumber = {1} AND EpisodeNumber NOT IN ({2})",
|
||||
series.SeriesId, season, episodesString);
|
||||
|
||||
_database.Execute(seasonQuery);
|
||||
}
|
||||
|
||||
//Delete Episodes not matching TvDbIds for this series
|
||||
var tvDbIds = tvDbSeriesInfo.Episodes.Select(e => e.Id);
|
||||
var tvDbIdString = String.Join(", ", tvDbIds);
|
||||
|
|
Loading…
Reference in New Issue