|
| 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