EpisodeProvider GetEpisode(s) returns episodes with EpisodeFiles.

This commit is contained in:
Mark McDowall 2011-06-19 18:46:32 -07:00
parent 7b5556199f
commit b084a3bc46
2 changed files with 198 additions and 18 deletions

View File

@ -41,10 +41,39 @@ namespace NzbDrone.Core.Test
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1); var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1);
//Assert //Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series).EqualTo(fakeEpisodes.First()); episode.ShouldHave().AllPropertiesBut(e => e.Series, e => e.EpisodeFile).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries); episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
} }
[Test]
public void GetEpisode_with_EpisodeFile()
{
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeFile = Builder<EpisodeFile>.CreateNew().With(f => f.EpisodeFileId).Build();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 1).WhereTheFirst(1).Have(e => e.EpisodeFileId = 1).Have(e => e.EpisodeFile = fakeFile).Build();
db.InsertMany(fakeEpisodes);
db.Insert(fakeFile);
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, e => e.EpisodeFile).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
episode.EpisodeFile.Should().NotBeNull();
}
[Test] [Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Sequence contains no elements")] [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Sequence contains no elements")]
public void GetEpisodes_invalid_series() public void GetEpisodes_invalid_series()
@ -79,7 +108,6 @@ namespace NzbDrone.Core.Test
result.Should().HaveCount(0); result.Should().HaveCount(0);
} }
[Test] [Test]
public void AttachSeries_list_success() public void AttachSeries_list_success()
{ {
@ -136,7 +164,6 @@ namespace NzbDrone.Core.Test
var returnedEpisode = mocker.Resolve<EpisodeProvider>().AttachSeries(fakeEpisodes); var returnedEpisode = mocker.Resolve<EpisodeProvider>().AttachSeries(fakeEpisodes);
} }
[Test] [Test]
public void GetEpisodesBySeason_success() public void GetEpisodesBySeason_success()
{ {
@ -196,7 +223,6 @@ namespace NzbDrone.Core.Test
mocker.VerifyAllMocks(); mocker.VerifyAllMocks();
} }
[Test] [Test]
public void new_episodes_only_calls_Insert() public void new_episodes_only_calls_Insert()
{ {
@ -217,7 +243,7 @@ namespace NzbDrone.Core.Test
.Returns(tvdbSeries); .Returns(tvdbSeries);
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
.Setup(d => d.Fetch<Episode>(It.IsAny<String>(), It.IsAny<Object[]>())) .Setup(d => d.Fetch<Episode, EpisodeFile>(It.IsAny<String>(), It.IsAny<Object[]>()))
.Returns(currentEpisodes); .Returns(currentEpisodes);
@ -231,7 +257,6 @@ namespace NzbDrone.Core.Test
mocker.VerifyAllMocks(); mocker.VerifyAllMocks();
} }
[Test] [Test]
public void existing_episodes_only_calls_Update() public void existing_episodes_only_calls_Update()
{ {
@ -255,7 +280,7 @@ namespace NzbDrone.Core.Test
.Returns(tvdbSeries); .Returns(tvdbSeries);
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
.Setup(d => d.Fetch<Episode>(It.IsAny<String>(), It.IsAny<Object[]>())) .Setup(d => d.Fetch<Episode, EpisodeFile>(It.IsAny<String>(), It.IsAny<Object[]>()))
.Returns(currentEpisodes); .Returns(currentEpisodes);
//Act //Act
@ -267,7 +292,6 @@ namespace NzbDrone.Core.Test
mocker.VerifyAllMocks(); mocker.VerifyAllMocks();
} }
[Test] [Test]
public void should_try_to_get_existing_episode_using_tvdbid_first() public void should_try_to_get_existing_episode_using_tvdbid_first()
{ {
@ -286,7 +310,7 @@ namespace NzbDrone.Core.Test
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
.Setup(d => d.Fetch<Episode>(It.IsAny<String>(), It.IsAny<Object[]>())) .Setup(d => d.Fetch<Episode, EpisodeFile>(It.IsAny<String>(), It.IsAny<Object[]>()))
.Returns(fakeEpisodeList); .Returns(fakeEpisodeList);
mocker.GetMock<TvDbProvider>() mocker.GetMock<TvDbProvider>()
@ -334,7 +358,7 @@ namespace NzbDrone.Core.Test
.Returns(tvdbSeries); .Returns(tvdbSeries);
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
.Setup(d => d.Fetch<Episode>(It.IsAny<String>(), It.IsAny<Object[]>())) .Setup(d => d.Fetch<Episode, EpisodeFile>(It.IsAny<String>(), It.IsAny<Object[]>()))
.Returns(new List<Episode> { localEpisode }); .Returns(new List<Episode> { localEpisode });
//Act //Act
@ -345,7 +369,6 @@ namespace NzbDrone.Core.Test
mocker.GetMock<IDatabase>().Verify(c => c.Update(localEpisode), Times.Once()); mocker.GetMock<IDatabase>().Verify(c => c.Update(localEpisode), Times.Once());
} }
[Test] [Test]
public void existing_episodes_keep_their_episodeId_file_id() public void existing_episodes_keep_their_episodeId_file_id()
{ {
@ -371,7 +394,7 @@ namespace NzbDrone.Core.Test
var updatedEpisodes = new List<Episode>(); var updatedEpisodes = new List<Episode>();
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
.Setup(d => d.Fetch<Episode>(It.IsAny<String>(), It.IsAny<Object[]>())) .Setup(d => d.Fetch<Episode, EpisodeFile>(It.IsAny<String>(), It.IsAny<Object[]>()))
.Returns(currentEpisodes); .Returns(currentEpisodes);
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
@ -389,7 +412,6 @@ namespace NzbDrone.Core.Test
updatedEpisodes.Should().OnlyContain(c => c.Ignored == true); updatedEpisodes.Should().OnlyContain(c => c.Ignored == true);
} }
[Test] [Test]
public void IsSeasonIgnored_should_return_true_if_all_episodes_ignored() public void IsSeasonIgnored_should_return_true_if_all_episodes_ignored()
{ {
@ -497,6 +519,112 @@ namespace NzbDrone.Core.Test
episodes.Should().NotBeEmpty(); episodes.Should().NotBeEmpty();
} }
[Test]
public void GetEpisode_by_Season_Episode_with_EpisodeFile()
{
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeFile = Builder<EpisodeFile>.CreateNew().With(f => f.EpisodeFileId).Build();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 1).WhereTheFirst(1).Have(e => e.EpisodeFileId = 1).Have(e => e.EpisodeFile = fakeFile).Build();
db.InsertMany(fakeEpisodes);
db.Insert(fakeFile);
mocker.GetMock<SeriesProvider>()
.Setup(p => p.GetSeries(1))
.Returns(fakeSeries);
//Act
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1, 1, 1);
//Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series, e => e.EpisodeFile).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
episode.EpisodeFile.Should().NotBeNull();
}
[Test]
public void GetEpisode_by_Season_Episode_without_EpisodeFile()
{
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).WhereTheFirst(1).Have(e => e.EpisodeFileId = 0).Build();
db.InsertMany(fakeEpisodes);
mocker.GetMock<SeriesProvider>()
.Setup(p => p.GetSeries(1))
.Returns(fakeSeries);
//Act
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1, 1, 1);
//Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
episode.EpisodeFile.Should().BeNull();
}
[Test]
public void GetEpisode_by_AirDate_with_EpisodeFile()
{
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeFile = Builder<EpisodeFile>.CreateNew().With(f => f.EpisodeFileId).Build();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 1).WhereTheFirst(1).Have(e => e.EpisodeFileId = 1).Have(e => e.EpisodeFile = fakeFile).Build();
db.InsertMany(fakeEpisodes);
db.Insert(fakeFile);
mocker.GetMock<SeriesProvider>()
.Setup(p => p.GetSeries(1))
.Returns(fakeSeries);
//Act
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1, fakeEpisodes[0].AirDate);
//Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series, e => e.EpisodeFile).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
episode.EpisodeFile.Should().NotBeNull();
}
[Test]
public void GetEpisode_by_AirDate_without_EpisodeFile()
{
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).WhereTheFirst(1).Have(e => e.EpisodeFileId = 0).Build();
db.InsertMany(fakeEpisodes);
mocker.GetMock<SeriesProvider>()
.Setup(p => p.GetSeries(1))
.Returns(fakeSeries);
//Act
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1, fakeEpisodes[0].AirDate);
//Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
episode.EpisodeFile.Should().BeNull();
}
} }
} }

