using System; using System.Collections.Generic; using System.Linq; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Core.Jobs.Implementations; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Tv; using NzbDrone.Core.Jobs; using NzbDrone.Core.Model.Notification; using NzbDrone.Core.Providers; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; using NzbDrone.Test.Common.AutoMoq; namespace NzbDrone.Core.Test.JobTests { [TestFixture] public class ImportNewSeriesJobTest : CoreTest { [Test] public void import_new_series_succesful() { var series = Builder.CreateListOfSize(2) .All().With(s => s.LastInfoSync = null) .TheFirst(1).With(s => s.Id = 12) .TheNext(1).With(s => s.Id = 15) .Build(); var notification = new ProgressNotification("Test"); WithStrictMocker(); Mocker.GetMock() .Setup(p => p.All()) .Returns(series); Mocker.GetMock() .Setup(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[0].Id))) .Callback(() => series[0].LastDiskSync = DateTime.Now); Mocker.GetMock() .Setup(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[1].Id))) .Callback(() => series[1].LastDiskSync = DateTime.Now); Mocker.GetMock() .Setup(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") > 0))); Mocker.GetMock() .Setup(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[0].Id))) .Callback(() => series[0].LastInfoSync = DateTime.Now); Mocker.GetMock() .Setup(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[1].Id))) .Callback(() => series[1].LastInfoSync = DateTime.Now); Mocker.GetMock() .Setup(s => s.Get(series[0].Id)).Returns(series[0]); Mocker.GetMock() .Setup(s => s.Get(series[1].Id)).Returns(series[1]); Mocker.GetMock() .Setup(s => s.GetFilesBySeries(It.IsAny())).Returns(new List()); Mocker.Resolve().Start(notification, null); Mocker.GetMock().Verify(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[0].Id)), Times.Once()); Mocker.GetMock().Verify(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[1].Id)), Times.Once()); Mocker.GetMock().Verify(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[0].Id)), Times.Once()); Mocker.GetMock().Verify(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[1].Id)), 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.Id = 12) .TheNext(1).With(s => s.Id = 15) .Build(); var notification = new ProgressNotification("Test"); WithStrictMocker(); Mocker.GetMock() .Setup(p => p.All()) .Returns(series); Mocker.GetMock() .Setup(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[0].Id))) .Callback(() => series[0].LastInfoSync = DateTime.Now); Mocker.GetMock() .Setup(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[1].Id))) .Throws(new InvalidOperationException()); Mocker.GetMock() .Setup(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[0].Id))) .Callback(() => series[0].LastDiskSync = DateTime.Now); Mocker.GetMock() .Setup(s => s.Get(series[0].Id)).Returns(series[0]); Mocker.GetMock() .Setup(s => s.GetFilesBySeries(It.IsAny())).Returns(new List()); Mocker.GetMock() .Setup(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[0].Id))); Mocker.Resolve().Start(notification, null); Mocker.GetMock().Verify(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[0].Id)), Times.Once()); Mocker.GetMock().Verify(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[1].Id)), Times.Once()); Mocker.GetMock().Verify(j => j.Start(notification, It.Is(d => d.GetPropertyValue("SeriesId") == series[0].Id)), Times.Once()); ExceptionVerification.ExpectedErrors(1); } [Test] public void AutoIgnoreSeason_new_series_should_not_ignore_any() { int seriesId = 12; WithStrictMocker(); Mocker.GetMock() .Setup(p => p.GetFilesBySeries(seriesId)) .Returns(new List()); Mocker.GetMock() .Setup(p => p.GetSeasonNumbers(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.GetFilesBySeries(seriesId)) .Returns(episodesFiles.ToList()); Mocker.GetMock() .Setup(p => p.GetSeasonNumbers(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.GetFilesBySeries(seriesId)) .Returns(episodesFiles.ToList()); Mocker.GetMock() .Setup(p => p.GetSeasonNumbers(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()); } } }