diff --git a/NzbDrone.Core.Test/EpisodeProviderTest.cs b/NzbDrone.Core.Test/EpisodeProviderTest.cs index 27b328035..9ca954590 100644 --- a/NzbDrone.Core.Test/EpisodeProviderTest.cs +++ b/NzbDrone.Core.Test/EpisodeProviderTest.cs @@ -218,6 +218,10 @@ public void AttachSeries_single_invalid_series() [Test] public void GetEpisodesBySeason_success() { + var fakeSeries = Builder.CreateNew() + .With(s => s.SeriesId = 12) + .Build(); + var episodes = Builder.CreateListOfSize(10) .WhereAll().Have(c => c.SeriesId = 12) .WhereTheFirst(5).Have(c => c.SeasonNumber = 1) @@ -227,7 +231,8 @@ public void GetEpisodesBySeason_success() var mocker = new AutoMoqer(); mocker.SetConstant(db); - episodes.ToList().ForEach(c => db.Insert(c)); + db.Insert(fakeSeries); + db.InsertMany(episodes); //Act var seasonEposodes = mocker.Resolve().GetEpisodesBySeason(12, 2); @@ -257,7 +262,10 @@ public void RefreshEpisodeInfo_emptyRepo() var mocker = new AutoMoqer(); - mocker.SetConstant(MockLib.GetEmptyDatabase()); + var db = MockLib.GetEmptyDatabase(); + mocker.SetConstant(db); + + db.Insert(fakeSeries); mocker.GetMock() .Setup(c => c.GetSeries(seriesId, true)) @@ -292,10 +300,12 @@ public void RefreshEpisodeInfo_should_set_older_than_1900_to_null() var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); - var mocker = new AutoMoqer(); - mocker.SetConstant(MockLib.GetEmptyDatabase()); + var db = MockLib.GetEmptyDatabase(); + mocker.SetConstant(db); + + db.Insert(fakeSeries); mocker.GetMock() .Setup(c => c.GetSeries(seriesId, true)) @@ -335,7 +345,7 @@ public void new_episodes_only_calls_Insert() .Returns(tvdbSeries); mocker.GetMock() - .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) + .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) .Returns(currentEpisodes); @@ -362,7 +372,7 @@ public void existing_episodes_only_calls_Update() var currentEpisodes = new List(); foreach (var tvDbEpisode in tvdbSeries.Episodes) { - currentEpisodes.Add(new Episode { TvDbEpisodeId = tvDbEpisode.Id }); + currentEpisodes.Add(new Episode { TvDbEpisodeId = tvDbEpisode.Id, Series = fakeSeries }); } var mocker = new AutoMoqer(); @@ -372,7 +382,7 @@ public void existing_episodes_only_calls_Update() .Returns(tvdbSeries); mocker.GetMock() - .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) + .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) .Returns(currentEpisodes); //Act @@ -397,12 +407,12 @@ public void should_try_to_get_existing_episode_using_tvdbid_first() ) .Build(); - var fakeEpisodeList = new List { new Episode { TvDbEpisodeId = 99, SeasonNumber = 10, EpisodeNumber = 10 } }; var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); - + var fakeEpisodeList = new List { new Episode { TvDbEpisodeId = 99, SeasonNumber = 10, EpisodeNumber = 10, Series = fakeSeries} }; + var mocker = new AutoMoqer(); mocker.GetMock() - .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) + .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) .Returns(fakeEpisodeList); mocker.GetMock() @@ -442,7 +452,6 @@ public void should_try_to_get_existing_episode_using_tvdbid_first_then_season_ep var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); - var mocker = new AutoMoqer(); mocker.GetMock(MockBehavior.Strict) @@ -450,7 +459,7 @@ public void should_try_to_get_existing_episode_using_tvdbid_first_then_season_ep .Returns(tvdbSeries); mocker.GetMock() - .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) + .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) .Returns(new List { localEpisode }); //Act @@ -474,7 +483,7 @@ public void existing_episodes_keep_their_episodeId_file_id() var currentEpisodes = new List(); foreach (var tvDbEpisode in tvdbSeries.Episodes) { - currentEpisodes.Add(new Episode { TvDbEpisodeId = tvDbEpisode.Id, EpisodeId = 99, EpisodeFileId = 69, Ignored = true }); + currentEpisodes.Add(new Episode { TvDbEpisodeId = tvDbEpisode.Id, EpisodeId = 99, EpisodeFileId = 69, Ignored = true, Series = fakeSeries}); } var mocker = new AutoMoqer(); @@ -486,7 +495,7 @@ public void existing_episodes_keep_their_episodeId_file_id() var updatedEpisodes = new List(); mocker.GetMock() - .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) + .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) .Returns(currentEpisodes); mocker.GetMock() @@ -1288,5 +1297,63 @@ public void EpisodesWithFiles_no_files() mocker.VerifyAllMocks(); } + + [Test] + public void GetEpisodesByFileId_multi_episodes() + { + var db = MockLib.GetEmptyDatabase(); + var mocker = new AutoMoqer(); + mocker.SetConstant(db); + + var series = Builder.CreateNew() + .With(s => s.SeriesId = 10) + .Build(); + + var fakeEpisodes = Builder.CreateListOfSize(2) + .WhereAll() + .Have(c => c.SeriesId = 10) + .Have(c => c.SeasonNumber = 1) + .Have(c => c.EpisodeFileId = 12345) + .Build(); + + db.Insert(series); + db.InsertMany(fakeEpisodes); + + //Act + var episodes = mocker.Resolve().GetEpisodesByFileId(12345); + + //Assert + episodes.Should().HaveCount(2); + mocker.VerifyAllMocks(); + } + + [Test] + public void GetEpisodesByFileId_single_episode() + { + var db = MockLib.GetEmptyDatabase(); + var mocker = new AutoMoqer(); + mocker.SetConstant(db); + + var series = Builder.CreateNew() + .With(s => s.SeriesId = 10) + .Build(); + + var fakeEpisode = Builder.CreateNew() + .With(c => c.SeriesId = 10) + .With(c => c.SeasonNumber = 1) + .With(c => c.EpisodeFileId = 12345) + .Build(); + + db.Insert(series); + db.Insert(fakeEpisode); + + //Act + var episodes = mocker.Resolve().GetEpisodesByFileId(12345); + + //Assert + episodes.Should().HaveCount(1); + episodes.First().ShouldHave().AllPropertiesBut(e => e.Series).EqualTo(fakeEpisode); + mocker.VerifyAllMocks(); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/EpisodeProvider.cs b/NzbDrone.Core/Providers/EpisodeProvider.cs index 36a711335..af2cdda2a 100644 --- a/NzbDrone.Core/Providers/EpisodeProvider.cs +++ b/NzbDrone.Core/Providers/EpisodeProvider.cs @@ -84,9 +84,10 @@ public virtual Episode GetEpisode(int seriesId, DateTime date) public virtual IList GetEpisodeBySeries(long seriesId) { - var episodes = AttachSeries(_database.Fetch(@"SELECT * FROM Episodes + var episodes = _database.Fetch(@"SELECT * FROM Episodes + INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId - WHERE Episodes.SeriesId = @0", seriesId)); + WHERE Episodes.SeriesId = @0", seriesId); foreach (var episode in episodes) { @@ -99,9 +100,10 @@ public virtual IList GetEpisodeBySeries(long seriesId) public virtual IList GetEpisodesBySeason(long seriesId, int seasonNumber) { - var episodes = AttachSeries(_database.Fetch(@"SELECT * FROM Episodes + var episodes = _database.Fetch(@"SELECT * FROM Episodes + INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId - WHERE Episodes.SeriesId = @0 AND Episodes.SeasonNumber = @1", seriesId, seasonNumber)); + WHERE Episodes.SeriesId = @0 AND Episodes.SeasonNumber = @1", seriesId, seasonNumber); foreach (var episode in episodes) { @@ -175,7 +177,9 @@ INNER JOIN Series public virtual IList GetEpisodesByFileId(int episodeFileId) { - return AttachSeries(_database.Fetch("WHERE EpisodeFileId = @0", episodeFileId)); + return _database.Fetch(@"SELECT * FROM Episodes + INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId + WHERE EpisodeFileId = @0", episodeFileId); } public virtual IList EpisodesWithFiles()