View File

@ -35,27 +35,73 @@ namespace NzbDrone.Core.Providers
public virtual Episode GetEpisode(long id) public virtual Episode GetEpisode(long id)
{ {
return AttachSeries(_database.Single<Episode>(id)); var episode = AttachSeries(_database.Fetch<Episode, EpisodeFile>(@"SELECT * FROM Episodes
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE EpisodeId = @0", id).Single());
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
return episode;
} }
public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber) public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
{ {
return AttachSeries(_database.SingleOrDefault<Episode>("WHERE SeriesId = @0 AND SeasonNumber = @1 AND EpisodeNumber = @2", seriesId, seasonNumber, episodeNumber)); var episode = AttachSeries(_database.Fetch<Episode, EpisodeFile>(@"SELECT * FROM Episodes
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE Episodes.SeriesId = @0 AND Episodes.SeasonNumber = @1 AND Episodes.EpisodeNumber = @2", seriesId, seasonNumber, episodeNumber).SingleOrDefault());
if (episode == null)
return null;
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
return episode;
} }
public virtual Episode GetEpisode(int seriesId, DateTime date) public virtual Episode GetEpisode(int seriesId, DateTime date)
{ {
return AttachSeries(_database.SingleOrDefault<Episode>("WHERE SeriesId = @0 AND AirDate = @1", seriesId, date.Date)); var episode = AttachSeries(_database.Fetch<Episode, EpisodeFile>(@"SELECT * FROM Episodes
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE Episodes.SeriesId = @0 AND AirDate = @1", seriesId, date.Date)).SingleOrDefault();
if (episode == null)
return null;
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
return episode;
} }
public virtual IList<Episode> GetEpisodeBySeries(long seriesId) public virtual IList<Episode> GetEpisodeBySeries(long seriesId)
{ {
return AttachSeries(_database.Fetch<Episode>("WHERE SeriesId = @0", seriesId)); var episodes = AttachSeries(_database.Fetch<Episode, EpisodeFile>(@"SELECT * FROM Episodes
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE Episodes.SeriesId = @0", seriesId));
foreach (var episode in episodes)
{
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
}
return episodes;
} }
public virtual IList<Episode> GetEpisodesBySeason(long seriesId, int seasonNumber) public virtual IList<Episode> GetEpisodesBySeason(long seriesId, int seasonNumber)
{ {
return AttachSeries(_database.Fetch<Episode>("WHERE SeriesId = @0 AND SeasonNumber = @1", seriesId, seasonNumber)); var episodes = AttachSeries(_database.Fetch<Episode, EpisodeFile>(@"SELECT * FROM Episodes
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE Episodes.SeriesId = @0 AND Episodes.SeasonNumber = @1", seriesId, seasonNumber));
foreach (var episode in episodes)
{
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
}
return episodes;
} }
public virtual List<Episode> GetEpisodes(EpisodeParseResult parseResult) public virtual List<Episode> GetEpisodes(EpisodeParseResult parseResult)
@ -78,6 +124,12 @@ namespace NzbDrone.Core.Providers
episodes.Add(episode); episodes.Add(episode);
} }
foreach (var episode in episodes)
{
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
}
return episodes; return episodes;
} }