diff --git a/CHANGELOG.md b/CHANGELOG.md index ee34ec81..654205b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Invalid cache data files no longer cause the program to exit. An error is printed and the application continues as if there was no cache at all. +- Fix a bug that resulted in certain custom formats not having their scores set in quality + profiles. ### Changed diff --git a/src/Trash.Tests/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStepTest.cs b/src/Trash.Tests/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStepTest.cs index e8d7be9c..32904d48 100644 --- a/src/Trash.Tests/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStepTest.cs +++ b/src/Trash.Tests/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStepTest.cs @@ -100,7 +100,7 @@ namespace Trash.Tests.Radarr.CustomFormat.Processors.PersistenceSteps } [Test] - public void Combination_of_create_update_and_no_change_and_verify_proper_json_merging() + public void Combination_of_create_update_and_unchanged_and_verify_proper_json_merging() { const string radarrCfData = @"[{ 'id': 1, @@ -178,7 +178,7 @@ namespace Trash.Tests.Radarr.CustomFormat.Processors.PersistenceSteps new("created", "", guideCfData[0]), new("updated_different_name", "", guideCfData[1]) { - CacheEntry = new TrashIdMapping("", "") {CustomFormatId = 2} + CacheEntry = new TrashIdMapping("", "", 2) }, new("no_change", "", guideCfData[2]) }; @@ -362,5 +362,66 @@ namespace Trash.Tests.Radarr.CustomFormat.Processors.PersistenceSteps expectedTransactions.DeletedCustomFormatIds.Add(new TrashIdMapping("testtrashid", "testname", 2)); processor.Transactions.Should().BeEquivalentTo(expectedTransactions); } + + [Test] + public void Updated_and_unchanged_custom_formats_have_cache_entry_set_when_there_is_no_cache() + { + const string radarrCfData = @"[{ + 'id': 1, + 'name': 'updated', + 'specifications': [{ + 'name': 'spec2', + 'fields': [{ + 'name': 'value', + 'value': 'value1' + }] + }] +}, { + 'id': 2, + 'name': 'no_change', + 'specifications': [{ + 'name': 'spec4', + 'negate': false, + 'fields': [{ + 'name': 'value', + 'value': 'value1' + }] + }] +}]"; + var guideCfData = JsonConvert.DeserializeObject>(@"[{ + 'name': 'updated', + 'specifications': [{ + 'name': 'spec2', + 'fields': { + 'value': 'value2' + } + }] +}, { + 'name': 'no_change', + 'specifications': [{ + 'name': 'spec4', + 'negate': false, + 'fields': { + 'value': 'value1' + } + }] +}]"); + + var radarrCfs = JsonConvert.DeserializeObject>(radarrCfData); + var guideCfs = new List + { + new("updated", "", guideCfData[0]), + new("no_change", "", guideCfData[1]) + }; + + var processor = new JsonTransactionStep(); + processor.Process(guideCfs, radarrCfs); + + processor.Transactions.UpdatedCustomFormats.First().CacheEntry.Should() + .BeEquivalentTo(new TrashIdMapping("", "updated", 1)); + + processor.Transactions.UnchangedCustomFormats.First().CacheEntry.Should() + .BeEquivalentTo(new TrashIdMapping("", "no_change", 2)); + } } } diff --git a/src/Trash/Radarr/CustomFormat/Api/CustomFormatService.cs b/src/Trash/Radarr/CustomFormat/Api/CustomFormatService.cs index 9d0cd256..9cebd8b6 100644 --- a/src/Trash/Radarr/CustomFormat/Api/CustomFormatService.cs +++ b/src/Trash/Radarr/CustomFormat/Api/CustomFormatService.cs @@ -36,13 +36,6 @@ namespace Trash.Radarr.CustomFormat.Api public async Task UpdateCustomFormat(ProcessedCustomFormatData cf) { - // Set the cache first, since it's needed to perform the update. This case will apply to CFs we update that - // exist in Radarr but not the cache (e.g. moving to a new machine, same-named CF was created manually) - if (cf.CacheEntry == null) - { - cf.SetCache((int) cf.Json["id"]); - } - await BaseUrl() .AppendPathSegment($"customformat/{cf.GetCustomFormatId()}") .PutJsonAsync(cf.Json) diff --git a/src/Trash/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStep.cs b/src/Trash/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStep.cs index 627b1221..e4ab8e43 100644 --- a/src/Trash/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStep.cs +++ b/src/Trash/Radarr/CustomFormat/Processors/PersistenceSteps/JsonTransactionStep.cs @@ -39,6 +39,14 @@ namespace Trash.Radarr.CustomFormat.Processors.PersistenceSteps guideCf.Json = (JObject) radarrCf.DeepClone(); UpdateRadarrCf(guideCf.Json, guideCfJson); + // Set the cache for use later (like updating scores) if it hasn't been updated already. + // This handles CFs that already exist in Radarr but aren't cached (they will be added to cache + // later). + if (guideCf.CacheEntry == null) + { + guideCf.SetCache((int) guideCf.Json["id"]); + } + if (!JToken.DeepEquals(radarrCf, guideCf.Json)) { Transactions.UpdatedCustomFormats.Add(guideCf);