using System.IO; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles.Events; using NzbDrone.Core.MediaFiles.MediaInfo; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Music; using NzbDrone.Test.Common; using NzbDrone.Core.Configuration; namespace NzbDrone.Core.Test.MediaFiles.MediaInfo { [TestFixture] public class UpdateMediaInfoServiceFixture : CoreTest { private Artist _artist; [SetUp] public void Setup() { _artist = new Artist { Id = 1, Path = @"C:\artist".AsOsAgnostic() }; Mocker.GetMock() .SetupGet(s => s.EnableMediaInfo) .Returns(true); } private void GivenFileExists() { Mocker.GetMock() .Setup(v => v.FileExists(It.IsAny())) .Returns(true); } private void GivenSuccessfulScan() { Mocker.GetMock() .Setup(v => v.GetMediaInfo(It.IsAny())) .Returns(new MediaInfoModel()); } private void GivenFailedScan(string path) { Mocker.GetMock() .Setup(v => v.GetMediaInfo(path)) .Returns((MediaInfoModel)null); } [Test] public void should_skip_up_to_date_media_info() { var trackFiles = Builder.CreateListOfSize(3) .All() .With(v => v.RelativePath = "media.flac") .TheFirst(1) .With(v => v.MediaInfo = new MediaInfoModel { SchemaRevision = VideoFileInfoReader.CURRENT_MEDIA_INFO_SCHEMA_REVISION }) .BuildList(); Mocker.GetMock() .Setup(v => v.GetFilesByArtist(1)) .Returns(trackFiles); GivenFileExists(); GivenSuccessfulScan(); Subject.Handle(new ArtistScannedEvent(_artist)); Mocker.GetMock() .Verify(v => v.GetMediaInfo(Path.Combine(_artist.Path, "media.flac")), Times.Exactly(2)); Mocker.GetMock() .Verify(v => v.Update(It.IsAny()), Times.Exactly(2)); } [Test] public void should_skip_not_yet_date_media_info() { var trackFiles = Builder.CreateListOfSize(3) .All() .With(v => v.RelativePath = "media.flac") .TheFirst(1) .With(v => v.MediaInfo = new MediaInfoModel { SchemaRevision = VideoFileInfoReader.MINIMUM_MEDIA_INFO_SCHEMA_REVISION }) .BuildList(); Mocker.GetMock() .Setup(v => v.GetFilesByArtist(1)) .Returns(trackFiles); GivenFileExists(); GivenSuccessfulScan(); Subject.Handle(new ArtistScannedEvent(_artist)); Mocker.GetMock() .Verify(v => v.GetMediaInfo(Path.Combine(_artist.Path, "media.flac")), Times.Exactly(2)); Mocker.GetMock() .Verify(v => v.Update(It.IsAny()), Times.Exactly(2)); } [Test] public void should_update_outdated_media_info() { var trackFiles = Builder.CreateListOfSize(3) .All() .With(v => v.RelativePath = "media.flac") .TheFirst(1) .With(v => v.MediaInfo = new MediaInfoModel()) .BuildList(); Mocker.GetMock() .Setup(v => v.GetFilesByArtist(1)) .Returns(trackFiles); GivenFileExists(); GivenSuccessfulScan(); Subject.Handle(new ArtistScannedEvent(_artist)); Mocker.GetMock() .Verify(v => v.GetMediaInfo(Path.Combine(_artist.Path, "media.flac")), Times.Exactly(3)); Mocker.GetMock() .Verify(v => v.Update(It.IsAny()), Times.Exactly(3)); } [Test] public void should_ignore_missing_files() { var trackFiles = Builder.CreateListOfSize(2) .All() .With(v => v.RelativePath = "media.flac") .BuildList(); Mocker.GetMock() .Setup(v => v.GetFilesByArtist(1)) .Returns(trackFiles); GivenSuccessfulScan(); Subject.Handle(new ArtistScannedEvent(_artist)); Mocker.GetMock() .Verify(v => v.GetMediaInfo("media.flac"), Times.Never()); Mocker.GetMock() .Verify(v => v.Update(It.IsAny()), Times.Never()); } [Test] public void should_continue_after_failure() { var episodeFiles = Builder.CreateListOfSize(2) .All() .With(v => v.RelativePath = "media.flac") .TheFirst(1) .With(v => v.RelativePath = "media2.flac") .BuildList(); Mocker.GetMock() .Setup(v => v.GetFilesByArtist(1)) .Returns(episodeFiles); GivenFileExists(); GivenSuccessfulScan(); GivenFailedScan(Path.Combine(_artist.Path, "media2.flac")); Subject.Handle(new ArtistScannedEvent(_artist)); Mocker.GetMock() .Verify(v => v.GetMediaInfo(Path.Combine(_artist.Path, "media.flac")), Times.Exactly(1)); Mocker.GetMock() .Verify(v => v.Update(It.IsAny()), Times.Exactly(1)); } } }