Skip to content

Commit

Permalink
cleaner mock
Browse files Browse the repository at this point in the history
  • Loading branch information
khvn26 committed May 20, 2024
1 parent 19fdfb1 commit 9ba5e29
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 37 deletions.
8 changes: 0 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,3 @@ 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
57 changes: 28 additions & 29 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
import json
import typing

import pytest
from pytest_mock import MockerFixture

from src.edge_proxy.settings import AppConfig
from edge_proxy.settings import get_settings


@pytest.mark.parametrize(
"config_file_raw_json, settings_tests",
"config_file_json, expected_config",
(
(
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",
},
),
{
"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",
}
),
{
"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],
mocker: MockerFixture,
config_file_json: dict[str, typing.Any],
expected_config: 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)
mocker.patch(
"edge_proxy.settings.json_config_settings_source",
return_value=config_file_json,
)

# When
config = AppConfig()
config = get_settings()

# Then
for key, value in settings_tests.items():
for key, value in expected_config.items():
assert getattr(config, key) == value

0 comments on commit 9ba5e29

Please sign in to comment.