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

Class TestBox

test/test_box.py:53–1866  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

51
52
53class TestBox:
54 @pytest.fixture(autouse=True)
55 def temp_dir_cleanup(self):
56 shutil.rmtree(str(tmp_dir), ignore_errors=True)
57 try:
58 os.mkdir(str(tmp_dir))
59 except OSError:
60 pass
61 yield
62 shutil.rmtree(str(tmp_dir), ignore_errors=True)
63
64 @pytest.mark.parametrize("removal", ["item", "attribute", "converted_attribute", "pop", "popitem"])
65 def test_removed_key_releases_conversion_alias(self, removal):
66 bx = Box({"a b": 1}, box_duplicates="error")
67 if removal == "item":
68 del bx["a b"]
69 elif removal == "attribute":
70 delattr(bx, "a b")
71 elif removal == "converted_attribute":
72 del bx.a_b
73 elif removal == "pop":
74 bx.pop("a b")
75 else:
76 bx.popitem()
77
78 bx["a-b"] = 2
79 assert bx.to_dict() == {"a-b": 2}
80 assert bx.a_b == 2
81 with pytest.raises(BoxError):
82 bx["a b"] = 3
83
84 def test_deleting_none_key_after_conversion_alias_owner(self):
85 bx = Box({None: 1, "None": 2})
86 del bx["None"]
87 del bx[None]
88 assert bx.to_dict() == {}
89
90 def test_deleting_colliding_key_keeps_other_conversion_alias(self):
91 bx = Box({"a b": 1, "a-b": 2})
92 del bx["a b"]
93 assert bx.a_b == 2
94
95 def test_safe_attrs(self):
96 assert Box()._safe_attr("BAD!KEY!1") == "BAD_KEY_1"
97 assert Box(camel_killer_box=True)._safe_attr("BAD!KEY!2") == "bad_key_2"
98 assert Box()._safe_attr((5, 6, 7)) == "x5_6_7"
99 assert Box()._safe_attr(356) == "x356"
100
101 def test_camel_killer(self):
102 assert _camel_killer("CamelCase") == "camel_case"
103 assert _camel_killer("Terrible321KeyA") == "terrible321_key_a"
104 bx = Box(camel_killer_box=True, conversion_box=False)
105
106 bx.DeadCamel = 3
107 assert bx["dead_camel"] == 3
108 assert bx.dead_camel == 3
109
110 bx["BigCamel"] = 4

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected