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_should_not_trigger_any_search() { //Setup var notification = new ProgressNotification("Backlog Search Job Test"); var episodes = new List(); var mocker = new AutoMoqer(MockBehavior.Strict); mocker.GetMock() .Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes); //Act mocker.Resolve().Start(notification, 0, 0); //Assert mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), It.IsAny()), Times.Never()); mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), 0), Times.Never()); } [Test] public void individual_missing_episode() { //Setup var notification = new ProgressNotification("Backlog Search Job Test"); var episodes = Builder.CreateListOfSize(1).Build(); var mocker = new AutoMoqer(MockBehavior.Strict); mocker.GetMock() .Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes); mocker.GetMock() .Setup(s => s.Start(notification, It.IsAny(), 0)).Verifiable(); //Act mocker.Resolve().Start(notification, 0, 0); //Assert mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), It.IsAny()), Times.Never()); mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), 0), Times.Once()); } [Test] public void individual_missing_episodes_only() { //Setup var notification = new ProgressNotification("Backlog Search Job Test"); var episodes = Builder.CreateListOfSize(5).Build(); var mocker = new AutoMoqer(MockBehavior.Strict); mocker.GetMock() .Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes); mocker.GetMock() .Setup(s => s.Start(notification, It.IsAny(), 0)).Verifiable(); //Act mocker.Resolve().Start(notification, 0, 0); //Assert mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), It.IsAny()), Times.Never()); mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), 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.CreateListOfSize(5) .All() .With(e => e.SeriesId = 1) .With(e => e.SeasonNumber = 1) .Build(); var mocker = new AutoMoqer(MockBehavior.Strict); mocker.GetMock() .Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes); mocker.GetMock() .Setup(s => s.Start(notification, It.IsAny(), 0)).Verifiable(); mocker.GetMock() .Setup(s => s.GetEpisodeNumbersBySeason(1, 1)).Returns(new List {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); //Act mocker.Resolve().Start(notification, 0, 0); //Assert mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), It.IsAny()), Times.Never()); mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), 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.CreateListOfSize(5) .All() .With(e => e.SeriesId = 1) .With(e => e.SeasonNumber = 1) .Build(); var mocker = new AutoMoqer(MockBehavior.Strict); mocker.GetMock() .Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes); mocker.GetMock() .Setup(s => s.Start(notification, It.IsAny(), It.IsAny())).Verifiable(); mocker.GetMock() .Setup(s => s.GetEpisodeNumbersBySeason(1, 1)).Returns(episodes.Select(e => e.EpisodeNumber).ToList()); //Act mocker.Resolve().Start(notification, 0, 0); //Assert mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), It.IsAny()), Times.Once()); mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), 0), Times.Never()); } [Test] public void multiple_missing_episodes() { //Setup var notification = new ProgressNotification("Backlog Search Job Test"); var episodes = Builder.CreateListOfSize(10) .TheFirst(5) .With(e => e.SeriesId = 1) .With(e => e.SeasonNumber = 1) .Build(); var mocker = new AutoMoqer(MockBehavior.Strict); mocker.GetMock() .Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes); mocker.GetMock() .Setup(s => s.Start(notification, It.IsAny(), It.IsAny())).Verifiable(); mocker.GetMock() .Setup(s => s.Start(notification, It.IsAny(), 0)).Verifiable(); mocker.GetMock() .Setup(s => s.GetEpisodeNumbersBySeason(1, 1)).Returns(new List{ 1, 2, 3, 4, 5 }); //Act mocker.Resolve().Start(notification, 0, 0); //Assert mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), It.IsAny()), Times.Once()); mocker.GetMock().Verify(c => c.Start(notification, It.IsAny(), 0), Times.Exactly(5)); } } }