"""The widget template library (#5692): copying a template under a new name, the file a template exports as, and the list, editor and preview pages.""" from datetime import datetime, timedelta, timezone from plugins.prometheus import library from plugins.prometheus.template_format import check, problems from tests._templates import render_plugin_template from tests.plugins.prometheus.test_templates import NOW, SERIES, TEMPLATE, T, latest from tests.test_style_tokens import hex_colours ARCHIVES = library.bundled()[0] def test_a_copy_gets_its_new_name_and_keeps_the_rest_as_written(): copy = library.renamed(ARCHIVES.text, "Fabled Archives (copy)") assert check(copy).name == "Fabled Archives (copy)" assert copy.splitlines()[0] == ARCHIVES.text.splitlines()[0] # its comments stay assert copy.count("name: ") == ARCHIVES.text.count("name: ") # only the top-level one changed # A name YAML would misread is quoted. assert check(library.renamed(ARCHIVES.text, "Archives: mine #2")).name == "Archives: mine #2" # No name line: one is added. nameless = "\n".join(ln for ln in ARCHIVES.text.splitlines() if not ln.startswith("name:")) assert check(library.renamed(nameless, "New")).name == "New" def test_a_copy_is_named_after_what_is_taken(): assert library.copy_name("Archives", set()) == "Archives (copy)" assert library.copy_name("Archives", {"Archives (copy)"}) == "Archives (copy 2)" assert library.copy_name("Archives", {"Archives (copy)", "Archives (copy 2)"}) == "Archives (copy 3)" assert len(library.copy_name("x" * 80, set())) == 80 def test_an_export_is_named_after_the_template(): assert library.file_name("Fabled Archives") == "fabled-archives.yaml" assert library.file_name("Archives: mine #2") == "archives-mine-2.yaml" assert library.file_name("…") == "template.yaml" def _row(name="Mine", ok=True, minutes=5): return {"key": f"custom:id-{name}", "id": f"id-{name}", "name": name, "bundled": False, "version": 1 if ok else None, "description": "" if not ok else "My service", "problem": "" if ok else "Line 4: `metric` should be a metric name.", "updated_at": datetime.now(timezone.utc) - timedelta(minutes=minutes), "used": 2 if ok else 1} async def _render(app, name, role="admin", **ctx): from quart import session async with app.test_request_context("/plugins/prometheus/templates/"): session["user_role"] = role session["user_id"] = "u1" page = await render_plugin_template(app, "prometheus", name, **ctx) return " ".join(page.split()) async def test_the_list_leads_with_a_template_that_no_longer_passes(app): bundled = {"key": ARCHIVES.key, "id": None, "name": ARCHIVES.name, "bundled": True, "version": 1, "description": "Backups", "problem": "", "updated_at": None, "used": 0} page = await _render(app, "prometheus/templates.html", templates=[_row("Broken", ok=False), bundled, _row("Mine")]) assert "1 template doesn't pass the checker" in page assert page.index("Broken") < page.index("Fabled Archives") < page.index("Mine") assert "Line 4: `metric` should be a metric name." in page assert 'built in' in page and "2 widgets" in page assert 'href="/plugins/prometheus/templates/new"' in page assert 'href="/plugins/prometheus/templates/new?from=bundled%3Afabled_archives"' in page assert 'href="/plugins/prometheus/templates/bundled:fabled_archives/export"' in page assert '