Skip to content

Commit 0d3899c

Browse files
committed
feat(wtsite): allow an edit comment on upload_page
- add comment field to WtSite.UploadPageParam - forward it to WtPage.edit, which already accepted one - closes #112
1 parent 0d32bc2 commit 0d3899c

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎src/osw/wtsite.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,8 @@ class UploadPageParam(OswBaseModel):
624624
"""If True, uploads the pages in parallel."""
625625
debug: Optional[bool] = False
626626
"""If True, debug messages will be printed."""
627+
comment: Optional[str] = None
628+
"""Edit comment for the page history, applied to every uploaded page."""
627629

628630
class Config:
629631
arbitrary_types_allowed = True
@@ -663,7 +665,7 @@ def upload_page_(page, index: int = None):
663665
f"WtSite from which this method is called from "
664666
f"are not matching!"
665667
)
666-
page.edit()
668+
page.edit(param.comment)
667669

668670
if index is None:
669671
print(f"Uploaded page to {page.get_url()}.")

‎tests/test_wtsite_upload_page.py‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""Unit tests for WtSite.upload_page().
2+
3+
Regression guard for #112: an edit comment supplied via UploadPageParam must
4+
reach WtPage.edit(), so bulk uploads are attributable in the page history.
5+
"""
6+
7+
import threading
8+
9+
from osw.wtsite import WtPage, WtSite
10+
11+
12+
class _FakeSite:
13+
"""Stands in for mwclient.Site, only get_url() touches it."""
14+
15+
host = "example.org"
16+
17+
18+
def _make_fake_wtsite():
19+
"""A WtSite that performs no network calls."""
20+
ws = WtSite.__new__(WtSite)
21+
ws._session_lock = threading.RLock()
22+
ws._site = _FakeSite()
23+
return ws
24+
25+
26+
def _make_page(wtsite, title, recorder, monkeypatch):
27+
page = WtPage(wtSite=wtsite, title=title, do_init=False)
28+
monkeypatch.setattr(page, "edit", lambda comment=None: recorder.append(comment))
29+
return page
30+
31+
32+
def test_upload_page_forwards_comment(monkeypatch):
33+
ws = _make_fake_wtsite()
34+
comments = []
35+
page = _make_page(ws, "Item:OSW123", comments, monkeypatch)
36+
37+
ws.upload_page(WtSite.UploadPageParam(pages=page, comment="[bot edit] import"))
38+
39+
assert comments == ["[bot edit] import"]
40+
41+
42+
def test_upload_page_forwards_comment_to_every_page(monkeypatch):
43+
ws = _make_fake_wtsite()
44+
comments = []
45+
pages = [_make_page(ws, f"Item:OSW{i}", comments, monkeypatch) for i in range(3)]
46+
47+
ws.upload_page(WtSite.UploadPageParam(pages=pages, comment="same for all"))
48+
49+
assert comments == ["same for all"] * 3
50+
51+
52+
def test_upload_page_without_comment_passes_none(monkeypatch):
53+
"""Default behaviour is unchanged: edit() is called with no comment."""
54+
ws = _make_fake_wtsite()
55+
comments = []
56+
page = _make_page(ws, "Item:OSW123", comments, monkeypatch)
57+
58+
ws.upload_page(page)
59+
60+
assert comments == [None]

0 commit comments

Comments
 (0)