2020-01-03 12:49:24 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using FluentAssertions;
|
2019-07-14 20:05:55 +00:00
|
|
|
using NUnit.Framework;
|
|
|
|
using NzbDrone.Core.Datastore.Migration;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.Test.Framework;
|
2019-07-14 20:05:55 +00:00
|
|
|
using NzbDrone.Test.Common;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Datastore.Migration
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class add_artistmetadataid_constraintFixture : MigrationTest<add_artistmetadataid_constraint>
|
|
|
|
{
|
|
|
|
private string _artistPath = null;
|
|
|
|
|
|
|
|
private void GivenArtistMetadata(add_artistmetadataid_constraint c, int id, string name)
|
|
|
|
{
|
|
|
|
c.Insert.IntoTable("ArtistMetadata").Row(new
|
2020-01-03 12:49:24 +00:00
|
|
|
{
|
|
|
|
Id = id,
|
|
|
|
ForeignArtistId = id,
|
|
|
|
Name = name,
|
|
|
|
Status = 1,
|
|
|
|
Images = "images"
|
|
|
|
});
|
2019-07-14 20:05:55 +00:00
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-07-14 20:05:55 +00:00
|
|
|
private void GivenArtist(add_artistmetadataid_constraint c, int id, int artistMetadataId, string name)
|
|
|
|
{
|
|
|
|
_artistPath = $"/mnt/data/path/{name}".AsOsAgnostic();
|
|
|
|
c.Insert.IntoTable("Artists").Row(new
|
2020-01-03 12:49:24 +00:00
|
|
|
{
|
|
|
|
Id = id,
|
|
|
|
ArtistMetadataId = artistMetadataId,
|
|
|
|
CleanName = name,
|
|
|
|
Path = _artistPath,
|
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-07-14 20:05:55 +00:00
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-07-14 20:05:55 +00:00
|
|
|
private void VerifyArtists(IDirectDataMapper db, List<int> ids)
|
|
|
|
{
|
2022-01-26 00:08:27 +00:00
|
|
|
var artists = db.Query("SELECT \"Artists\".* from \"Artists\"");
|
2019-07-14 20:05:55 +00:00
|
|
|
|
2019-09-16 17:01:28 +00:00
|
|
|
artists.Select(x => x["Id"]).Should().BeEquivalentTo(ids);
|
2019-07-14 20:05:55 +00:00
|
|
|
|
|
|
|
var duplicates = artists.GroupBy(x => x["ArtistMetadataId"])
|
|
|
|
.Where(x => x.Count() > 1);
|
|
|
|
|
|
|
|
duplicates.Should().BeEmpty();
|
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-07-14 20:05:55 +00:00
|
|
|
[Test]
|
|
|
|
public void migration_031_should_not_remove_unique_artist()
|
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
var db = WithMigrationTestDb(c =>
|
|
|
|
{
|
|
|
|
GivenArtistMetadata(c, 1, "test");
|
|
|
|
GivenArtist(c, 1, 1, "test");
|
|
|
|
});
|
2019-07-14 20:05:55 +00:00
|
|
|
|
|
|
|
VerifyArtists(db, new List<int> { 1 });
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void migration_031_should_not_remove_either_unique_artist()
|
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
var db = WithMigrationTestDb(c =>
|
|
|
|
{
|
|
|
|
GivenArtistMetadata(c, 1, "test");
|
|
|
|
GivenArtist(c, 1, 1, "test");
|
2019-07-14 20:05:55 +00:00
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
GivenArtistMetadata(c, 2, "test2");
|
|
|
|
GivenArtist(c, 2, 2, "test2");
|
|
|
|
});
|
2019-07-14 20:05:55 +00:00
|
|
|
|
|
|
|
VerifyArtists(db, new List<int> { 1, 2 });
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void migration_031_should_remove_duplicate_artist()
|
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
var db = WithMigrationTestDb(c =>
|
|
|
|
{
|
|
|
|
GivenArtistMetadata(c, 1, "test");
|
|
|
|
GivenArtist(c, 1, 1, "test");
|
2019-07-14 20:05:55 +00:00
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
GivenArtist(c, 2, 1, "test2");
|
|
|
|
});
|
2019-07-14 20:05:55 +00:00
|
|
|
|
|
|
|
VerifyArtists(db, new List<int> { 1 });
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void migration_031_should_remove_all_duplicate_artists()
|
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
var db = WithMigrationTestDb(c =>
|
|
|
|
{
|
|
|
|
GivenArtistMetadata(c, 1, "test");
|
|
|
|
GivenArtist(c, 1, 1, "test");
|
|
|
|
GivenArtist(c, 2, 1, "test");
|
|
|
|
GivenArtist(c, 3, 1, "test");
|
|
|
|
GivenArtist(c, 4, 1, "test");
|
|
|
|
|
|
|
|
GivenArtistMetadata(c, 2, "test2");
|
|
|
|
GivenArtist(c, 5, 2, "test2");
|
|
|
|
GivenArtist(c, 6, 2, "test2");
|
|
|
|
});
|
2019-07-14 20:05:55 +00:00
|
|
|
|
|
|
|
VerifyArtists(db, new List<int> { 1, 5 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|