2020-01-03 12:49:24 +00:00
|
|
|
using System.Collections.Generic;
|
2019-01-12 16:56:13 +00:00
|
|
|
using System.Linq;
|
|
|
|
using FizzWare.NBuilder;
|
2020-01-03 12:49:24 +00:00
|
|
|
using FluentAssertions;
|
2019-01-12 16:56:13 +00:00
|
|
|
using NUnit.Framework;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Common.Serializer;
|
2019-01-12 16:56:13 +00:00
|
|
|
using NzbDrone.Core.Datastore.Migration;
|
|
|
|
using NzbDrone.Core.Music;
|
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Datastore.Migration
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class add_release_groups_etcFixture : MigrationTest<add_release_groups_etc>
|
|
|
|
{
|
|
|
|
private void GivenArtist(add_release_groups_etc c, int id, string name)
|
|
|
|
{
|
|
|
|
c.Insert.IntoTable("Artists").Row(new
|
2020-01-03 12:49:24 +00:00
|
|
|
{
|
|
|
|
Id = id,
|
|
|
|
ForeignArtistId = id.ToString(),
|
|
|
|
Name = name,
|
|
|
|
CleanName = name,
|
|
|
|
Status = 1,
|
|
|
|
Images = "",
|
|
|
|
Path = $"/mnt/data/path/{name}",
|
2022-01-26 00:08:27 +00:00
|
|
|
Monitored = true,
|
|
|
|
AlbumFolder = true,
|
2020-01-03 12:49:24 +00:00
|
|
|
LanguageProfileId = 1,
|
|
|
|
MetadataProfileId = 1
|
|
|
|
});
|
2019-01-12 16:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void GivenAlbum(add_release_groups_etc c, int id, int artistId, string title, string currentRelease)
|
|
|
|
{
|
|
|
|
c.Insert.IntoTable("Albums").Row(new
|
2020-01-03 12:49:24 +00:00
|
|
|
{
|
|
|
|
Id = id,
|
|
|
|
ForeignAlbumId = id.ToString(),
|
|
|
|
ArtistId = artistId,
|
|
|
|
Title = title,
|
|
|
|
CleanTitle = title,
|
|
|
|
Images = "",
|
2022-01-26 00:08:27 +00:00
|
|
|
Monitored = true,
|
2020-01-03 12:49:24 +00:00
|
|
|
AlbumType = "Studio",
|
|
|
|
Duration = 100,
|
|
|
|
Media = "",
|
|
|
|
Releases = "",
|
|
|
|
CurrentRelease = currentRelease
|
|
|
|
});
|
2019-01-12 16:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void GivenTracks(add_release_groups_etc c, int artistid, int albumid, int firstId, int count)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
var id = firstId + i;
|
|
|
|
c.Insert.IntoTable("Tracks").Row(new
|
2020-01-03 12:49:24 +00:00
|
|
|
{
|
|
|
|
Id = id,
|
|
|
|
ForeignTrackId = id.ToString(),
|
|
|
|
ArtistId = artistid,
|
|
|
|
AlbumId = albumid,
|
2022-01-26 00:08:27 +00:00
|
|
|
Explicit = false,
|
|
|
|
Compilation = false,
|
|
|
|
Monitored = false,
|
2020-01-03 12:49:24 +00:00
|
|
|
Duration = 100,
|
|
|
|
MediumNumber = 1,
|
|
|
|
AbsoluteTrackNumber = i,
|
|
|
|
TrackNumber = i.ToString()
|
|
|
|
});
|
2019-01-12 16:56:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<AlbumRelease> VerifyAlbumReleases(IDirectDataMapper db)
|
|
|
|
{
|
2022-01-26 00:08:27 +00:00
|
|
|
var releases = db.Query<AlbumRelease>("SELECT * FROM \"AlbumReleases\"");
|
|
|
|
var albums = db.Query<Album>("SELECT * FROM \"Albums\"");
|
2019-01-12 16:56:13 +00:00
|
|
|
|
|
|
|
// we only put in one release per album
|
2023-04-12 02:31:27 +00:00
|
|
|
releases.Count.Should().Be(albums.Count);
|
2019-01-12 16:56:13 +00:00
|
|
|
|
|
|
|
// each album should be linked to exactly one release
|
|
|
|
releases.Select(x => x.AlbumId).SequenceEqual(albums.Select(x => x.Id)).Should().Be(true);
|
|
|
|
|
|
|
|
// each release should have at least one medium
|
|
|
|
releases.Select(x => x.Media.Count).Min().Should().BeGreaterOrEqualTo(1);
|
|
|
|
|
|
|
|
return releases;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void VerifyTracks(IDirectDataMapper db, int albumId, int albumReleaseId, int expectedCount)
|
|
|
|
{
|
2022-01-26 00:08:27 +00:00
|
|
|
var tracks = db.Query<Track>("SELECT \"Tracks\".* FROM \"Tracks\" " +
|
|
|
|
"JOIN \"AlbumReleases\" ON \"Tracks\".\"AlbumReleaseId\" = \"AlbumReleases\".\"Id\" " +
|
|
|
|
"JOIN \"Albums\" ON \"AlbumReleases\".\"AlbumId\" = \"Albums\".\"Id\" " +
|
|
|
|
"WHERE \"Albums\".\"Id\" = " + albumId).ToList();
|
2019-04-15 16:31:19 +00:00
|
|
|
|
2022-01-26 00:08:27 +00:00
|
|
|
var album = db.Query<Album>("SELECT * FROM \"Albums\" WHERE \"Albums\".\"Id\" = " + albumId).ToList().Single();
|
2019-04-15 16:31:19 +00:00
|
|
|
|
2019-01-12 16:56:13 +00:00
|
|
|
tracks.Count.Should().Be(expectedCount);
|
|
|
|
tracks.First().AlbumReleaseId.Should().Be(albumReleaseId);
|
2019-04-15 16:31:19 +00:00
|
|
|
tracks.All(t => t.ArtistMetadataId == album.ArtistMetadataId).Should().BeTrue();
|
2019-01-12 16:56:13 +00:00
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-01-12 16:56:13 +00:00
|
|
|
[Test]
|
|
|
|
public void migration_023_simple_case()
|
|
|
|
{
|
|
|
|
var release = Builder<add_release_groups_etc.LegacyAlbumRelease>
|
|
|
|
.CreateNew()
|
|
|
|
.Build();
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-01-12 16:56:13 +00:00
|
|
|
var db = WithMigrationTestDb(c =>
|
|
|
|
{
|
|
|
|
GivenArtist(c, 1, "TestArtist");
|
|
|
|
GivenAlbum(c, 1, 1, "TestAlbum", release.ToJson());
|
|
|
|
GivenTracks(c, 1, 1, 1, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
VerifyAlbumReleases(db);
|
|
|
|
VerifyTracks(db, 1, 1, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void migration_023_multiple_media()
|
|
|
|
{
|
|
|
|
var release = Builder<add_release_groups_etc.LegacyAlbumRelease>
|
|
|
|
.CreateNew()
|
|
|
|
.With(e => e.MediaCount = 2)
|
|
|
|
.Build();
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-01-12 16:56:13 +00:00
|
|
|
var db = WithMigrationTestDb(c =>
|
|
|
|
{
|
|
|
|
GivenArtist(c, 1, "TestArtist");
|
|
|
|
GivenAlbum(c, 1, 1, "TestAlbum", release.ToJson());
|
|
|
|
GivenTracks(c, 1, 1, 1, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
var migrated = VerifyAlbumReleases(db);
|
|
|
|
migrated.First().Media.Count.Should().Be(2);
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-01-12 16:56:13 +00:00
|
|
|
VerifyTracks(db, 1, 1, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void migration_023_null_title()
|
|
|
|
{
|
|
|
|
var release = Builder<add_release_groups_etc.LegacyAlbumRelease>
|
|
|
|
.CreateNew()
|
|
|
|
.With(e => e.Title = null)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
var db = WithMigrationTestDb(c =>
|
|
|
|
{
|
|
|
|
GivenArtist(c, 1, "TestArtist");
|
|
|
|
GivenAlbum(c, 1, 1, "TestAlbum", release.ToJson());
|
|
|
|
GivenTracks(c, 1, 1, 1, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
VerifyAlbumReleases(db);
|
|
|
|
VerifyTracks(db, 1, 1, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void migration_023_all_default_entries()
|
|
|
|
{
|
|
|
|
var release = new add_release_groups_etc.LegacyAlbumRelease();
|
|
|
|
|
|
|
|
var db = WithMigrationTestDb(c =>
|
|
|
|
{
|
|
|
|
GivenArtist(c, 1, "TestArtist");
|
|
|
|
GivenAlbum(c, 1, 1, "TestAlbum", release.ToJson());
|
|
|
|
GivenTracks(c, 1, 1, 1, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
VerifyAlbumReleases(db);
|
|
|
|
VerifyTracks(db, 1, 1, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void migration_023_empty_albumrelease()
|
|
|
|
{
|
|
|
|
var db = WithMigrationTestDb(c =>
|
|
|
|
{
|
|
|
|
GivenArtist(c, 1, "TestArtist");
|
|
|
|
GivenAlbum(c, 1, 1, "TestAlbum", "");
|
|
|
|
GivenTracks(c, 1, 1, 1, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
VerifyAlbumReleases(db);
|
|
|
|
VerifyTracks(db, 1, 1, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void migration_023_duplicate_albumrelease()
|
|
|
|
{
|
|
|
|
var release = Builder<add_release_groups_etc.LegacyAlbumRelease>
|
|
|
|
.CreateNew()
|
|
|
|
.Build();
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-01-12 16:56:13 +00:00
|
|
|
var db = WithMigrationTestDb(c =>
|
|
|
|
{
|
|
|
|
GivenArtist(c, 1, "TestArtist");
|
|
|
|
GivenAlbum(c, 1, 1, "TestAlbum1", release.ToJson());
|
|
|
|
GivenTracks(c, 1, 1, 1, 10);
|
|
|
|
GivenAlbum(c, 2, 1, "TestAlbum2", release.ToJson());
|
|
|
|
GivenTracks(c, 1, 2, 100, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
VerifyAlbumReleases(db);
|
|
|
|
VerifyTracks(db, 1, 1, 10);
|
|
|
|
VerifyTracks(db, 2, 2, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void migration_023_duplicate_foreignreleaseid()
|
|
|
|
{
|
|
|
|
var releases = Builder<add_release_groups_etc.LegacyAlbumRelease>
|
|
|
|
.CreateListOfSize(2)
|
|
|
|
.All()
|
|
|
|
.With(e => e.Id = "TestForeignId")
|
|
|
|
.Build();
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-01-12 16:56:13 +00:00
|
|
|
var db = WithMigrationTestDb(c =>
|
|
|
|
{
|
|
|
|
GivenArtist(c, 1, "TestArtist");
|
|
|
|
GivenAlbum(c, 1, 1, "TestAlbum1", releases[0].ToJson());
|
|
|
|
GivenTracks(c, 1, 1, 1, 10);
|
|
|
|
GivenAlbum(c, 2, 1, "TestAlbum2", releases[1].ToJson());
|
|
|
|
GivenTracks(c, 1, 2, 100, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
VerifyAlbumReleases(db);
|
|
|
|
VerifyTracks(db, 1, 1, 10);
|
|
|
|
VerifyTracks(db, 2, 2, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|