| 2230 | |
| 2231 | |
| 2232 | class TestCreateOnSetWithoutCreateOnGet: |
| 2233 | def test_issue_274(self): |
| 2234 | b = _no_create_box() |
| 2235 | assert b.foo.bar == Box() |
| 2236 | assert b == {} |
| 2237 | b.foo.bar = 1 |
| 2238 | assert b.to_dict() == {"foo": {"bar": 1}} |
| 2239 | |
| 2240 | def test_reads_never_create(self): |
| 2241 | b = _no_create_box() |
| 2242 | b.x.y.z |
| 2243 | b["a"]["b"] |
| 2244 | assert b == {} |
| 2245 | |
| 2246 | def test_deep_assignment_and_item_forms(self): |
| 2247 | b = _no_create_box() |
| 2248 | b.a.b.c.d = 1 |
| 2249 | b["e"]["f"] = 2 |
| 2250 | b.g.h.update({"i": 3}) |
| 2251 | b.j.k.setdefault("l", 4) |
| 2252 | b.m.merge_update({"n": 5}) |
| 2253 | assert b.to_dict() == { |
| 2254 | "a": {"b": {"c": {"d": 1}}}, |
| 2255 | "e": {"f": 2}, |
| 2256 | "g": {"h": {"i": 3}}, |
| 2257 | "j": {"k": {"l": 4}}, |
| 2258 | "m": {"n": 5}, |
| 2259 | } |
| 2260 | |
| 2261 | def test_failed_operations_do_not_create(self): |
| 2262 | b = _no_create_box() |
| 2263 | del b.x.nothing # a missing attribute of a default_box looks present, deleting it is a no-op |
| 2264 | with pytest.raises(BoxKeyError): |
| 2265 | b.x.items = 1 # protected key |
| 2266 | b.x.update({}) |
| 2267 | assert b == {} |
| 2268 | |
| 2269 | def test_children_do_not_leak_bookkeeping(self): |
| 2270 | b = _no_create_box() |
| 2271 | child = b.x |
| 2272 | assert "__pending_parent" in child._box_config |
| 2273 | assert child.to_dict() == {} and repr(child) == "Box({})" |
| 2274 | for clone in (copy.copy(child), copy.deepcopy(child), pickle.loads(pickle.dumps(child))): |
| 2275 | assert "__pending_parent" not in clone._box_config |
| 2276 | clone.z = 1 |
| 2277 | assert b == {} |
| 2278 | # box options handed to new children never include internal "__" keys |
| 2279 | child.sub.q = 1 |
| 2280 | assert child.to_dict() == {"sub": {"q": 1}} |
| 2281 | assert not any(k.startswith("__pending") for k in child.sub._box_config["box_namespace"] if isinstance(k, str)) |
| 2282 | # pickling a flushed child does not drag the old parent reference along |
| 2283 | assert "__pending_parent" not in pickle.loads(pickle.dumps(b.x))._box_config |
| 2284 | |
| 2285 | def test_pickled_child_does_not_pickle_parent(self): |
| 2286 | b = _no_create_box() |
| 2287 | b.big = list(range(5000)) |
| 2288 | child = b.x |
| 2289 | assert len(pickle.dumps(child)) < 2000 |
nothing calls this directly
no outgoing calls
no test coverage detected