From d2830a5cc5cc3ea89419dfd953291af9855340fe Mon Sep 17 00:00:00 2001 From: Salomon Popp Date: Wed, 11 Oct 2023 10:13:52 +0200 Subject: [PATCH] Refactor --- kpops/utils/environment.py | 18 +++++++++++------- tests/utils/test_environment.py | 11 +++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/kpops/utils/environment.py b/kpops/utils/environment.py index d3af199fc..006fd30d6 100644 --- a/kpops/utils/environment.py +++ b/kpops/utils/environment.py @@ -1,6 +1,6 @@ import os from collections import UserDict -from collections.abc import MutableMapping +from collections.abc import ItemsView, KeysView, MutableMapping, ValuesView class Environment(UserDict[str, str]): @@ -25,14 +25,18 @@ def __getitem__(self, key: str) -> str: def __contains__(self, key: object) -> bool: return super().__contains__(key) or self._global.__contains__(key) - def keys(self) -> set[str]: - return set(super().keys()).union(self._global.keys()) + @property + def dict(self) -> dict[str, str]: + return {**self._global, **self.data} - def values(self) -> set[str]: - return set(super().values()).union(self._global.values()) + def keys(self) -> KeysView[str]: + return KeysView(self.dict) - def items(self) -> dict[str, str]: - return {**self._global, **self.data} + def values(self) -> ValuesView[str]: + return ValuesView(self.dict) + + def items(self) -> ItemsView[str, str]: + return ItemsView(self.dict) ENV = Environment() diff --git a/tests/utils/test_environment.py b/tests/utils/test_environment.py index ae1b9d897..381431e8c 100644 --- a/tests/utils/test_environment.py +++ b/tests/utils/test_environment.py @@ -1,4 +1,5 @@ import os +from unittest.mock import ANY import pytest @@ -7,6 +8,8 @@ @pytest.fixture(autouse=True) def environment(monkeypatch: pytest.MonkeyPatch) -> Environment: + for key in os.environ: + monkeypatch.delenv(key) monkeypatch.setenv("MY", "fake") monkeypatch.setenv("ENVIRONMENT", "here") return Environment() @@ -54,3 +57,11 @@ def test_kwargs(): assert environment["ENVIRONMENT"] == "here" assert environment["kwarg1"] == "value1" assert environment["kwarg2"] == "value2" + + +def test_dict(environment: Environment): + assert environment.dict == { + "MY": "fake", + "ENVIRONMENT": "here", + "PYTEST_CURRENT_TEST": ANY, + }