using System; using System.Collections.Generic; using System.Linq; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Common.Extensions; using NzbDrone.Core.Exceptions; using NzbDrone.Core.MetadataSource; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Music; using NzbDrone.Core.Music.Commands; using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.MusicTests { [TestFixture] public class RefreshArtistServiceFixture : CoreTest { private Artist _artist; private Album _album1; private Album _album2; private List _albums; [SetUp] public void Setup() { _album1 = Builder.CreateNew() .With(s => s.ForeignAlbumId = "1") .Build(); _album2 = Builder.CreateNew() .With(s => s.ForeignAlbumId = "2") .Build(); _albums = new List {_album1, _album2}; _artist = Builder.CreateNew() .Build(); Mocker.GetMock() .Setup(s => s.GetArtist(_artist.Id)) .Returns(_artist); Mocker.GetMock() .Setup(s => s.GetAlbumsByArtist(It.IsAny())) .Returns(new List()); Mocker.GetMock() .Setup(s => s.GetArtistInfo(It.IsAny(), It.IsAny())) .Callback(() => { throw new ArtistNotFoundException(_artist.ForeignArtistId); }); } private void GivenNewArtistInfo(Artist artist) { Mocker.GetMock() .Setup(s => s.GetArtistInfo(_artist.ForeignArtistId, _artist.MetadataProfileId)) .Returns(new Tuple>(artist, _albums)); } [Test] public void should_log_error_if_musicbrainz_id_not_found() { Subject.Execute(new RefreshArtistCommand(_artist.Id)); Mocker.GetMock() .Verify(v => v.UpdateArtist(It.IsAny()), Times.Never()); ExceptionVerification.ExpectedErrors(1); } [Test] public void should_update_if_musicbrainz_id_changed() { var newArtistInfo = _artist.JsonClone(); newArtistInfo.ForeignArtistId = _artist.ForeignArtistId + 1; GivenNewArtistInfo(newArtistInfo); Subject.Execute(new RefreshArtistCommand(_artist.Id)); Mocker.GetMock() .Verify(v => v.UpdateArtist(It.Is(s => s.ForeignArtistId == newArtistInfo.ForeignArtistId))); ExceptionVerification.ExpectedWarns(1); } [Test] [Ignore("This test needs to be re-written as we no longer store albums in artist table or object")] public void should_not_throw_if_duplicate_album_is_in_existing_info() { var newArtistInfo = _artist.JsonClone(); newArtistInfo.Albums.Add(Builder.CreateNew() .With(s => s.ForeignAlbumId = "2") .Build()); _artist.Albums.Add(Builder.CreateNew() .With(s => s.ForeignAlbumId = "2") .Build()); _artist.Albums.Add(Builder.CreateNew() .With(s => s.ForeignAlbumId = "2") .Build()); GivenNewArtistInfo(newArtistInfo); Subject.Execute(new RefreshArtistCommand(_artist.Id)); Mocker.GetMock() .Verify(v => v.UpdateArtist(It.Is(s => s.Albums.Count == 2))); } [Test] [Ignore("This test needs to be re-written as we no longer store albums in artist table or object")] public void should_filter_duplicate_albums() { var newArtistInfo = _artist.JsonClone(); newArtistInfo.Albums.Add(Builder.CreateNew() .With(s => s.ForeignAlbumId = "2") .Build()); newArtistInfo.Albums.Add(Builder.CreateNew() .With(s => s.ForeignAlbumId = "2") .Build()); GivenNewArtistInfo(newArtistInfo); Subject.Execute(new RefreshArtistCommand(_artist.Id)); Mocker.GetMock() .Verify(v => v.UpdateArtist(It.Is(s => s.Albums.Count == 2))); } } }