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

Class TestPickleAndCopy

test/test_edge_cases.py:74–179  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

72
73
74class TestPickleAndCopy:
75 def test_pickle_nested_box_dots_keeps_working(self):
76 bx = Box({"a": {"b": {"c": 1}}, "l": [{"x": 1}]}, box_dots=True)
77 out = pickle.loads(pickle.dumps(bx))
78 assert out["a.b.c"] == 1
79 assert out["l[0].x"] == 1
80 out["a.b.d"] = 2
81 assert out.a.b.d == 2
82 assert out.a._box_config["box_dots"] is True
83 assert out.l.box_options["box_dots"] is True
84
85 def test_pickle_frozen_stays_frozen_and_hashable(self):
86 bx = Box({"a": [1, {"b": 2}], "c": 3}, frozen_box=True)
87 out = pickle.loads(pickle.dumps(bx))
88 assert out == bx
89 assert hash(out) == hash(bx)
90 assert isinstance(out.a, tuple)
91 with pytest.raises(BoxError):
92 out.c = 4
93 with pytest.raises(BoxError):
94 out.a[1].b = 5
95
96 def test_pickle_config_box_keeps_case_insensitivity(self):
97 cfg = ConfigBox({"Host": "h", "sub": {"Port": 1}})
98 assert cfg.host == "h" # builds the case index before pickling
99 out = pickle.loads(pickle.dumps(cfg))
100 assert type(out) is ConfigBox
101 assert type(out.sub) is ConfigBox
102 assert out["HOST"] == "h"
103 assert out.SUB.port == 1
104 out["HOST"] = "other"
105 assert list(out) == ["sub", "HOST"]
106
107 @pytest.mark.parametrize("module", [pickle, copy])
108 def test_subclass_defaults_survive_roundtrip(self, module):
109 bx = DotsDefaultBox({"n": {"x": 1}})
110 out = pickle.loads(pickle.dumps(bx)) if module is pickle else copy.deepcopy(bx)
111 assert type(out) is DotsDefaultBox
112 assert type(out.n) is DotsDefaultBox
113 assert out["n.x"] == 1
114 assert out.missing.deeper is not None
115 assert "missing" not in out
116
117 def test_pickle_ephemeral_child_drops_parent_reference(self):
118 parent = Box(default_box=True)
119 child = parent.ghost
120 assert "__pending_parent" in child._box_config
121 data = pickle.dumps(child)
122 assert b"__pending_parent" not in data
123 out = pickle.loads(data)
124 assert "__pending_parent" not in out._box_config
125 out.x = 1
126 assert out == {"x": 1}
127 assert "ghost" not in parent
128
129 def test_copy_deepcopy_and_pickle_internal_attrs(self):
130 bx = TrackedBox({"a": 1})
131 bx._meta = {"seen": [1]}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected