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

Class TestMergeUpdate

test/test_edge_cases.py:520–582  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

518
519
520class TestMergeUpdate:
521 def test_list_strategies(self):
522 def merged(strategy):
523 bx = Box({"l": [1, 2], "d": [{"a": 1}]})
524 bx.merge_update({"l": [2, 3], "d": [{"b": 2}, {"c": 3}]}, box_merge_lists=strategy)
525 return bx.to_dict()
526
527 assert merged(None) == {"l": [2, 3], "d": [{"b": 2}, {"c": 3}]}
528 assert merged(False) == {"l": [2, 3], "d": [{"b": 2}, {"c": 3}]}
529 assert merged("extend") == {"l": [1, 2, 2, 3], "d": [{"a": 1}, {"b": 2}, {"c": 3}]}
530 assert merged("unique") == {"l": [1, 2, 3], "d": [{"a": 1}, {"b": 2}, {"c": 3}]}
531 assert merged("merge") == {"l": [2, 3], "d": [{"a": 1, "b": 2}, {"c": 3}]}
532
533 def test_merge_strategy_applies_to_nested_boxes_and_nested_lists(self):
534 bx = Box({"a": {"b": {"l": [[1, {"x": 1}], 5]}}})
535 bx.merge_update({"a": {"b": {"l": [[9, {"y": 2}, 7], {"z": 1}]}}}, box_merge_lists="merge")
536 assert bx.to_dict() == {"a": {"b": {"l": [[9, {"x": 1, "y": 2}, 7], {"z": 1}]}}}
537 assert isinstance(bx.a.b.l[0][1], Box)
538
539 def test_unknown_strategy_is_rejected_before_any_change(self):
540 bx = Box({"a": 1, "l": [1]})
541 for bad in ("replace", "Extend", True, 1):
542 with pytest.raises(BoxValueError):
543 bx.merge_update({"a": 2, "l": [2]}, box_merge_lists=bad)
544 assert bx == {"a": 1, "l": [1]}
545
546 def test_merging_dicts_and_lists_into_incompatible_values(self):
547 bx = Box({"num": 1, "txt": "s", "lst": [1], "dct": {"a": 1}})
548 bx.merge_update({"num": {"n": 1}, "txt": [1], "lst": {"k": 1}, "dct": [1]}, box_merge_lists="extend")
549 assert bx == {"num": {"n": 1}, "txt": [1], "lst": {"k": 1}, "dct": [1]}
550 assert isinstance(bx.num, Box) and isinstance(bx.txt, BoxList)
551
552 def test_merge_with_itself_and_with_its_dict_is_idempotent(self):
553 bx = Box({"a": {"b": [1, {"c": 1}]}, "d": 1})
554 before = bx.to_dict()
555 bx.merge_update(bx)
556 assert bx.to_dict() == before
557 bx.merge_update(bx.to_dict(), box_merge_lists="merge")
558 assert bx.to_dict() == before
559 bx += bx
560 assert bx.to_dict() == before
561
562 def test_merge_update_argument_forms(self):
563 bx = Box({"a": {"x": 1}})
564 bx.merge_update([("b", 1), ("a", {"y": 2})])
565 bx.merge_update(iter([("c", {"d": 1})]))
566 bx.merge_update(e={"f": 1})
567 bx.merge_update({})
568 bx.merge_update()
569 assert bx == {"a": {"x": 1, "y": 2}, "b": 1, "c": {"d": 1}, "e": {"f": 1}}
570 with pytest.raises(BoxTypeError):
571 bx.merge_update({"z": 1}, y=2)
572 with pytest.raises(BoxTypeError):
573 bx.merge_update({"z": 1}, {"y": 2})
574 assert "z" not in bx
575
576 def test_box_intact_types_are_not_converted_or_merged(self):
577 bx = Box({"d": {"a": 1}}, box_intact_types=(IntactDict, IntactList))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected