From 571ffbccf8544224bc63c9e51688be6310fb05e3 Mon Sep 17 00:00:00 2001 From: Vitiko Date: Mon, 3 Oct 2022 20:44:31 -0400 Subject: [PATCH] Fix provider pool updates --- libs/subliminal_patch/core.py | 23 +++++++------ tests/subliminal_patch/test_core.py | 53 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/libs/subliminal_patch/core.py b/libs/subliminal_patch/core.py index 88b16db1f..7b8625383 100644 --- a/libs/subliminal_patch/core.py +++ b/libs/subliminal_patch/core.py @@ -193,11 +193,22 @@ class SZProviderPool(ProviderPool): # Check if any new provider has been added updated = providers != self.providers or ban_list != self.ban_list - removed_providers = list(sorted(self.providers - providers)) - new_providers = list(sorted(providers - self.providers)) + removed_providers = set(sorted(self.providers - providers)) + + logger.debug("Discarded providers: %s | New providers: %s", self.discarded_providers, providers) + self.discarded_providers.difference_update(providers) + logger.debug("Updated discarded providers: %s", self.discarded_providers) + + removed_providers.update(self.discarded_providers) + + logger.debug("Removed providers: %s", removed_providers) + + self.providers.difference_update(removed_providers) + self.providers.update(list(providers)) # Terminate and delete removed providers from instance for removed in removed_providers: + logger.debug("Removing provider: %s", removed) try: del self[removed] # If the user has updated the providers but hasn't made any @@ -206,14 +217,6 @@ class SZProviderPool(ProviderPool): except KeyError: pass - if updated: - logger.debug("Removed providers: %s", removed_providers) - logger.debug("New providers: %s", new_providers) - - self.discarded_providers.difference_update(new_providers) - self.providers.difference_update(removed_providers) - self.providers.update(list(providers)) - # self.provider_configs = provider_configs self.provider_configs.update(provider_configs) diff --git a/tests/subliminal_patch/test_core.py b/tests/subliminal_patch/test_core.py index 17451ccdf..d6481ee16 100644 --- a/tests/subliminal_patch/test_core.py +++ b/tests/subliminal_patch/test_core.py @@ -1,5 +1,7 @@ from pathlib import Path +import pytest + from subliminal_patch import core @@ -17,3 +19,54 @@ def test_scan_video_episode(tmpdir): result = core.scan_video(str(video_path)) assert isinstance(result, core.Episode) + + +@pytest.fixture +def pool_instance(): + yield core.SZProviderPool({"argenteam"}, {}) + + +def test_pool_update_w_nothing(pool_instance): + pool_instance.update({}, {}, [], {}) + assert pool_instance.providers == set() + assert pool_instance.discarded_providers == set() + + +def test_pool_update_w_multiple_providers(pool_instance): + assert pool_instance.providers == {"argenteam"} + pool_instance.update({"argenteam", "subdivx", "subf2m"}, {}, [], {}) + assert pool_instance.providers == {"argenteam", "subdivx", "subf2m"} + + +def test_pool_update_discarded_providers(pool_instance): + assert pool_instance.providers == {"argenteam"} + + # Provider was discarded internally + pool_instance.discarded_providers = {"argenteam"} + + assert pool_instance.discarded_providers == {"argenteam"} + + # Provider is set to be used again + pool_instance.update({"subdivx", "argenteam"}, {}, [], {}) + + assert pool_instance.providers == {"argenteam", "subdivx"} + + # Provider should disappear from discarded providers + assert pool_instance.discarded_providers == set() + + +def test_pool_update_discarded_providers_2(pool_instance): + assert pool_instance.providers == {"argenteam"} + + # Provider was discarded internally + pool_instance.discarded_providers = {"argenteam"} + + assert pool_instance.discarded_providers == {"argenteam"} + + # Provider is not set to be used again + pool_instance.update({"subdivx"}, {}, [], {}) + + assert pool_instance.providers == {"subdivx"} + + # Provider should not disappear from discarded providers + assert pool_instance.discarded_providers == {"argenteam"}