1
Fork 0
mirror of https://github.com/Steffo99/cfig.git synced 2024-11-21 23:44:21 +00:00

Make resolve and resolve_failfast return a dict with all the resolved values

This commit is contained in:
Steffo 2022-09-05 22:54:56 +02:00
parent 78880600b0
commit ff61824ca8
Signed by: steffo
GPG key ID: 6965406171929D01
2 changed files with 46 additions and 11 deletions

View file

@ -34,37 +34,49 @@ class Configuration:
An extended :class:`dict` with methods to perform some actions on the contained proxies. An extended :class:`dict` with methods to perform some actions on the contained proxies.
""" """
def resolve(self) -> None: def resolve(self) -> dict[str, t.Any]:
""" """
Resolve all values of the proxies inside this dictionary. Resolve all values of the proxies inside this dictionary.
:raises .errors.BatchResolutionFailure: If it was not possible to resolve at least one value. :raises .errors.BatchResolutionFailure: If it was not possible to resolve at least one value.
:returns: A :class:`dict` containing all the resolved values.
""" """
errors_dict = {} errors_dict = {}
result_dict = {}
log.debug("Resolving and caching all proxied values...") log.debug("Resolving and caching all proxied values...")
for key, proxy in self.items(): for key, proxy in self.items():
log.debug(f"Resolving: {proxy!r}") log.debug(f"Resolving: {proxy!r}")
try: try:
_ = proxy.__wrapped__ value = proxy.__wrapped__
except Exception as e: except Exception as e:
errors_dict[key] = e errors_dict[key] = e
else:
result_dict[key] = value
if errors_dict: if errors_dict:
raise errors.BatchResolutionFailure(errors=errors_dict) raise errors.BatchResolutionFailure(errors=errors_dict)
def resolve_failfast(self) -> None: return result_dict
def resolve_failfast(self) -> dict[str, t.Any]:
""" """
Resolve all values of the proxies inside this dictionary, failing immediately if an error occurs during a resolution, and raising the error itself. Resolve all values of the proxies inside this dictionary, failing immediately if an error occurs during a resolution, and raising the error itself.
:raises Exception: The error occurred during the resolution. :raises Exception: The error occurred during the resolution.
:returns: A :class:`dict` containing all the resolved values.
""" """
result_dict = {}
log.debug("Resolving and caching all proxied values in failfast mode...") log.debug("Resolving and caching all proxied values in failfast mode...")
for key, proxy in self.items(): for key, proxy in self.items():
log.debug(f"Resolving: {proxy!r}") log.debug(f"Resolving: {proxy!r}")
_ = proxy.__wrapped__ value = proxy.__wrapped__
result_dict[key] = value
return result_dict
def unresolve(self) -> None: def unresolve(self) -> None:
""" """

View file

