You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Radarr/src/NzbDrone.Core.Test/MediaFiles/RenameMovieFileServiceFixtu...

122 lines
3.8 KiB

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<RenameMovieFileService>
{
private Movie _movie;
private List<MovieFile> _movieFiles;
[SetUp]
public void Setup()
{
_movie = Builder<Movie>.CreateNew()
.Build();
_movieFiles = Builder<MovieFile>.CreateListOfSize(2)
.All()
.With(e => e.MovieId = _movie.Id)
.Build()
.ToList();
Mocker.GetMock<IMovieService>()
.Setup(s => s.GetMovie(_movie.Id))
.Returns(_movie);
}
private void GivenNoEpisodeFiles()
{
Mocker.GetMock<IMediaFileService>()
.Setup(s => s.GetMovies(It.IsAny<IEnumerable<int>>()))
.Returns(new List<MovieFile>());
}
private void GivenEpisodeFiles()
{
Mocker.GetMock<IMediaFileService>()
.Setup(s => s.GetMovies(It.IsAny<IEnumerable<int>>()))
.Returns(_movieFiles);
}
private void GivenMovedFiles()
{
Mocker.GetMock<IMoveMovieFiles>()
.Setup(s => s.MoveMovieFile(It.IsAny<MovieFile>(), _movie));
}
[Test]
public void should_not_publish_event_if_no_files_to_rename()
{
GivenNoEpisodeFiles();
Subject.Execute(new RenameMovieFilesCommand(_movie.Id, new List<int>{1}));
Mocker.GetMock<IEventAggregator>()
.Verify(v => v.PublishEvent(It.IsAny<MovieRenamedEvent>()), Times.Never());
}
[Test]
public void should_not_publish_event_if_no_files_are_renamed()
{
GivenEpisodeFiles();
Mocker.GetMock<IMoveMovieFiles>()
.Setup(s => s.MoveMovieFile(It.IsAny<MovieFile>(), It.IsAny<Movie>()))
.Throws(new SameFilenameException("Same file name", "Filename"));
Subject.Execute(new RenameMovieFilesCommand(_movie.Id, new List<int> { 1 }));
Mocker.GetMock<IEventAggregator>()
.Verify(v => v.PublishEvent(It.IsAny<MovieRenamedEvent>()), Times.Never());
}
[Test]
public void should_publish_event_if_files_are_renamed()
{
GivenEpisodeFiles();
GivenMovedFiles();
Subject.Execute(new RenameMovieFilesCommand(_movie.Id, new List<int> { 1 }));
Mocker.GetMock<IEventAggregator>()
.Verify(v => v.PublishEvent(It.IsAny<MovieRenamedEvent>()), Times.Once());
}
[Test]
public void should_update_moved_files()
{
GivenEpisodeFiles();
GivenMovedFiles();
Subject.Execute(new RenameMovieFilesCommand(_movie.Id, new List<int> { 1 }));
Mocker.GetMock<IMediaFileService>()
.Verify(v => v.Update(It.IsAny<MovieFile>()), Times.Exactly(2));
}
[Test]
public void should_get_episodefiles_by_ids_only()
{
GivenEpisodeFiles();
GivenMovedFiles();
var files = new List<int> { 1 };
Subject.Execute(new RenameMovieFilesCommand(_movie.Id, files));
Mocker.GetMock<IMediaFileService>()
.Verify(v => v.GetMovies(files), Times.Once());
}
}
}