MCPcopy Create free account
hub / github.com/cdgriffith/Box / TestBoxDots

Class TestBoxDots

test/test_edge_cases.py:255–350  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

253
254
255class TestBoxDots:
256 def test_negative_and_out_of_range_list_indexes(self):
257 bx = Box({"a": [1, 2, 3]}, box_dots=True)
258 assert bx["a[2]"] == 3
259 with pytest.raises(IndexError):
260 bx["a[10]"]
261 with pytest.raises(BoxTypeError):
262 bx["a[-1]"]
263 with pytest.raises(BoxTypeError):
264 bx["a[x]"]
265
266 def test_nested_lists_get_set_delete(self):
267 bx = Box({"a": [[1, 2], [3, {"y": 4}]]}, box_dots=True)
268 assert bx["a[0][1]"] == 2
269 assert bx["a[1][1].y"] == 4
270 bx["a[1][1].y"] = 5
271 bx["a[0][0]"] = {"n": 1}
272 assert bx.a[1][1].y == 5
273 assert isinstance(bx.a[0][0], Box)
274 del bx["a[1][1].y"]
275 assert bx.a[1][1] == {}
276 del bx["a[0]"]
277 assert len(bx.a) == 1
278
279 def test_keys_dotted_are_all_resolvable(self):
280 bx = Box({"a": {"b": 1, "c": [1, {"d": 2}, [3]]}, "e": 2}, box_dots=True)
281 keys = bx.keys(dotted=True)
282 assert keys == ["a.b", "a.c[0]", "a.c[1].d", "a.c[2][0]", "e"]
283 for key in keys:
284 assert bx[key] is not None
285 assert dict(bx.items(dotted=True)) == {"a.b": 1, "a.c[0]": 1, "a.c[1].d": 2, "a.c[2][0]": 3, "e": 2}
286 with pytest.raises(BoxError):
287 Box({"a": 1}).keys(dotted=True)
288
289 def test_contains_agrees_with_dotted_lookup_through_lists(self):
290 bx = Box({"a": [{"x": 1}], "b": {"c": 1}}, box_dots=True)
291 for key in bx.keys(dotted=True):
292 assert bx[key] is not None
293 assert key in bx, key
294
295 def test_get_agrees_with_getitem_for_list_paths(self):
296 bx = Box({"a": [{"x": 1}]}, box_dots=True)
297 assert bx["a[0].x"] == 1
298 assert bx.get("a[0].x") == 1
299
300 def test_box_dots_exclude_keeps_literal_dotted_keys(self):
301 bx = Box({"v1.0": 1, "a": {"b": 2}}, box_dots=True, box_dots_exclude=r"v\d")
302 assert bx["v1.0"] == 1
303 assert bx["a.b"] == 2
304 assert "v1.0" in bx
305 bx["v2.0"] = 5
306 assert dict.__contains__(bx, "v2.0")
307 assert list(bx) == ["v1.0", "a", "v2.0"]
308
309 def test_dotted_write_without_parent_needs_default_box(self):
310 with pytest.raises(BoxKeyError):
311 Box(box_dots=True)["a.b"] = 1
312 with pytest.raises(BoxKeyError):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected