EpisodeProvider now fills Series property for all episodes returned.

This commit is contained in:
kay.one 2011-06-18 22:57:24 -07:00
parent a990fb91d5
commit 3d3e864094
3 changed files with 149 additions and 12 deletions

View File

@ -19,6 +19,123 @@ namespace NzbDrone.Core.Test
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
public class EpisodeProviderTest : TestBase public class EpisodeProviderTest : TestBase
{ {
[Test]
public void GetEpisodes_exists()
{
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 1).Build();
db.InsertMany(fakeEpisodes);
mocker.GetMock<SeriesProvider>()
.Setup(p => p.GetSeries(1))
.Returns(fakeSeries);
//Act
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1);
//Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
}
[Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Sequence contains no elements")]
public void GetEpisodes_invalid_series()
{
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
mocker.Resolve<SeriesProvider>();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 1).Build();
db.InsertMany(fakeEpisodes);
//Act
mocker.Resolve<EpisodeProvider>().GetEpisode(1);
}
[Test]
public void AttachSeries_empty_list()
{
var mocker = new AutoMoqer();
//Act
var result = mocker.Resolve<EpisodeProvider>().AttachSeries(new List<Episode>());
//Assert
result.Should().HaveCount(0);
}
[Test]
public void AttachSeries_list_success()
{
var mocker = new AutoMoqer();
var fakeSeries = Builder<Series>.CreateNew().With(s => s.SeriesId = 12).Build();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 12).Build();
mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetSeries(12))
.Returns(fakeSeries);
//Act
fakeEpisodes.Should().OnlyContain(e => e.Series == null);
var returnedSeries = mocker.Resolve<EpisodeProvider>().AttachSeries(fakeEpisodes);
//Assert
fakeEpisodes.Should().OnlyContain(e => e.Series == fakeSeries);
returnedSeries.Should().BeEquivalentTo(fakeEpisodes);
}
[Test]
public void AttachSeries_single_success()
{
var mocker = new AutoMoqer();
var fakeSeries = Builder<Series>.CreateNew().With(s => s.SeriesId = 12).Build();
var fakeEpisodes = Builder<Episode>.CreateNew().With(e => e.SeriesId = 12).Build();
mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetSeries(12))
.Returns(fakeSeries);
//Act
var returnedEpisode = mocker.Resolve<EpisodeProvider>().AttachSeries(fakeEpisodes);
//Assert
fakeEpisodes.Series.Should().Be(fakeSeries);
returnedEpisode.Should().Be(fakeEpisodes);
}
[Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Sequence contains no elements")]
public void AttachSeries_single_invalid_series()
{
var mocker = new AutoMoqer();
mocker.SetConstant(MockLib.GetEmptyDatabase());
mocker.Resolve<SeriesProvider>();
var fakeEpisodes = Builder<Episode>.CreateNew().With(e => e.SeriesId = 12).Build();
//Act
var returnedEpisode = mocker.Resolve<EpisodeProvider>().AttachSeries(fakeEpisodes);
}
[Test] [Test]
public void GetEpisodesBySeason_success() public void GetEpisodesBySeason_success()
@ -311,7 +428,7 @@ namespace NzbDrone.Core.Test
.Build(); .Build();
episodes.ToList().ForEach(c => db.Insert(c)); episodes.ToList().ForEach(c => db.Insert(c));
//Act //Act
var result = mocker.Resolve<EpisodeProvider>().IsIgnored(10, 2); var result = mocker.Resolve<EpisodeProvider>().IsIgnored(10, 2);

View File

@ -91,7 +91,7 @@ namespace NzbDrone.Core.Test
[Test] [Test]
public void get_episode_by_series_seasons_episode_x5000() public void get_episode_by_series_seasons_episode_x5000()
{ {
var epProvider = new EpisodeProvider(null, null); var epProvider = new EpisodeProvider(null, null, null);
Thread.Sleep(1000); Thread.Sleep(1000);
@ -115,7 +115,7 @@ namespace NzbDrone.Core.Test
[Test] [Test]
public void get_episode_by_series_seasons_x1000() public void get_episode_by_series_seasons_x1000()
{ {
var epProvider = new EpisodeProvider( null, null); var epProvider = new EpisodeProvider(null, null, null);
Thread.Sleep(1000); Thread.Sleep(1000);

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using Ninject; using Ninject;
using NLog; using NLog;
using NzbDrone.Core.Model; using NzbDrone.Core.Model;
@ -15,12 +14,14 @@ namespace NzbDrone.Core.Providers
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly TvDbProvider _tvDbProvider; private readonly TvDbProvider _tvDbProvider;
private readonly IDatabase _database; private readonly IDatabase _database;
private readonly SeriesProvider _seriesProvider;
[Inject] [Inject]
public EpisodeProvider(TvDbProvider tvDbProviderProvider, IDatabase database) public EpisodeProvider(IDatabase database, SeriesProvider seriesProvider, TvDbProvider tvDbProviderProvider)
{ {
_tvDbProvider = tvDbProviderProvider; _tvDbProvider = tvDbProviderProvider;
_database = database; _database = database;
_seriesProvider = seriesProvider;
} }
public EpisodeProvider() public EpisodeProvider()
@ -34,27 +35,27 @@ namespace NzbDrone.Core.Providers
public virtual Episode GetEpisode(long id) public virtual Episode GetEpisode(long id)
{ {
return _database.Single<Episode>(id); return AttachSeries(_database.Single<Episode>(id));
} }
public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber) public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
{ {
return _database.SingleOrDefault<Episode>("WHERE SeriesId = @0 AND SeasonNumber = @1 AND EpisodeNumber = @2", seriesId, seasonNumber, episodeNumber); return AttachSeries(_database.SingleOrDefault<Episode>("WHERE SeriesId = @0 AND SeasonNumber = @1 AND EpisodeNumber = @2", seriesId, seasonNumber, episodeNumber));
} }
public virtual Episode GetEpisode(int seriesId, DateTime date) public virtual Episode GetEpisode(int seriesId, DateTime date)
{ {
return _database.SingleOrDefault<Episode>("WHERE SeriesId = @0 AND AirDate = @1", seriesId, date.Date); return AttachSeries(_database.SingleOrDefault<Episode>("WHERE SeriesId = @0 AND AirDate = @1", seriesId, date.Date));
} }
public virtual IList<Episode> GetEpisodeBySeries(long seriesId) public virtual IList<Episode> GetEpisodeBySeries(long seriesId)
{ {
return _database.Fetch<Episode>("WHERE SeriesId = @0", seriesId); return AttachSeries(_database.Fetch<Episode>("WHERE SeriesId = @0", seriesId));
} }
public virtual IList<Episode> GetEpisodesBySeason(long seriesId, int seasonNumber) public virtual IList<Episode> GetEpisodesBySeason(long seriesId, int seasonNumber)
{ {
return _database.Fetch<Episode>("WHERE SeriesId = @0 AND SeasonNumber = @1", seriesId, seasonNumber); return AttachSeries(_database.Fetch<Episode>("WHERE SeriesId = @0 AND SeasonNumber = @1", seriesId, seasonNumber));
} }
public virtual List<Episode> GetEpisodes(EpisodeParseResult parseResult) public virtual List<Episode> GetEpisodes(EpisodeParseResult parseResult)
@ -87,12 +88,12 @@ namespace NzbDrone.Core.Providers
if (includeSpecials) if (includeSpecials)
return episodes.Where(e => e.SeasonNumber > 0).ToList(); return episodes.Where(e => e.SeasonNumber > 0).ToList();
return episodes.ToList(); return AttachSeries(episodes.ToList());
} }
public virtual IList<Episode> EpisodesByFileId(int episodeFileId) public virtual IList<Episode> EpisodesByFileId(int episodeFileId)
{ {
return _database.Fetch<Episode>("WHERE EpisodeFileId = @0", episodeFileId); return AttachSeries(_database.Fetch<Episode>("WHERE EpisodeFileId = @0", episodeFileId));
} }
public virtual void RefreshEpisodeInfo(Series series) public virtual void RefreshEpisodeInfo(Series series)
@ -219,5 +220,24 @@ namespace NzbDrone.Core.Providers
} }
public IList<Episode> AttachSeries(IList<Episode> episodes)
{
if (episodes.Count == 0) return episodes;
if (episodes.Select(c => c.SeriesId).Distinct().Count() > 1)
throw new ArgumentException("Episodes belong to more than one series.");
var series = _seriesProvider.GetSeries(episodes.First().SeriesId);
episodes.ToList().ForEach(c => c.Series = series);
return episodes;
}
public Episode AttachSeries(Episode episode)
{
episode.Series = _seriesProvider.GetSeries(episode.SeriesId);
return episode;
}
} }
} }