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.Movies; namespace NzbDrone.Core.Test.MediaFiles { public class RenameMovieFileServiceFixture : CoreTest { private Movie _movie; private List _movieFiles; [SetUp] public void Setup() { _movie = Builder.CreateNew() .Build(); _movieFiles = Builder.CreateListOfSize(2) .All() .With(e => e.MovieId = _movie.Id) .Build() .ToList(); Mocker.GetMock() .Setup(s => s.GetMovie(_movie.Id)) .Returns(_movie); } private void GivenNoEpisodeFiles() { Mocker.GetMock() .Setup(s => s.GetMovies(It.IsAny>())) .Returns(new List()); } private void GivenEpisodeFiles() { Mocker.GetMock() .Setup(s => s.GetMovies(It.IsAny>())) .Returns(_movieFiles); } private void GivenMovedFiles() { Mocker.GetMock() .Setup(s => s.MoveMovieFile(It.IsAny(), _movie)); } [Test] public void should_not_publish_event_if_no_files_to_rename() { GivenNoEpisodeFiles(); Subject.Execute(new RenameMovieFilesCommand(_movie.Id, new List{1})); 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.MoveMovieFile(It.IsAny(), It.IsAny())) .Throws(new SameFilenameException("Same file name", "Filename")); Subject.Execute(new RenameMovieFilesCommand(_movie.Id, new List { 1 })); 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 RenameMovieFilesCommand(_movie.Id, new List { 1 })); Mocker.GetMock() .Verify(v => v.PublishEvent(It.IsAny()), Times.Once()); } [Test] public void should_update_moved_files() { GivenEpisodeFiles(); GivenMovedFiles(); Subject.Execute(new RenameMovieFilesCommand(_movie.Id, new List { 1 })); Mocker.GetMock() .Verify(v => v.Update(It.IsAny()), Times.Exactly(2)); } [Test] public void should_get_episodefiles_by_ids_only() { GivenEpisodeFiles(); GivenMovedFiles(); var files = new List { 1 }; Subject.Execute(new RenameMovieFilesCommand(_movie.Id, files)); Mocker.GetMock() .Verify(v => v.GetMovies(files), Times.Once()); } } }