using System; using System.Collections.Generic; using AutoMoq; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Core.Providers; using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Repository; using NzbDrone.Core.Test.Framework; namespace NzbDrone.Core.Test.ProviderTests { // ReSharper disable InconsistentNaming public class DiskScanProviderTest : TestBase { [Test] public void scan_series_should_update_the_last_scan_date() { var mocker = new AutoMoqer(); mocker.GetMock() .Setup(c => c.UpdateSeries(It.Is(s => s.LastDiskSync != null))).Verifiable(); mocker.GetMock() .Setup(c => c.GetEpisodeBySeries(It.IsAny())) .Returns(new List { new Episode() }); mocker.GetMock().Setup(c => c.FolderExists(It.IsAny())).Returns(true); mocker.GetMock() .Setup(c => c.GetSeriesFiles(It.IsAny())) .Returns(new List()); mocker.Resolve().Scan(new Series()); mocker.VerifyAllMocks(); } [Test] public void cleanup_should_skip_existing_files() { var mocker = new AutoMoqer(MockBehavior.Strict); var episodes = Builder.CreateListOfSize(10).Build(); mocker.GetMock() .Setup(e => e.FileExists(It.IsAny())) .Returns(true); //Act mocker.Resolve().CleanUp(episodes); //Assert mocker.VerifyAllMocks(); } [Test] public void cleanup_should_delete_none_existing_files() { var mocker = new AutoMoqer(MockBehavior.Strict); var episodes = Builder.CreateListOfSize(10).Build(); mocker.GetMock() .Setup(e => e.FileExists(It.IsAny())) .Returns(false); mocker.GetMock() .Setup(e => e.GetEpisodesByFileId(It.IsAny())) .Returns(new List()); mocker.GetMock() .Setup(e => e.Delete(It.IsAny())); //Act mocker.Resolve().CleanUp(episodes); //Assert mocker.VerifyAllMocks(); mocker.GetMock() .Verify(e => e.GetEpisodesByFileId(It.IsAny()), Times.Exactly(10)); mocker.GetMock() .Verify(e => e.Delete(It.IsAny()), Times.Exactly(10)); } [Test] public void cleanup_should_delete_none_existing_files_remove_links_to_episodes() { var mocker = new AutoMoqer(MockBehavior.Strict); var episodes = Builder.CreateListOfSize(10).Build(); mocker.GetMock() .Setup(e => e.FileExists(It.IsAny())) .Returns(false); mocker.GetMock() .Setup(e => e.GetEpisodesByFileId(It.IsAny())) .Returns(new List { new Episode { EpisodeFileId = 10 }, new Episode { EpisodeFileId = 10 } }); mocker.GetMock() .Setup(e => e.UpdateEpisode(It.IsAny())); mocker.GetMock() .Setup(e => e.Delete(It.IsAny())); //Act mocker.Resolve().CleanUp(episodes); //Assert mocker.VerifyAllMocks(); mocker.GetMock() .Verify(e => e.GetEpisodesByFileId(It.IsAny()), Times.Exactly(10)); mocker.GetMock() .Verify(e => e.UpdateEpisode(It.Is(g=>g.EpisodeFileId == 0)), Times.Exactly(20)); mocker.GetMock() .Verify(e => e.Delete(It.IsAny()), Times.Exactly(10)); mocker.GetMock() .Verify(e => e.Delete(It.IsAny()), Times.Exactly(10)); } [Test] public void scan_series_should_log_warning_if_path_doesnt_exist_on_disk() { //Setup var mocker = new AutoMoqer(MockBehavior.Strict); var series = Builder.CreateNew() .With(s => s.Path = @"C:\Test\TV\SeriesName\") .Build(); mocker.GetMock() .Setup(c => c.DeleteOrphaned()) .Returns(0); mocker.GetMock() .Setup(c => c.RepairLinks()) .Returns(0); mocker.GetMock() .Setup(c => c.FolderExists(series.Path)) .Returns(false); //Act mocker.Resolve().Scan(series, series.Path); //Assert mocker.VerifyAllMocks(); ExceptionVerification.ExcpectedWarns(1); } } }