diff --git a/poetry.lock b/poetry.lock index f12ba8fb8a..f9aadfbb99 100644 --- a/poetry.lock +++ b/poetry.lock @@ -3209,13 +3209,13 @@ files = [ [[package]] name = "pytest" -version = "7.4.4" +version = "8.0.0" description = "pytest: simple powerful testing with Python" optional = false -python-versions = ">=3.7" +python-versions = ">=3.8" files = [ - {file = "pytest-7.4.4-py3-none-any.whl", hash = "sha256:b090cdf5ed60bf4c45261be03239c2c1c22df034fbffe691abe93cd80cea01d8"}, - {file = "pytest-7.4.4.tar.gz", hash = "sha256:2cf0005922c6ace4a3e2ec8b4080eb0d9753fdc93107415332f50ce9e7994280"}, + {file = "pytest-8.0.0-py3-none-any.whl", hash = "sha256:50fb9cbe836c3f20f0dfa99c565201fb75dc54c8d76373cd1bde06b06657bdb6"}, + {file = "pytest-8.0.0.tar.gz", hash = "sha256:249b1b0864530ba251b7438274c4d251c58d868edaaec8762893ad4a0d71c36c"}, ] [package.dependencies] @@ -3223,7 +3223,7 @@ colorama = {version = "*", markers = "sys_platform == \"win32\""} exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""} iniconfig = "*" packaging = "*" -pluggy = ">=0.12,<2.0" +pluggy = ">=1.3.0,<2.0" tomli = {version = ">=1.0.0", markers = "python_version < \"3.11\""} [package.extras] diff --git a/tests/api/admin/test_config.py b/tests/api/admin/test_config.py index 3338c62133..2517710a13 100644 --- a/tests/api/admin/test_config.py +++ b/tests/api/admin/test_config.py @@ -3,24 +3,33 @@ from unittest.mock import MagicMock, patch import pytest +from _pytest.monkeypatch import MonkeyPatch from requests import RequestException from api.admin.config import Configuration as AdminConfig from api.admin.config import OperationalMode -class TestAdminUI: - @staticmethod - def _set_env(monkeypatch, key: str, value: str | None): +class MonkeyPatchEnvFixture: + def __init__(self, monkeypatch: MonkeyPatch): + self.monkeypatch = monkeypatch + + def __call__(self, key: str, value: str | None) -> None: if value: - monkeypatch.setenv(key, value) + self.monkeypatch.setenv(key, value) elif key in os.environ: - monkeypatch.delenv(key) + self.monkeypatch.delenv(key) + + +@pytest.fixture +def monkeypatch_env(monkeypatch: MonkeyPatch) -> MonkeyPatchEnvFixture: + return MonkeyPatchEnvFixture(monkeypatch) + - def test_package_version_cached(self, monkeypatch): - with patch( - "api.admin.config.Configuration.env_package_version" - ) as env_package_version: +class TestAdminUI: + def test_package_version_cached(self): + with patch.object(AdminConfig, "env_package_version") as env_package_version: + AdminConfig._version = None env_package_version.return_value = None # The first call to package_version() should call env_package_version() @@ -45,18 +54,16 @@ def test_package_version_cached(self, monkeypatch): ) def test_env_package_version( self, - monkeypatch, + monkeypatch_env: MonkeyPatchEnvFixture, package_version: str | None, resolves: bool, expected_result: str | None, ): - with patch( - "api.admin.config.Configuration.resolve_package_version" + with patch.object( + AdminConfig, "resolve_package_version" ) as resolve_package_version: resolve_package_version.return_value = "x.x.x" - self._set_env( - monkeypatch, "TPP_CIRCULATION_ADMIN_PACKAGE_VERSION", package_version - ) + monkeypatch_env("TPP_CIRCULATION_ADMIN_PACKAGE_VERSION", package_version) assert AdminConfig.env_package_version() == expected_result assert resolve_package_version.call_count == (1 if resolves else 0) @@ -110,16 +117,14 @@ def test_resolve_package_version(self, caplog): ) def test_package_url( self, - monkeypatch, + monkeypatch_env: MonkeyPatchEnvFixture, package_name: str | None, package_version: str | None, mode: OperationalMode, expected_result_startswith: str, ): - self._set_env(monkeypatch, "TPP_CIRCULATION_ADMIN_PACKAGE_NAME", package_name) - self._set_env( - monkeypatch, "TPP_CIRCULATION_ADMIN_PACKAGE_VERSION", package_version - ) + monkeypatch_env("TPP_CIRCULATION_ADMIN_PACKAGE_NAME", package_name) + monkeypatch_env("TPP_CIRCULATION_ADMIN_PACKAGE_VERSION", package_version) result = AdminConfig.package_url(_operational_mode=mode) assert result.startswith(expected_result_startswith) # Reset the cached version @@ -143,15 +148,13 @@ def test_package_url( ) def test_package_development_directory( self, - monkeypatch, + monkeypatch_env: MonkeyPatchEnvFixture, package_name: str | None, package_version: str | None, expected_result: str, ): - self._set_env(monkeypatch, "TPP_CIRCULATION_ADMIN_PACKAGE_NAME", package_name) - self._set_env( - monkeypatch, "TPP_CIRCULATION_ADMIN_PACKAGE_VERSION", package_version - ) + monkeypatch_env("TPP_CIRCULATION_ADMIN_PACKAGE_NAME", package_name) + monkeypatch_env("TPP_CIRCULATION_ADMIN_PACKAGE_VERSION", package_version) result = AdminConfig.package_development_directory(_base_dir="/my-base-dir") assert result == expected_result @@ -192,15 +195,13 @@ def test_package_development_directory( ) def test_lookup_asset_url( self, - monkeypatch, + monkeypatch_env: MonkeyPatchEnvFixture, asset_key: str, operational_mode: OperationalMode, expected_result: str, ): - self._set_env( - monkeypatch, "TPP_CIRCULATION_ADMIN_PACKAGE_NAME", "known-package-name" - ) - self._set_env(monkeypatch, "TPP_CIRCULATION_ADMIN_PACKAGE_VERSION", "1.0.0") + monkeypatch_env("TPP_CIRCULATION_ADMIN_PACKAGE_NAME", "known-package-name") + monkeypatch_env("TPP_CIRCULATION_ADMIN_PACKAGE_VERSION", "1.0.0") result = AdminConfig.lookup_asset_url( key=asset_key, _operational_mode=operational_mode )