mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-10-21 15:28:13 +00:00
Chore: New helper for JSON parsing during settings, to provide a better error message to users (#14419)
This commit is contained in:
1 parent
1fb463b55c
commit
6f496f741b
4 files changed
+116
-89
No files matched your search
@@ -8,6 +8,7 @@ from paperless.settings.parsers import get_bool_from_env
|
||||
from paperless.settings.parsers import get_choice_from_env
|
||||
from paperless.settings.parsers import get_float_from_env
|
||||
from paperless.settings.parsers import get_int_from_env
|
||||
from paperless.settings.parsers import get_json_from_env
|
||||
from paperless.settings.parsers import get_list_from_env
|
||||
from paperless.settings.parsers import get_path_from_env
|
||||
from paperless.settings.parsers import parse_dict_from_str
|
||||
@@ -611,3 +612,43 @@ class TestGetEnvChoice:
|
||||
result = get_choice_from_env("TEST_ENV", large_choices)
|
||||
|
||||
assert result == "option_50"
|
||||
|
||||
|
||||
class TestGetJsonFromEnv:
|
||||
@pytest.mark.parametrize(
|
||||
("env_value", "expected_type", "expected"),
|
||||
[
|
||||
pytest.param(None, list, ["default"], id="unset-uses-default"),
|
||||
pytest.param('["a", "b"]', list, ["a", "b"], id="list"),
|
||||
pytest.param('["a"]', None, ["a"], id="no-type-check"),
|
||||
],
|
||||
)
|
||||
def test_returns_value(
|
||||
self,
|
||||
mocker: MockerFixture,
|
||||
env_value: str | None,
|
||||
expected_type: type | None,
|
||||
expected: list[str],
|
||||
) -> None:
|
||||
env = {} if env_value is None else {"TEST_ENV": env_value}
|
||||
mocker.patch.dict("os.environ", env, clear=True)
|
||||
|
||||
assert get_json_from_env("TEST_ENV", ["default"], expected_type) == expected
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("env_value", "match"),
|
||||
[
|
||||
pytest.param('["a", , "b"]', "'TEST_ENV' is not valid JSON", id="invalid"),
|
||||
pytest.param('{"a": 1}', "must be a JSON list, got dict", id="wrong-type"),
|
||||
],
|
||||
)
|
||||
def test_raises(
|
||||
self,
|
||||
mocker: MockerFixture,
|
||||
env_value: str,
|
||||
match: str,
|
||||
) -> None:
|
||||
mocker.patch.dict("os.environ", {"TEST_ENV": env_value})
|
||||
|
||||
with pytest.raises(ValueError, match=match):
|
||||
get_json_from_env("TEST_ENV", [], list)
|
||||
@@ -7,7 +7,6 @@ from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
from paperless.settings import _get_allauth_trusted_proxy_count
|
||||
from paperless.settings import _get_classifier_language_setting
|
||||
from paperless.settings import _get_llm_extra_params
|
||||
from paperless.settings import _get_search_language_setting
|
||||
from paperless.settings import _parse_paperless_url
|
||||
from paperless.settings import default_threads_per_worker
|
||||
@@ -167,45 +166,3 @@ class TestPaperlessURLSettings(TestCase):
|
||||
|
||||
self.assertIn(url, settings.CSRF_TRUSTED_ORIGINS)
|
||||
self.assertIn(url, settings.CORS_ALLOWED_ORIGINS)
|
||||
|
||||
|
||||
class TestLlmExtraParams:
|
||||
@pytest.mark.parametrize(
|
||||
("env_value", "expected"),
|
||||
[
|
||||
pytest.param(None, {}, id="unset"),
|
||||
pytest.param(
|
||||
'{"reasoning_effort": "none"}',
|
||||
{"reasoning_effort": "none"},
|
||||
id="json-object",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_parses(
|
||||
self,
|
||||
monkeypatch,
|
||||
env_value,
|
||||
expected,
|
||||
):
|
||||
if env_value is None:
|
||||
monkeypatch.delenv("PAPERLESS_AI_LLM_EXTRA_PARAMS", raising=False)
|
||||
else:
|
||||
monkeypatch.setenv("PAPERLESS_AI_LLM_EXTRA_PARAMS", env_value)
|
||||
assert _get_llm_extra_params() == expected
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("env_value", "match"),
|
||||
[
|
||||
pytest.param("reasoning_effort=none", "valid JSON", id="invalid-json"),
|
||||
pytest.param('["none"]', "JSON object", id="not-an-object"),
|
||||
],
|
||||
)
|
||||
def test_invalid_raises(
|
||||
self,
|
||||
monkeypatch,
|
||||
env_value,
|
||||
match,
|
||||
):
|
||||
monkeypatch.setenv("PAPERLESS_AI_LLM_EXTRA_PARAMS", env_value)
|
||||
with pytest.raises(ImproperlyConfigured, match=match):
|
||||
_get_llm_extra_params()
|
||||
Reference in new issue
Block a user