| 19 | |
| 20 | |
| 21 | class TestBoxDefaults: |
| 22 | def test_defaults_apply(self): |
| 23 | b = DefaultBox() |
| 24 | assert b.missing == DefaultBox() |
| 25 | assert b._box_config["default_box"] is True |
| 26 | assert Box()._box_config["default_box"] is False |
| 27 | |
| 28 | def test_explicit_option_wins(self): |
| 29 | b = DefaultBox(default_box=False) |
| 30 | with pytest.raises(AttributeError): |
| 31 | b.missing |
| 32 | |
| 33 | def test_nested_boxes_keep_class_and_options(self): |
| 34 | b = DefaultBox({"a": {"b": {"c": 1}}, "l": [{"x": 1}]}) |
| 35 | assert type(b.a) is DefaultBox and type(b.a.b) is DefaultBox |
| 36 | assert type(b.l[0]) is DefaultBox |
| 37 | assert b.a.b.missing == DefaultBox() |
| 38 | b.new.deep = 1 |
| 39 | assert type(b.new) is DefaultBox |
| 40 | assert b.to_dict() == {"a": {"b": {"c": 1}}, "l": [{"x": 1}], "new": {"deep": 1}} |
| 41 | |
| 42 | def test_explicit_option_carries_to_nested_boxes(self): |
| 43 | b = DefaultBox({"a": {"b": 1}}, default_box=False) |
| 44 | with pytest.raises(AttributeError): |
| 45 | b.a.missing |
| 46 | |
| 47 | def test_inherited_and_extended(self): |
| 48 | b = FrozenDefaultBox({"a": 1}) |
| 49 | assert b._box_config["default_box"] is True |
| 50 | assert b._box_config["frozen_box"] is True |
| 51 | with pytest.raises(BoxError): |
| 52 | b.a = 2 |
| 53 | assert DefaultBox._box_resolved_defaults == {"default_box": True} |
| 54 | |
| 55 | def test_unknown_option_is_rejected_at_class_creation(self): |
| 56 | with pytest.raises(BoxError, match="defualt_box"): |
| 57 | |
| 58 | class Typo(Box): |
| 59 | _box_defaults = {"defualt_box": True} |
| 60 | |
| 61 | def test_no_init_override_needed(self): |
| 62 | # The pattern people tried in #296 / #223 fails, this is the replacement |
| 63 | class MyConfig(Box): |
| 64 | _box_defaults = {"default_box": True, "box_dots": True} |
| 65 | |
| 66 | config = MyConfig() |
| 67 | config["server.host"] = "localhost" |
| 68 | assert config.to_dict() == {"server": {"host": "localhost"}} |
| 69 | |
| 70 | def test_copy_pickle_and_conversions_keep_defaults(self): |
| 71 | b = DefaultBox({"a": {"b": 1}}) |
| 72 | for other in (b.copy(), copy.deepcopy(b), pickle.loads(pickle.dumps(b)), DefaultBox.from_json(b.to_json())): |
| 73 | assert type(other) is DefaultBox |
| 74 | assert other.a.missing == DefaultBox() |
| 75 | |
| 76 | def test_box_list_conversion(self): |
| 77 | bl = BoxList([{"a": 1}], box_class=DefaultBox) |
| 78 | assert type(bl[0]) is DefaultBox |
nothing calls this directly
no outgoing calls
no test coverage detected