forked from wikimedia/pywikibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeletionbot_tests.py
executable file
·146 lines (113 loc) · 4.38 KB
/
deletionbot_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
"""Tests for scripts/delete.py."""
#
# (C) Pywikibot team, 2014-2024
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations
import unittest
from contextlib import suppress
import pywikibot
from scripts import delete
from tests.aspects import ScriptMainTestCase
from tests.utils import empty_sites
class TestDeletionBotWrite(ScriptMainTestCase):
"""Test deletionbot script."""
family = 'wikipedia'
code = 'test'
rights = 'undelete'
write = True
def test_delete(self):
"""Test deletionbot on the test wiki."""
site = self.get_site()
cat = pywikibot.Category(site, 'Pywikibot Delete Test')
delete.main('-cat:Pywikibot_Delete_Test', '-always')
self.assertIsEmpty(list(cat.members()))
delete.main('-page:User:Unicodesnowman/DeleteTest1', '-always',
'-undelete', '-summary:pywikibot unit tests')
delete.main('-page:User:Unicodesnowman/DeleteTest2', '-always',
'-undelete', '-summary:pywikibot unit tests')
self.assertLength(list(cat.members()), 2)
def test_undelete_existing(self):
"""Test undeleting an existing page."""
site = self.get_site()
p1 = pywikibot.Page(site, 'User:Unicodesnowman/ExistingPage')
if not p1.exists():
p1.text = 'pywikibot unit test page'
p1.save('unit test')
delete.main('-page:User:Unicodesnowman/ExistingPage', '-always',
'-undelete', '-summary:pywikibot unit tests')
class TestDeletionBotUser(ScriptMainTestCase):
"""Test deletionbot as a user (no 'deletion' right)."""
family = 'wikipedia'
code = 'test'
write = True
@classmethod
def setUpClass(cls):
"""Set up test class."""
super().setUpClass()
cls.page = pywikibot.Page(cls.site, 'User:Unicodesnowman/DeleteMark')
if not cls.page.exists():
cls.save_page() # pragma: no cover
@classmethod
def tearDownClass(cls):
"""Tear down test class."""
cls.save_page()
super().tearDownClass()
@classmethod
def save_page(cls):
"""Reset the test page content."""
cls.page.text = 'Pywikibot deletion test.'
cls.page.save('Pywikibot unit test')
@unittest.expectedFailure # T367299
def test_delete_mark(self):
"""Test marking User:Unicodesnowman/DeleteMark for deletion."""
delete.main('-page:User:Unicodesnowman/DeleteMark', '-always',
'-summary:pywikibot unit test. Do NOT actually delete.')
text = self.page.get(force=True)
self.assertEqual(
text,
'{{delete|1=pywikibot unit test. Do NOT actually delete.}}\n'
'Pywikibot deletion test.')
class TestDeletionBot(ScriptMainTestCase):
"""Test deletionbot with patching to make it non-write."""
family = 'wikipedia'
code = 'test'
cached = True
login = True
delete_args = []
undelete_args = []
def setUp(self):
"""Set up unit test."""
self._original_delete = pywikibot.Page.delete
self._original_undelete = pywikibot.Page.undelete
pywikibot.Page.delete = delete_dummy
pywikibot.Page.undelete = undelete_dummy
super().setUp()
def tearDown(self):
"""Tear down unit test."""
pywikibot.Page.delete = self._original_delete
pywikibot.Page.undelete = self._original_undelete
super().tearDown()
def test_dry(self):
"""Test dry run of bot."""
with empty_sites():
delete.main('-page:Main Page', '-always', '-summary:foo')
self.assertEqual(self.delete_args,
['[[Main Page]]', 'foo', False, True, True])
with empty_sites():
delete.main(
'-page:FoooOoOooO', '-always', '-summary:foo', '-undelete')
self.assertEqual(self.undelete_args, ['[[FoooOoOooO]]', 'foo'])
def delete_dummy(self, reason, prompt, mark, automatic_quit):
"""Dummy delete method."""
TestDeletionBot.delete_args = [self.title(as_link=True), reason, prompt,
mark, automatic_quit]
return 0
def undelete_dummy(self, reason):
"""Dummy undelete method."""
TestDeletionBot.undelete_args = [self.title(as_link=True), reason]
if __name__ == '__main__':
with suppress(SystemExit):
unittest.main()