| 2165 | |
| 2166 | |
| 2167 | class TestCreateOnSetWithoutCreateOnGet: |
| 2168 | def test_issue_274(self): |
| 2169 | b = _no_create_box() |
| 2170 | assert b.foo.bar == Box() |
| 2171 | assert b == {} |
| 2172 | b.foo.bar = 1 |
| 2173 | assert b.to_dict() == {"foo": {"bar": 1}} |
| 2174 | |
| 2175 | def test_reads_never_create(self): |
| 2176 | b = _no_create_box() |
| 2177 | b.x.y.z |
| 2178 | b["a"]["b"] |
| 2179 | assert b == {} |
| 2180 | |
| 2181 | def test_deep_assignment_and_item_forms(self): |
| 2182 | b = _no_create_box() |
| 2183 | b.a.b.c.d = 1 |
| 2184 | b["e"]["f"] = 2 |
| 2185 | b.g.h.update({"i": 3}) |
| 2186 | b.j.k.setdefault("l", 4) |
| 2187 | b.m.merge_update({"n": 5}) |
| 2188 | assert b.to_dict() == { |
| 2189 | "a": {"b": {"c": {"d": 1}}}, |
| 2190 | "e": {"f": 2}, |
| 2191 | "g": {"h": {"i": 3}}, |
| 2192 | "j": {"k": {"l": 4}}, |
| 2193 | "m": {"n": 5}, |
| 2194 | } |
| 2195 | |
| 2196 | def test_failed_operations_do_not_create(self): |
| 2197 | b = _no_create_box() |
| 2198 | del b.x.nothing # a missing attribute of a default_box looks present, deleting it is a no-op |
| 2199 | with pytest.raises(BoxKeyError): |
| 2200 | b.x.items = 1 # protected key |
| 2201 | b.x.update({}) |
| 2202 | assert b == {} |
| 2203 | |
| 2204 | def test_children_do_not_leak_bookkeeping(self): |
| 2205 | b = _no_create_box() |
| 2206 | child = b.x |
| 2207 | assert "__pending_parent" in child._box_config |
| 2208 | assert child.to_dict() == {} and repr(child) == "Box({})" |
| 2209 | for clone in (copy.copy(child), copy.deepcopy(child), pickle.loads(pickle.dumps(child))): |
| 2210 | assert "__pending_parent" not in clone._box_config |
| 2211 | clone.z = 1 |
| 2212 | assert b == {} |
| 2213 | # box options handed to new children never include internal "__" keys |
| 2214 | child.sub.q = 1 |
| 2215 | assert child.to_dict() == {"sub": {"q": 1}} |
| 2216 | assert not any(k.startswith("__pending") for k in child.sub._box_config["box_namespace"] if isinstance(k, str)) |
| 2217 | # pickling a flushed child does not drag the old parent reference along |
| 2218 | assert "__pending_parent" not in pickle.loads(pickle.dumps(b.x))._box_config |
| 2219 | |
| 2220 | def test_pickled_child_does_not_pickle_parent(self): |
| 2221 | b = _no_create_box() |
| 2222 | b.big = list(range(5000)) |
| 2223 | child = b.x |
| 2224 | assert len(pickle.dumps(child)) < 2000 |
nothing calls this directly
no outgoing calls
no test coverage detected