diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-08-14 10:18:53 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-08-14 10:18:53 +0000 |
commit | 82bb919828cbe531a83835b87dade5a3a74c7c9c (patch) | |
tree | 67c33bb03cc7bbd4854d3e8e73209b9193e92204 /tests | |
parent | Adding upstream version 3.3.1. (diff) | |
download | cfgv-82bb919828cbe531a83835b87dade5a3a74c7c9c.tar.xz cfgv-82bb919828cbe531a83835b87dade5a3a74c7c9c.zip |
Adding upstream version 3.4.0.upstream/3.4.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-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, |