-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathtest_parsers.py
331 lines (275 loc) · 11.8 KB
/
test_parsers.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import json
from contextlib import contextmanager
from pathlib import Path
from unittest import mock
import pytest
from django_dynamic_fixture import get
from readthedocs.builds.storage import BuildMediaFileSystemStorage
from readthedocs.projects.constants import GENERIC, MKDOCS, SPHINX
from readthedocs.projects.models import Feature, HTMLFile, Project
data_path = Path(__file__).parent.resolve() / 'data'
@pytest.mark.django_db
@pytest.mark.search
class TestParsers:
def setup_method(self):
self.feature = get(
Feature,
feature_id=Feature.INDEX_FROM_HTML_FILES,
)
self.project = get(
Project,
slug='test',
main_language_project=None,
)
self.version = self.project.versions.first()
def _mock_open(self, content):
@contextmanager
def f(*args, **kwargs):
read_mock = mock.MagicMock()
read_mock.read.return_value = content
yield read_mock
return f
@mock.patch.object(BuildMediaFileSystemStorage, 'exists')
@mock.patch.object(BuildMediaFileSystemStorage, 'open')
def test_mkdocs_default_theme(self, storage_open, storage_exists):
local_path = data_path / 'mkdocs/in/mkdocs-1.1/'
storage_exists.return_value = True
self.project.feature_set.add(self.feature)
self.version.documentation_type = MKDOCS
self.version.save()
parsed_json = []
all_files = [
'index.html',
'404.html',
'configuration.html',
'no-title.html',
'no-main-header.html',
]
for file_name in all_files:
file = local_path / file_name
storage_open.reset_mock()
storage_open.side_effect = self._mock_open(file.open().read())
file = get(
HTMLFile,
project=self.project,
version=self.version,
path=file_name,
)
parsed_json.append(file.processed_json)
expected_json = json.load(open(data_path / 'mkdocs/out/mkdocs-1.1.json'))
assert parsed_json == expected_json
@mock.patch.object(BuildMediaFileSystemStorage, 'exists')
@mock.patch.object(BuildMediaFileSystemStorage, 'open')
def test_mkdocs_gitbook_theme(self, storage_open, storage_exists):
file = data_path / 'mkdocs/in/gitbook/index.html'
storage_exists.return_value = True
self.project.feature_set.add(self.feature)
self.version.documentation_type = MKDOCS
self.version.save()
storage_open.side_effect = self._mock_open(file.open().read())
file = get(
HTMLFile,
project=self.project,
version=self.version,
path='index.html',
)
parsed_json = [file.processed_json]
expected_json = json.load(open(data_path / 'mkdocs/out/gitbook.json'))
assert parsed_json == expected_json
@mock.patch.object(BuildMediaFileSystemStorage, 'exists')
@mock.patch.object(BuildMediaFileSystemStorage, 'open')
def test_mkdocs_material_theme(self, storage_open, storage_exists):
file = data_path / 'mkdocs/in/material/index.html'
storage_exists.return_value = True
self.project.feature_set.add(self.feature)
self.version.documentation_type = MKDOCS
self.version.save()
storage_open.side_effect = self._mock_open(file.open().read())
file = get(
HTMLFile,
project=self.project,
version=self.version,
path='index.html',
)
parsed_json = [file.processed_json]
expected_json = json.load(open(data_path / 'mkdocs/out/material.json'))
assert parsed_json == expected_json
@mock.patch.object(BuildMediaFileSystemStorage, 'exists')
@mock.patch.object(BuildMediaFileSystemStorage, 'open')
def test_mkdocs_windmill_theme(self, storage_open, storage_exists):
file = data_path / 'mkdocs/in/windmill/index.html'
storage_exists.return_value = True
self.project.feature_set.add(self.feature)
self.version.documentation_type = MKDOCS
self.version.save()
storage_open.side_effect = self._mock_open(file.open().read())
file = get(
HTMLFile,
project=self.project,
version=self.version,
path='index.html',
)
parsed_json = [file.processed_json]
expected_json = json.load(open(data_path / 'mkdocs/out/windmill.json'))
assert parsed_json == expected_json
@mock.patch.object(BuildMediaFileSystemStorage, 'exists')
@mock.patch.object(BuildMediaFileSystemStorage, 'open')
def test_mkdocs_readthedocs_theme(self, storage_open, storage_exists):
self.project.feature_set.add(self.feature)
storage_exists.return_value = True
self.version.documentation_type = MKDOCS
self.version.save()
local_path = data_path / 'mkdocs/in/readthedocs-1.1/'
parsed_json = []
for file_name in ['index.html', '404.html', 'versions.html']:
file = local_path / file_name
storage_open.reset_mock()
storage_open.side_effect = self._mock_open(file.open().read())
file = get(
HTMLFile,
project=self.project,
version=self.version,
path=file_name,
)
parsed_json.append(file.processed_json)
expected_json = json.load(open(data_path / 'mkdocs/out/readthedocs-1.1.json'))
assert parsed_json == expected_json
@mock.patch.object(BuildMediaFileSystemStorage, 'exists')
@mock.patch.object(BuildMediaFileSystemStorage, 'open')
def test_sphinx(self, storage_open, storage_exists):
json_file = data_path / 'sphinx/in/page.json'
html_content = data_path / 'sphinx/in/page.html'
json_content = json.load(json_file.open())
json_content['body'] = html_content.open().read()
storage_open.side_effect = self._mock_open(
json.dumps(json_content)
)
storage_exists.return_value = True
self.version.documentation_type = SPHINX
self.version.save()
page_file = get(
HTMLFile,
project=self.project,
version=self.version,
path='page.html',
)
parsed_json = page_file.processed_json
expected_json = json.load(open(data_path / 'sphinx/out/page.json'))
assert parsed_json == expected_json
@mock.patch.object(BuildMediaFileSystemStorage, 'exists')
@mock.patch.object(BuildMediaFileSystemStorage, 'open')
def test_sphinx_page_without_title(self, storage_open, storage_exists):
json_file = data_path / 'sphinx/in/no-title.json'
html_content = data_path / 'sphinx/in/no-title.html'
json_content = json.load(json_file.open())
json_content['body'] = html_content.open().read()
storage_open.side_effect = self._mock_open(
json.dumps(json_content)
)
storage_exists.return_value = True
self.version.documentation_type = SPHINX
self.version.save()
page_file = get(
HTMLFile,
project=self.project,
version=self.version,
path='no-title.html',
)
parsed_json = page_file.processed_json
expected_json = json.load(open(data_path / 'sphinx/out/no-title.json'))
assert parsed_json == expected_json
@mock.patch.object(BuildMediaFileSystemStorage, "exists")
@mock.patch.object(BuildMediaFileSystemStorage, "open")
def test_sphinx_httpdomain(self, storage_open, storage_exists):
json_file = data_path / "sphinx/in/httpdomain.json"
html_content = data_path / "sphinx/in/httpdomain.html"
json_content = json.load(json_file.open())
json_content["body"] = html_content.open().read()
storage_open.side_effect = self._mock_open(json.dumps(json_content))
storage_exists.return_value = True
self.version.save()
page_file = get(
HTMLFile,
project=self.project,
version=self.version,
path="httpdomain.html",
)
parsed_json = page_file.processed_json
expected_json = json.load(open(data_path / "sphinx/out/httpdomain.json"))
assert parsed_json == expected_json
@mock.patch.object(BuildMediaFileSystemStorage, "exists")
@mock.patch.object(BuildMediaFileSystemStorage, "open")
def test_sphinx_autodoc(self, storage_open, storage_exists):
# Source:
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#directive-automodule
json_file = data_path / "sphinx/in/autodoc.json"
html_content = data_path / "sphinx/in/autodoc.html"
json_content = json.load(json_file.open())
json_content["body"] = html_content.open().read()
storage_open.side_effect = self._mock_open(json.dumps(json_content))
storage_exists.return_value = True
self.version.save()
page_file = get(
HTMLFile,
project=self.project,
version=self.version,
path="autodoc.html",
)
parsed_json = page_file.processed_json
expected_json = json.load(open(data_path / "sphinx/out/autodoc.json"))
assert parsed_json == expected_json
@mock.patch.object(BuildMediaFileSystemStorage, "exists")
@mock.patch.object(BuildMediaFileSystemStorage, "open")
def test_sphinx_requests(self, storage_open, storage_exists):
# Source:
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#directive-automodule
json_file = data_path / "sphinx/in/requests.json"
html_content = data_path / "sphinx/in/requests.html"
json_content = json.load(json_file.open())
json_content["body"] = html_content.open().read()
storage_open.side_effect = self._mock_open(json.dumps(json_content))
storage_exists.return_value = True
self.version.save()
page_file = get(
HTMLFile,
project=self.project,
version=self.version,
path="requests.html",
)
parsed_json = page_file.processed_json
expected_json = json.load(open(data_path / "sphinx/out/requests.json"))
assert parsed_json == expected_json
@mock.patch.object(BuildMediaFileSystemStorage, "exists")
@mock.patch.object(BuildMediaFileSystemStorage, "open")
def test_generic_simple_page(self, storage_open, storage_exists):
file = data_path / "generic/in/basic.html"
storage_exists.return_value = True
self.version.documentation_type = GENERIC
self.version.save()
storage_open.side_effect = self._mock_open(file.open().read())
file = get(
HTMLFile,
project=self.project,
version=self.version,
path="basic.html",
)
parsed_json = [file.processed_json]
expected_json = json.load(open(data_path / "generic/out/basic.json"))
assert parsed_json == expected_json
@mock.patch.object(BuildMediaFileSystemStorage, "exists")
@mock.patch.object(BuildMediaFileSystemStorage, "open")
def test_generic_pelican_default_theme(self, storage_open, storage_exists):
file = data_path / "pelican/in/default/index.html"
storage_exists.return_value = True
self.version.documentation_type = GENERIC
self.version.save()
storage_open.side_effect = self._mock_open(file.open().read())
file = get(
HTMLFile,
project=self.project,
version=self.version,
path="index.html",
)
parsed_json = [file.processed_json]
expected_json = json.load(open(data_path / "pelican/out/default.json"))
assert parsed_json == expected_json