diff --git a/src/paperless/tests/settings/test_settings.py b/src/paperless/tests/settings/test_settings.py index 36761a83f..95155aaef 100644 --- a/src/paperless/tests/settings/test_settings.py +++ b/src/paperless/tests/settings/test_settings.py @@ -169,65 +169,43 @@ class TestPaperlessURLSettings(TestCase): self.assertIn(url, settings.CORS_ALLOWED_ORIGINS) -class TestLlmExtraParams(TestCase): - def test_unset_is_empty(self) -> None: - """ - GIVEN: - - No extra LLM params configured - WHEN: - - The setting is parsed - THEN: - - An empty dict is returned, so nothing is added to requests - """ - with mock.patch.dict(os.environ, {}, clear=True): - self.assertEqual(_get_llm_extra_params(), {}) - - def test_parses_json_object(self) -> None: - """ - GIVEN: - - A JSON object of provider parameters - WHEN: - - The setting is parsed - THEN: - - It is returned as a dict - """ - with mock.patch.dict( - os.environ, - {"PAPERLESS_AI_LLM_EXTRA_PARAMS": '{"reasoning_effort": "none"}'}, - ): - self.assertEqual( - _get_llm_extra_params(), +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 - def test_invalid_json_raises(self) -> None: - """ - GIVEN: - - A value which is not valid JSON - WHEN: - - The setting is parsed - THEN: - - Startup fails with a clear error instead of being ignored - """ - with mock.patch.dict( - os.environ, - {"PAPERLESS_AI_LLM_EXTRA_PARAMS": "reasoning_effort=none"}, - ): - with pytest.raises(ImproperlyConfigured, match="valid JSON"): - _get_llm_extra_params() - - def test_non_object_raises(self) -> None: - """ - GIVEN: - - Valid JSON which is not an object - WHEN: - - The setting is parsed - THEN: - - Startup fails with a clear error - """ - with mock.patch.dict( - os.environ, - {"PAPERLESS_AI_LLM_EXTRA_PARAMS": '["none"]'}, - ): - with pytest.raises(ImproperlyConfigured, match="JSON object"): - _get_llm_extra_params() + @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()