/
test_routes.py
357 lines (318 loc) · 15.3 KB
/
test_routes.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""
Test for routes functions : ensure responses are correct with mocked call to API
"""
from .resources import NemoResource
from .test_controller import NemoTestControllers
from flask_nemo import Nemo
from flask_nemo.plugins.default import Breadcrumb
from flask import Markup, Flask
from lxml import etree
from mock import Mock, patch, call
import MyCapytain
from jinja2.exceptions import TemplateNotFound
class NemoTestRoutes(NemoResource):
""" Test Suite for Nemo
"""
def test_route_index(self):
""" Check that index return the template
"""
self.assertEqual(self.nemo.r_index(), {"template": "main::index.html"})
def test_route_collection(self):
""" Test return values of route collection (list of textgroups
"""
with patch('requests.get', return_value=self.getCapabilities) as patched:
view = self.nemo.r_collection("latinLit")
self.assertEqual(view["template"], "main::textgroups.html")
self.assertEqual(len(view["textgroups"]), 3)
self.assertIn("urn:cts:latinLit:phi1294", [str(textgroup.urn) for textgroup in view["textgroups"]])
self.assertIsInstance(view["textgroups"][0], MyCapytain.resources.inventory.TextGroup)
def test_route_texts(self):
""" Test return values of route texts (list of texts for a textgroup
"""
with patch('requests.get', return_value=self.getCapabilities) as patched:
view = self.nemo.r_texts("latinLit", "phi1294")
self.assertEqual(view["template"], "main::texts.html")
self.assertEqual(len(view["texts"]), 2)
self.assertEqual(
sorted([str(view["texts"][0].urn), str(view["texts"][1].urn)]),
sorted(["urn:cts:latinLit:phi1294.phi002.perseus-lat2", "urn:cts:latinLit:phi1294.phi002.perseus-eng2"])
)
self.assertIsInstance(view["texts"][0], MyCapytain.resources.inventory.Text)
def test_route_version_chunker_replacement(self):
""" Try to get valid reffs
"""
urn = "urn:cts:latinLit:phi1294.phi002.perseus-lat2"
def chunker(text, level):
self.assertIsInstance(text, MyCapytain.resources.inventory.Text)
self.assertEqual(str(text.urn), "urn:cts:latinLit:phi1294.phi002.perseus-lat2")
return True
nemo = Nemo(
api_url=NemoTestControllers.endpoint,
inventory="annotsrc",
chunker={"default": chunker}
)
with patch('requests.get', return_value=self.getValidReff) as patched:
view = nemo.r_version("latinLit", "phi1294", "phi002", "perseus-lat2")
self.assertIsInstance(view["version"], MyCapytain.resources.inventory.Text)
patched.assert_called_once_with(
NemoTestControllers.endpoint,
params={
"inv": "annotsrc",
"request": "GetCapabilities"
}
)
self.assertEqual(view["reffs"], True)
def test_route_version_default_chunker(self):
""" Try to get valid reffs
"""
urn = "urn:cts:latinLit:phi1294.phi002.perseus-lat2"
with patch('requests.get', return_value=self.getValidReff) as patched:
view = self.nemo.r_version("latinLit", "phi1294", "phi002", "perseus-lat2")
self.assertIsInstance(view["version"], MyCapytain.resources.inventory.Text)
self.assertEqual(view["reffs"][0], ("1.pr.1", "1.pr.1"))
def test_route_text_without_transform(self):
""" Try to get valid reffs
"""
urn = "urn:cts:latinLit:phi1294.phi002.perseus-lat2"
with patch('requests.get', return_value=self.getValidReff) as patched:
view = self.nemo.r_version("latinLit", "phi1294", "phi002", "perseus-lat2")
self.assertIsInstance(view["version"], MyCapytain.resources.inventory.Text)
self.assertEqual(view["reffs"][0], ("1.pr.1", "1.pr.1"))
def test_route_passage_without_xslt(self):
nemo = Nemo(
api_url=NemoTestControllers.endpoint,
inventory="annotsrc"
)
with patch('requests.get', return_value=self.getPassage_Route) as patched:
view = self.nemo.r_passage("latinLit", "phi1294", "phi002", "perseus-lat2", "1.pr.1")
self.assertEqual(view["template"], "main::text.html")
self.assertIsInstance(view["version"], MyCapytain.resources.inventory.Text)
self.assertEqual(str(view["version"].urn), "urn:cts:latinLit:phi1294.phi002.perseus-lat2")
self.assertEqual(view["prev"], "1.1.1")
self.assertEqual(view["next"], "1.1.3")
self.assertIsInstance(view["text_passage"], Markup)
# Reparsing xml
xml = etree.fromstring(str(view["text_passage"]))
self.assertEqual(
len(xml.xpath("//tei:body", namespaces={"tei":"http://www.tei-c.org/ns/1.0"})),
1
)
self.assertEqual(
len(xml.xpath("//tei:l", namespaces={"tei":"http://www.tei-c.org/ns/1.0"})),
6
)
def test_route_passage_with_transform(self):
""" Try with a non xslt just to be sure
"""
urn = "urn:cts:latinLit:phi1294.phi002.perseus-lat2"
def transformer(version, text):
self.assertEqual(str(version.urn), "urn:cts:latinLit:phi1294.phi002.perseus-lat2")
self.assertIsInstance(text, etree._Element)
return "<a>Hello</a>"
nemo = Nemo(
api_url=NemoTestControllers.endpoint,
inventory="annotsrc",
transform={"default": transformer}
)
with patch('requests.get', return_value=self.getPassage_Route) as patched:
view = nemo.r_passage("latinLit", "phi1294", "phi002", "perseus-lat2", "1.pr.1")
self.assertEqual(view["text_passage"], Markup("<a>Hello</a>"))
def test_route_passage_with_xslt(self):
nemo = Nemo(
api_url=NemoTestControllers.endpoint,
inventory="annotsrc",
transform={"default": NemoTestControllers.body_xsl}
)
with patch('requests.get', return_value=self.getPassage_Route) as patched:
view = nemo.r_passage("latinLit", "phi1294", "phi002", "perseus-lat2", "1.pr.1")
self.assertEqual(view["template"], "main::text.html")
self.assertIsInstance(view["version"], MyCapytain.resources.inventory.Text)
self.assertEqual(str(view["version"].urn), "urn:cts:latinLit:phi1294.phi002.perseus-lat2")
self.assertEqual(view["prev"], "1.1.1")
self.assertEqual(view["next"], "1.1.3")
self.assertIsInstance(view["text_passage"], Markup)
# Reparsing xml
xml = etree.fromstring(str(view["text_passage"]))
self.assertEqual(
len(xml.xpath("//tei:notbody", namespaces={"tei":"http://www.tei-c.org/ns/1.0"})),
1
)
def test_route_passage_with_urn_xslt(self):
nemo = Nemo(
api_url=NemoTestControllers.endpoint,
inventory="annotsrc",
transform={"urn:cts:latinLit:phi1294.phi002.perseus-lat2": NemoTestControllers.body_xsl}
)
with patch('requests.get', return_value=self.getPassage_Route) as patched:
view = nemo.r_passage("latinLit", "phi1294", "phi002", "perseus-lat2", "1.pr.1")
# Reparsing xml
xml = etree.fromstring(str(view["text_passage"]))
self.assertEqual(
len(xml.xpath("//tei:notbody", namespaces={"tei": "http://www.tei-c.org/ns/1.0"})),
1
)
def test_route_passage_without_urn_xslt(self):
nemo = Nemo(
api_url=NemoTestControllers.endpoint,
inventory="annotsrc",
transform={"urn:cts:latinLit:phi1294.phi002.perseus-lat3": NemoTestControllers.body_xsl}
)
with patch('requests.get', return_value=self.getPassage_Route) as patched:
view = nemo.r_passage("latinLit", "phi1294", "phi002", "perseus-lat2", "1.pr.1")
# Reparsing xml
xml = etree.fromstring(str(view["text_passage"]))
self.assertEqual(
len(xml.xpath("//tei:body", namespaces={"tei": "http://www.tei-c.org/ns/1.0"})),
1
)
def test_route_assets_404(self):
with patch('flask_nemo.abort') as abort:
self.nemo.r_assets("js", "wrong-js.js")
abort.assert_called_once_with(404)
def test_route_assets_all(self):
nemo = Nemo(
statics=["testing_data/getcapabilities.xml"],
js=["testing_data/getcapabilities.xml"],
css=["testing_data/style.css"]
)
nemo.blueprint = Mock()
nemo.register_assets()
with patch("flask_nemo.send_from_directory") as patched:
nemo.r_assets("js", "getcapabilities.xml")
nemo.r_assets("static", "getcapabilities.xml")
nemo.r_assets("css", "style.css")
patched.assert_has_calls([
call(directory="testing_data", filename="getcapabilities.xml"),
call(directory="testing_data", filename="getcapabilities.xml"),
call(directory="testing_data", filename="style.css")
])
def test_view_maker(self):
""" View maker should take care of returning a lambda using the function self.route and the function
identified by the parameter name
"""
nemo = Nemo()
nemo.route = Mock()
view = nemo.view_maker("r_collection")
view(collection="latinLit")
nemo.route.assert_called_with(nemo.r_collection, collection="latinLit")
def test_render_normal(self):
""" Render adds informations, including url parameters in a url dict
"""
with patch("requests.get", return_value=self.getCapabilities):
with patch("flask_nemo.render_template") as patched:
self.nemo.render("main::index.html", test="123", value="value", url={})
patched.assert_called_once_with(
"main::index.html",
collections={'latinLit', 'greekLit'},
test="123",
value="value",
template="main::index.html",
lang="eng",
assets=self.nemo.assets,
url={},
breadcrumbs=[]
)
def test_render_textgroups(self):
""" Render adds informations, including url parameters in a url dict
"""
with patch("requests.get", return_value=self.getCapabilities):
with patch("flask_nemo.render_template") as patched:
data = self.nemo.render(
"index.html",
test="123",
value="value",
url={
"collection": "latinLit",
"textgroup": "phi1294"
})
patched.assert_called_once_with(
"index.html",
collections={'latinLit', 'greekLit'},
test="123",
template="index.html",
value="value",
lang="eng",
assets=self.nemo.assets,
url={
"collection": "latinLit",
"textgroup": "phi1294",
},
breadcrumbs=[
{'link': '.r_collection', 'title': 'latinLit', 'args': {'collection': 'latinLit'}},
{'link': None, 'title': 'Martial', 'args': {'textgroup': 'phi1294', 'collection': 'latinLit'}}
],
textgroups=self.nemo.get_textgroups("latinLit")
)
def test_render_text(self):
""" Render adds informations, including url parameters in a url dict
"""
with patch("requests.get", return_value=self.getCapabilities):
with patch("flask_nemo.render_template") as patched:
self.nemo.render(
"index.html",
test="123",
value="value",
url={
"collection": "latinLit",
"textgroup": "phi1294",
"work": "phi002",
"text": "perseus-lat2"
})
patched.assert_called_once_with(
"index.html",
template="index.html",
collections={'latinLit', 'greekLit'},
test="123",
value="value",
lang="eng",
assets=self.nemo.assets,
url={
"collection": "latinLit",
"textgroup": "phi1294",
"work": "phi002",
"text": "perseus-lat2"
},
breadcrumbs=[
{'link': '.r_collection', 'title': 'latinLit', 'args': {'collection': 'latinLit'}},
{'link': None, 'title': 'Martial', 'args': {'textgroup': 'phi1294', 'collection': 'latinLit'}}
],
textgroups=self.nemo.get_textgroups("latinLit"),
texts=self.nemo.get_texts("latinLit", "phi1294")
)
def test_route(self):
""" nemo.route should apply fn and the args given
"""
self.nemo.render = Mock()
with patch("requests.get", return_value=self.getCapabilities):
route = self.nemo.route(self.nemo.r_collection, collection="latinLit")
self.nemo.render.assert_called_once_with(
template="main::textgroups.html",
textgroups=self.nemo.get_textgroups("latinLit"),
url={"collection": "latinLit"}
)
def test_register_route(self):
app = Flask(__name__)
nemo = Nemo(app=app, base_url="/perseus")
nemo.register()
self.assertIn("flask_nemo", app.blueprints)
rules = [(rule.rule, rule.endpoint) for rule in app.url_map.iter_rules()]
self.assertIn("/perseus/read/<collection>/<textgroup>/<work>/<version>/<passage_identifier>", [rule[0] for rule in rules])
self.assertIn("flask_nemo.r_passage", [rule[1] for rule in rules])
app = Flask(__name__)
nemo = Nemo("nemo", app=app)
nemo.register()
self.assertIn("nemo", app.blueprints)
rules = [(rule.rule, rule.endpoint) for rule in app.url_map.iter_rules()]
self.assertIn("/nemo/read/<collection>/<textgroup>/<work>/<version>/<passage_identifier>", [rule[0] for rule in rules])
self.assertIn("nemo.r_passage", [rule[1] for rule in rules])
nemo = Nemo()
self.assertEqual(nemo.register(), None)
def test_additional_template(self):
# Line 568-575
app = Flask(__name__)
nemo = Nemo(app=app, templates={"addendum": "tests/test_data/plugin_templates_main/main"})
blueprint = nemo.create_blueprint()
html, path, function = blueprint.jinja_loader.get_source("", "addendum::container.html")
self.assertIn("I am A CONTAINER ! Isn't it sweet !", html)
with self.assertRaises(TemplateNotFound):
html, path, function = blueprint.jinja_loader.get_source("", "addendum::unknown.html")