-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtest.py
256 lines (235 loc) · 6.13 KB
/
test.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
245
246
247
248
249
250
251
252
253
254
255
256
import json
import unittest
import mwparserfromhell as mwp
from wikitables import ftag, WikiTable
class TestWikiTables(unittest.TestCase):
def _load(self, source, lang='en'):
raw_tables = mwp.parse(source).filter_tags(matches=ftag('table'))
return WikiTable("Test Table", raw_tables[0], lang)
def _compare(self, table, expected):
self.assertEqual(len(table.rows), len(expected))
self.assertSetEqual(set(table.head), set(expected[0].keys()))
rowsdata = json.loads(table.json())
for expected_value, row_data in zip(expected, rowsdata):
self.assertDictEqual(expected_value, row_data)
def test_simple_table(self):
source = """
{| class="wikitable"
|-
! Column 1 header !! Column 2 header
|-
| Row 1 Column 1 || Row 1 Column 2
|-
| Row 2 Column 1 || Row 2 Column 1
|}
"""
expected = [
{
"Column 1 header": "Row 1 Column 1",
"Column 2 header": "Row 1 Column 2"
},
{
"Column 1 header": "Row 2 Column 1",
"Column 2 header": "Row 2 Column 1"
}
]
table = self._load(source)
self._compare(table, expected)
def test_complex_table(self):
source = """
{| class="wikitable sortable"
! 2018<br>rank
! [[Municipalities of Brazil|City]]
! [[States of Brazil|State]]
! 2018<br>Estimate
! 2010<br>Census
! Change
|-
! 1
|'''''[[São Paulo]]'''''
| {{flag|São Paulo}}
| {{change|invert=on|12176866|10659386}}
|-
! 2
| '''''[[Rio de Janeiro]]'''''
| {{flag|Rio de Janeiro}}
| {{change|invert=on|6688927|5940224}}
|}
"""
expected = [
{
"2018rank": 1,
"City": "São Paulo",
"State": "São Paulo",
"2018Estimate": 12176866,
"2010Census": 10659386,
"Change": 14.236092022561152
},
{
"2018rank": 2,
"City": "Rio de Janeiro",
"State": "Rio de Janeiro",
"2018Estimate": 6688927,
"2010Census": 5940224,
"Change": 12.603952308869172
}
]
table = self._load(source)
self._compare(table, expected)
def test_flag_template(self):
source = """
{| class="wikitable"
! Year
! Name
! Nationality
! Citation
|-
| 1978
| [[Carl Djerassi]]
| {{AUT}} / {{USA}}
| for his work in bioorganic chemistry.
|-
| 1980
| [[Henry Eyring (chemist)|Henry Eyring]]
| {{MEX}} / {{USA}}
| for his development of absolute rate theory.
|}
"""
expected = [
{
"Year": 1978,
"Name": "Carl Djerassi",
"Nationality": "Austria / United States",
"Citation": "for his work in bioorganic chemistry."
},
{
"Year": 1980,
"Name": "Henry Eyring",
"Nationality": "Mexico / United States",
"Citation": "for his development of absolute rate theory."
}
]
table = self._load(source)
self._compare(table, expected)
def test_flag_template_other_language(self):
source = """
{| class="wikitable"
! Year
! Name
! Nationality
! Citation
|-
| 1978
| [[Carl Djerassi]]
| {{AUT}} / {{USA}}
| for his work in bioorganic chemistry.
|-
| 1980
| [[Henry Eyring (chemist)|Henry Eyring]]
| {{MEX}} / {{USA}}
| for his development of absolute rate theory.
|}
"""
expected = [
{
"Year": 1978,
"Name": "Carl Djerassi",
"Nationality": "Österreich / Vereinigte Staaten",
"Citation": "for his work in bioorganic chemistry."
},
{
"Year": 1980,
"Name": "Henry Eyring",
"Nationality": "Mexiko / Vereinigte Staaten",
"Citation": "for his development of absolute rate theory."
}
]
table = self._load(source, 'de')
self._compare(table, expected)
def test_empty_fields(self):
source = """
{| class="wikitable sortable" border="1" style="font-size:85%;"
! Archi-<br>tecture
! Bits
! Version
! Intro-<br>duced
! Max #<br>[[operand]]s
! Type
! Design <!-- Design Strategy/Philosophy -->
! [[Processor register|Registers]]<br>(excluding FP/vector)
! Instruction encoding
! [[Branch (computer science)|Branch]] evaluation
! [[Endianness|Endian-<br>ness]]
! Extensions
! Open
! Royalty<br>free
|-
| [[MOS Technology 6502|6502]]
| 8
|
| 1975
| 1
| Register Memory
| CISC
| 3
| Variable <small>(8- to 32-bit)</small>
| Condition register
| Little
|
|
|
|-
| 68000 / [[Motorola 68000 series|680x0]]
| 32
|
| 1979
| 2
| Register Memory
| [[Complex instruction set computer|CISC]]
| 8 data and 8 address
| Variable
| Condition register
| Big
|
|
|
|}
"""
expected = [
{
"Archi-tecture": 6502,
"Bits": 8,
"Branch evaluation": "Condition register",
"Design": "CISC",
"Endian-ness": "Little",
"Extensions": "",
"Instruction encoding": "Variable (8- to 32-bit)",
"Intro-duced": 1975,
"Max #operands": 1,
"Open": "",
"Registers(excluding FP/vector)": 3,
"Royaltyfree": "",
"Type": "Register Memory",
"Version": ""
},
{
"Archi-tecture": "68000 / 680x0",
"Bits": 32,
"Branch evaluation": "Condition register",
"Design": "CISC",
"Endian-ness": "Big",
"Extensions": "",
"Instruction encoding": "Variable",
"Intro-duced": 1979,
"Max #operands": 2,
"Open": "",
"Registers(excluding FP/vector)": "8 data and 8 address",
"Royaltyfree": "",
"Type": "Register Memory",
"Version": ""
}
]
table = self._load(source)
self._compare(table, expected)
if __name__ == '__main__':
unittest.main()