1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
"""Test suite for prance.util.path ."""
__author__ = "Jens Finkhaeuser"
__copyright__ = "Copyright (c) 2018 Jens Finkhaeuser"
__license__ = "MIT"
__all__ = ()
import pytest
from prance.util.path import path_set, path_get
def test_get_bad_path():
# Raise with bad path types
with pytest.raises(TypeError):
path_get({}, 42)
with pytest.raises(TypeError):
path_get({}, 3.14)
with pytest.raises(KeyError):
path_get([], ("a", "b"))
def test_get_value_default():
value = 42
# No path can be resolved in a value type
result = None
with pytest.raises(TypeError):
result = path_get(value, ("foo", "bar"), 123)
assert result is None
# However, we can resolve zero length paths
result = path_get(value, (), 123)
assert 42 == result
result = path_get(None, (), 123)
assert 123 == result
# Also we can resolve None-type paths
result = path_get(value, None, 321)
assert 42 == result
result = path_get(None, None, 321)
assert 321 == result
def test_get_value_no_default():
value = 42
# No path can be resolved in a value type
result = 666
with pytest.raises(TypeError):
result = path_get(value, ("foo", "bar"))
assert result == 666
# However, we can resolve zero length paths
result = path_get(value, ())
assert 42 == result
result = path_get(None, ())
assert result is None
# Also we can resolve None-type paths
result = path_get(value, None)
assert 42 == result
result = path_get(None, None)
assert result is None
def test_get_collection_default():
value = (1, 2, 3)
# String paths in a Sequence should raise KeyError
result = None
with pytest.raises(KeyError):
result = path_get(value, ("foo", "bar"), 123)
assert result is None
# A numeric path should work, though
result = path_get(value, (1,), 123)
assert 2 == result
# Zero length paths should return the value or default value
result = path_get(value, (), 123)
assert (1, 2, 3) == result
result = path_get(None, (), 123)
assert 123 == result
# And None paths as well
result = path_get(value, None, 321)
assert (1, 2, 3) == result
result = path_get(None, None, 321)
assert 321 == result
def test_get_collection_no_default():
value = (1, 2, 3)
# String paths in a Sequence should raise KeyError
result = None
with pytest.raises(KeyError):
result = path_get(value, ("foo", "bar"))
assert result is None
# A numeric path should work, though
result = path_get(value, (1,))
assert 2 == result
# Zero length paths should return the value or default value
result = path_get(value, ())
assert (1, 2, 3) == result
result = path_get(None, ())
assert result is None
# And None paths as well
result = path_get(value, None)
assert (1, 2, 3) == result
result = path_get(None, None)
assert result is None
def test_get_mapping_default():
value = {"foo": 1, "bar": 2, 3: 3}
# String paths should work in a Mapping
result = path_get(value, ("foo",), 123)
assert 1 == result
# So should numeric keys
result = path_get(value, (3,), 123)
assert 3 == result
# Zero length paths should return the value or default value
result = path_get(value, (), 123)
assert {"foo": 1, "bar": 2, 3: 3} == result
result = path_get(None, (), 123)
assert 123 == result
# And None paths as well
result = path_get(value, None, 321)
assert {"foo": 1, "bar": 2, 3: 3} == result
result = path_get(None, None, 321)
assert 321 == result
def test_set_bad_path():
# Raise with bad path types
with pytest.raises(TypeError):
path_set({}, 42, None)
with pytest.raises(TypeError):
path_set({}, 3.14, None)
with pytest.raises(KeyError):
path_set([], ("a", "b"), None)
def test_set_value():
value = 42
# Setting on a value type with a path should raise...
with pytest.raises(TypeError):
path_set(42, ("foo", "bar"), "something")
with pytest.raises(TypeError):
path_set(42, (0, 1), "something")
# ... and without a path or an empty path should raise an error
with pytest.raises(KeyError):
path_set(42, (), "something")
with pytest.raises(TypeError):
path_set(42, None, "something")
def test_set_sequence_no_create():
# Tuples are not mutable - this must raise
result = None
with pytest.raises(TypeError):
result = path_set((42,), (0,), "something")
# Lists are mutable - this must work
result = path_set(
[
42,
],
(0,),
"something",
)
assert ["something"] == result
# Try nested paths
result = path_set([42, [1, 2]], (1, 0), "something")
assert [42, ["something", 2]] == result
# Bad paths must raise
with pytest.raises(IndexError):
path_set([], (0,), "something")
with pytest.raises(IndexError):
path_set([[]], (0, 1), "something")
def test_set_sequence_create():
# If we want to create entries, then mutable sequences should have further
# sequences added to it.
result = path_set([], (1, 0), "something", create=True)
assert [None, ["something"]] == result
def test_set_mapping_no_create():
# Setting existing keys must work
result = path_set({"foo": "bar"}, ("foo",), "new")
assert {"foo": "new"} == result
# Nested mappings should work just as well.
result = path_set(
{"foo": {"bar": "baz"}},
(
"foo",
"bar",
),
"new",
)
assert {"foo": {"bar": "new"}} == result
# Bad paths must raise
with pytest.raises(KeyError):
path_set({}, ("foo",), "something")
with pytest.raises(KeyError):
path_set(
{"foo": {}},
(
"foo",
"bar",
),
"something",
)
def test_set_mapping_create():
# Setting missing keys must work
result = path_set({}, ("foo",), "bar", create=True)
assert {"foo": "bar"} == result
# Recursive setting must work
result = path_set(
{},
(
"foo",
"bar",
),
"baz",
create=True,
)
assert {"foo": {"bar": "baz"}} == result
def test_set_mixed_no_create():
# Sequence in Mapping must work
result = path_set({"foo": [0]}, ("foo", 0), 42)
assert {"foo": [42]} == result
# And Mapping in Sequence likewise
result = path_set([{"foo": "bar"}], (0, "foo"), 42)
assert [{"foo": 42}] == result
def test_set_mixed_create():
# Creation should work based on the path element types
result = path_set({}, ("foo", 0), 42, create=True)
assert {"foo": [42]} == result
result = path_set([], (0, "foo"), 42, create=True)
assert [{"foo": 42}] == result
# Create int keys in dict
result = path_set({}, (0,), 42, create=True)
assert {0: 42} == result
def test_set_mixed_create_no_fill():
# Creation should not add items that are not necessary
base = {"foo": [123]}
result = path_set(base, ("foo", 0), 42, create=True)
assert {"foo": [42]} == result
def test_get_informative_key_error():
base = {"foo": {"bar": [123]}}
# Match that the object being examining has its path printed, as
# well as that the key is included.
with pytest.raises(KeyError, match=r'.*"/".*asdf'):
path_get(base, ("asdf",))
with pytest.raises(KeyError, match=r'.*"/foo".*asdf'):
path_get(base, ("foo", "asdf"))
def test_get_informative_index_error():
base = {"foo": {"bar": [123]}}
with pytest.raises(IndexError, match=r'.*"/foo/bar".*123'):
path_get(base, ("foo", "bar", 123))
|