Fixed: Adding Bluray 576p to some profiles

pull/7236/head
Mark McDowall 5 months ago committed by Mark McDowall
parent c9aa59340c
commit 9875e550a8

@ -1,41 +0,0 @@
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Datastore.Migration;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Datastore.Migration
{
[TestFixture]
public class add_bluray576p_in_profileFixture : MigrationTest<add_blurary576p_quality_in_profiles>
{
private string GenerateQualityJson(int quality, bool allowed)
{
return $"{{ \"quality\": {quality}, \"allowed\": {allowed.ToString().ToLowerInvariant()} }}";
}
[Test]
public void should_add_bluray576p_to_old_profile()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("QualityProfiles").Row(new
{
Id = 0,
Name = "Bluray",
Cutoff = 7,
Items = $"[{GenerateQualityJson((int)Quality.DVD, true)}, {GenerateQualityJson((int)Quality.Bluray480p, true)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]"
});
});
var profiles = db.Query<Profile122>("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1");
var items = profiles.First().Items;
items.Should().HaveCount(4);
items.Select(v => v.Quality).Should().Equal((int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p, (int)Quality.Bluray720p);
items.Select(v => v.Allowed).Should().Equal(true, true, true, false);
items.Select(v => v.Name).Should().Equal(null, null, null, null);
}
}
}

@ -0,0 +1,141 @@
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Datastore.Migration;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Datastore.Migration
{
[TestFixture]
public class add_blurary576p_quality_in_profiles_with_grouped_blurary480pFixture : MigrationTest<add_blurary576p_quality_in_profiles_with_grouped_blurary480p>
{
private string GenerateQualityJson(int quality, bool allowed)
{
return $"{{ \"quality\": {quality}, \"allowed\": {allowed.ToString().ToLowerInvariant()} }}";
}
private string GenerateQualityGroupJson(int id, string name, int[] qualities, bool allowed)
{
return $"{{ \"id\": {id}, \"name\": \"{name}\", \"items\": [{string.Join(", ", qualities.Select(q => $"{{ \"quality\": {q} }}"))}], \"allowed\": {allowed.ToString().ToLowerInvariant()} }}";
}
[Test]
public void should_add_bluray576p_to_old_profile()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("QualityProfiles").Row(new
{
Id = 0,
Name = "Bluray",
Cutoff = 7,
Items = $"[{GenerateQualityJson((int)Quality.DVD, true)}, {GenerateQualityJson((int)Quality.Bluray480p, true)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]"
});
});
var profiles = db.Query<Profile122>("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1");
var items = profiles.First().Items;
items.Should().HaveCount(4);
items.Select(v => v.Quality).Should().Equal((int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p, (int)Quality.Bluray720p);
items.Select(v => v.Allowed).Should().Equal(true, true, true, false);
items.Select(v => v.Name).Should().Equal(null, null, null, null);
}
[Test]
public void should_not_allow_bluray576p_if_blurary480p_not_allowed()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("QualityProfiles").Row(new
{
Id = 0,
Name = "Bluray",
Cutoff = 7,
Items = $"[{GenerateQualityJson((int)Quality.DVD, true)}, {GenerateQualityJson((int)Quality.Bluray480p, false)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]"
});
});
var profiles = db.Query<Profile122>("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1");
var items = profiles.First().Items;
items.Should().HaveCount(4);
items.Select(v => v.Quality).Should().Equal((int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p, (int)Quality.Bluray720p);
items.Select(v => v.Allowed).Should().Equal(true, false, false, false);
items.Select(v => v.Name).Should().Equal(null, null, null, null);
}
[Test]
public void should_add_bluray576p_to_old_profile_with_grouped_bluray_480p()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("QualityProfiles").Row(new
{
Id = 0,
Name = "Bluray",
Cutoff = 7,
Items = $"[{GenerateQualityGroupJson(1000, "DVD", new[] { (int)Quality.DVD, (int)Quality.Bluray480p }, true)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]"
});
});
var profiles = db.Query<Profile122>("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1");
var items = profiles.First().Items;
items.Should().HaveCount(3);
items.Select(v => v.Quality).Should().Equal(null, (int)Quality.Bluray576p, (int)Quality.Bluray720p);
items.Select(v => v.Id).Should().Equal(1000, 0, 0);
items.Select(v => v.Allowed).Should().Equal(true, true, false);
items.Select(v => v.Name).Should().Equal("DVD", null, null);
}
[Test]
public void should_not_add_bluray576p_to_profile_with_bluray_576p()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("QualityProfiles").Row(new
{
Id = 0,
Name = "Bluray",
Cutoff = 7,
Items = $"[{GenerateQualityJson((int)Quality.DVD, true)}, {GenerateQualityJson((int)Quality.Bluray480p, false)}, {GenerateQualityJson((int)Quality.Bluray576p, false)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]"
});
});
var profiles = db.Query<Profile122>("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1");
var items = profiles.First().Items;
items.Should().HaveCount(4);
items.Select(v => v.Quality).Should().Equal((int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p, (int)Quality.Bluray720p);
items.Select(v => v.Allowed).Should().Equal(true, false, false, false);
items.Select(v => v.Name).Should().Equal(null, null, null, null);
}
[Test]
public void should_not_add_bluray576p_to_profile_with_grouped_bluray_576p()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("QualityProfiles").Row(new
{
Id = 0,
Name = "Bluray",
Cutoff = 7,
Items = $"[{GenerateQualityGroupJson(1000, "DVD", new[] { (int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p }, true)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]"
});
});
var profiles = db.Query<Profile122>("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1");
var items = profiles.First().Items;
items.Should().HaveCount(2);
items.Select(v => v.Quality).Should().Equal(null, (int)Quality.Bluray720p);
items.Select(v => v.Id).Should().Equal(1000, 0);
items.Select(v => v.Allowed).Should().Equal(true, false);
items.Select(v => v.Name).Should().Equal("DVD", null);
items.First().Items.Select(v => v.Quality).Should().Equal((int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p);
}
}
}

