From b27a3c1705f589fdc5ad2cdd1a5130aea0498718 Mon Sep 17 00:00:00 2001 From: Matthew Elwell Date: Mon, 20 May 2024 16:17:27 +0100 Subject: [PATCH] Add test --- tests/conftest.py | 8 ++++++ tests/test_settings.py | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/test_settings.py diff --git a/tests/conftest.py b/tests/conftest.py index b7d9f47..e940aff 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -46,3 +46,11 @@ def client(): with TestClient(app) as c: yield c + + +@pytest.fixture +def mock_json_config_file(mocker: MockerFixture) -> typing.Callable[[str], None]: + def _inner(raw_json: str) -> None: + mocker.patch("edge_proxy.settings.Path.read_text", return_value=raw_json) + + return _inner diff --git a/tests/test_settings.py b/tests/test_settings.py new file mode 100644 index 0000000..7b8c8d7 --- /dev/null +++ b/tests/test_settings.py @@ -0,0 +1,58 @@ +import json +import typing + +import pytest + +from src.edge_proxy.settings import AppConfig + + +@pytest.mark.parametrize( + "config_file_raw_json, settings_tests", + ( + ( + json.dumps( + { + "environment_key_pairs": [ + {"server_side_key": "abc123", "client_side_key": "ser.def456"} + ], + "api_poll_frequency": 10, + "api_poll_timeout": 10, + "api_url": "https://api.flagsmith.com/api/v1", + }, + ), + {"api_poll_frequency_seconds": 10, "api_poll_timeout_seconds": 10}, + ), + ( + json.dumps( + { + "environment_key_pairs": [ + {"server_side_key": "abc123", "client_side_key": "ser.def456"} + ], + "api_poll_frequency_seconds": 10, + "api_poll_timeout_seconds": 10, + "api_url": "https://api.flagsmith.com/api/v1", + } + ), + {"api_poll_frequency_seconds": 10, "api_poll_timeout_seconds": 10}, + ), + ), +) +def test_settings_are_loaded_correctly( + mock_json_config_file: typing.Callable[[str], None], + config_file_raw_json: str, + settings_tests: dict[str, typing.Any], +) -> None: + """ + Parametrized test which accepts a raw json config file, and a dictionary representing the + specific tests that are required against the resulting config. + """ + + # Given + mock_json_config_file(config_file_raw_json) + + # When + config = AppConfig() + + # Then + for key, value in settings_tests.items(): + assert getattr(config, key) == value