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