def make_client(*args, **kwargs): app = Flask("Nemo") app.debug = True if len(args): nemo = Nemo(app=app, base_url="", plugins=list(args), **kwargs) else: nemo = Nemo(app=app, base_url="", plugins=None, **kwargs) return app.test_client()
def test_static_folder(self): """ Test the use of static fikder parameter to implement assets customization """ app = Flask(__name__) nemo = Nemo(app=app, static_folder="/examples") nemo.register() self.assertEqual(nemo.static_folder, "/examples") self.assertEqual(nemo.blueprint.static_folder, "/examples")
def test_template_folder(self): """ Test the parameter to set up a different template folder """ app = Flask(__name__) nemo = Nemo(app=app, template_folder="/examples") nemo.register() self.assertEqual(nemo.template_folder, "/examples") self.assertEqual(nemo.blueprint.template_folder, "/examples")
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_transform_default_none(self): """ Test that the transform default is called and applied """ nemo = Nemo() transformed = nemo.transform( MyCapytain.resources.inventory.Text( urn="urn:cts:phi1294.phi002.perseus-lat2" ), etree.fromstring("<a/>") ) self.assertEqual(transformed, "<a/>")
def test_static_url_path(self): """ Test that the extension static url path is changing when set up """ app = Flask(__name__) nemo = Nemo(app=app, static_url_path="/assets/nemoOo") nemo.register() self.assertIn("flask_nemo", app.blueprints, "Nemo is a registered blueprint") rules = [(rule.rule, rule.endpoint) for rule in app.url_map.iter_rules()] self.assertIn("/nemo/assets/nemoOo/<path:filename>", [rule[0] for rule in rules]) self.assertIn("/nemo/assets/nemoOo.secondary/<type>/<asset>", [rule[0] for rule in rules])
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")
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_overwrite_urls(self): """ Check that routes can be added or modified """ routes = [("/index.html", "r_index", ["GET"])] + Nemo.ROUTES[1:] app = Flask(__name__) nemo = Nemo(app=app, urls=routes) nemo.register() self.assertIn("flask_nemo", app.blueprints) rules = [(rule.rule, rule.endpoint) for rule in app.url_map.iter_rules()] self.assertIn("/nemo/index.html", [rule[0] for rule in rules]) self.assertNotIn("/nemo/", [rule[0] for rule in rules])
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_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_transform_urn_xslt(self): """ Test that the transform default is called and applied """ nemo = Nemo(transform={ "default": "tests/test_data/xsl_test.xml" }) transformed = nemo.transform( MyCapytain.resources.inventory.Text( urn="urn:cts:phi1294.phi002.perseus-lat2" ), etree.fromstring('<tei:body xmlns:tei="http://www.tei-c.org/ns/1.0" />') ) self.assertEqual(transformed, '<tei:notbody xmlns:tei="http://www.tei-c.org/ns/1.0"></tei:notbody>', "It should autoclose the tag" )
def test_urntransform_override_function(self): """ Test that the transform override is called and applied """ def override(urn): self.assertEqual(str(urn), "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.pr") return "override" nemo = Nemo(urntransform={ "urn:cts:latinLit:phi1294.phi002.perseus-lat2": override }) transformed = nemo.transform_urn( MyCapytain.resources.inventory.Text( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.pr" ).urn ) self.assertEqual(transformed, "override")
def runner(method, address, port, host, css, xslt, groupby, debug): resolver = None app = Flask(__name__) if method == "cts-api": resolver = HttpCtsResolver(HttpCtsRetriever(address)) elif method == "cts-local": resolver = CtsCapitainsLocalResolver([address]) if xslt is not None: xslt = {"default": xslt} # We set up Nemo nemo = Nemo(app=app, name="nemo", base_url="", css=css, transform=xslt, resolver=resolver, chunker={ "default": lambda x, y: level_grouper(x, y, groupby=groupby) }) # We run the app app.debug = debug app.run(port=port, host=host) # For test purposes return nemo, app
def test_urntransform_default_function(self): """ Test that the transform default is called and applied """ def default(urn): self.assertEqual(str(urn), "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.pr") return str(urn) nemo = Nemo(urntransform={ "default": default }) transformed = nemo.transform_urn( MyCapytain.resources.inventory.Text( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.pr" ).urn ) self.assertEqual(transformed, "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.pr")
def test_register_filter(self): app = Flask(__name__) self.nemo = Nemo(app=app) self.nemo.register_filters() self.assertEqual(self.nemo.app.jinja_env.filters["formatting_passage_reference"], f_formatting_passage_reference) self.assertEqual(self.nemo.app.jinja_env.filters["collection_i18n"], f_collection_i18n) self.assertEqual(self.nemo.app.jinja_env.filters["active_link"], f_active_link)
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_transform_default_function(self): """ Test that the transform default is called and applied when it's a function """ def default(work, xml): self.assertEqual(str(work.urn), "urn:cts:phi1294.phi002.perseus-lat2") self.assertEqual(xml, "<a></a>") return "<b></b>" nemo = Nemo(transform={ "default": default }) transformed = nemo.transform( MyCapytain.resources.inventory.Text( urn="urn:cts:phi1294.phi002.perseus-lat2" ), "<a></a>" ) self.assertEqual(transformed, "<b></b>")
def test_chunker_default(self): """ Test that the chunker default is called and applied """ def default(text, reffs): self.assertEqual(str(text.urn), "urn:cts:phi1294.phi002.perseus-lat2") self.assertEqual(reffs, ["1.pr"]) return [("1.pr", "I PR")] nemo = Nemo(chunker={ "default": default }) chunked = nemo.chunk( MyCapytain.resources.inventory.Text( urn="urn:cts:phi1294.phi002.perseus-lat2" ), ["1.pr"] ) self.assertEqual(chunked, [("1.pr", "I PR")])
def test_prevnext_default(self): """ Test that the chunker default is called and applied """ def default(text, cb): self.assertEqual(str(text.urn), "urn:cts:phi1294.phi002.perseus-lat2") self.assertEqual(cb(1), 1) return [("1.pr", "I PR")] nemo = Nemo(prevnext={ "default": default }) prevnext = nemo.getprevnext( MyCapytain.resources.inventory.Text( urn="urn:cts:phi1294.phi002.perseus-lat2" ), lambda x: x ) self.assertEqual(prevnext, [("1.pr", "I PR")])
def test_register_filter_override(self): app = Flask(__name__) plug = FilterPluginOverride() self.nemo = Nemo(app=app, plugins=[plug]) self.assertEqual( self.nemo.app.jinja_env.filters["formatting_passage_reference"], plug.f_formatting_passage_reference, "Original Nemo filters should be override by newly registered filters" )
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_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_register_filter(self): app = Flask(__name__) plug = FilterPlugin() self.nemo = Nemo(app=app, plugins=[plug]) self.assertEqual( self.nemo.app.jinja_env.filters["formatting_passage_reference"], f_formatting_passage_reference, "Original Nemo filters should be kept") self.assertEqual(self.nemo.app.jinja_env.filters["make_string"], plug.f_make_string, "Plugin filters should be registered")
def make_client(*args, **kwargs): app = Flask("Nemo") app.debug = True if len(args): nemo = Nemo( app=app, base_url="", retriever=NautilusDummy, chunker={"default": lambda x, y: Nemo.level_grouper(x, y, groupby=30)}, plugins=list(args), **kwargs ) else: nemo = Nemo( app=app, base_url="", retriever=NautilusDummy, chunker={"default": lambda x, y: Nemo.level_grouper(x, y, groupby=30)}, plugins=None, **kwargs ) return app.test_client()
class TestFilters(NemoResource): def test_f_order_author(self): """ Test ordering authors """ a = MyCapytain.resources.inventory.TextGroup() b = MyCapytain.resources.inventory.TextGroup() c = MyCapytain.resources.inventory.TextGroup() d = MyCapytain.resources.inventory.TextGroup() e = MyCapytain.resources.inventory.TextGroup() f = MyCapytain.resources.inventory.TextGroup( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2" ) a.metadata["groupname"]["eng"] = "Martial" b.metadata["groupname"]["eng"] = "Virgil" c.metadata["groupname"]["eng"] = "Cicero" d.metadata["groupname"]["eng"] = "Augustine" e.metadata["groupname"]["eng"] = "Suetone" f.metadata["groupname"]["eng"] = None sort = f_order_author([a, b, c, d, e, f]) self.assertEqual( sort, [d, c, a, e, b, f] ) def test_f_active_link(self): """ Test checking if something is in the path """ wrong = f_active_link("phi003", {"collection": "latinLit"}) wrong2 = f_active_link("phi003", {"collection": "latinLit", "textgroup": "phi0003"}) right = f_active_link("phi003", {"collection": "latinLit", "textgroup": "phi0003", "work": "phi003"}) self.assertEqual(wrong, "") self.assertEqual(wrong2, "") self.assertEqual(right, "active") def test_f_collection_i18n(self): """ Test internationalization of collection identifier """ self.assertEqual(f_collection_i18n("latinLit"), "Latin") self.assertEqual(f_collection_i18n("greekLit"), "Ancient Greek") self.assertEqual(f_collection_i18n("freLit"), "freLit") def test_f_formatting_passage_reference(self): """ Test split of passage range identifier """ self.assertEqual(f_formatting_passage_reference("1.1-1.2"), "1.1") self.assertEqual(f_formatting_passage_reference("1.1"), "1.1") def test_f_i18n_iso(self): """ Test split of passage range identifier """ self.assertEqual(f_i18n_iso("eng"), "English") self.assertEqual(f_i18n_iso("eng", "fre"), "anglais") self.assertEqual(f_i18n_iso("eng", "ger"), "English") def test_f_order_text_edition_translation(self): """ Check the reordering filter """ Text = MyCapytain.resources.inventory.Text a, b, c, d = Text(subtype="Translation"), Text(subtype="Edition"), Text(subtype="Edition"), Text(subtype="Translation") self.assertEqual(f_order_text_edition_translation([a,b,c,d]), [b, c, a, d]) def test_register_filter(self): app = Flask(__name__) self.nemo = Nemo(app=app) self.nemo.register_filters() self.assertEqual(self.nemo.app.jinja_env.filters["formatting_passage_reference"], f_formatting_passage_reference) self.assertEqual(self.nemo.app.jinja_env.filters["collection_i18n"], f_collection_i18n) self.assertEqual(self.nemo.app.jinja_env.filters["active_link"], f_active_link) def test_f_hierarchical_passages(self): """Test for normal, simple passage hierarchical conversion :return: """ reffs = [("1.5.8", "Line 8"), ("1.5.9", "Line 9"), ("1.6.8", "Line 7"), ("2.5.8", "Line 12")] citation_line = MyCapytain.common.reference.Citation(name="line") citation_poem = MyCapytain.common.reference.Citation(name="poem", child=citation_line) citation_book = MyCapytain.common.reference.Citation(name="book", child=citation_poem) text = MyCapytain.resources.inventory.Text() text.citation = citation_book converted = f_hierarchical_passages(reffs, text) self.assertEqual(converted["%book|1%"]["%poem|5%"]["Line 8"], "1.5.8") self.assertEqual(converted["%book|1%"]["%poem|5%"]["Line 9"], "1.5.9") self.assertEqual(converted["%book|1%"]["%poem|6%"]["Line 7"], "1.6.8") self.assertEqual(converted["%book|2%"]["%poem|5%"]["Line 12"], "2.5.8") self.assertEqual(len(converted), 2) self.assertEqual(len(converted["%book|1%"]), 2) self.assertEqual(len(converted["%book|1%"]["%poem|5%"]), 2) self.assertEqual(len(converted["%book|1%"]["%poem|6%"]), 1) self.assertEqual(len(converted["%book|2%"]), 1) def test_f_hierarchical_range_passages(self): """Test for range passage hierarchical conversion :return: """ reffs = [("1.5.8-1.5.9", "Line 8"), ("1.5.9-1.5.15", "Line 9"), ("1.6.8-2.9.16", "Line 7"), ("2.5.8-16.45.928", "Line 12")] citation_line = MyCapytain.common.reference.Citation(name="line") citation_poem = MyCapytain.common.reference.Citation(name="poem", child=citation_line) citation_book = MyCapytain.common.reference.Citation(name="book", child=citation_poem) text = MyCapytain.resources.inventory.Text() text.citation = citation_book converted = f_hierarchical_passages(reffs, text) self.assertEqual(converted["%book|1%"]["%poem|5%"]["Line 8"], "1.5.8-1.5.9") self.assertEqual(converted["%book|1%"]["%poem|5%"]["Line 9"], "1.5.9-1.5.15") self.assertEqual(converted["%book|1%"]["%poem|6%"]["Line 7"], "1.6.8-2.9.16") self.assertEqual(converted["%book|2%"]["%poem|5%"]["Line 12"], "2.5.8-16.45.928") self.assertEqual(len(converted), 2) self.assertEqual(len(converted["%book|1%"]), 2) self.assertEqual(len(converted["%book|1%"]["%poem|5%"]), 2) self.assertEqual(len(converted["%book|1%"]["%poem|6%"]), 1) self.assertEqual(len(converted["%book|2%"]), 1) def test_f_is_str(self): """ Test string """ self.assertEqual(f_is_str("h"), True) self.assertEqual(f_is_str([]), False) def test_f_i18n_citation_type(self): self.assertEqual(f_i18n_citation_type("%book|1%"), "Book 1")
def make_nemo(self, app, **kwargs): return Nemo(app=app, cache=Cache(app=app, config={"CACHE_TYPE": "simple"}), **kwargs)
def make_nemo(self, app, **kwargs): return Nemo(app=app, **kwargs)
) nemo = Nemo( app=app, base_url="/elec/geste", resolver=NautilusDummy, chunker={"default": scheme_grouper}, plugins=None, cache=cache, transform={ "default": "./geste.xslt" }, css=[ # USE Own CSS "./styles/geste.css" ], js=[ # use own js file to load a script to go from normalized edition to diplomatic one. "./styles/geste.js" ], templates={ "main": "./templates" }, statics=["./images/logo-enc2.png","./fonts/Junicode-Regular.ttf","./fonts/Junicode-Regular.woff"] #, #additional_static=[ # "img/logo-enc2.jpg" #] ) cache.init_app(app)
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/collections/<objectId>", [rule[0] for rule in rules]) self.assertIn("flask_nemo.r_collection", [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/collections/<objectId>", [rule[0] for rule in rules]) self.assertIn("nemo.r_collection", [rule[1] for rule in rules]) nemo = Nemo() self.assertEqual(nemo.register(), None)