diff --git a/src/NzbDrone.Core.Test/MediaFiles/MediaFileRepositoryFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/MediaFileRepositoryFixture.cs index d96b2c947..e504d6a52 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/MediaFileRepositoryFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/MediaFileRepositoryFixture.cs @@ -35,23 +35,31 @@ namespace NzbDrone.Core.Test.MediaFiles .Build(); Db.Insert(album); - var release = Builder.CreateNew() + var releases = Builder.CreateListOfSize(2) + .All() .With(a => a.Id = 0) .With(a => a.AlbumId = album.Id) + .TheFirst(1) .With(a => a.Monitored = true) + .TheNext(1) + .With(a => a.Monitored = false) .Build(); - Db.Insert(release); + Db.InsertMany(releases); var files = Builder.CreateListOfSize(10) .All() .With(c => c.Id = 0) .With(c => c.Quality =new QualityModel(Quality.MP3_192)) - .TheFirst(4) + .TheFirst(5) .With(c => c.AlbumId = album.Id) .BuildListOfNew(); Db.InsertMany(files); var track = Builder.CreateListOfSize(10) + .All() + .With(a => a.Id = 0) + .TheFirst(4) + .With(a => a.AlbumReleaseId = releases[0].Id) .TheFirst(1) .With(a => a.TrackFileId = files[0].Id) .TheNext(1) @@ -60,11 +68,11 @@ namespace NzbDrone.Core.Test.MediaFiles .With(a => a.TrackFileId = files[2].Id) .TheNext(1) .With(a => a.TrackFileId = files[3].Id) - .TheNext(6) + .TheNext(1) + .With(a => a.TrackFileId = files[4].Id) + .With(a => a.AlbumReleaseId = releases[1].Id) + .TheNext(5) .With(a => a.TrackFileId = 0) - .All() - .With(a => a.Id = 0) - .With(a => a.AlbumReleaseId = release.Id) .Build(); Db.InsertMany(track); } @@ -76,19 +84,28 @@ namespace NzbDrone.Core.Test.MediaFiles 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_artist_should_only_return_tracks_for_monitored_releases() + { + VerifyData(); + var artistFiles = Subject.GetFilesByArtist(artist.Id); + VerifyEagerLoaded(artistFiles); + + artistFiles.Should().HaveCount(4); + } + [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); + files.Should().HaveCount(5); } [Test] @@ -98,10 +115,20 @@ namespace NzbDrone.Core.Test.MediaFiles 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"); } + + [Test] + public void get_files_by_relative_path_should_only_contain_monitored_releases() + { + VerifyData(); + + // file 5 is linked to an unmonitored release + var files = Subject.GetFilesWithRelativePath(artist.Id, "RelativePath5"); + + files.Should().BeEmpty(); + } private void VerifyData() { diff --git a/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs b/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs index 06aafda30..0d0dc6862 100644 --- a/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs +++ b/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs @@ -34,7 +34,7 @@ namespace NzbDrone.Core.MediaFiles public List GetFilesByArtist(int artistId) { return Query - .Join(JoinType.Inner, a => a.AlbumReleases, (a, r) => a.Id == r.AlbumId) + .Join(JoinType.Inner, t => t.AlbumRelease, (t, r) => t.AlbumReleaseId == r.Id) .Where(r => r.Monitored == true) .AndWhere(t => t.Artist.Value.Id == artistId) .ToList(); @@ -58,10 +58,9 @@ namespace NzbDrone.Core.MediaFiles public List GetFilesWithRelativePath(int artistId, string relativePath) { return Query - .Join(JoinType.Inner, a => a.AlbumReleases, (a, r) => a.Id == r.AlbumId) + .Join(JoinType.Inner, t => t.AlbumRelease, (t, r) => t.AlbumReleaseId == r.Id) .Where(r => r.Monitored == true) - .AndWhere(t => t.Artist.Value.Id == artistId) - .AndWhere(t => t.RelativePath == relativePath) + .AndWhere(t => t.Artist.Value.Id == artistId && t.RelativePath == relativePath) .ToList(); } }