You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
7.3 KiB
212 lines
7.3 KiB
5 years ago
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
4 years ago
|
using Dapper;
|
||
7 years ago
|
using FizzWare.NBuilder;
|
||
11 years ago
|
using NUnit.Framework;
|
||
|
using NzbDrone.Core.Datastore;
|
||
5 years ago
|
using NzbDrone.Core.MediaFiles;
|
||
7 years ago
|
using NzbDrone.Core.Music;
|
||
5 years ago
|
using NzbDrone.Core.Profiles.Qualities;
|
||
11 years ago
|
using NzbDrone.Core.Qualities;
|
||
5 years ago
|
using NzbDrone.Core.Test.Framework;
|
||
11 years ago
|
|
||
|
namespace NzbDrone.Core.Test.Datastore
|
||
|
{
|
||
|
[TestFixture]
|
||
4 years ago
|
public class LazyLoadingFixture : DbTest
|
||
11 years ago
|
{
|
||
|
[SetUp]
|
||
|
public void Setup()
|
||
|
{
|
||
4 years ago
|
SqlBuilderExtensions.LogSql = true;
|
||
|
|
||
6 years ago
|
var profile = new QualityProfile
|
||
7 years ago
|
{
|
||
|
Name = "Test",
|
||
7 years ago
|
Cutoff = Quality.MP3_320.Id,
|
||
7 years ago
|
Items = Qualities.QualityFixture.GetDefaultQualities()
|
||
|
};
|
||
|
|
||
11 years ago
|
profile = Db.Insert(profile);
|
||
11 years ago
|
|
||
6 years ago
|
var metadata = Builder<ArtistMetadata>.CreateNew()
|
||
|
.With(v => v.Id = 0)
|
||
|
.Build();
|
||
|
Db.Insert(metadata);
|
||
|
|
||
7 years ago
|
var artist = Builder<Artist>.CreateListOfSize(1)
|
||
11 years ago
|
.All()
|
||
6 years ago
|
.With(v => v.Id = 0)
|
||
6 years ago
|
.With(v => v.QualityProfileId = profile.Id)
|
||
6 years ago
|
.With(v => v.ArtistMetadataId = metadata.Id)
|
||
11 years ago
|
.BuildListOfNew();
|
||
|
|
||
7 years ago
|
Db.InsertMany(artist);
|
||
11 years ago
|
|
||
7 years ago
|
var albums = Builder<Album>.CreateListOfSize(3)
|
||
|
.All()
|
||
6 years ago
|
.With(v => v.Id = 0)
|
||
|
.With(v => v.ArtistMetadataId = metadata.Id)
|
||
7 years ago
|
.BuildListOfNew();
|
||
|
|
||
|
Db.InsertMany(albums);
|
||
|
|
||
6 years ago
|
var releases = new List<AlbumRelease>();
|
||
|
foreach (var album in albums)
|
||
|
{
|
||
|
releases.Add(
|
||
|
Builder<AlbumRelease>.CreateNew()
|
||
|
.With(v => v.Id = 0)
|
||
|
.With(v => v.AlbumId = album.Id)
|
||
|
.With(v => v.ForeignReleaseId = "test" + album.Id)
|
||
|
.Build());
|
||
|
}
|
||
5 years ago
|
|
||
6 years ago
|
Db.InsertMany(releases);
|
||
|
|
||
7 years ago
|
var trackFiles = Builder<TrackFile>.CreateListOfSize(1)
|
||
11 years ago
|
.All()
|
||
6 years ago
|
.With(v => v.Id = 0)
|
||
6 years ago
|
.With(v => v.AlbumId = albums[0].Id)
|
||
10 years ago
|
.With(v => v.Quality = new QualityModel())
|
||
11 years ago
|
.BuildListOfNew();
|
||
|
|
||
7 years ago
|
Db.InsertMany(trackFiles);
|
||
11 years ago
|
|
||
7 years ago
|
var tracks = Builder<Track>.CreateListOfSize(10)
|
||
11 years ago
|
.All()
|
||
6 years ago
|
.With(v => v.Id = 0)
|
||
7 years ago
|
.With(v => v.TrackFileId = trackFiles[0].Id)
|
||
6 years ago
|
.With(v => v.AlbumReleaseId = releases[0].Id)
|
||
11 years ago
|
.BuildListOfNew();
|
||
|
|
||
7 years ago
|
Db.InsertMany(tracks);
|
||
11 years ago
|
}
|
||
|
|
||
6 years ago
|
[Test]
|
||
|
public void should_lazy_load_artist_for_track()
|
||
|
{
|
||
4 years ago
|
var db = Mocker.Resolve<TrackRepository>();
|
||
6 years ago
|
|
||
4 years ago
|
var tracks = db.All();
|
||
6 years ago
|
|
||
|
Assert.IsNotEmpty(tracks);
|
||
|
foreach (var track in tracks)
|
||
|
{
|
||
|
Assert.IsFalse(track.Artist.IsLoaded);
|
||
|
Assert.IsNotNull(track.Artist.Value);
|
||
|
Assert.IsTrue(track.Artist.IsLoaded);
|
||
|
Assert.IsTrue(track.Artist.Value.Metadata.IsLoaded);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void should_lazy_load_artist_for_trackfile()
|
||
|
{
|
||
|
var db = Mocker.Resolve<IDatabase>();
|
||
4 years ago
|
var tracks = db.Query<TrackFile>(new SqlBuilder()).ToList();
|
||
6 years ago
|
|
||
|
Assert.IsNotEmpty(tracks);
|
||
|
foreach (var track in tracks)
|
||
|
{
|
||
|
Assert.IsFalse(track.Artist.IsLoaded);
|
||
|
Assert.IsNotNull(track.Artist.Value);
|
||
|
Assert.IsTrue(track.Artist.IsLoaded);
|
||
|
Assert.IsTrue(track.Artist.Value.Metadata.IsLoaded);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void should_lazy_load_trackfile_if_not_joined()
|
||
|
{
|
||
|
var db = Mocker.Resolve<IDatabase>();
|
||
4 years ago
|
var tracks = db.Query<Track>(new SqlBuilder()).ToList();
|
||
6 years ago
|
|
||
|
foreach (var track in tracks)
|
||
|
{
|
||
|
Assert.IsFalse(track.TrackFile.IsLoaded);
|
||
|
Assert.IsNotNull(track.TrackFile.Value);
|
||
|
Assert.IsTrue(track.TrackFile.IsLoaded);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void should_explicit_load_everything_if_joined()
|
||
|
{
|
||
|
var db = Mocker.Resolve<IDatabase>();
|
||
4 years ago
|
var files = MediaFileRepository.Query(db,
|
||
|
new SqlBuilder()
|
||
|
.Join<TrackFile, Track>((f, t) => f.Id == t.TrackFileId)
|
||
|
.Join<TrackFile, Album>((t, a) => t.AlbumId == a.Id)
|
||
|
.Join<Album, Artist>((album, artist) => album.ArtistMetadataId == artist.ArtistMetadataId)
|
||
|
.Join<Artist, ArtistMetadata>((a, m) => a.ArtistMetadataId == m.Id));
|
||
6 years ago
|
|
||
|
Assert.IsNotEmpty(files);
|
||
|
foreach (var file in files)
|
||
|
{
|
||
|
Assert.IsTrue(file.Tracks.IsLoaded);
|
||
|
Assert.IsNotEmpty(file.Tracks.Value);
|
||
|
Assert.IsTrue(file.Album.IsLoaded);
|
||
|
Assert.IsTrue(file.Artist.IsLoaded);
|
||
|
Assert.IsTrue(file.Artist.Value.Metadata.IsLoaded);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void should_lazy_load_tracks_if_not_joined_to_trackfile()
|
||
|
{
|
||
|
var db = Mocker.Resolve<IDatabase>();
|
||
4 years ago
|
var files = db.QueryJoined<TrackFile, Album, Artist, ArtistMetadata>(
|
||
|
new SqlBuilder()
|
||
|
.Join<TrackFile, Album>((t, a) => t.AlbumId == a.Id)
|
||
|
.Join<Album, Artist>((album, artist) => album.ArtistMetadataId == artist.ArtistMetadataId)
|
||
|
.Join<Artist, ArtistMetadata>((a, m) => a.ArtistMetadataId == m.Id),
|
||
|
(file, album, artist, metadata) =>
|
||
|
{
|
||
|
file.Album = album;
|
||
|
file.Artist = artist;
|
||
|
file.Artist.Value.Metadata = metadata;
|
||
|
return file;
|
||
|
});
|
||
6 years ago
|
|
||
|
Assert.IsNotEmpty(files);
|
||
|
foreach (var file in files)
|
||
|
{
|
||
|
Assert.IsFalse(file.Tracks.IsLoaded);
|
||
|
Assert.IsNotNull(file.Tracks.Value);
|
||
|
Assert.IsNotEmpty(file.Tracks.Value);
|
||
|
Assert.IsTrue(file.Tracks.IsLoaded);
|
||
|
Assert.IsTrue(file.Album.IsLoaded);
|
||
|
Assert.IsTrue(file.Artist.IsLoaded);
|
||
|
Assert.IsTrue(file.Artist.Value.Metadata.IsLoaded);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void should_lazy_load_tracks_if_not_joined()
|
||
|
{
|
||
|
var db = Mocker.Resolve<IDatabase>();
|
||
4 years ago
|
var release = db.Query<AlbumRelease>(new SqlBuilder().Where<AlbumRelease>(x => x.Id == 1)).SingleOrDefault();
|
||
6 years ago
|
|
||
|
Assert.IsFalse(release.Tracks.IsLoaded);
|
||
|
Assert.IsNotNull(release.Tracks.Value);
|
||
|
Assert.IsNotEmpty(release.Tracks.Value);
|
||
|
Assert.IsTrue(release.Tracks.IsLoaded);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void should_lazy_load_track_if_not_joined()
|
||
|
{
|
||
|
var db = Mocker.Resolve<IDatabase>();
|
||
4 years ago
|
var tracks = db.Query<TrackFile>(new SqlBuilder()).ToList();
|
||
6 years ago
|
|
||
|
foreach (var track in tracks)
|
||
|
{
|
||
|
Assert.IsFalse(track.Tracks.IsLoaded);
|
||
|
Assert.IsNotNull(track.Tracks.Value);
|
||
|
Assert.IsTrue(track.Tracks.IsLoaded);
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
7 years ago
|
}
|