// ReSharper disable RedundantUsingDirective using System; using System.Collections.Generic; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Core.Jobs; using NzbDrone.Core.Model.Notification; using NzbDrone.Core.Providers; using NzbDrone.Core.Repository; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; using NzbDrone.Test.Common.AutoMoq; namespace NzbDrone.Core.Test.JobTests { [TestFixture] // ReSharper disable InconsistentNaming public class ImportNewSeriesJobTest : CoreTest { [Test] public void import_new_series_succesfull() { var series = Builder.CreateListOfSize(2) .All().With(s => s.LastInfoSync = null) .TheFirst(1).With(s => s.SeriesId = 12) .TheNext(1).With(s => s.SeriesId = 15) .Build(); var notification = new ProgressNotification("Test"); WithStrictMocker(); Mocker.GetMock() .Setup(p => p.GetAllSeries()) .Returns(series); Mocker.GetMock() .Setup(j => j.Start(notification, new { SeriesId = series[0].SeriesId, SeasonNumber = 0})) .Callback(() => series[0].LastDiskSync = DateTime.Now); Mocker.GetMock() .Setup(j => j.Start(notification, new { SeriesId = series[1].SeriesId, SeasonNumber = 0 })) .Callback(() => series[1].LastDiskSync = DateTime.Now); Mocker.GetMock() .Setup(j => j.Start(notification, new { SeriesId = It.IsAny(), SeasonNumber = 0 })); Mocker.GetMock() .Setup(j => j.Start(notification, new { SeriesId = series[0].SeriesId, SeasonNumber = 0 })) .Callback(() => series[0].LastInfoSync = DateTime.Now); Mocker.GetMock() .Setup(j => j.Start(notification, new { SeriesId = series[1].SeriesId, SeasonNumber = 0 })) .Callback(() => series[1].LastInfoSync = DateTime.Now); Mocker.GetMock() .Setup(s => s.GetSeries(series[0].SeriesId)).Returns(series[0]); Mocker.GetMock() .Setup(s => s.GetSeries(series[1].SeriesId)).Returns(series[1]); Mocker.GetMock() .Setup(s => s.GetSeriesFiles(It.IsAny())).Returns(new List()); //Act Mocker.Resolve().Start(notification, null); //Assert Mocker.GetMock().Verify(j => j.Start(notification, new { SeriesId = series[0].SeriesId, SeasonNumber = 0 }), Times.Once()); Mocker.GetMock().Verify(j => j.Start(notification, new { SeriesId = series[1].SeriesId, SeasonNumber = 0 }), Times.Once()); Mocker.GetMock().Verify(j => j.Start(notification, new { SeriesId = series[0].SeriesId, SeasonNumber = 0 }), Times.Once()); Mocker.GetMock().Verify(j => j.Start(notification, new { SeriesId = series[1].SeriesId, SeasonNumber = 0 }), Times.Once()); } [Test] [Timeout(3000)] public void failed_import_should_not_be_stuck_in_loop() { var series = Builder.CreateListOfSize(2) .All().With(s => s.LastInfoSync = null) .TheFirst(1).With(s => s.SeriesId = 12) .TheNext(1).With(s => s.SeriesId = 15) .Build(); var notification = new ProgressNotification("Test"); WithStrictMocker(); Mocker.GetMock() .Setup(p => p.GetAllSeries()) .Returns(series); Mocker.GetMock() .Setup(j => j.Start(notification, new { SeriesId = series[0].SeriesId, SeasonNumber = 0 })) .Callback(() => series[0].LastInfoSync = DateTime.Now); Mocker.GetMock() .Setup(j => j.Start(notification, new { SeriesId = series[1].SeriesId, SeasonNumber = 0 })) .Throws(new InvalidOperationException()); Mocker.GetMock() .Setup(j => j.Start(notification, new { SeriesId = series[0].SeriesId, SeasonNumber = 0 })) .Callback(() => series[0].LastDiskSync = DateTime.Now); Mocker.GetMock() .Setup(j => j.Start(notification, new { SeriesId = series[0].SeriesId, SeasonNumber = 0 })); Mocker.GetMock() .Setup(s => s.GetSeries(series[0].SeriesId)).Returns(series[0]); Mocker.GetMock() .Setup(s => s.GetSeriesFiles(It.IsAny())).Returns(new List()); //Act Mocker.Resolve().Start(notification, null); //Assert Mocker.VerifyAllMocks(); Mocker.GetMock().Verify(j => j.Start(notification, new { SeriesId = series[0].SeriesId, SeasonNumber = 0 }), Times.Once()); Mocker.GetMock().Verify(j => j.Start(notification, new { SeriesId = series[1].SeriesId, SeasonNumber = 0 }), Times.Once()); Mocker.GetMock().Verify(j => j.Start(notification, new { SeriesId = series[0].SeriesId, SeasonNumber = 0}), Times.Once()); ExceptionVerification.ExpectedErrors(1); } [Test] public void AutoIgnoreSeason_new_series_should_not_ignore_any() { int seriesId = 12; WithStrictMocker(); Mocker.GetMock() .Setup(p => p.GetSeriesFiles(seriesId)) .Returns(new List()); Mocker.GetMock() .Setup(p => p.GetSeasons(seriesId)) .Returns(new List { 0, 1, 2, 3, 4 }); Mocker.Resolve().AutoIgnoreSeasons(seriesId); Mocker.GetMock().Verify(p => p.SetIgnore(seriesId, It.IsAny(), It.IsAny()), Times.Never()); } [Test] public void AutoIgnoreSeason_existing_should_not_ignore_currentseason() { int seriesId = 12; var episodesFiles = Builder.CreateListOfSize(2) .All().With(e => e.SeriesId = seriesId) .Build(); episodesFiles[0].SeasonNumber = 0; episodesFiles[1].SeasonNumber = 1; WithStrictMocker(); Mocker.GetMock() .Setup(p => p.GetSeriesFiles(seriesId)) .Returns(episodesFiles); Mocker.GetMock() .Setup(p => p.GetSeasons(seriesId)) .Returns(new List { 0, 1, 2 }); Mocker.Resolve().AutoIgnoreSeasons(seriesId); Mocker.GetMock().Verify(p => p.SetIgnore(seriesId, 2, It.IsAny()), Times.Never()); } [Test] public void AutoIgnoreSeason_existing_should_ignore_seasons_with_no_file() { int seriesId = 12; var episodesFiles = Builder.CreateListOfSize(2) .All().With(e => e.SeriesId = seriesId) .Build(); episodesFiles[0].SeasonNumber = 1; Mocker.GetMock() .Setup(p => p.GetSeriesFiles(seriesId)) .Returns(episodesFiles); Mocker.GetMock() .Setup(p => p.GetSeasons(seriesId)) .Returns(new List { 0, 1, 2 }); Mocker.Resolve().AutoIgnoreSeasons(seriesId); Mocker.GetMock().Verify(p => p.SetIgnore(seriesId, 0, true), Times.Once()); Mocker.GetMock().Verify(p => p.SetIgnore(seriesId, 1, true), Times.Never()); Mocker.GetMock().Verify(p => p.SetIgnore(seriesId, 2, It.IsAny()), Times.Never()); } } }