-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_csv2wiki.py
159 lines (133 loc) · 4.98 KB
/
test_csv2wiki.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Tests for csv2wiki.
#
# Copyright (C) 2017, 2018 Open Tech Strategies, LLC
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__doc__ = """These tests check that csv2wiki is putting the right things
in the mediawiki instance. If mediawiki isn't up, these tests will fail.
"""
config_fname = "../csv2wiki-config"
sanitized_fname = ".../path/to/original_input.csv"
import os
import re
# We named csv2wiki without the .py extension, so these next lines
# are basically just a regular old import:
import imp
if os.path.isdir('csv2wiki'):
csv2wiki = imp.load_source('csv2wiki', 'csv2wiki/csv2wiki')
else:
csv2wiki = imp.load_source('csv2wiki', 'csv2wiki')
from bs4 import BeautifulSoup
import contextlib
import os
import urllib.request, urllib.error, urllib.parse
# Change working directory context manager
@contextlib.contextmanager
def cwd(direc):
curdir= os.getcwd()
os.chdir(direc)
try: yield
finally: os.chdir(curdir)
## Load config file
with cwd(os.path.dirname(os.path.abspath(__file__))):
config = csv2wiki.parse_config_file(config_fname)
mediawiki_url = ""
def get_mediawiki_url():
# Get wiki access url
global mediawiki_url
if not mediawiki_url:
req = urllib.request.urlopen(config['wiki_url'])
mediawiki_url = '/'.join(req.geturl().split('/')[:-1]) + '/'
return mediawiki_url
created = False
def create_pages():
"""Do the conversion once and then we can run tests on the results. Note the pare=1. There should be a test option for quick vs thorough that adjusts pare."""
global created
if not created:
null_as_value = False
pare = 1
with cwd(os.path.dirname(os.path.abspath(__file__))):
config = csv2wiki.parse_config_file(config_fname)
csv_in = csv2wiki.CSVInput(sanitized_fname, config)
wiki_sess = csv2wiki.WikiSession(config)
csv2wiki.create_pages(wiki_sess, csv_in, null_as_value, pare)
created = True
def fetch_page(name):
"""Pull page named NAME from our mediawiki instance and return it as a
string"""
create_pages()
return urllib.request.urlopen(get_mediawiki_url() + name).read().decode("utf-8")
def fetch_entry(num):
"""Given an entry number, fetch it from the mediawiki"""
toc = BeautifulSoup(fetch_page('Test_TOC'), "html.parser")
toc.select("<li>")
toc = None
def fetch_toc_soup():
global toc
if not toc:
return BeautifulSoup(fetch_page('Test_TOC'), "html.parser")
return toc
def test_config_file():
"""Make sure the config file is there and we find an expected field."""
with cwd(os.path.dirname(os.path.abspath(__file__))):
config = csv2wiki.parse_config_file(config_fname)
assert config['wiki_url'] != ""
def test_toc():
"""Make sure there is a table of contents."""
html = fetch_page('Test_TOC')
assert html != ""
def test_categories():
"""Make sure categories got added to the TOC"""
toc = fetch_toc_soup()
categories = toc.select("span.mw-headline")
assert categories != []
def test_entries():
"""Make sure there are entries and we can download them."""
toc = fetch_toc_soup()
entries = toc.find('div', id='mw-content-text').select("a")
prefix = '/'+'/'.join(get_mediawiki_url().split('/')[3:])
if not prefix.endswith('/'):
prefix += '/'
assert len(entries) > 0
fetched_one = False
for e in entries:
href = e.get('href', "")
if not href:
continue
if not prefix in href:
continue
url = "Entry"+href.split("/Entry")[1]
print("Trying to fetch " + url)
html = fetch_page(url)
assert html != ""
# test wikify_anchors
if "<a " in html:
for lt in re.findall(r"<a.*>", html):
print(lt)
assert not "<a href=" in html
# Test for <tbody>
assert not "<tbody" in html
fetched_one = True
print("If we didn't actually fetch one, the prefix detection for entry items is not finding any entry items, which is a problem.")
assert fetched_one == True
def test_input_csv():
"""The sanitized csv input shouldn't have any "%lt;a href" anchors in
it."""
with cwd(os.path.dirname(os.path.abspath(__file__))):
with open(sanitized_fname) as INF:
csv = INF.read()
assert not "<a href" in csv