-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_wbformat.py
45 lines (38 loc) · 1.65 KB
/
test_wbformat.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
from markupsafe import Markup
import mwapi # type: ignore
import pytest
import wbformat
@pytest.mark.filterwarnings('ignore::bs4.MarkupResemblesLocatorWarning')
def test_format_value_escapes_html():
session = mwapi.Session('https://test.wikidata.org',
user_agent='Ranker unit tests')
value = {'value': '<script>alert("!Mediengruppe Bitnik");</script>',
'type': 'string'}
expected = Markup(r'<script>alert("!Mediengruppe'
r' Bitnik");</script>')
assert wbformat.format_value(session, 'P95', value) == expected
def test_format_entity_P31():
session = mwapi.Session('https://www.wikidata.org',
user_agent='Ranker unit tests')
expected = Markup(r'<a title="Property:P31"'
r' href="https://www.wikidata.org/wiki'
r'/Property:P31">instance of</a>')
assert wbformat.format_entity(session, 'P31') == expected
def test_prefetch_entities():
class FakeSession:
host = 'host'
get_calls = 0
def get(self, ids, **kwargs):
self.get_calls += 1
return {'wbformatentities': {
entity_id: f'label of {entity_id}' for entity_id in ids
}}
session = FakeSession()
entity_ids = ([f'P{id}' for id in range(1, 60)]
+ [f'Q{id}' for id in range(1, 60)])
wbformat.prefetch_entities(session, entity_ids)
assert session.get_calls == 3
for id in range(1, 60):
for entity_id in [f'P{id}', f'Q{id}']:
key = ('host', entity_id)
assert wbformat.format_entity_cache[key] == f'label of {entity_id}'