| 946 | |
| 947 | |
| 948 | class TestDictCompat: |
| 949 | def test_dict_conversion_and_equality(self): |
| 950 | bx = Box({"a": {"b": 1}, "l": [1]}) |
| 951 | flat = dict(bx) |
| 952 | assert type(flat) is dict and type(flat["a"]) is Box |
| 953 | assert bx == {"a": {"b": 1}, "l": [1]} |
| 954 | assert {"a": {"b": 1}, "l": [1]} == bx |
| 955 | assert bx == Box({"l": [1], "a": {"b": 1}}, default_box=True) |
| 956 | assert bx != {"a": {"b": 2}, "l": [1]} |
| 957 | assert bx.a == {"b": 1} and bx.l == [1] |
| 958 | assert bx.l == BoxList([1]) |
| 959 | assert bx.a != BoxList([1]) |
| 960 | |
| 961 | def test_constructor_options_are_not_data_when_passed_in_a_dict(self): |
| 962 | bx = Box({"frozen_box": True, "default_box": 1}) |
| 963 | assert not bx._box_config["frozen_box"] |
| 964 | assert not bx._box_config["default_box"] |
| 965 | assert bx == {"frozen_box": True, "default_box": 1} |
| 966 | bx.new = 1 |
| 967 | opts = Box(a=1, frozen_box=True) |
| 968 | assert opts == {"a": 1} |
| 969 | assert Box({"a": 1}, b=2) == {"a": 1, "b": 2} |
| 970 | assert Box([("a", 1)], b=2) == {"a": 1, "b": 2} |
| 971 | with pytest.raises(BoxValueError): |
| 972 | Box("text") |
| 973 | with pytest.raises(BoxValueError): |
| 974 | Box(5) |
| 975 | with pytest.raises(BoxTypeError): |
| 976 | Box({"a": 1}, {"b": 2}) |
| 977 | |
| 978 | def test_fromkeys_and_get_default_conversion(self): |
| 979 | bx = Box.fromkeys(["a", "b"], {"x": 1}) |
| 980 | assert type(bx) is Box and type(bx.a) is Box and bx.a is not bx.b |
| 981 | got = bx.get("none", {"q": [{"r": 1}]}) |
| 982 | assert type(got) is Box and type(got.q) is BoxList |
| 983 | assert type(bx.get("none", [{"r": 1}])[0]) is Box |
| 984 | assert bx.get("none") is None |
| 985 | assert bx.get("none", None) is None |
| 986 | assert "none" not in bx |
| 987 | dflt = Box(default_box=True).get("zz") |
| 988 | assert dflt == {} |
| 989 | |
| 990 | def test_live_views_and_reversed(self): |
| 991 | bx = Box(a=1, b=2) |
| 992 | keys, values, items = bx.keys(), bx.values(), bx.items() |
| 993 | bx.c = {"d": 1} |
| 994 | assert "c" in keys and len(keys) == 3 |
| 995 | assert type(list(values)[-1]) is Box |
| 996 | assert ("a", 1) in items |
| 997 | assert list(reversed(bx)) == ["c", "b", "a"] |
| 998 | del bx.a |
| 999 | assert list(keys) == ["b", "c"] |
| 1000 | assert bx.popitem() == ("c", {"d": 1}) and bx.popitem() == ("b", 2) |
| 1001 | with pytest.raises(BoxKeyError): |
| 1002 | bx.popitem() |
| 1003 | |
| 1004 | def test_repr_and_str_of_nested_frozen_and_subclass_boxes(self): |
| 1005 | bx = Box({"a": {"b": [1, {"c": 2}]}}) |
nothing calls this directly
no outgoing calls
no test coverage detected