File size: 6,004 Bytes
ec94fc1 | 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 | """Tests for utility functions & renderers."""
from __future__ import annotations
import pytest
from app.services.utils import (
format_money_figures,
h,
handbook_anchor,
hb_slug,
is_truthy,
sort_sections_stable,
)
from app.services.renderers import (
render_global_blocks,
render_toc,
sort_toc,
)
# ββ h() ββ
def test_h_escapes_html():
assert h('<script>alert("xss")</script>') == '<script>alert("xss")</script>'
def test_h_preserves_normal_text():
assert h("Hello World") == "Hello World"
# ββ hb_slug() ββ
def test_hb_slug_basic():
assert hb_slug("Hello World") == "hello_world"
def test_hb_slug_special_chars():
assert hb_slug("Montana State University!") == "montana_state_university"
# ββ handbook_anchor() ββ
def test_handbook_anchor():
result = handbook_anchor("uni", "Drew University", 5)
assert result == "uni-drew-university-5"
def test_handbook_anchor_empty_text():
result = handbook_anchor("g", "", 3)
assert result == "g-g-3-3"
# ββ is_truthy() ββ
def test_is_truthy_true_values():
assert is_truthy(True) is True
assert is_truthy(1) is True
assert is_truthy("yes") is True
assert is_truthy("1") is True
def test_is_truthy_false_values():
assert is_truthy(False) is False
assert is_truthy(0) is False
assert is_truthy("0") is False
assert is_truthy("false") is False
assert is_truthy("") is False
# ββ format_money_figures() ββ
def test_format_money_removes_usd_prefix():
result = format_money_figures("USD 5000")
assert "USD" not in result
assert "$" in result
def test_format_money_adds_dollar_sign():
result = format_money_figures("The cost is 15000 per year")
assert "$15,000" in result
def test_format_money_preserves_percentages():
result = format_money_figures("50% discount on 15000")
assert "50%" in result
# ββ sort_sections_stable() ββ
def test_sort_sections_by_sort_order():
sections = [
{"section_key": "b", "sort_order": 2, "id": 2},
{"section_key": "a", "sort_order": 1, "id": 1},
{"section_key": "c", "sort_order": 3, "id": 3},
]
result = sort_sections_stable(sections)
assert [s["section_key"] for s in result] == ["a", "b", "c"]
def test_sort_sections_null_sort_order_last():
sections = [
{"section_key": "a", "sort_order": None, "id": 1},
{"section_key": "b", "sort_order": 1, "id": 2},
]
result = sort_sections_stable(sections)
assert result[0]["section_key"] == "b"
assert result[1]["section_key"] == "a"
# ββ sort_toc() ββ
def test_sort_toc_stable():
items = [
{"title": "B", "sort": 2},
{"title": "A", "sort": 1},
{"title": "C"},
]
result = sort_toc(items)
assert result[0]["title"] == "A"
assert result[1]["title"] == "B"
assert result[2]["title"] == "C"
# ββ render_toc() ββ
def test_render_toc_produces_html():
items = [
{"title": "Overview", "target": "#overview", "level": 0, "bold": True},
{"title": "Drew University", "target": "#uni-drew", "level": 1, "bold": False},
]
html = render_toc(items)
assert "HANDBOOK_TOC_V2" in html
assert "OVERVIEW" in html # level 0 is uppercased
assert "Drew University" in html
assert '<table class="toc-table"' in html
# ββ render_global_blocks() ββ
def test_render_global_blocks_paragraph():
json_data = {"text": "This is a paragraph."}
html = render_global_blocks("test", "Test Section", json_data)
assert "Test Section" in html
assert "This is a paragraph." in html
def test_render_global_blocks_steps():
json_data = {
"steps": [
{"title": "Apply", "body": "Submit your application."},
{"title": "Enrol", "body": "Complete enrolment."},
]
}
html = render_global_blocks("enrolment_steps", "Steps", json_data)
assert "Step 1: Apply" in html
assert "Step 2: Enrol" in html
def test_render_global_blocks_bullets():
json_data = {"bullets": ["Item A", "Item B", "Item C"]}
html = render_global_blocks("test", "Test", json_data)
assert "<li>" in html
assert "Item A" in html
assert "Item C" in html
def test_render_global_blocks_table():
json_data = {
"columns": ["Name", "Value"],
"rows": [["Alpha", "100"], ["Beta", "200"]],
}
html = render_global_blocks("test", "Test", json_data)
assert '<table class="tbl">' in html
assert "Alpha" in html
assert "200" in html
def test_render_global_blocks_doc_v1():
json_data = {
"layout": "doc_v1",
"blocks": [
{"type": "paragraph", "text": "Hello paragraph."},
{"type": "subheading", "text": "A Subheading"},
{"type": "bullets", "items": ["Bullet 1", "Bullet 2"]},
{"type": "note", "text": "Important note."},
],
}
html = render_global_blocks("overview", "Overview", json_data)
assert "Hello paragraph." in html
assert "A Subheading" in html
assert "Bullet 1" in html
assert "Important note." in html
def test_render_global_blocks_summary_of_universities():
universities = [
{"university_name": "Drew University"},
{"university_name": "Montana State University"},
]
json_data = {"intro": "We partner with great universities."}
html = render_global_blocks(
"summary_of_universities",
"Summary of Universities",
json_data,
universities=universities,
)
assert "Summary of Universities" in html
assert "Drew University" in html
assert "Montana State University" in html
assert "<ol" in html
def test_render_global_blocks_empty_returns_warning(caplog):
with caplog.at_level("WARNING"):
html = render_global_blocks("empty_key", "Empty", {})
assert html.strip() != "" or "Empty section render" in caplog.text
|