1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-25 09:18:34 +00:00

Backlog search added (disabled) - It will search for a full season if a full season is missing.

This commit is contained in:
Mark McDowall 2011-08-28 12:07:56 -07:00
parent 273530eda2
commit f604c35768
7 changed files with 298 additions and 7 deletions

View file

@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMoq;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Model;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Jobs;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Test
{
[TestFixture]
public class BacklogSearchJobTest
{
[Test]
public void no_missing_epsiodes()
{
//Setup
var notification = new ProgressNotification("Backlog Search Job Test");
var episodes = new List<Episode>();
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.GetMock<EpisodeProvider>()
.Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes);
//Act
mocker.Resolve<BacklogSearchJob>().Start(notification, 0, 0);
//Assert
mocker.GetMock<SeasonSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), It.IsAny<int>()),
Times.Never());
mocker.GetMock<EpisodeSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), 0),
Times.Never());
}
[Test]
public void individual_missing_episode_only()
{
//Setup
var notification = new ProgressNotification("Backlog Search Job Test");
var episodes = Builder<Episode>.CreateListOfSize(1).Build();
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.GetMock<EpisodeProvider>()
.Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes);
mocker.GetMock<EpisodeSearchJob>()
.Setup(s => s.Start(notification, It.IsAny<int>(), 0)).Verifiable();
//Act
mocker.Resolve<BacklogSearchJob>().Start(notification, 0, 0);
//Assert
mocker.GetMock<SeasonSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), It.IsAny<int>()),
Times.Never());
mocker.GetMock<EpisodeSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), 0),
Times.Once());
}
[Test]
public void individual_missing_episodes_only()
{
//Setup
var notification = new ProgressNotification("Backlog Search Job Test");
var episodes = Builder<Episode>.CreateListOfSize(5).Build();
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.GetMock<EpisodeProvider>()
.Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes);
mocker.GetMock<EpisodeSearchJob>()
.Setup(s => s.Start(notification, It.IsAny<int>(), 0)).Verifiable();
//Act
mocker.Resolve<BacklogSearchJob>().Start(notification, 0, 0);
//Assert
mocker.GetMock<SeasonSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), It.IsAny<int>()),
Times.Never());
mocker.GetMock<EpisodeSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), 0),
Times.Exactly(episodes.Count));
}
[Test]
public void series_season_missing_episodes_only_mismatch_count()
{
//Setup
var notification = new ProgressNotification("Backlog Search Job Test");
var episodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll()
.Have(e => e.SeriesId = 1)
.Have(e => e.SeasonNumber = 1)
.Build();
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.GetMock<EpisodeProvider>()
.Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes);
mocker.GetMock<EpisodeSearchJob>()
.Setup(s => s.Start(notification, It.IsAny<int>(), 0)).Verifiable();
mocker.GetMock<EpisodeProvider>()
.Setup(s => s.GetEpisodeNumbersBySeason(1, 1)).Returns(new List<int> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
//Act
mocker.Resolve<BacklogSearchJob>().Start(notification, 0, 0);
//Assert
mocker.GetMock<SeasonSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), It.IsAny<int>()),
Times.Never());
mocker.GetMock<EpisodeSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), 0),
Times.Exactly(episodes.Count));
}
[Test]
public void series_season_missing_episodes_only()
{
//Setup
var notification = new ProgressNotification("Backlog Search Job Test");
var episodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll()
.Have(e => e.SeriesId = 1)
.Have(e => e.SeasonNumber = 1)
.Build();
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.GetMock<EpisodeProvider>()
.Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes);
mocker.GetMock<SeasonSearchJob>()
.Setup(s => s.Start(notification, It.IsAny<int>(), It.IsAny<int>())).Verifiable();
mocker.GetMock<EpisodeProvider>()
.Setup(s => s.GetEpisodeNumbersBySeason(1, 1)).Returns(episodes.Select(e => e.EpisodeNumber).ToList());
//Act
mocker.Resolve<BacklogSearchJob>().Start(notification, 0, 0);
//Assert
mocker.GetMock<SeasonSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), It.IsAny<int>()),
Times.Once());
mocker.GetMock<EpisodeSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), 0),
Times.Never());
}
[Test]
public void multiple_missing_episodes()
{
//Setup
var notification = new ProgressNotification("Backlog Search Job Test");
var episodes = Builder<Episode>.CreateListOfSize(10)
.WhereTheFirst(5)
.Have(e => e.SeriesId = 1)
.Have(e => e.SeasonNumber = 1)
.Build();
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.GetMock<EpisodeProvider>()
.Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes);
mocker.GetMock<SeasonSearchJob>()
.Setup(s => s.Start(notification, It.IsAny<int>(), It.IsAny<int>())).Verifiable();
mocker.GetMock<EpisodeSearchJob>()
.Setup(s => s.Start(notification, It.IsAny<int>(), 0)).Verifiable();
mocker.GetMock<EpisodeProvider>()
.Setup(s => s.GetEpisodeNumbersBySeason(1, 1)).Returns(new List<int>{ 1, 2, 3, 4, 5 });
//Act
mocker.Resolve<BacklogSearchJob>().Start(notification, 0, 0);
//Assert
mocker.GetMock<SeasonSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), It.IsAny<int>()),
Times.Once());
mocker.GetMock<EpisodeSearchJob>().Verify(c => c.Start(notification, It.IsAny<int>(), 0),
Times.Exactly(5));
}
}
}

