// ReSharper disable RedundantUsingDirective using System; using System.Linq; using System.Collections.Generic; using System.Linq.Expressions; using AutoMoq; using FizzWare.NBuilder; using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; using NzbDrone.Core.Repository; using NzbDrone.Core.Repository.Quality; using NzbDrone.Core.Test.Framework; using SubSonic.Repository; using TvdbLib.Data; namespace NzbDrone.Core.Test { [TestFixture] // ReSharper disable InconsistentNaming public class EpisodeProviderTest : TestBase { [Test] public void RefreshEpisodeInfo_emptyRepo() { //Arrange const int seriesId = 71663; const int episodeCount = 10; var fakeEpisodes = Builder.CreateNew().With( c => c.Episodes = new List(Builder.CreateListOfSize(episodeCount). WhereAll() .Have(l => l.Language = new TvdbLanguage(0, "eng", "a")) .Build()) ).With(c => c.Id = seriesId).Build(); var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); var mocker = new AutoMoqer(); mocker.SetConstant(MockLib.GetEmptyRepository()); mocker.GetMock() .Setup(c => c.GetSeries(seriesId, true)) .Returns(fakeEpisodes); //Act mocker.Resolve().RefreshEpisodeInfo(fakeSeries); //Assert var actualCount = mocker.Resolve().GetEpisodeBySeries(seriesId).Count; mocker.GetMock().VerifyAll(); actualCount.Should().Be(episodeCount); mocker.VerifyAllMocks(); } [Test] public void EnsureSeason_is_called_once_per_season() { const int seriesId = 71663; var fakeEpisodes = Builder.CreateNew() .With(c => c.Episodes = new List(Builder.CreateListOfSize(6). WhereAll().Have( l => l.Language = new TvdbLanguage(0, "eng", "a")) .WhereTheFirst(3).Have(d => d.SeasonNumber = 1).And( d => d.SeasonId = 11) .AndTheRemaining().Have(d => d.SeasonNumber = 2).And( d => d.SeasonId = 22) .Build()) ).With(c => c.Id = seriesId).Build(); var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); var mocker = new AutoMoqer(); mocker.GetMock(MockBehavior.Strict) .Setup(c => c.GetSeries(seriesId, true)) .Returns(fakeEpisodes); mocker.Resolve().RefreshEpisodeInfo(fakeSeries); mocker.GetMock().Verify(c => c.EnsureSeason(seriesId, 11, 1), Times.Once()); mocker.GetMock().Verify(c => c.EnsureSeason(seriesId, 22, 2), Times.Once()); mocker.VerifyAllMocks(); } [Test] public void new_episodes_only_calls_AddMany() { const int seriesId = 71663; var fakeEpisodes = Builder.CreateNew() .With(c => c.Episodes = new List(Builder.CreateListOfSize(5).Build()) ).With(c => c.Id = seriesId).Build(); var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); var mocker = new AutoMoqer(); mocker.GetMock(MockBehavior.Strict) .Setup(c => c.GetSeries(seriesId, true)) .Returns(fakeEpisodes); //Act mocker.Resolve().RefreshEpisodeInfo(fakeSeries); //Assert mocker.GetMock().Verify( c => c.AddMany(It.Is>(e => e.Count() == fakeEpisodes.Episodes.Count)), Times.Once()); mocker.GetMock().Verify(c => c.UpdateMany(It.Is>(e => e.Count() == 0)), Times.AtMostOnce()); mocker.VerifyAllMocks(); } [Test] public void existing_episodes_only_calls_UpdateMany() { const int seriesId = 71663; var fakeEpisodes = Builder.CreateNew() .With(c => c.Episodes = new List(Builder.CreateListOfSize(5).Build()) ).With(c => c.Id = seriesId).Build(); var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); var mocker = new AutoMoqer(); mocker.GetMock(MockBehavior.Strict) .Setup(c => c.GetSeries(seriesId, true)) .Returns(fakeEpisodes); mocker.GetMock() .Setup(c => c.Single(It.IsAny>>())) .Returns(Builder.CreateNew().Build); //Act mocker.Resolve().RefreshEpisodeInfo(fakeSeries); //Assert mocker.GetMock().Verify(c => c.AddMany(It.Is>(e => e.Count() == 0)), Times.AtMostOnce()); mocker.GetMock().Verify( c => c.UpdateMany(It.Is>(e => e.Count() == fakeEpisodes.Episodes.Count)), Times.Once()); mocker.VerifyAllMocks(); } [Test] public void should_try_to_get_existing_episode_using_tvdbid_first() { const int seriesId = 71663; var fakeEpisodes = Builder.CreateNew() .With(c => c.Id = seriesId) .With(c => c.Episodes = new List( Builder.CreateListOfSize(1) .WhereAll().Have(g => g.Id = 99) .Build()) ) .Build(); var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); var mocker = new AutoMoqer(); var repo = MockLib.GetEmptyRepository(); repo.Add( Builder.CreateNew().With(c => c.TvDbEpisodeId = fakeEpisodes.Episodes[0].Id).Build()); mocker.SetConstant(repo); mocker.GetMock(MockBehavior.Strict) .Setup(c => c.GetSeries(seriesId, true)) .Returns(fakeEpisodes); //Act mocker.Resolve().RefreshEpisodeInfo(fakeSeries); //Assert mocker.VerifyAllMocks(); repo.All().Should().HaveCount(1); } [Test] public void should_try_to_get_existing_episode_using_tvdbid_first_then_season_episode() { const int seriesId = 71663; var fakeEpisodes = Builder.CreateNew() .With(c => c.Id = seriesId) .With(c => c.Episodes = new List{ Builder.CreateNew() .With(g => g.Id = 99) .With(g => g.SeasonNumber = 4) .With(g => g.EpisodeNumber = 15) .With(g=>g.SeriesId = seriesId) .Build() }) .Build(); var localEpisode = Builder.CreateNew() .With(c => c.SeriesId = seriesId) .With(c => c.SeasonNumber = 4) .With(c => c.EpisodeNumber = 15) .Build(); var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); var mocker = new AutoMoqer(); var repo = MockLib.GetEmptyRepository(); repo.Add(localEpisode); mocker.SetConstant(repo); mocker.GetMock(MockBehavior.Strict) .Setup(c => c.GetSeries(seriesId, true)) .Returns(fakeEpisodes); //Act mocker.Resolve().RefreshEpisodeInfo(fakeSeries); //Assert mocker.VerifyAllMocks(); repo.All().Should().HaveCount(1); } [Test] public void existing_episodes_keep_their_episodeId_file_id() { const int seriesId = 71663; var faketvDbResponse = Builder.CreateNew() .With(c => c.Episodes = new List(Builder.CreateListOfSize(5).Build()) ).With(c => c.Id = seriesId).Build(); var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); var fakeEpisode = Builder.CreateNew().With(c => c.EpisodeFileId = 69).And(c => c.EpisodeId = 99).Build(); var mocker = new AutoMoqer(); mocker.GetMock(MockBehavior.Strict) .Setup(c => c.GetSeries(seriesId, true)) .Returns(faketvDbResponse); IEnumerable updatedEpisodes = null; mocker.GetMock() .Setup(c => c.Single(It.IsAny>>())) .Returns(fakeEpisode); mocker.GetMock() .Setup(c => c.UpdateMany(It.IsAny>())) .Returns(faketvDbResponse.Episodes.Count) .Callback>(r => updatedEpisodes = r); //Act mocker.Resolve().RefreshEpisodeInfo(fakeSeries); //Assert mocker.GetMock().Verify(c => c.AddMany(It.Is>(e => e.Count() == 0)), Times.AtMostOnce()); mocker.GetMock().Verify( c => c.UpdateMany(It.Is>(e => e.Count() == faketvDbResponse.Episodes.Count)), Times.Once()); mocker.GetMock().Verify( c => c.UpdateMany( It.Is>( e => e.Where(g => g.EpisodeFileId == 69).Count() == faketvDbResponse.Episodes.Count)), Times.Once()); updatedEpisodes.Should().HaveSameCount(faketvDbResponse.Episodes); updatedEpisodes.Should().OnlyContain(c => c.EpisodeId == 99); updatedEpisodes.Should().OnlyContain(c => c.EpisodeFileId == 69); } [Test] [Explicit] public void Add_daily_show_episodes() { var mocker = new AutoMoqer(); mocker.SetConstant(MockLib.GetEmptyRepository()); mocker.Resolve(); const int tvDbSeriesId = 71256; //act var seriesProvider = mocker.Resolve(); seriesProvider.AddSeries("c:\\test\\", tvDbSeriesId, 0); var episodeProvider = mocker.Resolve(); episodeProvider.RefreshEpisodeInfo(seriesProvider.GetSeries(tvDbSeriesId)); //assert var episodes = episodeProvider.GetEpisodeBySeries(tvDbSeriesId); episodes.Should().NotBeEmpty(); } } }