using System.Collections.Generic; using System.Linq; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles.Commands; using NzbDrone.Core.MediaFiles.Events; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Tv; namespace NzbDrone.Core.Test.MediaFiles { public class RenameEpisodeFileServiceFixture : CoreTest { private Series _series; private List _episodeFiles; [SetUp] public void Setup() { _series = Builder.CreateNew() .Build(); _episodeFiles = Builder.CreateListOfSize(2) .All() .With(e => e.SeriesId = _series.Id) .With(e => e.SeasonNumber = 1) .Build() .ToList(); Mocker.GetMock() .Setup(s => s.GetSeries(_series.Id)) .Returns(_series); } private void GivenNoEpisodeFiles() { Mocker.GetMock() .Setup(s => s.GetFilesBySeries(_series.Id)) .Returns(new List()); Mocker.GetMock() .Setup(s => s.GetFilesBySeason(_series.Id, _episodeFiles.First().SeasonNumber)) .Returns(new List()); } private void GivenEpisodeFiles() { Mocker.GetMock() .Setup(s => s.GetFilesBySeries(_series.Id)) .Returns(_episodeFiles); Mocker.GetMock() .Setup(s => s.GetFilesBySeason(_series.Id, _episodeFiles.First().SeasonNumber)) .Returns(_episodeFiles); } private void GivenMovedFiles() { Mocker.GetMock() .Setup(s => s.MoveEpisodeFile(It.IsAny(), _series)); } [Test] public void should_not_publish_event_if_no_files_to_rename() { GivenNoEpisodeFiles(); Subject.Execute(new RenameSeriesCommand(_series.Id)); Mocker.GetMock() .Verify(v => v.PublishEvent(It.IsAny()), Times.Never()); } [Test] public void should_not_publish_event_if_no_files_are_renamed() { GivenEpisodeFiles(); Mocker.GetMock() .Setup(s => s.MoveEpisodeFile(It.IsAny(), It.IsAny())) .Throws(new SameFilenameException("Same file name", "Filename")); Subject.Execute(new RenameSeriesCommand(_series.Id)); Mocker.GetMock() .Verify(v => v.PublishEvent(It.IsAny()), Times.Never()); } [Test] public void should_publish_event_if_files_are_renamed() { GivenEpisodeFiles(); GivenMovedFiles(); Subject.Execute(new RenameSeriesCommand(_series.Id)); Mocker.GetMock() .Verify(v => v.PublishEvent(It.IsAny()), Times.Once()); } [Test] public void should_update_moved_files() { GivenEpisodeFiles(); GivenMovedFiles(); Subject.Execute(new RenameSeriesCommand(_series.Id)); Mocker.GetMock() .Verify(v => v.Update(It.IsAny()), Times.Exactly(2)); } [Test] public void rename_season_should_get_episodefiles_for_season() { GivenEpisodeFiles(); GivenMovedFiles(); Subject.Execute(new RenameSeasonCommand(_series.Id, _episodeFiles.First().SeasonNumber)); Mocker.GetMock() .Verify(v => v.GetFilesBySeries(_series.Id), Times.Never()); Mocker.GetMock() .Verify(v => v.GetFilesBySeason(_series.Id, _episodeFiles.First().SeasonNumber), Times.Once()); } [Test] public void rename_series_should_get_episodefiles_for_series() { GivenEpisodeFiles(); GivenMovedFiles(); Subject.Execute(new RenameSeriesCommand(_series.Id)); Mocker.GetMock() .Verify(v => v.GetFilesBySeries(_series.Id), Times.Once()); Mocker.GetMock() .Verify(v => v.GetFilesBySeason(_series.Id, _episodeFiles.First().SeasonNumber), Times.Never()); } } }