From a305ad84bb1d9efd051813c5468897958503b7e1 Mon Sep 17 00:00:00 2001 From: Sam Cranford Date: Wed, 10 Jun 2026 12:54:35 +0000 Subject: [PATCH] Fix OGC support tests to never use local values --- ogc/test/conftest.py | 15 ++++++++++ ogc/test/test_servers.py | 63 +++++++--------------------------------- 2 files changed, 26 insertions(+), 52 deletions(-) create mode 100644 ogc/test/conftest.py diff --git a/ogc/test/conftest.py b/ogc/test/conftest.py new file mode 100644 index 0000000..770f8f6 --- /dev/null +++ b/ogc/test/conftest.py @@ -0,0 +1,15 @@ +import pytest +import importlib +from unittest.mock import patch +from ogc import settings + + +@pytest.fixture(scope="module", autouse=True) +def set_env_vars(): + """Setup the environmental variables for the module to support WMS and WCS.""" + with patch.dict("os.environ", {"OGC_SUPPORTED_FORMATS": "wms,wcs"}): + importlib.reload(settings) + yield + + # Fix imports after patching for test + importlib.reload(settings) diff --git a/ogc/test/test_servers.py b/ogc/test/test_servers.py index 10871cb..ca8a9f3 100644 --- a/ogc/test/test_servers.py +++ b/ogc/test/test_servers.py @@ -5,7 +5,6 @@ from ogc.ogc_common import WCSException from pygeoapi.api import APIRequest from unittest.mock import patch -from typing import Callable, Generator import importlib import podpac @@ -13,33 +12,6 @@ import numpy as np -@pytest.fixture -def supported_formats() -> Generator[Callable[[str], None], None, None]: - """Fixture used to patch OGC supported formats. - - Returns - ------- - Generator[Callable[[str], None], None, None] - A generator which yields a function which patches the OGC supported formats based on input string. - """ - - def _supported_formats(formats: str): - """Patch the supported formats setting. - - Parameters - ---------- - formats : str - The formats which should be supported by the server as a string. - """ - with patch.dict("os.environ", {"OGC_SUPPORTED_FORMATS": formats}): - importlib.reload(settings) - - yield _supported_formats - - # Fix imports after patching for test - importlib.reload(settings) - - @pytest.fixture def client(): """ @@ -73,6 +45,15 @@ def client(): yield app.test_client() +@pytest.fixture +def disable_all_formats_in_env(): + """Setup the environmental variables for no supported formats.""" + with patch.dict("os.environ", {"OGC_SUPPORTED_FORMATS": ""}): + importlib.reload(settings) + yield + importlib.reload(settings) + + def test_server_construction(client): """ Test the construction of the server. @@ -148,43 +129,21 @@ def test_server_with_default_supported_services(client): assert response.status_code == 404 -def test_server_without_wcs_supported_service(supported_formats, client): +def test_server_without_wcs_supported_service(disable_all_formats_in_env, client): """ Test the WCS service is unavailable when WCS is not a supported format. """ - supported_formats("wms") - - response = client.get("/ogc?service=WMS&request=GetCapabilities") - assert response.status_code == 200 - response = client.get("/ogc?service=WCS&request=GetCapabilities") assert response.status_code == 400 - response = client.get("/ogc?service=WMTS&request=GetCapabilities") - assert response.status_code == 400 - response = client.get("/ogc/edr") - assert response.status_code == 404 - - -def test_server_without_wms_supported_service(supported_formats, client): +def test_server_without_wms_supported_service(disable_all_formats_in_env, client): """ Test the WMS service is unavailable when WMS is not a supported format. """ - supported_formats("wcs") - - response = client.get("/ogc?service=WCS&request=GetCapabilities") - assert response.status_code == 200 - response = client.get("/ogc?service=WMS&request=GetCapabilities") assert response.status_code == 400 - response = client.get("/ogc?service=WMTS&request=GetCapabilities") - assert response.status_code == 400 - - response = client.get("/ogc/edr") - assert response.status_code == 404 - # --------------------------------------------------------------------------- # edr_render tests