Newer
Older
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
# encoding=utf-8
if __name__ == "__main__":
print
print "Note: Running in stand-alone mode. Consult the README"
print " (section 'Contributing') for advice on running tests."
print
import unittest
import pytest
import logging
import requests
import responses
import mock
from mwclient.page import Page
try:
import json
except ImportError:
import simplejson as json
class TestPage(unittest.TestCase):
def setUp(self):
pass
@mock.patch('mwclient.client.Site')
def test_api_call_on_page_init(self, mock_site):
# Check that site.api() is called once on Page init
title = 'Some page'
mock_site.api.return_value = {
'query': {'pages': {'1': {}}}
}
page = Page(mock_site, title)
# test that Page called site.api with the right parameters
mock_site.api.assert_called_once_with('query', inprop='protection', titles=title, prop='info')
@mock.patch('mwclient.client.Site')
def test_nonexisting_page(self, mock_site):
# Check that API response results in page.exists being set to False
title = 'Some nonexisting page'
mock_site.api.return_value = {
'query': {'pages': {'-1': {'missing': ''}}}
}
page = Page(mock_site, title)
assert page.exists is False
@mock.patch('mwclient.client.Site')
def test_existing_page(self, mock_site):
# Check that API response results in page.exists being set to True
title = 'Norge'
mock_site.api.return_value = {
'query': {'pages': {'728': {}}}
}
page = Page(mock_site, title)
assert page.exists is True
@mock.patch('mwclient.client.Site')
def test_pageprops(self, mock_site):
# Check that variouse page props are read correctly from API response
title = 'Some page'
mock_site.api.return_value = {
'query': {
'pages': {
'728': {
'contentmodel': 'wikitext',
'counter': '',
'lastrevid': 13355471,
'length': 58487,
'ns': 0,
'pageid': 728,
'pagelanguage': 'nb',
'protection': [],
'title': title,
'touched': '2014-09-14T21:11:52Z'
}
}
}
}
page = Page(mock_site, title)
assert page.exists is True
assert page.redirect is False
assert page.revision == 13355471
assert page.length == 58487
assert page.namespace == 0
assert page.name == title
assert page.page_title == title
@mock.patch('mwclient.client.Site')
def test_proection_levels(self, mock_site):
# If page is protected, check that protection is parsed correctly
title = 'Some page'
mock_site.api.return_value = {
'query': {
'pages': {
'728': {
'protection': [
{
'expiry': 'infinity',
'level': 'autoconfirmed',
'type': 'edit'
},
{
'expiry': 'infinity',
'level': 'sysop',
'type': 'move'
}
}
}
}
}
mock_site.rights = ['read', 'edit', 'move']
page = Page(mock_site, title)
assert page.protection == {'edit': ('autoconfirmed', 'infinity'), 'move': ('sysop', 'infinity')}
assert page.can('read') is True
assert page.can('edit') is False # User does not have 'autoconfirmed' right
assert page.can('move') is False # User does not have 'sysop' right
mock_site.rights = ['read', 'edit', 'move', 'autoconfirmed']
assert page.can('edit') is True # User has 'autoconfirmed' right
assert page.can('move') is False # User doesn't have 'sysop' right
mock_site.rights = ['read', 'edit', 'move', 'autoconfirmed', 'editprotected']
assert page.can('edit') is True # User has 'autoconfirmed' right
assert page.can('move') is True # User has 'sysop' right
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
@mock.patch('mwclient.client.Site')
def test_redirect(self, mock_site):
# Check that page.redirect is set correctly
title = 'Some redirect page'
mock_site.api.return_value = {
"query": {
"pages": {
"796917": {
"contentmodel": "wikitext",
"counter": "",
"lastrevid": 9342494,
"length": 70,
"ns": 0,
"pageid": 796917,
"pagelanguage": "nb",
"protection": [],
"redirect": "",
"title": title,
"touched": "2014-08-29T22:25:15Z"
}
}
}
}
page = Page(mock_site, title)
assert page.exists is True
assert page.redirect is True
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
@mock.patch('mwclient.client.Site')
def test_page_get_text(self, mock_site):
# Check that page.text() works, and that a correct API call is made
title = 'Some page'
some_text = 'Hello world'
mock_site.api.return_value = {'query': {'pages': {'1': {'title': title}}}}
mock_site.rights = ['read']
page = Page(mock_site, title)
mock_site.api.return_value = {
'query': {
'pages': {
'796917': {
'ns': 0,
'pageid': 796917,
'revisions': [
{
'*': some_text,
'contentformat': 'text/x-wiki',
'contentmodel': 'wikitext',
'timestamp': '2011-09-12T12:36:08Z'
}
],
'title': title
}
}
}
}
text = page.text()
# test that Page called site.api with the right parameters
mock_site.api.assert_called_with('query', ('prop', 'revisions'), ('rvdir', 'older'), ('titles', title), ('rvprop', 'content|timestamp'), ('rvlimit', '1'))
assert text == some_text
@mock.patch('mwclient.client.Site')
def test_section_get_text(self, mock_site):
# Check that the 'rvsection' parameter is sent to the API
title = 'Some page'
some_text = 'Hello world'
mock_site.api.return_value = {'query': {'pages': {'1': {'title': title}}}}
mock_site.rights = ['read']
page = Page(mock_site, title)
mock_site.api.return_value = {
'query': {
'pages': {
'796917': {
'ns': 0,
'pageid': 796917,
'revisions': [
{
'*': some_text,
'contentformat': 'text/x-wiki',
'contentmodel': 'wikitext',
'timestamp': '2011-09-12T12:36:08Z'
}
],
'title': title
}
}
}
}
text = page.text(section=0)
# test that Page called site.api with the right parameters
mock_site.api.assert_called_with('query', ('prop', 'revisions'), ('rvdir', 'older'), ('titles', title), ('rvsection', '0'), ('rvprop', 'content|timestamp'), ('rvlimit', '1'))
if __name__ == '__main__':
unittest.main()