From 00ed14f55ce6f90e904922d58efca933f11ff4aa Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sat, 15 May 2021 16:33:57 -0500 Subject: [PATCH] fix(radarr): do not invoke api for profile when there's no updates --- CHANGELOG.md | 1 + .../QualityProfileApiPersistenceStepTest.cs | 46 +++++++++++++++++++ .../QualityProfileApiPersistenceStep.cs | 6 +++ 3 files changed, 53 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d42a789c..ee34ec81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The log message listing custom formats without scores in the guide now prints information one per line (improved readability) - Duplicate custom formats in the guide now issue a warning and get skipped. +- Do not invoke the Radarr API to update a quality profile if there are no updated scores inside it. ## [1.4.0] - 2021-05-14 diff --git a/src/Trash.Tests/Radarr/CustomFormat/Processors/PersistenceSteps/QualityProfileApiPersistenceStepTest.cs b/src/Trash.Tests/Radarr/CustomFormat/Processors/PersistenceSteps/QualityProfileApiPersistenceStepTest.cs index ca71a6ae..411d7652 100644 --- a/src/Trash.Tests/Radarr/CustomFormat/Processors/PersistenceSteps/QualityProfileApiPersistenceStepTest.cs +++ b/src/Trash.Tests/Radarr/CustomFormat/Processors/PersistenceSteps/QualityProfileApiPersistenceStepTest.cs @@ -18,6 +18,52 @@ namespace Trash.Tests.Radarr.CustomFormat.Processors.PersistenceSteps [Parallelizable(ParallelScope.All)] public class QualityProfileApiPersistenceStepTest { + [Test] + public void Do_not_invoke_api_if_no_scores_to_update() + { + const string radarrQualityProfileData = @"[{ + 'name': 'profile1', + 'formatItems': [{ + 'format': 1, + 'name': 'cf1', + 'score': 1 + }, + { + 'format': 2, + 'name': 'cf2', + 'score': 2 + }, + { + 'format': 3, + 'name': 'cf3', + 'score': 3 + } + ], + 'id': 1 +}]"; + + var api = Substitute.For(); + api.GetQualityProfiles().Returns(JsonConvert.DeserializeObject>(radarrQualityProfileData)); + + var cfScores = new Dictionary> + { + { + "profile1", new List + { + new(new ProcessedCustomFormatData("", "", new JObject()) + { + CacheEntry = new TrashIdMapping("", "") {CustomFormatId = 4} + }, 100) + } + } + }; + + var processor = new QualityProfileApiPersistenceStep(); + processor.Process(api, cfScores); + + api.DidNotReceive().UpdateQualityProfile(Arg.Any(), Arg.Any()); + } + [Test] public void Invalid_quality_profile_names_are_reported() { diff --git a/src/Trash/Radarr/CustomFormat/Processors/PersistenceSteps/QualityProfileApiPersistenceStep.cs b/src/Trash/Radarr/CustomFormat/Processors/PersistenceSteps/QualityProfileApiPersistenceStep.cs index 8ddf9bb9..e79228ff 100644 --- a/src/Trash/Radarr/CustomFormat/Processors/PersistenceSteps/QualityProfileApiPersistenceStep.cs +++ b/src/Trash/Radarr/CustomFormat/Processors/PersistenceSteps/QualityProfileApiPersistenceStep.cs @@ -54,6 +54,12 @@ namespace Trash.Radarr.CustomFormat.Processors.PersistenceSteps _updatedScores.GetOrCreate(profileName).Add(score); } + if (!_updatedScores.TryGetValue(profileName, out var updatedScores) || updatedScores.Count == 0) + { + // No scores to update, so don't bother with the API call + continue; + } + var jsonRoot = (JObject) jsonList.First().Root; await api.UpdateQualityProfile(jsonRoot, (int) jsonRoot["id"]); }