using System.Collections.Generic; using System.IO; using FizzWare.NBuilder; using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; using NzbDrone.Core.Books; using NzbDrone.Core.Datastore; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.RootFolders; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.MediaFiles { public class UpgradeMediaFileServiceFixture : CoreTest { private BookFile _trackFile; private LocalBook _localTrack; private string _rootPath = @"C:\Test\Music\Artist".AsOsAgnostic(); [SetUp] public void Setup() { _localTrack = new LocalBook(); _localTrack.Author = new Author { Path = _rootPath }; _trackFile = Builder .CreateNew() .Build(); Mocker.GetMock() .Setup(c => c.FileExists(It.IsAny())) .Returns(true); Mocker.GetMock() .Setup(c => c.FolderExists(It.IsAny())) .Returns(true); Mocker.GetMock() .Setup(c => c.GetParentFolder(It.IsAny())) .Returns(c => Path.GetDirectoryName(c)); Mocker.GetMock() .Setup(c => c.GetBestRootFolder(It.IsAny())) .Returns(new RootFolder()); } private void GivenSingleTrackWithSingleTrackFile() { _localTrack.Book = Builder.CreateNew() .With(e => e.BookFiles = new LazyLoaded>( new List { new BookFile { Id = 1, Path = Path.Combine(_rootPath, @"Season 01\30.rock.s01e01.avi"), } })) .Build(); } [Test] public void should_delete_single_track_file_once() { GivenSingleTrackWithSingleTrackFile(); Subject.UpgradeBookFile(_trackFile, _localTrack); Mocker.GetMock().Verify(v => v.DeleteFile(It.IsAny(), It.IsAny()), Times.Once()); } [Test] public void should_delete_track_file_from_database() { GivenSingleTrackWithSingleTrackFile(); Subject.UpgradeBookFile(_trackFile, _localTrack); Mocker.GetMock().Verify(v => v.Delete(It.IsAny(), DeleteMediaFileReason.Upgrade), Times.Once()); } [Test] public void should_delete_existing_file_fromdb_if_file_doesnt_exist() { GivenSingleTrackWithSingleTrackFile(); Mocker.GetMock() .Setup(c => c.FileExists(It.IsAny())) .Returns(false); Subject.UpgradeBookFile(_trackFile, _localTrack); // Mocker.GetMock().Verify(v => v.Delete(_localTrack.Album.BookFiles.Value, DeleteMediaFileReason.Upgrade), Times.Once()); } [Test] public void should_not_try_to_recyclebin_existing_file_if_file_doesnt_exist() { GivenSingleTrackWithSingleTrackFile(); Mocker.GetMock() .Setup(c => c.FileExists(It.IsAny())) .Returns(false); Subject.UpgradeBookFile(_trackFile, _localTrack); Mocker.GetMock().Verify(v => v.DeleteFile(It.IsAny(), It.IsAny()), Times.Never()); } [Test] public void should_return_old_track_file_in_oldFiles() { GivenSingleTrackWithSingleTrackFile(); Subject.UpgradeBookFile(_trackFile, _localTrack).OldFiles.Count.Should().Be(1); } [Test] [Ignore("Pending readarr fix")] public void should_import_if_existing_file_doesnt_exist_in_db() { _localTrack.Book = Builder.CreateNew() .With(e => e.BookFiles = new LazyLoaded>()) .Build(); Subject.UpgradeBookFile(_trackFile, _localTrack); // Mocker.GetMock().Verify(v => v.Delete(_localTrack.Album.BookFiles.Value, It.IsAny()), Times.Never()); } } }