using FizzWare.NBuilder; using FluentAssertions; using NUnit.Framework; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Qualities; using NzbDrone.Core.Music; using NzbDrone.Core.Test.Framework; using System.Collections.Generic; namespace NzbDrone.Core.Test.MediaFiles { [TestFixture] public class MediaFileRepositoryFixture : DbTest { private Artist artist; private Album album; [SetUp] public void Setup() { var meta = Builder.CreateNew() .With(a => a.Id = 0) .Build(); Db.Insert(meta); artist = Builder.CreateNew() .With(a => a.ArtistMetadataId = meta.Id) .With(a => a.Id = 0) .Build(); Db.Insert(artist); album = Builder.CreateNew() .With(a => a.Id = 0) .With(a => a.ArtistMetadataId = artist.ArtistMetadataId) .Build(); Db.Insert(album); var release = Builder.CreateNew() .With(a => a.Id = 0) .With(a => a.AlbumId = album.Id) .With(a => a.Monitored = true) .Build(); Db.Insert(release); var files = Builder.CreateListOfSize(10) .All() .With(c => c.Id = 0) .With(c => c.Quality =new QualityModel(Quality.MP3_192)) .TheFirst(4) .With(c => c.AlbumId = album.Id) .BuildListOfNew(); Db.InsertMany(files); var track = Builder.CreateListOfSize(10) .TheFirst(1) .With(a => a.TrackFileId = files[1].Id) .TheNext(1) .With(a => a.TrackFileId = files[2].Id) .TheNext(1) .With(a => a.TrackFileId = files[3].Id) .TheNext(1) .With(a => a.TrackFileId = files[4].Id) .TheNext(6) .With(a => a.TrackFileId = 0) .All() .With(a => a.Id = 0) .With(a => a.AlbumReleaseId = release.Id) .Build(); Db.InsertMany(track); } [Test] public void get_files_by_artist() { VerifyData(); var artistFiles = Subject.GetFilesByArtist(artist.Id); VerifyEagerLoaded(artistFiles); artistFiles.Should().HaveCount(4); artistFiles.Should().OnlyContain(c => c.Artist.Value.Id == artist.Id); } [Test] public void get_files_by_album() { VerifyData(); var files = Subject.GetFilesByAlbum(album.Id); VerifyEagerLoaded(files); files.Should().HaveCount(4); files.Should().OnlyContain(c => c.AlbumId == album.Id); } [Test] public void get_files_by_relative_path() { VerifyData(); var files = Subject.GetFilesWithRelativePath(artist.Id, "RelativePath2"); VerifyEagerLoaded(files); files.Should().HaveCount(1); files.Should().OnlyContain(c => c.AlbumId == album.Id); files.Should().OnlyContain(c => c.RelativePath == "RelativePath2"); } private void VerifyData() { Db.All().Should().HaveCount(1); Db.All().Should().HaveCount(1); Db.All().Should().HaveCount(10); Db.All().Should().HaveCount(10); } private void VerifyEagerLoaded(List files) { foreach (var file in files) { file.Album.IsLoaded.Should().BeTrue(); file.Artist.IsLoaded.Should().BeTrue(); file.Artist.Value.Metadata.IsLoaded.Should().BeTrue(); } } } }