|
| 1 | +"""Unit tests for WtPage.update_dict and its merge helper. |
| 2 | +
|
| 3 | +Regression guard for #15: update_dict called wt.combine_into, a function that |
| 4 | +has never existed in this repository, so every nested dict raised |
| 5 | +AttributeError. It was also declared as an instance method but called as |
| 6 | +WtPage.update_dict(a, b) in set_value, which raised TypeError before the |
| 7 | +missing reference was ever reached. |
| 8 | +""" |
| 9 | + |
| 10 | +from typing import Any, Dict, Union |
| 11 | + |
| 12 | +from osw.wtsite import WtPage, _combine_into |
| 13 | + |
| 14 | + |
| 15 | +class OfflineWtPage(WtPage): |
| 16 | + """A WtPage that never touches a wiki, copied from tests/test_osl.py.""" |
| 17 | + |
| 18 | + def __init__(self, wtSite: Any = None, title: str = None): |
| 19 | + self.wtSite = wtSite |
| 20 | + self.title = title |
| 21 | + self.exists = True |
| 22 | + self._original_content = "" |
| 23 | + self.changed: bool = False |
| 24 | + self._dict = [] |
| 25 | + self._slots: Dict[str, Union[str, dict]] = {"main": ""} |
| 26 | + self._slots_changed: Dict[str, bool] = {"main": False} |
| 27 | + self._content_model: Dict[str, str] = {"main": "wikitext"} |
| 28 | + |
| 29 | + |
| 30 | +def test_flat_values_are_replaced(): |
| 31 | + combined = {"a": 1, "b": 2} |
| 32 | + |
| 33 | + _combine_into({"b": 3}, combined) |
| 34 | + |
| 35 | + assert combined == {"a": 1, "b": 3} |
| 36 | + |
| 37 | + |
| 38 | +def test_nested_dicts_are_merged_not_replaced(): |
| 39 | + """The point of the recursion: 'keep' has to survive the merge.""" |
| 40 | + combined = {"outer": {"keep": 1, "change": 2}} |
| 41 | + |
| 42 | + _combine_into({"outer": {"change": 3, "add": 4}}, combined) |
| 43 | + |
| 44 | + assert combined == {"outer": {"keep": 1, "change": 3, "add": 4}} |
| 45 | + |
| 46 | + |
| 47 | +def test_merging_recurses_to_any_depth(): |
| 48 | + combined = {"a": {"b": {"c": {"keep": 1}}}} |
| 49 | + |
| 50 | + _combine_into({"a": {"b": {"c": {"add": 2}}}}, combined) |
| 51 | + |
| 52 | + assert combined == {"a": {"b": {"c": {"keep": 1, "add": 2}}}} |
| 53 | + |
| 54 | + |
| 55 | +def test_a_dict_replaces_a_scalar(): |
| 56 | + combined = {"a": "scalar"} |
| 57 | + |
| 58 | + _combine_into({"a": {"b": 1}}, combined) |
| 59 | + |
| 60 | + assert combined == {"a": {"b": 1}} |
| 61 | + |
| 62 | + |
| 63 | +def test_a_scalar_replaces_a_dict(): |
| 64 | + combined = {"a": {"b": 1}} |
| 65 | + |
| 66 | + _combine_into({"a": "scalar"}, combined) |
| 67 | + |
| 68 | + assert combined == {"a": "scalar"} |
| 69 | + |
| 70 | + |
| 71 | +def test_a_new_nested_key_is_copied_not_aliased(): |
| 72 | + """Otherwise editing the result would reach back into the update dict.""" |
| 73 | + update = {"a": {"b": 1}} |
| 74 | + combined = {} |
| 75 | + |
| 76 | + _combine_into(update, combined) |
| 77 | + combined["a"]["b"] = 2 |
| 78 | + |
| 79 | + assert update == {"a": {"b": 1}} |
| 80 | + |
| 81 | + |
| 82 | +def test_keys_absent_from_update_are_untouched(): |
| 83 | + combined = {"a": 1} |
| 84 | + |
| 85 | + _combine_into({}, combined) |
| 86 | + |
| 87 | + assert combined == {"a": 1} |
| 88 | + |
| 89 | + |
| 90 | +def test_update_dict_merges_in_place_and_returns_none(): |
| 91 | + combined = {"outer": {"keep": 1}} |
| 92 | + |
| 93 | + assert WtPage.update_dict(combined, {"outer": {"add": 2}}) is None |
| 94 | + assert combined == {"outer": {"keep": 1, "add": 2}} |
| 95 | + |
| 96 | + |
| 97 | +def test_set_value_merges_into_the_existing_entry(): |
| 98 | + """set_value(replace=False) is the only caller, and it was broken.""" |
| 99 | + page = OfflineWtPage(title="Test") |
| 100 | + page._dict = [{"Template": {"keep": "yes", "change": "old"}}] |
| 101 | + |
| 102 | + page.set_value("$.*.Template", {"change": "new"}) |
| 103 | + |
| 104 | + assert page._dict == [{"Template": {"keep": "yes", "change": "new"}}] |
| 105 | + |
| 106 | + |
| 107 | +def test_set_value_with_replace_discards_the_existing_entry(): |
| 108 | + page = OfflineWtPage(title="Test") |
| 109 | + page._dict = [{"Template": {"keep": "yes", "change": "old"}}] |
| 110 | + |
| 111 | + page.set_value("$.*.Template", {"change": "new"}, replace=True) |
| 112 | + |
| 113 | + assert page._dict == [{"Template": {"change": "new"}}] |
0 commit comments