diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-08-14 10:18:57 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-08-14 10:18:57 +0000 |
commit | 5879b93d587f602920623c1b9abaa392ba9b52f6 (patch) | |
tree | 69552f74b49d8744fa8e743c08ab0063cfdb1bec /tests | |
parent | Releasing debian version 3.3.1-3. (diff) | |
download | cfgv-5879b93d587f602920623c1b9abaa392ba9b52f6.tar.xz cfgv-5879b93d587f602920623c1b9abaa392ba9b52f6.zip |
Merging upstream version 3.4.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/cfgv_test.py | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/tests/cfgv_test.py b/tests/cfgv_test.py index 15bf581..3819ade 100644 --- a/tests/cfgv_test.py +++ b/tests/cfgv_test.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json from unittest import mock @@ -34,11 +36,12 @@ from cfgv import WarnAdditionalKeys def _assert_exception_trace(e, trace): - inner = e - for ctx in trace[:-1]: - assert inner.ctx == ctx - inner = inner.error_msg - assert inner.error_msg == trace[-1] + parts = [] + while e.ctx is not None: + parts.append(e.ctx) + e = e.error_msg + parts.append(e.error_msg) + assert tuple(parts) == trace def test_ValidationError_simple_str(): @@ -580,6 +583,35 @@ def test_load_from_filename_applies_defaults(tmpdir): assert ret == {'key': False} +def test_load_from_filename_custom_display_no_file(tmp_path): + with pytest.raises(ValidationError) as excinfo: + load_from_filename( + tmp_path.joinpath('cfg.json'), + map_required, + json.loads, + display_filename='cfg.json', + ) + _assert_exception_trace(excinfo.value.args[0], ('cfg.json is not a file',)) + + +def test_load_from_filename_custom_display_error(tmp_path): + f = tmp_path.joinpath('cfg.json') + f.write_text('{}') + with pytest.raises(ValidationError) as excinfo: + load_from_filename( + f, + map_required, + json.loads, + display_filename='cfg.json', + ) + expected = ( + 'File cfg.json', + 'At foo(key=MISSING)', + 'Missing required key: key', + ) + _assert_exception_trace(excinfo.value.args[0], expected) + + conditional_recurse = Map( 'Map', None, |