@ -0,0 +1,14 @@
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(214)]
public class add_blurary576p_quality_in_profiles : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
// Replaced with 215
}
}
}

@ -9,8 +9,8 @@ using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration namespace NzbDrone.Core.Datastore.Migration
{ {
[Migration(214)] [Migration(215)]
public class add_blurary576p_quality_in_profiles : NzbDroneMigrationBase public class add_blurary576p_quality_in_profiles_with_grouped_blurary480p : NzbDroneMigrationBase
{ {
protected override void MainDbUpgrade() protected override void MainDbUpgrade()
{ {
@ -19,46 +19,46 @@ namespace NzbDrone.Core.Datastore.Migration
private void ConvertProfile(IDbConnection conn, IDbTransaction tran) private void ConvertProfile(IDbConnection conn, IDbTransaction tran)
{ {
var updater = new ProfileUpdater214(conn, tran); var updater = new ProfileUpdater215(conn, tran);
updater.InsertQualityAfter(13, 22); // Group Bluray576p with Bluray480p updater.InsertQualityAfter(13, 22); // Group Bluray576p with Bluray480p
updater.Commit(); updater.Commit();
} }
} }
public class Profile214 public class Profile215
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public int Cutoff { get; set; } public int Cutoff { get; set; }
public List<ProfileItem214> Items { get; set; } public List<ProfileItem215> Items { get; set; }
} }
public class ProfileItem214 public class ProfileItem215
{ {
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public int? Quality { get; set; } public int? Quality { get; set; }
public List<ProfileItem214> Items { get; set; } public List<ProfileItem215> Items { get; set; }
public bool Allowed { get; set; } public bool Allowed { get; set; }
public ProfileItem214() public ProfileItem215()
{ {
Items = new List<ProfileItem214>(); Items = new List<ProfileItem215>();
} }
} }
public class ProfileUpdater214 public class ProfileUpdater215
{ {
private readonly IDbConnection _connection; private readonly IDbConnection _connection;
private readonly IDbTransaction _transaction; private readonly IDbTransaction _transaction;
private List<Profile214> _profiles; private List<Profile215> _profiles;
private HashSet<Profile214> _changedProfiles = new HashSet<Profile214>(); private HashSet<Profile215> _changedProfiles = new HashSet<Profile215>();
public ProfileUpdater214(IDbConnection conn, IDbTransaction tran) public ProfileUpdater215(IDbConnection conn, IDbTransaction tran)
{ {
_connection = conn; _connection = conn;
_transaction = tran; _transaction = tran;
@ -86,11 +86,17 @@ namespace NzbDrone.Core.Datastore.Migration
{ {
foreach (var profile in _profiles) foreach (var profile in _profiles)
{ {
var findIndex = profile.Items.FindIndex(v => v.Quality == find); // Don't update if Bluray 576p was already added to the profile in 214
if (profile.Items.FindIndex(v => v.Quality == quality || v.Items.Any(i => i.Quality == quality)) > -1)
{
continue;
}
var findIndex = profile.Items.FindIndex(v => v.Quality == find || v.Items.Any(i => i.Quality == find));
if (findIndex > -1) if (findIndex > -1)
{ {
profile.Items.Insert(findIndex + 1, new ProfileItem214 profile.Items.Insert(findIndex + 1, new ProfileItem215
{ {
Quality = quality, Quality = quality,
Allowed = profile.Items[findIndex].Allowed Allowed = profile.Items[findIndex].Allowed
@ -101,9 +107,9 @@ namespace NzbDrone.Core.Datastore.Migration
} }
} }
private List<Profile214> GetProfiles() private List<Profile215> GetProfiles()
{ {
var profiles = new List<Profile214>(); var profiles = new List<Profile215>();
using (var getProfilesCmd = _connection.CreateCommand()) using (var getProfilesCmd = _connection.CreateCommand())
{ {
@ -114,12 +120,12 @@ namespace NzbDrone.Core.Datastore.Migration
{ {
while (profileReader.Read()) while (profileReader.Read())
{ {
profiles.Add(new Profile214 profiles.Add(new Profile215
{ {
Id = profileReader.GetInt32(0), Id = profileReader.GetInt32(0),
Name = profileReader.GetString(1), Name = profileReader.GetString(1),
Cutoff = profileReader.GetInt32(2), Cutoff = profileReader.GetInt32(2),
Items = Json.Deserialize<List<ProfileItem214>>(profileReader.GetString(3)) Items = Json.Deserialize<List<ProfileItem215>>(profileReader.GetString(3))
}); });
} }
} }
Loading…
Cancel
Save