View file

@ -88,6 +88,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="BacklogSearchJobTest.cs" />
<Compile Include="SearchJobTest.cs" />
<Compile Include="SeriesSearchJobTest.cs" />
<Compile Include="SearchProviderTest_Season.cs" />

View file

@ -200,6 +200,7 @@
<Compile Include="Model\Xbmc\ErrorResult.cs" />
<Compile Include="Model\Xbmc\IconType.cs" />
<Compile Include="Providers\Core\UdpProvider.cs" />
<Compile Include="Providers\Jobs\BacklogSearchJob.cs" />
<Compile Include="Providers\Jobs\RenameSeriesJob.cs" />
<Compile Include="Providers\Jobs\SeriesSearchJob.cs" />
<Compile Include="Providers\Jobs\RenameSeasonJob.cs" />

View file

@ -296,6 +296,11 @@ namespace NzbDrone.Core.Providers
return _database.Fetch<Int32>("SELECT DISTINCT SeasonNumber FROM Episodes WHERE SeriesId=@0", seriesId).OrderBy(c => c).ToList();
}
public virtual IList<int> GetEpisodeNumbersBySeason(int seriesId, int seasonNumber)
{
return _database.Fetch<int>("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)
{
Logger.Info("Setting ignore flag on Series:{0} Season:{1} to {2}", seriesId, seasonNumber, isIgnored);

View file

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Providers.Jobs
{
public class BacklogSearchJob : IJob
{
private readonly EpisodeProvider _episodeProvider;
private readonly EpisodeSearchJob _episodeSearchJob;
private readonly SeasonSearchJob _seasonSearchJob;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public BacklogSearchJob(EpisodeProvider episodeProvider, EpisodeSearchJob episodeSearchJob,
SeasonSearchJob seasonSearchJob)
{
_episodeProvider = episodeProvider;
_episodeSearchJob = episodeSearchJob;
_seasonSearchJob = seasonSearchJob;
}
public string Name
{
get { return "Backlog Search"; }
}
public int DefaultInterval
{
get { return 0; }
}
public void Start(ProgressNotification notification, int targetId, int secondaryTargetId)
{
var missingEpisodes =
_episodeProvider.EpisodesWithoutFiles(true).GroupBy(e => new { e.SeriesId, e.SeasonNumber });
var individualEpisodes = new List<Episode>();
Logger.Trace("Processing missing episodes list");
foreach (var group in missingEpisodes)
{
var count = group.Count();
if (count == 1)
individualEpisodes.Add(group.First());
else
{
//Get count and compare to the actual number of episodes for this season
//If numbers don't match then add to individual episodes, else process as full season...
var seriesId = group.Key.SeriesId;
var seasonNumber = group.Key.SeasonNumber;
var countInDb = _episodeProvider.GetEpisodeNumbersBySeason(seriesId, seasonNumber).Count;
if (count != countInDb)
{
//Add the episodes to be processed manually
individualEpisodes.AddRange(group);
}
else
{
//Process as a full season
Logger.Debug("Processing Full Season: {0} Season {1}", seriesId, seasonNumber);
_seasonSearchJob.Start(notification, seriesId, seasonNumber);
}
}
}
Logger.Debug("Processing standalone episodes");
//Process the list of remaining episodes, 1 by 1
foreach (var episode in individualEpisodes)
{
_episodeSearchJob.Start(notification, episode.EpisodeId, 0);
}
}
}
}

View file

@ -53,7 +53,7 @@ namespace NzbDrone.Core.Providers.Jobs
Logger.Debug("Getting episodes from database for series: {0} and season: {1}", targetId, secondaryTargetId);
var episodes = _episodeProvider.GetEpisodesBySeason(targetId, secondaryTargetId);
if (episodes == null)
if (episodes == null || episodes.Count == 0)
{
Logger.Warn("No episodes in database found for series: {0} and season: {1}.", targetId, secondaryTargetId);
return;

View file

@ -82,17 +82,14 @@ namespace NzbDrone.Core.Providers
return false;
Logger.Debug("Getting episodes from database for series: {0} and season: {1}", seriesId, seasonNumber);
var episodes = _episodeProvider.GetEpisodesBySeason(seriesId, seasonNumber);
var episodeNumbers = _episodeProvider.GetEpisodeNumbersBySeason(seriesId, seasonNumber);
if (episodes == null)
if (episodeNumbers == null || episodeNumbers.Count == 0)
{
Logger.Warn("No episodes in database found for series: {0} and season: {1}.", seriesId, seasonNumber);
return false;
}
var episodeNumbers = new List<int>();
episodeNumbers.AddRange(episodes.Select(e => e.EpisodeNumber));
notification.CurrentMessage = "Processing search results";
var reportsToProcess = reports.Where(p => p.FullSeason && p.SeasonNumber == seasonNumber).ToList();
@ -100,7 +97,7 @@ namespace NzbDrone.Core.Providers
reportsToProcess.ForEach(c =>
{
c.Series = series;
c.EpisodeNumbers = episodeNumbers;
c.EpisodeNumbers = episodeNumbers.ToList();
});
return ProcessSeasonSearchResults(notification, series, seasonNumber, reportsToProcess);