Skip to content

Commit cf78de9

Browse files
committed
fix(core): drop skipped pages from StoreEntityPartialError.stored
- 'stored' now lists only the pages that were actually written - add 'skipped' to the error, mirroring StoreEntityResult - 'result.pages' unchanged, still lists kept pages
1 parent 10808a0 commit cf78de9

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎src/osw/core.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,7 @@ class StoreEntityResult(OswBaseModel):
17821782
skipped: Dict[str, WtPage] = {}
17831783
"""Existing pages that were not written because the applicable overwrite
17841784
setting was 'keep existing', keyed by full page title. These also appear in
1785-
'pages', where they are indistinguishable from pages that were written."""
1785+
'pages'; subtract them to get the pages that were actually written."""
17861786
failed: Dict[str, Exception] = {}
17871787
"""Entities that could not be stored, keyed by full page title and mapped to
17881788
the exception that caused the failure. Empty on full success."""
@@ -1794,13 +1794,20 @@ class StoreEntityPartialError(Exception):
17941794
"""Raised by store_entity() when one or more entities could not be stored.
17951795
17961796
Carries the partial ``StoreEntityResult`` so callers can learn exactly which
1797-
entities were written (``stored`` / ``result.pages``) and which failed
1798-
(``failed`` / ``result.failed``) without a separate existence query.
1797+
entities were written (``stored``), which existed already and were left
1798+
untouched by 'keep existing' (``skipped``), and which failed (``failed`` /
1799+
``result.failed``) without a separate existence query.
17991800
"""
18001801

18011802
def __init__(self, result: OSW.StoreEntityResult):
18021803
self.result = result
1803-
self.stored = list(result.pages.keys())
1804+
# a page kept by 'keep existing' was not written, so it does not
1805+
# belong in 'stored'. 'result.pages' still lists it, for callers
1806+
# that only care about which entities are on the wiki afterwards.
1807+
self.skipped = list(result.skipped.keys())
1808+
self.stored = [
1809+
title for title in result.pages if title not in result.skipped
1810+
]
18041811
self.failed = result.failed
18051812
total = len(result.pages) + len(result.failed)
18061813
failed_titles = ", ".join(result.failed.keys())

‎tests/test_store_entity_failure.py‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99

1010
import osw.model.entity as model
11-
from osw.core import OSW
11+
from osw.core import OSW, AddOverwriteClassOptions
1212
from osw.utils.wiki import get_namespace, get_title
1313
from osw.wtsite import WtPage
1414

@@ -86,6 +86,37 @@ def test_store_entity_all_success_returns_result(offline_osw, monkeypatch):
8686
assert result.failed == {}
8787

8888

89+
def test_partial_error_does_not_count_a_skipped_page_as_stored(
90+
offline_osw, monkeypatch
91+
):
92+
"""'keep existing' leaves a page untouched, so it was not stored."""
93+
keep, ok, boom = (
94+
model.Item(label=[model.Label(text=text)]) for text in ("Keep", "Ok", "Boom")
95+
)
96+
keep_title, ok_title, boom_title = (_title(it) for it in (keep, ok, boom))
97+
# only the kept entity is already on the wiki; the other two are created
98+
monkeypatch.setattr(
99+
WtPage, "init", lambda self: setattr(self, "exists", self.title == keep_title)
100+
)
101+
_install_edit(monkeypatch, {boom_title})
102+
103+
with pytest.raises(OSW.StoreEntityPartialError) as exc_info:
104+
offline_osw.store_entity(
105+
OSW.StoreEntityParam(
106+
entities=[keep, ok, boom],
107+
overwrite=AddOverwriteClassOptions.keep_existing,
108+
parallel=False,
109+
)
110+
)
111+
112+
err = exc_info.value
113+
assert err.stored == [ok_title]
114+
assert err.skipped == [keep_title]
115+
assert set(err.failed) == {boom_title}
116+
# the kept page is still on the wiki, so it stays in 'pages'
117+
assert keep_title in err.result.pages
118+
119+
89120
def test_store_entity_serial_all_success_returns_result(offline_osw, monkeypatch):
90121
items = [model.Item(label=[model.Label(text=f"Ser{i}")]) for i in range(2)]
91122
titles = [_title(it) for it in items]

0 commit comments

Comments
 (0)