|
| 1 | +"""Unit tests for store_entity() dropping entities without recording a failure. |
| 2 | +
|
| 3 | +Regression guard for #183: https://github.com/OpenSemanticLab/osw-python/issues/183 |
| 4 | +store_entity_() returned plainly (instead of raising) when it could not determine |
| 5 | +an entity's title or namespace. A plain return is not an exception, so the |
| 6 | +collector loop in store_entity() never recorded the entity in failed, and it was |
| 7 | +never in created_pages either: the entity was silently dropped without being |
| 8 | +reported to the caller. store_entity_() must now raise in both cases so the |
| 9 | +existing collector loop records them. |
| 10 | +
|
| 11 | +These run fully offline: WtPage.init, the overwrite policy, WtPage.edit and the |
| 12 | +write verification are stubbed, so no network is required. |
| 13 | +""" |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +import osw.core as core_mod |
| 18 | +import osw.model.entity as model |
| 19 | +from osw.core import OSW |
| 20 | +from osw.utils.wiki import get_namespace, get_title |
| 21 | +from osw.wtsite import WtPage |
| 22 | + |
| 23 | + |
| 24 | +def _title(entity): |
| 25 | + return f"{get_namespace(entity)}:{get_title(entity)}" |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def offline_osw(monkeypatch): |
| 30 | + # no network when a WtPage is constructed with do_init=True; mimic the |
| 31 | + # do_init=False branch of WtPage.__init__ which sets .exists |
| 32 | + monkeypatch.setattr(WtPage, "init", lambda self: setattr(self, "exists", False)) |
| 33 | + # bypass the overwrite policy: return the page that was built for the entity |
| 34 | + monkeypatch.setattr( |
| 35 | + OSW, "_apply_overwrite_policy", staticmethod(lambda param: param.page) |
| 36 | + ) |
| 37 | + monkeypatch.setattr(WtPage, "edit", lambda self, *a, **kw: None) |
| 38 | + # the write verification would query the wiki. These tests are not about |
| 39 | + # verification, so report every edited page as existing. See |
| 40 | + # tests/test_store_entity_verify.py for the write verification tests. |
| 41 | + monkeypatch.setattr(OSW, "_get_missing_page_titles", lambda self, titles: []) |
| 42 | + return OSW.construct(site=object()) |
| 43 | + |
| 44 | + |
| 45 | +def test_title_failure_is_reported_serial(offline_osw, monkeypatch): |
| 46 | + def _raise_get_title(entity): |
| 47 | + raise RuntimeError("boom") |
| 48 | + |
| 49 | + monkeypatch.setattr(core_mod, "get_title", _raise_get_title) |
| 50 | + item = model.Item(label=[model.Label(text="Solo")]) |
| 51 | + |
| 52 | + with pytest.raises(OSW.StoreEntityPartialError) as exc_info: |
| 53 | + offline_osw.store_entity(OSW.StoreEntityParam(entities=[item], parallel=False)) |
| 54 | + |
| 55 | + err = exc_info.value |
| 56 | + assert len(err.failed) == 1 |
| 57 | + (exc,) = err.failed.values() |
| 58 | + assert isinstance(exc, ValueError) |
| 59 | + assert "Error getting title for entity" in str(exc) |
| 60 | + |
| 61 | + |
| 62 | +def test_title_failure_is_reported_parallel(offline_osw, monkeypatch): |
| 63 | + def _raise_get_title(entity): |
| 64 | + raise RuntimeError("boom") |
| 65 | + |
| 66 | + monkeypatch.setattr(core_mod, "get_title", _raise_get_title) |
| 67 | + item = model.Item(label=[model.Label(text="Solo")]) |
| 68 | + |
| 69 | + with pytest.raises(OSW.StoreEntityPartialError) as exc_info: |
| 70 | + offline_osw.store_entity(OSW.StoreEntityParam(entities=[item], parallel=True)) |
| 71 | + |
| 72 | + err = exc_info.value |
| 73 | + assert len(err.failed) == 1 |
| 74 | + (exc,) = err.failed.values() |
| 75 | + assert isinstance(exc, ValueError) |
| 76 | + assert "Error getting title for entity" in str(exc) |
| 77 | + |
| 78 | + |
| 79 | +def test_missing_namespace_is_reported(offline_osw, monkeypatch): |
| 80 | + monkeypatch.setattr(core_mod, "get_namespace", lambda entity: None) |
| 81 | + item = model.Item(label=[model.Label(text="NoNamespace")]) |
| 82 | + |
| 83 | + with pytest.raises(OSW.StoreEntityPartialError) as exc_info: |
| 84 | + offline_osw.store_entity(OSW.StoreEntityParam(entities=[item], parallel=False)) |
| 85 | + |
| 86 | + err = exc_info.value |
| 87 | + assert len(err.failed) == 1 |
| 88 | + (exc,) = err.failed.values() |
| 89 | + assert isinstance(exc, TypeError) |
| 90 | + assert "Unsupported entity type" in str(exc) |
| 91 | + |
| 92 | + |
| 93 | +def test_missing_title_is_reported(offline_osw, monkeypatch): |
| 94 | + monkeypatch.setattr(core_mod, "get_title", lambda entity: None) |
| 95 | + item = model.Item(label=[model.Label(text="NoTitle")]) |
| 96 | + |
| 97 | + with pytest.raises(OSW.StoreEntityPartialError) as exc_info: |
| 98 | + offline_osw.store_entity(OSW.StoreEntityParam(entities=[item], parallel=False)) |
| 99 | + |
| 100 | + err = exc_info.value |
| 101 | + assert len(err.failed) == 1 |
| 102 | + (exc,) = err.failed.values() |
| 103 | + assert isinstance(exc, TypeError) |
| 104 | + assert "Unsupported entity type" in str(exc) |
| 105 | + |
| 106 | + |
| 107 | +def test_only_the_failing_entity_of_a_batch_is_reported(offline_osw, monkeypatch): |
| 108 | + real_get_title = get_title |
| 109 | + good_item = model.Item(label=[model.Label(text="Good")]) |
| 110 | + bad_item = model.Item(label=[model.Label(text="Bad")]) |
| 111 | + good_title = _title(good_item) |
| 112 | + |
| 113 | + # store_entity() re-validates its 'entities' param, so the object identity |
| 114 | + # of good_item/bad_item is not preserved past that point. Recognize the |
| 115 | + # failing entity by its label instead. |
| 116 | + def _get_title(entity): |
| 117 | + if entity.label and entity.label[0].text == "Bad": |
| 118 | + raise RuntimeError("boom") |
| 119 | + return real_get_title(entity) |
| 120 | + |
| 121 | + monkeypatch.setattr(core_mod, "get_title", _get_title) |
| 122 | + |
| 123 | + with pytest.raises(OSW.StoreEntityPartialError) as exc_info: |
| 124 | + offline_osw.store_entity( |
| 125 | + OSW.StoreEntityParam(entities=[good_item, bad_item], parallel=False) |
| 126 | + ) |
| 127 | + |
| 128 | + err = exc_info.value |
| 129 | + assert len(err.result.pages) == 1 |
| 130 | + assert len(err.failed) == 1 |
| 131 | + assert good_title in err.result.pages |
| 132 | + assert err.stored == [good_title] |
| 133 | + |
| 134 | + |
| 135 | +def test_normal_path_is_unaffected(offline_osw): |
| 136 | + item = model.Item(label=[model.Label(text="Fine")]) |
| 137 | + title = _title(item) |
| 138 | + |
| 139 | + result = offline_osw.store_entity( |
| 140 | + OSW.StoreEntityParam(entities=[item], parallel=False) |
| 141 | + ) |
| 142 | + |
| 143 | + assert set(result.pages.keys()) == {title} |
| 144 | + assert result.failed == {} |
0 commit comments