@ -131,7 +131,7 @@ class TestConfig:
assert not first_number.__resolved__ assert not first_number.__resolved__
assert not second_number.__resolved__ assert not second_number.__resolved__
numbers_config.proxies.resolve() result_dict = numbers_config.proxies.resolve()
assert first_number.__resolved__ assert first_number.__resolved__
assert first_number == 1 assert first_number == 1
@ -140,6 +140,10 @@ class TestConfig:
assert second_number == None assert second_number == None
assert second_number is not None assert second_number is not None
assert result_dict["FIRST_NUMBER"] == 1
assert result_dict["SECOND_NUMBER"] == None
assert result_dict["SECOND_NUMBER"] is None
def test_resolve_ff_required(self, numbers_config, monkeypatch): def test_resolve_ff_required(self, numbers_config, monkeypatch):
monkeypatch.setenv("FIRST_NUMBER", "1") monkeypatch.setenv("FIRST_NUMBER", "1")
monkeypatch.setenv("SECOND_NUMBER", "") monkeypatch.setenv("SECOND_NUMBER", "")
@ -153,7 +157,7 @@ class TestConfig:
assert not first_number.__resolved__ assert not first_number.__resolved__
assert not second_number.__resolved__ assert not second_number.__resolved__
numbers_config.proxies.resolve_failfast() result_dict = numbers_config.proxies.resolve_failfast()
assert first_number.__resolved__ assert first_number.__resolved__
assert first_number == 1 assert first_number == 1
@ -162,6 +166,10 @@ class TestConfig:
assert second_number == None assert second_number == None
assert second_number is not None assert second_number is not None
assert result_dict["FIRST_NUMBER"] == 1
assert result_dict["SECOND_NUMBER"] == None
assert result_dict["SECOND_NUMBER"] is None
def test_resolve_optional(self, numbers_config, monkeypatch): def test_resolve_optional(self, numbers_config, monkeypatch):
monkeypatch.setenv("FIRST_NUMBER", "1") monkeypatch.setenv("FIRST_NUMBER", "1")
monkeypatch.setenv("SECOND_NUMBER", "2") monkeypatch.setenv("SECOND_NUMBER", "2")
@ -175,7 +183,7 @@ class TestConfig:
assert not first_number.__resolved__ assert not first_number.__resolved__
assert not second_number.__resolved__ assert not second_number.__resolved__
numbers_config.proxies.resolve() result_dict = numbers_config.proxies.resolve()
assert first_number.__resolved__ assert first_number.__resolved__
assert first_number == 1 assert first_number == 1
@ -183,6 +191,9 @@ class TestConfig:
assert second_number.__resolved__ assert second_number.__resolved__
assert second_number == 2 assert second_number == 2
assert result_dict["FIRST_NUMBER"] == 1
assert result_dict["SECOND_NUMBER"] == 2
def test_resolve_ff_optional(self, numbers_config, monkeypatch): def test_resolve_ff_optional(self, numbers_config, monkeypatch):
monkeypatch.setenv("FIRST_NUMBER", "1") monkeypatch.setenv("FIRST_NUMBER", "1")
monkeypatch.setenv("SECOND_NUMBER", "2") monkeypatch.setenv("SECOND_NUMBER", "2")
@ -196,7 +207,7 @@ class TestConfig:
assert not first_number.__resolved__ assert not first_number.__resolved__
assert not second_number.__resolved__ assert not second_number.__resolved__
numbers_config.proxies.resolve_failfast() result_dict = numbers_config.proxies.resolve_failfast()
assert first_number.__resolved__ assert first_number.__resolved__
assert first_number == 1 assert first_number == 1
@ -204,6 +215,9 @@ class TestConfig:
assert second_number.__resolved__ assert second_number.__resolved__
assert second_number == 2 assert second_number == 2
assert result_dict["FIRST_NUMBER"] == 1
assert result_dict["SECOND_NUMBER"] == 2
def test_resolve_unresolve(self, numbers_config, monkeypatch): def test_resolve_unresolve(self, numbers_config, monkeypatch):
monkeypatch.setenv("FIRST_NUMBER", "1") monkeypatch.setenv("FIRST_NUMBER", "1")
monkeypatch.setenv("SECOND_NUMBER", "2") monkeypatch.setenv("SECOND_NUMBER", "2")
@ -217,7 +231,7 @@ class TestConfig:
assert not first_number.__resolved__ assert not first_number.__resolved__
assert not second_number.__resolved__ assert not second_number.__resolved__
numbers_config.proxies.resolve() result_dict = numbers_config.proxies.resolve()
assert first_number.__resolved__ assert first_number.__resolved__
assert first_number == 1 assert first_number == 1
@ -225,13 +239,16 @@ class TestConfig:
assert second_number.__resolved__ assert second_number.__resolved__
assert second_number == 2 assert second_number == 2
assert result_dict["FIRST_NUMBER"] == 1
assert result_dict["SECOND_NUMBER"] == 2
monkeypatch.setenv("FIRST_NUMBER", "3") monkeypatch.setenv("FIRST_NUMBER", "3")
monkeypatch.setenv("SECOND_NUMBER", "4") monkeypatch.setenv("SECOND_NUMBER", "4")
assert os.environ.get("FIRST_NUMBER") == "3" assert os.environ.get("FIRST_NUMBER") == "3"
assert os.environ.get("SECOND_NUMBER") == "4" assert os.environ.get("SECOND_NUMBER") == "4"
numbers_config.proxies.resolve() result_dict = numbers_config.proxies.resolve()
assert first_number.__resolved__ assert first_number.__resolved__
assert first_number == 1 assert first_number == 1
@ -239,12 +256,15 @@ class TestConfig:
assert second_number.__resolved__ assert second_number.__resolved__
assert second_number == 2 assert second_number == 2
assert result_dict["FIRST_NUMBER"] == 1
assert result_dict["SECOND_NUMBER"] == 2
numbers_config.proxies.unresolve() numbers_config.proxies.unresolve()
assert not first_number.__resolved__ assert not first_number.__resolved__
assert not second_number.__resolved__ assert not second_number.__resolved__
numbers_config.proxies.resolve() result_dict = numbers_config.proxies.resolve()
assert first_number.__resolved__ assert first_number.__resolved__
assert first_number == 3 assert first_number == 3
@ -252,6 +272,9 @@ class TestConfig:
assert second_number.__resolved__ assert second_number.__resolved__
assert second_number == 4 assert second_number == 4
assert result_dict["FIRST_NUMBER"] == 3
assert result_dict["SECOND_NUMBER"] == 4
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
def click_runner(self): def click_runner(self):
yield click.testing.CliRunner() yield click.testing.CliRunner()