Test dashboard: full menu system, all-service tests, filtering
- Run tests for all 10 services via per-service pytest subprocesses - Group results by service with section headers - Clickable summary cards filter by outcome (passed/failed/errors/skipped) - Service filter nav using ~nav-link buttons in menu bar - Full menu integration: ~header-row + ~header-child + ~menu-row - Show logo image via cart-mini rendering - Mount full service directories in docker-compose for test access - Add 24 unit test files across 9 services Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
0
account/tests/__init__.py
Normal file
0
account/tests/__init__.py
Normal file
39
account/tests/test_auth_operations.py
Normal file
39
account/tests/test_auth_operations.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""Unit tests for account auth operations."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from account.bp.auth.services.auth_operations import validate_email
|
||||
|
||||
|
||||
class TestValidateEmail:
|
||||
def test_valid_email(self):
|
||||
ok, email = validate_email("user@example.com")
|
||||
assert ok is True
|
||||
assert email == "user@example.com"
|
||||
|
||||
def test_uppercase_lowered(self):
|
||||
ok, email = validate_email("USER@EXAMPLE.COM")
|
||||
assert ok is True
|
||||
assert email == "user@example.com"
|
||||
|
||||
def test_whitespace_stripped(self):
|
||||
ok, email = validate_email(" user@example.com ")
|
||||
assert ok is True
|
||||
assert email == "user@example.com"
|
||||
|
||||
def test_empty_string(self):
|
||||
ok, email = validate_email("")
|
||||
assert ok is False
|
||||
|
||||
def test_no_at_sign(self):
|
||||
ok, email = validate_email("notanemail")
|
||||
assert ok is False
|
||||
|
||||
def test_just_at(self):
|
||||
ok, email = validate_email("@")
|
||||
assert ok is True # has "@", passes the basic check
|
||||
|
||||
def test_spaces_only(self):
|
||||
ok, email = validate_email(" ")
|
||||
assert ok is False
|
||||
164
account/tests/test_ghost_membership.py
Normal file
164
account/tests/test_ghost_membership.py
Normal file
@@ -0,0 +1,164 @@
|
||||
"""Unit tests for Ghost membership helpers."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from account.services.ghost_membership import (
|
||||
_iso, _to_str_or_none, _member_email,
|
||||
_price_cents, _sanitize_member_payload,
|
||||
)
|
||||
|
||||
|
||||
class TestIso:
|
||||
def test_none(self):
|
||||
assert _iso(None) is None
|
||||
|
||||
def test_empty(self):
|
||||
assert _iso("") is None
|
||||
|
||||
def test_z_suffix(self):
|
||||
result = _iso("2024-06-15T12:00:00Z")
|
||||
assert isinstance(result, datetime)
|
||||
assert result.year == 2024
|
||||
|
||||
def test_offset(self):
|
||||
result = _iso("2024-06-15T12:00:00+00:00")
|
||||
assert isinstance(result, datetime)
|
||||
|
||||
|
||||
class TestToStrOrNone:
|
||||
def test_none(self):
|
||||
assert _to_str_or_none(None) is None
|
||||
|
||||
def test_dict(self):
|
||||
assert _to_str_or_none({"a": 1}) is None
|
||||
|
||||
def test_list(self):
|
||||
assert _to_str_or_none([1, 2]) is None
|
||||
|
||||
def test_bytes(self):
|
||||
assert _to_str_or_none(b"hello") is None
|
||||
|
||||
def test_empty_string(self):
|
||||
assert _to_str_or_none("") is None
|
||||
|
||||
def test_whitespace_only(self):
|
||||
assert _to_str_or_none(" ") is None
|
||||
|
||||
def test_valid_string(self):
|
||||
assert _to_str_or_none("hello") == "hello"
|
||||
|
||||
def test_int(self):
|
||||
assert _to_str_or_none(42) == "42"
|
||||
|
||||
def test_strips_whitespace(self):
|
||||
assert _to_str_or_none(" hi ") == "hi"
|
||||
|
||||
def test_set(self):
|
||||
assert _to_str_or_none({1, 2}) is None
|
||||
|
||||
def test_tuple(self):
|
||||
assert _to_str_or_none((1,)) is None
|
||||
|
||||
def test_bytearray(self):
|
||||
assert _to_str_or_none(bytearray(b"x")) is None
|
||||
|
||||
|
||||
class TestMemberEmail:
|
||||
def test_normal(self):
|
||||
assert _member_email({"email": "USER@EXAMPLE.COM"}) == "user@example.com"
|
||||
|
||||
def test_none(self):
|
||||
assert _member_email({"email": None}) is None
|
||||
|
||||
def test_empty(self):
|
||||
assert _member_email({"email": ""}) is None
|
||||
|
||||
def test_whitespace(self):
|
||||
assert _member_email({"email": " "}) is None
|
||||
|
||||
def test_missing_key(self):
|
||||
assert _member_email({}) is None
|
||||
|
||||
def test_strips(self):
|
||||
assert _member_email({"email": " a@b.com "}) == "a@b.com"
|
||||
|
||||
|
||||
class TestPriceCents:
|
||||
def test_valid(self):
|
||||
assert _price_cents({"price": {"amount": 1500}}) == 1500
|
||||
|
||||
def test_string_amount(self):
|
||||
assert _price_cents({"price": {"amount": "2000"}}) == 2000
|
||||
|
||||
def test_missing_price(self):
|
||||
assert _price_cents({}) is None
|
||||
|
||||
def test_missing_amount(self):
|
||||
assert _price_cents({"price": {}}) is None
|
||||
|
||||
def test_none_amount(self):
|
||||
assert _price_cents({"price": {"amount": None}}) is None
|
||||
|
||||
def test_nested_none(self):
|
||||
assert _price_cents({"price": None}) is None
|
||||
|
||||
|
||||
class TestSanitizeMemberPayload:
|
||||
def test_email_lowercased(self):
|
||||
result = _sanitize_member_payload({"email": "USER@EXAMPLE.COM"})
|
||||
assert result["email"] == "user@example.com"
|
||||
|
||||
def test_empty_email_excluded(self):
|
||||
result = _sanitize_member_payload({"email": ""})
|
||||
assert "email" not in result
|
||||
|
||||
def test_name_included(self):
|
||||
result = _sanitize_member_payload({"name": "Alice"})
|
||||
assert result["name"] == "Alice"
|
||||
|
||||
def test_note_included(self):
|
||||
result = _sanitize_member_payload({"note": "VIP"})
|
||||
assert result["note"] == "VIP"
|
||||
|
||||
def test_subscribed_bool(self):
|
||||
result = _sanitize_member_payload({"subscribed": 1})
|
||||
assert result["subscribed"] is True
|
||||
|
||||
def test_labels_with_id(self):
|
||||
result = _sanitize_member_payload({
|
||||
"labels": [{"id": "abc"}, {"name": "VIP"}]
|
||||
})
|
||||
assert result["labels"] == [{"id": "abc"}, {"name": "VIP"}]
|
||||
|
||||
def test_labels_empty_items_excluded(self):
|
||||
result = _sanitize_member_payload({
|
||||
"labels": [{"id": None, "name": None}]
|
||||
})
|
||||
assert "labels" not in result
|
||||
|
||||
def test_newsletters_with_id(self):
|
||||
result = _sanitize_member_payload({
|
||||
"newsletters": [{"id": "n1", "subscribed": True}]
|
||||
})
|
||||
assert result["newsletters"] == [{"subscribed": True, "id": "n1"}]
|
||||
|
||||
def test_newsletters_default_subscribed(self):
|
||||
result = _sanitize_member_payload({
|
||||
"newsletters": [{"name": "Weekly"}]
|
||||
})
|
||||
assert result["newsletters"][0]["subscribed"] is True
|
||||
|
||||
def test_dict_email_excluded(self):
|
||||
result = _sanitize_member_payload({"email": {"bad": "input"}})
|
||||
assert "email" not in result
|
||||
|
||||
def test_id_passthrough(self):
|
||||
result = _sanitize_member_payload({"id": "ghost-member-123"})
|
||||
assert result["id"] == "ghost-member-123"
|
||||
|
||||
def test_empty_payload(self):
|
||||
result = _sanitize_member_payload({})
|
||||
assert result == {}
|
||||
0
blog/tests/__init__.py
Normal file
0
blog/tests/__init__.py
Normal file
44
blog/tests/test_card_fragments.py
Normal file
44
blog/tests/test_card_fragments.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""Unit tests for card fragment parser."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from blog.bp.blog.services.posts_data import _parse_card_fragments
|
||||
|
||||
|
||||
class TestParseCardFragments:
|
||||
def test_empty_string(self):
|
||||
assert _parse_card_fragments("") == {}
|
||||
|
||||
def test_single_block(self):
|
||||
html = '<!-- card-widget:42 --><div>card</div><!-- /card-widget:42 -->'
|
||||
result = _parse_card_fragments(html)
|
||||
assert result == {"42": "<div>card</div>"}
|
||||
|
||||
def test_multiple_blocks(self):
|
||||
html = (
|
||||
'<!-- card-widget:1 -->A<!-- /card-widget:1 -->'
|
||||
'<!-- card-widget:2 -->B<!-- /card-widget:2 -->'
|
||||
)
|
||||
result = _parse_card_fragments(html)
|
||||
assert result == {"1": "A", "2": "B"}
|
||||
|
||||
def test_empty_inner_skipped(self):
|
||||
html = '<!-- card-widget:99 --> <!-- /card-widget:99 -->'
|
||||
result = _parse_card_fragments(html)
|
||||
assert result == {}
|
||||
|
||||
def test_multiline_content(self):
|
||||
html = '<!-- card-widget:5 -->\n<p>line1</p>\n<p>line2</p>\n<!-- /card-widget:5 -->'
|
||||
result = _parse_card_fragments(html)
|
||||
assert "5" in result
|
||||
assert "<p>line1</p>" in result["5"]
|
||||
|
||||
def test_mismatched_ids_not_captured(self):
|
||||
html = '<!-- card-widget:1 -->content<!-- /card-widget:2 -->'
|
||||
result = _parse_card_fragments(html)
|
||||
assert result == {}
|
||||
|
||||
def test_no_markers(self):
|
||||
html = '<div>no markers here</div>'
|
||||
assert _parse_card_fragments(html) == {}
|
||||
103
blog/tests/test_ghost_sync.py
Normal file
103
blog/tests/test_ghost_sync.py
Normal file
@@ -0,0 +1,103 @@
|
||||
"""Unit tests for Ghost sync helper functions."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from blog.bp.blog.ghost.ghost_sync import _iso, _build_ap_post_data
|
||||
|
||||
|
||||
class TestIso:
|
||||
def test_none(self):
|
||||
assert _iso(None) is None
|
||||
|
||||
def test_empty_string(self):
|
||||
assert _iso("") is None
|
||||
|
||||
def test_z_suffix(self):
|
||||
result = _iso("2024-01-15T10:30:00Z")
|
||||
assert isinstance(result, datetime)
|
||||
assert result.tzinfo is not None
|
||||
assert result.year == 2024
|
||||
assert result.month == 1
|
||||
assert result.hour == 10
|
||||
|
||||
def test_offset_suffix(self):
|
||||
result = _iso("2024-06-01T08:00:00+00:00")
|
||||
assert isinstance(result, datetime)
|
||||
assert result.hour == 8
|
||||
|
||||
|
||||
class TestBuildApPostData:
|
||||
def _post(self, **kwargs):
|
||||
defaults = {
|
||||
"title": "My Post",
|
||||
"plaintext": "Some body text.",
|
||||
"custom_excerpt": None,
|
||||
"excerpt": None,
|
||||
"feature_image": None,
|
||||
"feature_image_alt": None,
|
||||
"html": None,
|
||||
}
|
||||
defaults.update(kwargs)
|
||||
return SimpleNamespace(**defaults)
|
||||
|
||||
def _tag(self, slug):
|
||||
return SimpleNamespace(slug=slug)
|
||||
|
||||
def test_basic_post(self):
|
||||
post = self._post()
|
||||
result = _build_ap_post_data(post, "https://blog.example.com/post/", [])
|
||||
assert result["name"] == "My Post"
|
||||
assert "My Post" in result["content"]
|
||||
assert "Some body text." in result["content"]
|
||||
assert result["url"] == "https://blog.example.com/post/"
|
||||
|
||||
def test_no_title(self):
|
||||
post = self._post(title=None)
|
||||
result = _build_ap_post_data(post, "https://example.com/", [])
|
||||
assert result["name"] == ""
|
||||
|
||||
def test_feature_image(self):
|
||||
post = self._post(feature_image="https://img.com/photo.jpg",
|
||||
feature_image_alt="A photo")
|
||||
result = _build_ap_post_data(post, "https://example.com/", [])
|
||||
assert "attachment" in result
|
||||
assert result["attachment"][0]["url"] == "https://img.com/photo.jpg"
|
||||
assert result["attachment"][0]["name"] == "A photo"
|
||||
|
||||
def test_inline_images_capped_at_4(self):
|
||||
html = "".join(f'<img src="https://img.com/{i}.jpg">' for i in range(10))
|
||||
post = self._post(html=html)
|
||||
result = _build_ap_post_data(post, "https://example.com/", [])
|
||||
assert len(result["attachment"]) == 4
|
||||
|
||||
def test_tags(self):
|
||||
tags = [self._tag("my-tag"), self._tag("another")]
|
||||
post = self._post()
|
||||
result = _build_ap_post_data(post, "https://example.com/", tags)
|
||||
assert "tag" in result
|
||||
assert len(result["tag"]) == 2
|
||||
assert result["tag"][0]["name"] == "#mytag" # dashes removed
|
||||
assert result["tag"][0]["type"] == "Hashtag"
|
||||
|
||||
def test_hashtag_in_content(self):
|
||||
tags = [self._tag("web-dev")]
|
||||
post = self._post()
|
||||
result = _build_ap_post_data(post, "https://example.com/", tags)
|
||||
assert "#webdev" in result["content"]
|
||||
|
||||
def test_no_duplicate_images(self):
|
||||
post = self._post(
|
||||
feature_image="https://img.com/same.jpg",
|
||||
html='<img src="https://img.com/same.jpg">',
|
||||
)
|
||||
result = _build_ap_post_data(post, "https://example.com/", [])
|
||||
assert len(result["attachment"]) == 1
|
||||
|
||||
def test_multiline_body(self):
|
||||
post = self._post(plaintext="Para one.\n\nPara two.\n\nPara three.")
|
||||
result = _build_ap_post_data(post, "https://example.com/", [])
|
||||
assert result["content"].count("<p>") >= 4 # title + 3 paras + read more
|
||||
393
blog/tests/test_lexical_renderer.py
Normal file
393
blog/tests/test_lexical_renderer.py
Normal file
@@ -0,0 +1,393 @@
|
||||
"""Unit tests for the Lexical JSON → HTML renderer."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from blog.bp.blog.ghost.lexical_renderer import (
|
||||
render_lexical, _wrap_format, _align_style,
|
||||
_FORMAT_BOLD, _FORMAT_ITALIC, _FORMAT_STRIKETHROUGH,
|
||||
_FORMAT_UNDERLINE, _FORMAT_CODE, _FORMAT_SUBSCRIPT,
|
||||
_FORMAT_SUPERSCRIPT, _FORMAT_HIGHLIGHT,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _wrap_format
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestWrapFormat:
|
||||
def test_no_format(self):
|
||||
assert _wrap_format("hello", 0) == "hello"
|
||||
|
||||
def test_bold(self):
|
||||
assert _wrap_format("x", _FORMAT_BOLD) == "<strong>x</strong>"
|
||||
|
||||
def test_italic(self):
|
||||
assert _wrap_format("x", _FORMAT_ITALIC) == "<em>x</em>"
|
||||
|
||||
def test_strikethrough(self):
|
||||
assert _wrap_format("x", _FORMAT_STRIKETHROUGH) == "<s>x</s>"
|
||||
|
||||
def test_underline(self):
|
||||
assert _wrap_format("x", _FORMAT_UNDERLINE) == "<u>x</u>"
|
||||
|
||||
def test_code(self):
|
||||
assert _wrap_format("x", _FORMAT_CODE) == "<code>x</code>"
|
||||
|
||||
def test_subscript(self):
|
||||
assert _wrap_format("x", _FORMAT_SUBSCRIPT) == "<sub>x</sub>"
|
||||
|
||||
def test_superscript(self):
|
||||
assert _wrap_format("x", _FORMAT_SUPERSCRIPT) == "<sup>x</sup>"
|
||||
|
||||
def test_highlight(self):
|
||||
assert _wrap_format("x", _FORMAT_HIGHLIGHT) == "<mark>x</mark>"
|
||||
|
||||
def test_bold_italic(self):
|
||||
result = _wrap_format("x", _FORMAT_BOLD | _FORMAT_ITALIC)
|
||||
assert "<strong>" in result
|
||||
assert "<em>" in result
|
||||
|
||||
def test_all_flags(self):
|
||||
all_flags = (
|
||||
_FORMAT_BOLD | _FORMAT_ITALIC | _FORMAT_STRIKETHROUGH |
|
||||
_FORMAT_UNDERLINE | _FORMAT_CODE | _FORMAT_SUBSCRIPT |
|
||||
_FORMAT_SUPERSCRIPT | _FORMAT_HIGHLIGHT
|
||||
)
|
||||
result = _wrap_format("x", all_flags)
|
||||
for tag in ["strong", "em", "s", "u", "code", "sub", "sup", "mark"]:
|
||||
assert f"<{tag}>" in result
|
||||
assert f"</{tag}>" in result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _align_style
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestAlignStyle:
|
||||
def test_no_format(self):
|
||||
assert _align_style({}) == ""
|
||||
|
||||
def test_format_zero(self):
|
||||
assert _align_style({"format": 0}) == ""
|
||||
|
||||
def test_left(self):
|
||||
assert _align_style({"format": 1}) == ' style="text-align: left"'
|
||||
|
||||
def test_center(self):
|
||||
assert _align_style({"format": 2}) == ' style="text-align: center"'
|
||||
|
||||
def test_right(self):
|
||||
assert _align_style({"format": 3}) == ' style="text-align: right"'
|
||||
|
||||
def test_justify(self):
|
||||
assert _align_style({"format": 4}) == ' style="text-align: justify"'
|
||||
|
||||
def test_string_format(self):
|
||||
assert _align_style({"format": "center"}) == ' style="text-align: center"'
|
||||
|
||||
def test_unmapped_int(self):
|
||||
assert _align_style({"format": 99}) == ""
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# render_lexical — text nodes
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestRenderLexicalText:
|
||||
def test_empty_doc(self):
|
||||
assert render_lexical({"root": {"children": []}}) == ""
|
||||
|
||||
def test_plain_text(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "text", "text": "hello"}
|
||||
]}}
|
||||
assert render_lexical(doc) == "hello"
|
||||
|
||||
def test_html_escape(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "text", "text": "<script>alert('xss')</script>"}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "<script>" not in result
|
||||
assert "<script>" in result
|
||||
|
||||
def test_bold_text(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "text", "text": "bold", "format": _FORMAT_BOLD}
|
||||
]}}
|
||||
assert render_lexical(doc) == "<strong>bold</strong>"
|
||||
|
||||
def test_string_input(self):
|
||||
import json
|
||||
doc = {"root": {"children": [{"type": "text", "text": "hi"}]}}
|
||||
assert render_lexical(json.dumps(doc)) == "hi"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# render_lexical — block nodes
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestRenderLexicalBlocks:
|
||||
def test_paragraph(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "paragraph", "children": [
|
||||
{"type": "text", "text": "hello"}
|
||||
]}
|
||||
]}}
|
||||
assert render_lexical(doc) == "<p>hello</p>"
|
||||
|
||||
def test_empty_paragraph(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "paragraph", "children": []}
|
||||
]}}
|
||||
assert render_lexical(doc) == "<p><br></p>"
|
||||
|
||||
def test_heading_default(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "heading", "children": [
|
||||
{"type": "text", "text": "title"}
|
||||
]}
|
||||
]}}
|
||||
assert render_lexical(doc) == "<h2>title</h2>"
|
||||
|
||||
def test_heading_h3(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "heading", "tag": "h3", "children": [
|
||||
{"type": "text", "text": "title"}
|
||||
]}
|
||||
]}}
|
||||
assert render_lexical(doc) == "<h3>title</h3>"
|
||||
|
||||
def test_blockquote(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "quote", "children": [
|
||||
{"type": "text", "text": "quoted"}
|
||||
]}
|
||||
]}}
|
||||
assert render_lexical(doc) == "<blockquote>quoted</blockquote>"
|
||||
|
||||
def test_linebreak(self):
|
||||
doc = {"root": {"children": [{"type": "linebreak"}]}}
|
||||
assert render_lexical(doc) == "<br>"
|
||||
|
||||
def test_horizontal_rule(self):
|
||||
doc = {"root": {"children": [{"type": "horizontalrule"}]}}
|
||||
assert render_lexical(doc) == "<hr>"
|
||||
|
||||
def test_unordered_list(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "list", "listType": "bullet", "children": [
|
||||
{"type": "listitem", "children": [
|
||||
{"type": "text", "text": "item"}
|
||||
]}
|
||||
]}
|
||||
]}}
|
||||
assert render_lexical(doc) == "<ul><li>item</li></ul>"
|
||||
|
||||
def test_ordered_list(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "list", "listType": "number", "children": [
|
||||
{"type": "listitem", "children": [
|
||||
{"type": "text", "text": "one"}
|
||||
]}
|
||||
]}
|
||||
]}}
|
||||
assert render_lexical(doc) == "<ol><li>one</li></ol>"
|
||||
|
||||
def test_ordered_list_custom_start(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "list", "listType": "number", "start": 5, "children": []}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert 'start="5"' in result
|
||||
|
||||
def test_link(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "link", "url": "https://example.com", "children": [
|
||||
{"type": "text", "text": "click"}
|
||||
]}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert 'href="https://example.com"' in result
|
||||
assert "click" in result
|
||||
|
||||
def test_link_xss_url(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "link", "url": 'javascript:alert("xss")', "children": []}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "javascript:alert("xss")" in result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# render_lexical — cards
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestRenderLexicalCards:
|
||||
def test_image(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "image", "src": "photo.jpg", "alt": "A photo"}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "kg-image-card" in result
|
||||
assert 'src="photo.jpg"' in result
|
||||
assert 'alt="A photo"' in result
|
||||
assert 'loading="lazy"' in result
|
||||
|
||||
def test_image_wide(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "image", "src": "x.jpg", "cardWidth": "wide"}
|
||||
]}}
|
||||
assert "kg-width-wide" in render_lexical(doc)
|
||||
|
||||
def test_image_with_caption(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "image", "src": "x.jpg", "caption": "Caption text"}
|
||||
]}}
|
||||
assert "<figcaption>Caption text</figcaption>" in render_lexical(doc)
|
||||
|
||||
def test_codeblock(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "codeblock", "code": "print('hi')", "language": "python"}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert 'class="language-python"' in result
|
||||
assert "print('hi')" in result
|
||||
|
||||
def test_html_card(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "html", "html": "<div>raw</div>"}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "<!--kg-card-begin: html-->" in result
|
||||
assert "<div>raw</div>" in result
|
||||
|
||||
def test_markdown_card(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "markdown", "markdown": "**bold**"}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "<!--kg-card-begin: markdown-->" in result
|
||||
assert "<strong>bold</strong>" in result
|
||||
|
||||
def test_callout(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "callout", "backgroundColor": "blue",
|
||||
"calloutEmoji": "💡", "children": [
|
||||
{"type": "text", "text": "Note"}
|
||||
]}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "kg-callout-card-blue" in result
|
||||
assert "💡" in result
|
||||
|
||||
def test_button(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "button", "buttonText": "Click",
|
||||
"buttonUrl": "https://example.com", "alignment": "left"}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "kg-align-left" in result
|
||||
assert "Click" in result
|
||||
|
||||
def test_toggle(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "toggle", "heading": "FAQ", "children": [
|
||||
{"type": "text", "text": "Answer"}
|
||||
]}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "kg-toggle-card" in result
|
||||
assert "FAQ" in result
|
||||
|
||||
def test_audio_duration(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "audio", "src": "a.mp3", "title": "Song", "duration": 185}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "3:05" in result
|
||||
|
||||
def test_audio_zero_duration(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "audio", "src": "a.mp3", "duration": 0}
|
||||
]}}
|
||||
assert "0:00" in render_lexical(doc)
|
||||
|
||||
def test_video(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "video", "src": "v.mp4", "loop": True}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "kg-video-card" in result
|
||||
assert " loop" in result
|
||||
|
||||
def test_file_size_kb(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "file", "src": "f.pdf", "fileName": "doc.pdf",
|
||||
"fileSize": 512000} # 500 KB
|
||||
]}}
|
||||
assert "500 KB" in render_lexical(doc)
|
||||
|
||||
def test_file_size_mb(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "file", "src": "f.zip", "fileName": "big.zip",
|
||||
"fileSize": 5242880} # 5 MB
|
||||
]}}
|
||||
assert "5.0 MB" in render_lexical(doc)
|
||||
|
||||
def test_paywall(self):
|
||||
doc = {"root": {"children": [{"type": "paywall"}]}}
|
||||
assert render_lexical(doc) == "<!--members-only-->"
|
||||
|
||||
def test_embed(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "embed", "html": "<iframe></iframe>",
|
||||
"caption": "Video"}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "kg-embed-card" in result
|
||||
assert "<figcaption>Video</figcaption>" in result
|
||||
|
||||
def test_bookmark(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "bookmark", "url": "https://example.com",
|
||||
"metadata": {"title": "Example", "description": "A site"}}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "kg-bookmark-card" in result
|
||||
assert "Example" in result
|
||||
|
||||
def test_unknown_node_ignored(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "unknown-future-thing"}
|
||||
]}}
|
||||
assert render_lexical(doc) == ""
|
||||
|
||||
def test_product_stars(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "product", "productTitle": "Widget",
|
||||
"rating": 3, "productDescription": "Nice"}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "kg-product-card" in result
|
||||
assert result.count("kg-product-card-rating-active") == 3
|
||||
|
||||
def test_header_card(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "header", "heading": "Welcome",
|
||||
"size": "large", "style": "dark"}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "kg-header-card" in result
|
||||
assert "kg-size-large" in result
|
||||
assert "Welcome" in result
|
||||
|
||||
def test_signup_card(self):
|
||||
doc = {"root": {"children": [
|
||||
{"type": "signup", "heading": "Subscribe",
|
||||
"buttonText": "Join", "style": "light"}
|
||||
]}}
|
||||
result = render_lexical(doc)
|
||||
assert "kg-signup-card" in result
|
||||
assert "Join" in result
|
||||
83
blog/tests/test_lexical_validator.py
Normal file
83
blog/tests/test_lexical_validator.py
Normal file
@@ -0,0 +1,83 @@
|
||||
"""Unit tests for lexical document validator."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from blog.bp.blog.ghost.lexical_validator import (
|
||||
validate_lexical, ALLOWED_NODE_TYPES,
|
||||
)
|
||||
|
||||
|
||||
class TestValidateLexical:
|
||||
def test_valid_empty_doc(self):
|
||||
ok, reason = validate_lexical({"root": {"type": "root", "children": []}})
|
||||
assert ok is True
|
||||
assert reason is None
|
||||
|
||||
def test_non_dict_input(self):
|
||||
ok, reason = validate_lexical("not a dict")
|
||||
assert ok is False
|
||||
assert "JSON object" in reason
|
||||
|
||||
def test_list_input(self):
|
||||
ok, reason = validate_lexical([])
|
||||
assert ok is False
|
||||
|
||||
def test_missing_root(self):
|
||||
ok, reason = validate_lexical({"foo": "bar"})
|
||||
assert ok is False
|
||||
assert "'root'" in reason
|
||||
|
||||
def test_root_not_dict(self):
|
||||
ok, reason = validate_lexical({"root": "string"})
|
||||
assert ok is False
|
||||
|
||||
def test_valid_paragraph(self):
|
||||
doc = {"root": {"type": "root", "children": [
|
||||
{"type": "paragraph", "children": [
|
||||
{"type": "text", "text": "hello"}
|
||||
]}
|
||||
]}}
|
||||
ok, _ = validate_lexical(doc)
|
||||
assert ok is True
|
||||
|
||||
def test_disallowed_type(self):
|
||||
doc = {"root": {"type": "root", "children": [
|
||||
{"type": "script"}
|
||||
]}}
|
||||
ok, reason = validate_lexical(doc)
|
||||
assert ok is False
|
||||
assert "Disallowed node type: script" in reason
|
||||
|
||||
def test_nested_disallowed_type(self):
|
||||
doc = {"root": {"type": "root", "children": [
|
||||
{"type": "paragraph", "children": [
|
||||
{"type": "list", "children": [
|
||||
{"type": "evil-widget"}
|
||||
]}
|
||||
]}
|
||||
]}}
|
||||
ok, reason = validate_lexical(doc)
|
||||
assert ok is False
|
||||
assert "evil-widget" in reason
|
||||
|
||||
def test_node_without_type_allowed(self):
|
||||
"""Nodes with type=None are allowed by _walk."""
|
||||
doc = {"root": {"type": "root", "children": [
|
||||
{"children": []} # no "type" key
|
||||
]}}
|
||||
ok, _ = validate_lexical(doc)
|
||||
assert ok is True
|
||||
|
||||
def test_all_allowed_types(self):
|
||||
"""Every type in the allowlist should pass."""
|
||||
for node_type in ALLOWED_NODE_TYPES:
|
||||
doc = {"root": {"type": "root", "children": [
|
||||
{"type": node_type, "children": []}
|
||||
]}}
|
||||
ok, reason = validate_lexical(doc)
|
||||
assert ok is True, f"{node_type} should be allowed but got: {reason}"
|
||||
|
||||
def test_allowed_types_count(self):
|
||||
"""Sanity: at least 30 types in the allowlist."""
|
||||
assert len(ALLOWED_NODE_TYPES) >= 30
|
||||
50
blog/tests/test_slugify.py
Normal file
50
blog/tests/test_slugify.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""Unit tests for blog slugify utility."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from blog.bp.post.services.markets import slugify
|
||||
|
||||
|
||||
class TestSlugify:
|
||||
def test_basic_ascii(self):
|
||||
assert slugify("Hello World") == "hello-world"
|
||||
|
||||
def test_unicode_stripped(self):
|
||||
assert slugify("café") == "cafe"
|
||||
|
||||
def test_slashes_to_dashes(self):
|
||||
assert slugify("foo/bar") == "foo-bar"
|
||||
|
||||
def test_special_chars(self):
|
||||
assert slugify("foo!!bar") == "foo-bar"
|
||||
|
||||
def test_multiple_dashes_collapsed(self):
|
||||
assert slugify("foo---bar") == "foo-bar"
|
||||
|
||||
def test_leading_trailing_dashes_stripped(self):
|
||||
assert slugify("--foo--") == "foo"
|
||||
|
||||
def test_empty_string_fallback(self):
|
||||
assert slugify("") == "market"
|
||||
|
||||
def test_none_fallback(self):
|
||||
assert slugify(None) == "market"
|
||||
|
||||
def test_max_len_truncation(self):
|
||||
result = slugify("a" * 300, max_len=10)
|
||||
assert len(result) <= 10
|
||||
|
||||
def test_truncation_no_trailing_dash(self):
|
||||
# "abcde-fgh" truncated to 5 should not end with dash
|
||||
result = slugify("abcde fgh", max_len=5)
|
||||
assert not result.endswith("-")
|
||||
|
||||
def test_already_clean(self):
|
||||
assert slugify("hello-world") == "hello-world"
|
||||
|
||||
def test_numbers_preserved(self):
|
||||
assert slugify("item-42") == "item-42"
|
||||
|
||||
def test_accented_characters(self):
|
||||
assert slugify("über straße") == "uber-strae"
|
||||
0
cart/tests/__init__.py
Normal file
0
cart/tests/__init__.py
Normal file
59
cart/tests/test_calendar_cart.py
Normal file
59
cart/tests/test_calendar_cart.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""Unit tests for calendar/ticket total functions."""
|
||||
from __future__ import annotations
|
||||
|
||||
from decimal import Decimal
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from cart.bp.cart.services.calendar_cart import calendar_total, ticket_total
|
||||
|
||||
|
||||
def _entry(cost):
|
||||
return SimpleNamespace(cost=cost)
|
||||
|
||||
|
||||
def _ticket(price):
|
||||
return SimpleNamespace(price=price)
|
||||
|
||||
|
||||
class TestCalendarTotal:
|
||||
def test_empty(self):
|
||||
assert calendar_total([]) == 0
|
||||
|
||||
def test_single_entry(self):
|
||||
assert calendar_total([_entry(25.0)]) == Decimal("25.0")
|
||||
|
||||
def test_none_cost_excluded(self):
|
||||
result = calendar_total([_entry(None)])
|
||||
assert result == 0
|
||||
|
||||
def test_zero_cost(self):
|
||||
# cost=0 is falsy, so it produces Decimal(0) via the else branch
|
||||
result = calendar_total([_entry(0)])
|
||||
assert result == Decimal("0")
|
||||
|
||||
def test_multiple(self):
|
||||
result = calendar_total([_entry(10.0), _entry(20.0)])
|
||||
assert result == Decimal("30.0")
|
||||
|
||||
|
||||
class TestTicketTotal:
|
||||
def test_empty(self):
|
||||
assert ticket_total([]) == Decimal("0")
|
||||
|
||||
def test_single_ticket(self):
|
||||
assert ticket_total([_ticket(15.0)]) == Decimal("15.0")
|
||||
|
||||
def test_none_price_treated_as_zero(self):
|
||||
# ticket_total includes all tickets, None → Decimal(0)
|
||||
result = ticket_total([_ticket(None)])
|
||||
assert result == Decimal("0")
|
||||
|
||||
def test_multiple(self):
|
||||
result = ticket_total([_ticket(5.0), _ticket(10.0)])
|
||||
assert result == Decimal("15.0")
|
||||
|
||||
def test_mixed_with_none(self):
|
||||
result = ticket_total([_ticket(10.0), _ticket(None), _ticket(5.0)])
|
||||
assert result == Decimal("15.0")
|
||||
139
cart/tests/test_checkout.py
Normal file
139
cart/tests/test_checkout.py
Normal file
@@ -0,0 +1,139 @@
|
||||
"""Unit tests for cart checkout helpers."""
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from cart.bp.cart.services.checkout import (
|
||||
build_sumup_description,
|
||||
build_sumup_reference,
|
||||
build_webhook_url,
|
||||
validate_webhook_secret,
|
||||
)
|
||||
|
||||
|
||||
def _ci(title=None, qty=1):
|
||||
return SimpleNamespace(product_title=title, quantity=qty)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# build_sumup_description
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestBuildSumupDescription:
|
||||
def test_empty_cart_no_tickets(self):
|
||||
result = build_sumup_description([], 1)
|
||||
assert "Order 1" in result
|
||||
assert "0 items" in result
|
||||
assert "order items" in result
|
||||
|
||||
def test_single_item(self):
|
||||
result = build_sumup_description([_ci("Widget", 1)], 42)
|
||||
assert "Order 42" in result
|
||||
assert "1 item)" in result
|
||||
assert "Widget" in result
|
||||
|
||||
def test_quantity_counted(self):
|
||||
result = build_sumup_description([_ci("Widget", 3)], 1)
|
||||
assert "3 items" in result
|
||||
|
||||
def test_three_titles(self):
|
||||
items = [_ci("A"), _ci("B"), _ci("C")]
|
||||
result = build_sumup_description(items, 1)
|
||||
assert "A, B, C" in result
|
||||
assert "more" not in result
|
||||
|
||||
def test_four_titles_truncated(self):
|
||||
items = [_ci("A"), _ci("B"), _ci("C"), _ci("D")]
|
||||
result = build_sumup_description(items, 1)
|
||||
assert "A, B, C" in result
|
||||
assert "+ 1 more" in result
|
||||
|
||||
def test_none_titles_excluded(self):
|
||||
items = [_ci(None), _ci("Visible")]
|
||||
result = build_sumup_description(items, 1)
|
||||
assert "Visible" in result
|
||||
|
||||
def test_tickets_singular(self):
|
||||
result = build_sumup_description([], 1, ticket_count=1)
|
||||
assert "1 ticket" in result
|
||||
assert "tickets" not in result
|
||||
|
||||
def test_tickets_plural(self):
|
||||
result = build_sumup_description([], 1, ticket_count=3)
|
||||
assert "3 tickets" in result
|
||||
|
||||
def test_mixed_items_and_tickets(self):
|
||||
result = build_sumup_description([_ci("A", 2)], 1, ticket_count=1)
|
||||
assert "3 items" in result # 2 + 1
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# build_sumup_reference
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestBuildSumupReference:
|
||||
def test_with_page_config(self):
|
||||
pc = SimpleNamespace(sumup_checkout_prefix="SHOP-")
|
||||
assert build_sumup_reference(42, pc) == "SHOP-42"
|
||||
|
||||
def test_without_page_config(self):
|
||||
with patch("cart.bp.cart.services.checkout.config",
|
||||
return_value={"sumup": {"checkout_reference_prefix": "RA-"}}):
|
||||
assert build_sumup_reference(99) == "RA-99"
|
||||
|
||||
def test_no_prefix(self):
|
||||
with patch("cart.bp.cart.services.checkout.config",
|
||||
return_value={"sumup": {}}):
|
||||
assert build_sumup_reference(1) == "1"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# build_webhook_url
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestBuildWebhookUrl:
|
||||
def test_no_secret(self):
|
||||
with patch("cart.bp.cart.services.checkout.config",
|
||||
return_value={"sumup": {}}):
|
||||
assert build_webhook_url("https://x.com/hook") == "https://x.com/hook"
|
||||
|
||||
def test_with_secret_no_query(self):
|
||||
with patch("cart.bp.cart.services.checkout.config",
|
||||
return_value={"sumup": {"webhook_secret": "s3cret"}}):
|
||||
result = build_webhook_url("https://x.com/hook")
|
||||
assert "?token=s3cret" in result
|
||||
|
||||
def test_with_secret_existing_query(self):
|
||||
with patch("cart.bp.cart.services.checkout.config",
|
||||
return_value={"sumup": {"webhook_secret": "s3cret"}}):
|
||||
result = build_webhook_url("https://x.com/hook?a=1")
|
||||
assert "&token=s3cret" in result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# validate_webhook_secret
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestValidateWebhookSecret:
|
||||
def test_no_secret_configured(self):
|
||||
with patch("cart.bp.cart.services.checkout.config",
|
||||
return_value={"sumup": {}}):
|
||||
assert validate_webhook_secret(None) is True
|
||||
|
||||
def test_correct_token(self):
|
||||
with patch("cart.bp.cart.services.checkout.config",
|
||||
return_value={"sumup": {"webhook_secret": "abc"}}):
|
||||
assert validate_webhook_secret("abc") is True
|
||||
|
||||
def test_wrong_token(self):
|
||||
with patch("cart.bp.cart.services.checkout.config",
|
||||
return_value={"sumup": {"webhook_secret": "abc"}}):
|
||||
assert validate_webhook_secret("wrong") is False
|
||||
|
||||
def test_none_token_with_secret(self):
|
||||
with patch("cart.bp.cart.services.checkout.config",
|
||||
return_value={"sumup": {"webhook_secret": "abc"}}):
|
||||
assert validate_webhook_secret(None) is False
|
||||
77
cart/tests/test_ticket_groups.py
Normal file
77
cart/tests/test_ticket_groups.py
Normal file
@@ -0,0 +1,77 @@
|
||||
"""Unit tests for ticket grouping logic."""
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from cart.bp.cart.services.ticket_groups import group_tickets
|
||||
|
||||
|
||||
def _ticket(entry_id=1, entry_name="Event", ticket_type_id=None,
|
||||
ticket_type_name=None, price=10.0,
|
||||
entry_start_at=None, entry_end_at=None):
|
||||
return SimpleNamespace(
|
||||
entry_id=entry_id,
|
||||
entry_name=entry_name,
|
||||
entry_start_at=entry_start_at or datetime(2025, 6, 1, 10, 0),
|
||||
entry_end_at=entry_end_at,
|
||||
ticket_type_id=ticket_type_id,
|
||||
ticket_type_name=ticket_type_name,
|
||||
price=price,
|
||||
)
|
||||
|
||||
|
||||
class TestGroupTickets:
|
||||
def test_empty(self):
|
||||
assert group_tickets([]) == []
|
||||
|
||||
def test_single_ticket(self):
|
||||
result = group_tickets([_ticket()])
|
||||
assert len(result) == 1
|
||||
assert result[0]["quantity"] == 1
|
||||
assert result[0]["line_total"] == 10.0
|
||||
|
||||
def test_same_group_merged(self):
|
||||
tickets = [_ticket(entry_id=1), _ticket(entry_id=1)]
|
||||
result = group_tickets(tickets)
|
||||
assert len(result) == 1
|
||||
assert result[0]["quantity"] == 2
|
||||
assert result[0]["line_total"] == 20.0
|
||||
|
||||
def test_different_entries_separate(self):
|
||||
tickets = [_ticket(entry_id=1), _ticket(entry_id=2)]
|
||||
result = group_tickets(tickets)
|
||||
assert len(result) == 2
|
||||
|
||||
def test_different_ticket_types_separate(self):
|
||||
tickets = [
|
||||
_ticket(entry_id=1, ticket_type_id=1, ticket_type_name="Adult"),
|
||||
_ticket(entry_id=1, ticket_type_id=2, ticket_type_name="Child"),
|
||||
]
|
||||
result = group_tickets(tickets)
|
||||
assert len(result) == 2
|
||||
|
||||
def test_none_price(self):
|
||||
result = group_tickets([_ticket(price=None)])
|
||||
assert result[0]["line_total"] == 0.0
|
||||
|
||||
def test_ordering_preserved(self):
|
||||
tickets = [
|
||||
_ticket(entry_id=2, entry_name="Second"),
|
||||
_ticket(entry_id=1, entry_name="First"),
|
||||
]
|
||||
result = group_tickets(tickets)
|
||||
assert result[0]["entry_name"] == "Second"
|
||||
assert result[1]["entry_name"] == "First"
|
||||
|
||||
def test_metadata_from_first_ticket(self):
|
||||
tickets = [
|
||||
_ticket(entry_id=1, entry_name="A", price=5.0),
|
||||
_ticket(entry_id=1, entry_name="B", price=10.0),
|
||||
]
|
||||
result = group_tickets(tickets)
|
||||
assert result[0]["entry_name"] == "A" # from first
|
||||
assert result[0]["price"] == 5.0 # from first
|
||||
assert result[0]["line_total"] == 15.0 # accumulated
|
||||
47
cart/tests/test_total.py
Normal file
47
cart/tests/test_total.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""Unit tests for cart total calculations."""
|
||||
from __future__ import annotations
|
||||
|
||||
from decimal import Decimal
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from cart.bp.cart.services.total import total
|
||||
|
||||
|
||||
def _item(special=None, regular=None, qty=1):
|
||||
return SimpleNamespace(
|
||||
product_special_price=special,
|
||||
product_regular_price=regular,
|
||||
quantity=qty,
|
||||
)
|
||||
|
||||
|
||||
class TestTotal:
|
||||
def test_empty_cart(self):
|
||||
assert total([]) == 0
|
||||
|
||||
def test_regular_price_only(self):
|
||||
result = total([_item(regular=10.0, qty=2)])
|
||||
assert result == Decimal("20.0")
|
||||
|
||||
def test_special_price_preferred(self):
|
||||
result = total([_item(special=5.0, regular=10.0, qty=1)])
|
||||
assert result == Decimal("5.0")
|
||||
|
||||
def test_none_prices_excluded(self):
|
||||
result = total([_item(special=None, regular=None, qty=1)])
|
||||
assert result == 0
|
||||
|
||||
def test_mixed_items(self):
|
||||
items = [
|
||||
_item(special=5.0, qty=2), # 10
|
||||
_item(regular=3.0, qty=3), # 9
|
||||
_item(), # excluded
|
||||
]
|
||||
result = total(items)
|
||||
assert result == Decimal("19.0")
|
||||
|
||||
def test_quantity_multiplication(self):
|
||||
result = total([_item(regular=7.5, qty=4)])
|
||||
assert result == Decimal("30.0")
|
||||
@@ -367,25 +367,16 @@ services:
|
||||
- ./test/runner.py:/app/runner.py
|
||||
- ./test/path_setup.py:/app/path_setup.py
|
||||
- ./test/entrypoint.sh:/usr/local/bin/entrypoint.sh
|
||||
# sibling models
|
||||
- ./blog/__init__.py:/app/blog/__init__.py:ro
|
||||
- ./blog/models:/app/blog/models:ro
|
||||
- ./market/__init__.py:/app/market/__init__.py:ro
|
||||
- ./market/models:/app/market/models:ro
|
||||
- ./cart/__init__.py:/app/cart/__init__.py:ro
|
||||
- ./cart/models:/app/cart/models:ro
|
||||
- ./events/__init__.py:/app/events/__init__.py:ro
|
||||
- ./events/models:/app/events/models:ro
|
||||
- ./federation/__init__.py:/app/federation/__init__.py:ro
|
||||
- ./federation/models:/app/federation/models:ro
|
||||
- ./account/__init__.py:/app/account/__init__.py:ro
|
||||
- ./account/models:/app/account/models:ro
|
||||
- ./relations/__init__.py:/app/relations/__init__.py:ro
|
||||
- ./relations/models:/app/relations/models:ro
|
||||
- ./likes/__init__.py:/app/likes/__init__.py:ro
|
||||
- ./likes/models:/app/likes/models:ro
|
||||
- ./orders/__init__.py:/app/orders/__init__.py:ro
|
||||
- ./orders/models:/app/orders/models:ro
|
||||
# sibling service code + tests
|
||||
- ./blog:/app/blog:ro
|
||||
- ./market:/app/market:ro
|
||||
- ./cart:/app/cart:ro
|
||||
- ./events:/app/events:ro
|
||||
- ./federation:/app/federation:ro
|
||||
- ./account:/app/account:ro
|
||||
- ./relations:/app/relations:ro
|
||||
- ./likes:/app/likes:ro
|
||||
- ./orders:/app/orders:ro
|
||||
|
||||
test-unit:
|
||||
build:
|
||||
|
||||
0
events/tests/__init__.py
Normal file
0
events/tests/__init__.py
Normal file
30
events/tests/conftest.py
Normal file
30
events/tests/conftest.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""Events test fixtures — direct module loading to avoid bp __init__ chains."""
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import sys
|
||||
|
||||
|
||||
def _load(name: str, path: str):
|
||||
"""Import a .py file directly, bypassing package __init__ chains."""
|
||||
if name in sys.modules:
|
||||
return sys.modules[name]
|
||||
spec = importlib.util.spec_from_file_location(name, path)
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
sys.modules[name] = mod
|
||||
spec.loader.exec_module(mod)
|
||||
return mod
|
||||
|
||||
|
||||
# Ensure events/models is importable as 'models'
|
||||
sys.path.insert(0, "/app/events")
|
||||
|
||||
# Pre-load target modules that would fail via normal package import
|
||||
_load("events.bp.calendar.services.calendar_view",
|
||||
"/app/events/bp/calendar/services/calendar_view.py")
|
||||
_load("events.bp.calendar.services.slots",
|
||||
"/app/events/bp/calendar/services/slots.py")
|
||||
_load("events.bp.calendars.services.calendars",
|
||||
"/app/events/bp/calendars/services/calendars.py")
|
||||
_load("events.bp.calendar_entries.routes",
|
||||
"/app/events/bp/calendar_entries/routes.py")
|
||||
82
events/tests/test_calendar_view.py
Normal file
82
events/tests/test_calendar_view.py
Normal file
@@ -0,0 +1,82 @@
|
||||
"""Unit tests for calendar view helpers."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
|
||||
from events.bp.calendar.services.calendar_view import add_months, build_calendar_weeks
|
||||
|
||||
|
||||
class TestAddMonths:
|
||||
def test_same_month(self):
|
||||
assert add_months(2025, 6, 0) == (2025, 6)
|
||||
|
||||
def test_forward_one(self):
|
||||
assert add_months(2025, 6, 1) == (2025, 7)
|
||||
|
||||
def test_december_to_january(self):
|
||||
assert add_months(2025, 12, 1) == (2026, 1)
|
||||
|
||||
def test_january_to_december(self):
|
||||
assert add_months(2025, 1, -1) == (2024, 12)
|
||||
|
||||
def test_multi_year_forward(self):
|
||||
assert add_months(2025, 1, 24) == (2027, 1)
|
||||
|
||||
def test_multi_year_backward(self):
|
||||
assert add_months(2025, 3, -15) == (2023, 12)
|
||||
|
||||
def test_negative_large(self):
|
||||
y, m = add_months(2025, 6, -30)
|
||||
assert m >= 1 and m <= 12
|
||||
assert y == 2022 # 2025-06 minus 30 months = 2022-12
|
||||
|
||||
def test_forward_eleven(self):
|
||||
assert add_months(2025, 1, 11) == (2025, 12)
|
||||
|
||||
def test_forward_twelve(self):
|
||||
assert add_months(2025, 1, 12) == (2026, 1)
|
||||
|
||||
|
||||
class TestBuildCalendarWeeks:
|
||||
def test_returns_weeks(self):
|
||||
weeks = build_calendar_weeks(2025, 6)
|
||||
assert len(weeks) >= 4
|
||||
assert len(weeks) <= 6
|
||||
|
||||
def test_seven_days_per_week(self):
|
||||
weeks = build_calendar_weeks(2025, 1)
|
||||
for week in weeks:
|
||||
assert len(week) == 7
|
||||
|
||||
def test_day_structure(self):
|
||||
weeks = build_calendar_weeks(2025, 6)
|
||||
day = weeks[0][0]
|
||||
assert "date" in day
|
||||
assert "in_month" in day
|
||||
assert "is_today" in day
|
||||
assert isinstance(day["date"], date)
|
||||
|
||||
def test_in_month_flag(self):
|
||||
weeks = build_calendar_weeks(2025, 6)
|
||||
# First day of first week might be in May
|
||||
june_days = [
|
||||
d for week in weeks for d in week if d["in_month"]
|
||||
]
|
||||
assert len(june_days) == 30 # June has 30 days
|
||||
|
||||
def test_february_leap_year(self):
|
||||
weeks = build_calendar_weeks(2024, 2)
|
||||
feb_days = [d for week in weeks for d in week if d["in_month"]]
|
||||
assert len(feb_days) == 29
|
||||
|
||||
def test_february_non_leap(self):
|
||||
weeks = build_calendar_weeks(2025, 2)
|
||||
feb_days = [d for week in weeks for d in week if d["in_month"]]
|
||||
assert len(feb_days) == 28
|
||||
|
||||
def test_starts_on_monday(self):
|
||||
weeks = build_calendar_weeks(2025, 6)
|
||||
# First day of first week should be a Monday
|
||||
assert weeks[0][0]["date"].weekday() == 0 # Monday
|
||||
57
events/tests/test_entry_cost.py
Normal file
57
events/tests/test_entry_cost.py
Normal file
@@ -0,0 +1,57 @@
|
||||
"""Unit tests for entry cost calculation."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, time
|
||||
from decimal import Decimal
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from events.bp.calendar_entries.routes import calculate_entry_cost
|
||||
|
||||
|
||||
def _slot(cost=None, flexible=False, time_start=None, time_end=None):
|
||||
return SimpleNamespace(
|
||||
cost=cost,
|
||||
flexible=flexible,
|
||||
time_start=time_start or time(9, 0),
|
||||
time_end=time_end or time(17, 0),
|
||||
)
|
||||
|
||||
|
||||
class TestCalculateEntryCost:
|
||||
def test_no_cost(self):
|
||||
assert calculate_entry_cost(_slot(cost=None),
|
||||
datetime(2025, 1, 1, 9), datetime(2025, 1, 1, 17)) == Decimal("0")
|
||||
|
||||
def test_fixed_slot(self):
|
||||
result = calculate_entry_cost(_slot(cost=100, flexible=False),
|
||||
datetime(2025, 1, 1, 10), datetime(2025, 1, 1, 12))
|
||||
assert result == Decimal("100")
|
||||
|
||||
def test_flexible_full_duration(self):
|
||||
slot = _slot(cost=80, flexible=True, time_start=time(9, 0), time_end=time(17, 0))
|
||||
# 8 hours slot, booking full 8 hours
|
||||
result = calculate_entry_cost(slot,
|
||||
datetime(2025, 1, 1, 9, 0), datetime(2025, 1, 1, 17, 0))
|
||||
assert result == Decimal("80")
|
||||
|
||||
def test_flexible_half_duration(self):
|
||||
slot = _slot(cost=80, flexible=True, time_start=time(9, 0), time_end=time(17, 0))
|
||||
# 4 hours of 8-hour slot = half
|
||||
result = calculate_entry_cost(slot,
|
||||
datetime(2025, 1, 1, 9, 0), datetime(2025, 1, 1, 13, 0))
|
||||
assert result == Decimal("40")
|
||||
|
||||
def test_flexible_no_time_end(self):
|
||||
slot = _slot(cost=50, flexible=True, time_end=None)
|
||||
result = calculate_entry_cost(slot,
|
||||
datetime(2025, 1, 1, 9), datetime(2025, 1, 1, 12))
|
||||
# When time_end is None, function still calculates based on booking duration
|
||||
assert isinstance(result, Decimal)
|
||||
|
||||
def test_flexible_zero_slot_duration(self):
|
||||
slot = _slot(cost=50, flexible=True, time_start=time(9, 0), time_end=time(9, 0))
|
||||
result = calculate_entry_cost(slot,
|
||||
datetime(2025, 1, 1, 9, 0), datetime(2025, 1, 1, 10, 0))
|
||||
assert result == Decimal("0")
|
||||
59
events/tests/test_slots.py
Normal file
59
events/tests/test_slots.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""Unit tests for slot helper functions."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from events.bp.calendar.services.slots import _b
|
||||
|
||||
|
||||
class TestBoolParse:
|
||||
def test_true_bool(self):
|
||||
assert _b(True) is True
|
||||
|
||||
def test_false_bool(self):
|
||||
assert _b(False) is False
|
||||
|
||||
def test_string_true(self):
|
||||
assert _b("true") is True
|
||||
|
||||
def test_string_True(self):
|
||||
assert _b("True") is True
|
||||
|
||||
def test_string_1(self):
|
||||
assert _b("1") is True
|
||||
|
||||
def test_string_yes(self):
|
||||
assert _b("yes") is True
|
||||
|
||||
def test_string_y(self):
|
||||
assert _b("y") is True
|
||||
|
||||
def test_string_t(self):
|
||||
assert _b("t") is True
|
||||
|
||||
def test_string_on(self):
|
||||
assert _b("on") is True
|
||||
|
||||
def test_string_false(self):
|
||||
assert _b("false") is False
|
||||
|
||||
def test_string_0(self):
|
||||
assert _b("0") is False
|
||||
|
||||
def test_string_no(self):
|
||||
assert _b("no") is False
|
||||
|
||||
def test_string_off(self):
|
||||
assert _b("off") is False
|
||||
|
||||
def test_string_empty(self):
|
||||
assert _b("") is False
|
||||
|
||||
def test_int_1(self):
|
||||
assert _b(1) is True
|
||||
|
||||
def test_int_0(self):
|
||||
assert _b(0) is False
|
||||
|
||||
def test_random_string(self):
|
||||
assert _b("maybe") is False
|
||||
42
events/tests/test_slugify.py
Normal file
42
events/tests/test_slugify.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""Unit tests for events slugify utility."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from events.bp.calendars.services.calendars import slugify
|
||||
|
||||
|
||||
class TestSlugify:
|
||||
def test_basic(self):
|
||||
assert slugify("My Calendar") == "my-calendar"
|
||||
|
||||
def test_unicode(self):
|
||||
assert slugify("café résumé") == "cafe-resume"
|
||||
|
||||
def test_slashes(self):
|
||||
assert slugify("foo/bar") == "foo-bar"
|
||||
|
||||
def test_special_chars(self):
|
||||
assert slugify("hello!!world") == "hello-world"
|
||||
|
||||
def test_collapse_dashes(self):
|
||||
assert slugify("a---b") == "a-b"
|
||||
|
||||
def test_strip_dashes(self):
|
||||
assert slugify("--hello--") == "hello"
|
||||
|
||||
def test_empty_fallback(self):
|
||||
assert slugify("") == "calendar"
|
||||
|
||||
def test_none_fallback(self):
|
||||
assert slugify(None) == "calendar"
|
||||
|
||||
def test_max_len(self):
|
||||
result = slugify("a" * 300, max_len=10)
|
||||
assert len(result) <= 10
|
||||
|
||||
def test_numbers(self):
|
||||
assert slugify("event-2025") == "event-2025"
|
||||
|
||||
def test_already_clean(self):
|
||||
assert slugify("my-event") == "my-event"
|
||||
0
federation/tests/__init__.py
Normal file
0
federation/tests/__init__.py
Normal file
137
federation/tests/test_dto_converters.py
Normal file
137
federation/tests/test_dto_converters.py
Normal file
@@ -0,0 +1,137 @@
|
||||
"""Unit tests for federation DTO conversion functions."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from shared.services.federation_impl import (
|
||||
_actor_to_dto, _activity_to_dto, _follower_to_dto,
|
||||
_remote_actor_to_dto, _remote_post_to_dto,
|
||||
)
|
||||
|
||||
|
||||
def _actor(**kwargs):
|
||||
defaults = {
|
||||
"id": 1, "user_id": 10,
|
||||
"preferred_username": "alice",
|
||||
"public_key_pem": "-----BEGIN PUBLIC KEY-----",
|
||||
"display_name": "Alice", "summary": "Hello",
|
||||
"created_at": datetime(2025, 1, 1),
|
||||
}
|
||||
defaults.update(kwargs)
|
||||
return SimpleNamespace(**defaults)
|
||||
|
||||
|
||||
def _activity(**kwargs):
|
||||
defaults = {
|
||||
"id": 1, "activity_id": "https://example.com/activity/1",
|
||||
"activity_type": "Create", "actor_profile_id": 1,
|
||||
"object_type": "Note", "object_data": {"content": "hi"},
|
||||
"published": datetime(2025, 1, 1), "is_local": True,
|
||||
"source_type": "post", "source_id": 42, "ipfs_cid": None,
|
||||
}
|
||||
defaults.update(kwargs)
|
||||
return SimpleNamespace(**defaults)
|
||||
|
||||
|
||||
def _follower(**kwargs):
|
||||
defaults = {
|
||||
"id": 1, "actor_profile_id": 1,
|
||||
"follower_acct": "bob@remote.com",
|
||||
"follower_inbox": "https://remote.com/inbox",
|
||||
"follower_actor_url": "https://remote.com/users/bob",
|
||||
"created_at": datetime(2025, 1, 1),
|
||||
"app_domain": "federation.rose-ash.com",
|
||||
}
|
||||
defaults.update(kwargs)
|
||||
return SimpleNamespace(**defaults)
|
||||
|
||||
|
||||
def _remote_actor(**kwargs):
|
||||
defaults = {
|
||||
"id": 1, "actor_url": "https://remote.com/users/bob",
|
||||
"inbox_url": "https://remote.com/inbox",
|
||||
"preferred_username": "bob", "domain": "remote.com",
|
||||
"display_name": "Bob", "summary": "Hi",
|
||||
"icon_url": "https://remote.com/avatar.jpg",
|
||||
"shared_inbox_url": None, "public_key_pem": None,
|
||||
}
|
||||
defaults.update(kwargs)
|
||||
return SimpleNamespace(**defaults)
|
||||
|
||||
|
||||
def _remote_post(**kwargs):
|
||||
defaults = {
|
||||
"id": 1, "remote_actor_id": 1,
|
||||
"object_id": "https://remote.com/note/1",
|
||||
"content": "<p>Hello</p>", "summary": None,
|
||||
"url": "https://remote.com/note/1",
|
||||
"attachment_data": [{"type": "Image", "url": "img.jpg"}],
|
||||
"tag_data": [{"type": "Hashtag", "name": "#test"}],
|
||||
"published": datetime(2025, 1, 1),
|
||||
}
|
||||
defaults.update(kwargs)
|
||||
return SimpleNamespace(**defaults)
|
||||
|
||||
|
||||
class TestActorToDto:
|
||||
@patch("shared.services.federation_impl._domain", return_value="fed.example.com")
|
||||
def test_basic(self, mock_domain):
|
||||
dto = _actor_to_dto(_actor())
|
||||
assert dto.preferred_username == "alice"
|
||||
assert dto.inbox_url == "https://fed.example.com/users/alice/inbox"
|
||||
assert dto.outbox_url == "https://fed.example.com/users/alice/outbox"
|
||||
assert dto.user_id == 10
|
||||
assert dto.display_name == "Alice"
|
||||
|
||||
|
||||
class TestActivityToDto:
|
||||
def test_fields(self):
|
||||
dto = _activity_to_dto(_activity())
|
||||
assert dto.activity_type == "Create"
|
||||
assert dto.object_type == "Note"
|
||||
assert dto.is_local is True
|
||||
assert dto.ipfs_cid is None
|
||||
|
||||
|
||||
class TestFollowerToDto:
|
||||
def test_fields(self):
|
||||
dto = _follower_to_dto(_follower())
|
||||
assert dto.follower_acct == "bob@remote.com"
|
||||
assert dto.app_domain == "federation.rose-ash.com"
|
||||
|
||||
|
||||
class TestRemoteActorToDto:
|
||||
def test_fields(self):
|
||||
dto = _remote_actor_to_dto(_remote_actor())
|
||||
assert dto.preferred_username == "bob"
|
||||
assert dto.domain == "remote.com"
|
||||
assert dto.icon_url == "https://remote.com/avatar.jpg"
|
||||
|
||||
|
||||
class TestRemotePostToDto:
|
||||
def test_with_actor(self):
|
||||
actor = _remote_actor()
|
||||
dto = _remote_post_to_dto(_remote_post(), actor=actor)
|
||||
assert dto.content == "<p>Hello</p>"
|
||||
assert dto.actor is not None
|
||||
assert dto.actor.preferred_username == "bob"
|
||||
|
||||
def test_without_actor(self):
|
||||
dto = _remote_post_to_dto(_remote_post(), actor=None)
|
||||
assert dto.actor is None
|
||||
|
||||
def test_none_content_becomes_empty(self):
|
||||
dto = _remote_post_to_dto(_remote_post(content=None))
|
||||
assert dto.content == ""
|
||||
|
||||
def test_none_attachments_becomes_list(self):
|
||||
dto = _remote_post_to_dto(_remote_post(attachment_data=None))
|
||||
assert dto.attachments == []
|
||||
|
||||
def test_none_tags_becomes_list(self):
|
||||
dto = _remote_post_to_dto(_remote_post(tag_data=None))
|
||||
assert dto.tags == []
|
||||
70
federation/tests/test_identity.py
Normal file
70
federation/tests/test_identity.py
Normal file
@@ -0,0 +1,70 @@
|
||||
"""Unit tests for federation identity validation."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from federation.bp.identity.routes import USERNAME_RE, RESERVED
|
||||
|
||||
|
||||
class TestUsernameRegex:
|
||||
def test_valid_simple(self):
|
||||
assert USERNAME_RE.match("alice")
|
||||
|
||||
def test_valid_with_numbers(self):
|
||||
assert USERNAME_RE.match("user42")
|
||||
|
||||
def test_valid_with_underscore(self):
|
||||
assert USERNAME_RE.match("alice_bob")
|
||||
|
||||
def test_min_length(self):
|
||||
assert USERNAME_RE.match("abc")
|
||||
|
||||
def test_too_short(self):
|
||||
assert not USERNAME_RE.match("ab")
|
||||
|
||||
def test_single_char(self):
|
||||
assert not USERNAME_RE.match("a")
|
||||
|
||||
def test_max_length(self):
|
||||
assert USERNAME_RE.match("a" * 32)
|
||||
|
||||
def test_too_long(self):
|
||||
assert not USERNAME_RE.match("a" * 33)
|
||||
|
||||
def test_starts_with_digit(self):
|
||||
assert not USERNAME_RE.match("1abc")
|
||||
|
||||
def test_starts_with_underscore(self):
|
||||
assert not USERNAME_RE.match("_abc")
|
||||
|
||||
def test_uppercase_rejected(self):
|
||||
assert not USERNAME_RE.match("Alice")
|
||||
|
||||
def test_hyphen_rejected(self):
|
||||
assert not USERNAME_RE.match("alice-bob")
|
||||
|
||||
def test_spaces_rejected(self):
|
||||
assert not USERNAME_RE.match("alice bob")
|
||||
|
||||
def test_empty_rejected(self):
|
||||
assert not USERNAME_RE.match("")
|
||||
|
||||
|
||||
class TestReservedUsernames:
|
||||
def test_admin_reserved(self):
|
||||
assert "admin" in RESERVED
|
||||
|
||||
def test_root_reserved(self):
|
||||
assert "root" in RESERVED
|
||||
|
||||
def test_api_reserved(self):
|
||||
assert "api" in RESERVED
|
||||
|
||||
def test_inbox_reserved(self):
|
||||
assert "inbox" in RESERVED
|
||||
|
||||
def test_normal_name_not_reserved(self):
|
||||
assert "alice" not in RESERVED
|
||||
|
||||
def test_at_least_20_reserved(self):
|
||||
assert len(RESERVED) >= 20
|
||||
0
likes/tests/__init__.py
Normal file
0
likes/tests/__init__.py
Normal file
76
likes/tests/test_data_guards.py
Normal file
76
likes/tests/test_data_guards.py
Normal file
@@ -0,0 +1,76 @@
|
||||
"""Unit tests for likes data endpoint guard logic.
|
||||
|
||||
The actual data handlers require Quart request context and DB,
|
||||
but we can test the guard validation logic patterns used throughout.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class TestLikedGuardPatterns:
|
||||
"""Test the validation patterns used in likes data endpoints.
|
||||
|
||||
The handlers return early with empty/false results when required
|
||||
params are missing. These tests verify those conditions.
|
||||
"""
|
||||
|
||||
def test_is_liked_requires_user_id(self):
|
||||
"""Without user_id, is_liked returns {'liked': False}."""
|
||||
# Simulates: user_id = None, target_type = "product"
|
||||
user_id = None
|
||||
target_type = "product"
|
||||
if not user_id or not target_type:
|
||||
result = {"liked": False}
|
||||
else:
|
||||
result = {"liked": True} # would check DB
|
||||
assert result == {"liked": False}
|
||||
|
||||
def test_is_liked_requires_target_type(self):
|
||||
user_id = 1
|
||||
target_type = ""
|
||||
if not user_id or not target_type:
|
||||
result = {"liked": False}
|
||||
else:
|
||||
result = {"liked": True}
|
||||
assert result == {"liked": False}
|
||||
|
||||
def test_is_liked_requires_slug_or_id(self):
|
||||
"""Without target_slug or target_id, returns {'liked': False}."""
|
||||
target_slug = None
|
||||
target_id = None
|
||||
if target_slug is None and target_id is None:
|
||||
result = {"liked": False}
|
||||
else:
|
||||
result = {"liked": True}
|
||||
assert result == {"liked": False}
|
||||
|
||||
def test_liked_slugs_empty_without_user_id(self):
|
||||
user_id = None
|
||||
target_type = "product"
|
||||
if not user_id or not target_type:
|
||||
result = []
|
||||
else:
|
||||
result = ["slug-1"]
|
||||
assert result == []
|
||||
|
||||
def test_liked_ids_empty_without_target_type(self):
|
||||
user_id = 1
|
||||
target_type = ""
|
||||
if not user_id or not target_type:
|
||||
result = []
|
||||
else:
|
||||
result = [1, 2]
|
||||
assert result == []
|
||||
|
||||
def test_all_params_present(self):
|
||||
user_id = 1
|
||||
target_type = "product"
|
||||
target_slug = "my-product"
|
||||
if not user_id or not target_type:
|
||||
result = {"liked": False}
|
||||
elif target_slug is not None:
|
||||
result = {"liked": True} # would check DB
|
||||
else:
|
||||
result = {"liked": False}
|
||||
assert result == {"liked": True}
|
||||
0
market/tests/__init__.py
Normal file
0
market/tests/__init__.py
Normal file
21
market/tests/conftest.py
Normal file
21
market/tests/conftest.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""Market test fixtures — direct module loading to avoid bp __init__ chains."""
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import sys
|
||||
|
||||
|
||||
def _load(name: str, path: str):
|
||||
"""Import a .py file directly, bypassing package __init__ chains."""
|
||||
if name in sys.modules:
|
||||
return sys.modules[name]
|
||||
spec = importlib.util.spec_from_file_location(name, path)
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
sys.modules[name] = mod
|
||||
spec.loader.exec_module(mod)
|
||||
return mod
|
||||
|
||||
|
||||
sys.path.insert(0, "/app/market")
|
||||
|
||||
_load("market.scrape.listings", "/app/market/scrape/listings.py")
|
||||
78
market/tests/test_listings.py
Normal file
78
market/tests/test_listings.py
Normal file
@@ -0,0 +1,78 @@
|
||||
"""Unit tests for listings parsing utilities."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from market.scrape.listings import (
|
||||
parse_total_pages_from_text,
|
||||
_first_from_srcset,
|
||||
_dedupe_preserve_order_by,
|
||||
_filename_key,
|
||||
)
|
||||
|
||||
|
||||
class TestParseTotalPages:
|
||||
def test_standard(self):
|
||||
assert parse_total_pages_from_text("Showing 36 of 120") == 4
|
||||
|
||||
def test_exact_fit(self):
|
||||
assert parse_total_pages_from_text("Showing 36 of 36") == 1
|
||||
|
||||
def test_partial_page(self):
|
||||
assert parse_total_pages_from_text("Showing 36 of 37") == 2
|
||||
|
||||
def test_no_match(self):
|
||||
assert parse_total_pages_from_text("no data") is None
|
||||
|
||||
def test_case_insensitive(self):
|
||||
# shown=12 is in {12,24,36} so per_page=36, ceil(60/36)=2
|
||||
assert parse_total_pages_from_text("showing 12 of 60") == 2
|
||||
|
||||
def test_small_shown(self):
|
||||
# shown=10, not in {12,24,36}, so per_page=10
|
||||
assert parse_total_pages_from_text("Showing 10 of 100") == 10
|
||||
|
||||
|
||||
class TestFirstFromSrcset:
|
||||
def test_single_entry(self):
|
||||
assert _first_from_srcset("img.jpg 1x") == "img.jpg"
|
||||
|
||||
def test_multiple_entries(self):
|
||||
assert _first_from_srcset("a.jpg 1x, b.jpg 2x") == "a.jpg"
|
||||
|
||||
def test_empty(self):
|
||||
assert _first_from_srcset("") is None
|
||||
|
||||
def test_none(self):
|
||||
assert _first_from_srcset(None) is None
|
||||
|
||||
|
||||
class TestDedupePreserveOrder:
|
||||
def test_no_dupes(self):
|
||||
result = _dedupe_preserve_order_by(["a", "b", "c"], key=str)
|
||||
assert result == ["a", "b", "c"]
|
||||
|
||||
def test_removes_dupes(self):
|
||||
result = _dedupe_preserve_order_by(["a", "b", "a"], key=str)
|
||||
assert result == ["a", "b"]
|
||||
|
||||
def test_empty_strings_skipped(self):
|
||||
result = _dedupe_preserve_order_by(["a", "", "b"], key=str)
|
||||
assert result == ["a", "b"]
|
||||
|
||||
def test_preserves_order(self):
|
||||
result = _dedupe_preserve_order_by(["c", "b", "a", "b"], key=str)
|
||||
assert result == ["c", "b", "a"]
|
||||
|
||||
|
||||
class TestFilenameKey:
|
||||
def test_basic(self):
|
||||
assert _filename_key("https://img.com/photos/pic.jpg") == "img.com:pic.jpg"
|
||||
|
||||
def test_trailing_slash(self):
|
||||
k = _filename_key("https://img.com/photos/")
|
||||
assert k == "img.com:photos"
|
||||
|
||||
def test_case_insensitive(self):
|
||||
k = _filename_key("https://IMG.COM/PIC.JPG")
|
||||
assert k == "img.com:pic.jpg"
|
||||
96
market/tests/test_price.py
Normal file
96
market/tests/test_price.py
Normal file
@@ -0,0 +1,96 @@
|
||||
"""Unit tests for price parsing utilities."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from market.scrape.product.helpers.price import parse_price, parse_case_size
|
||||
|
||||
|
||||
class TestParsePrice:
|
||||
def test_gbp(self):
|
||||
val, cur, raw = parse_price("£30.50")
|
||||
assert val == 30.5
|
||||
assert cur == "GBP"
|
||||
|
||||
def test_eur(self):
|
||||
val, cur, raw = parse_price("€1,234.00")
|
||||
assert val == 1234.0
|
||||
assert cur == "EUR"
|
||||
|
||||
def test_usd(self):
|
||||
val, cur, raw = parse_price("$9.99")
|
||||
assert val == 9.99
|
||||
assert cur == "USD"
|
||||
|
||||
def test_no_symbol(self):
|
||||
val, cur, raw = parse_price("42.50")
|
||||
assert val == 42.5
|
||||
assert cur is None
|
||||
|
||||
def test_no_match(self):
|
||||
val, cur, raw = parse_price("no price here")
|
||||
assert val is None
|
||||
assert cur is None
|
||||
|
||||
def test_empty_string(self):
|
||||
val, cur, raw = parse_price("")
|
||||
assert val is None
|
||||
assert raw == ""
|
||||
|
||||
def test_none_input(self):
|
||||
val, cur, raw = parse_price(None)
|
||||
assert val is None
|
||||
assert raw == ""
|
||||
|
||||
def test_thousands_comma_stripped(self):
|
||||
val, cur, raw = parse_price("£1,000.50")
|
||||
assert val == 1000.5
|
||||
|
||||
def test_whitespace_around_symbol(self):
|
||||
val, cur, raw = parse_price("£ 5.00")
|
||||
assert val == 5.0
|
||||
assert cur == "GBP"
|
||||
|
||||
def test_raw_preserved(self):
|
||||
_, _, raw = parse_price(" £10.00 ")
|
||||
assert raw == "£10.00"
|
||||
|
||||
|
||||
class TestParseCaseSize:
|
||||
def test_standard(self):
|
||||
count, qty, unit, _ = parse_case_size("6 x 500g")
|
||||
assert count == 6
|
||||
assert qty == 500.0
|
||||
assert unit == "g"
|
||||
|
||||
def test_no_space(self):
|
||||
count, qty, unit, _ = parse_case_size("12x1L")
|
||||
assert count == 12
|
||||
assert qty == 1.0
|
||||
assert unit == "L"
|
||||
|
||||
def test_multiplication_sign(self):
|
||||
count, qty, unit, _ = parse_case_size("24 × 330 ml")
|
||||
assert count == 24
|
||||
assert qty == 330.0
|
||||
assert unit == "ml"
|
||||
|
||||
def test_uppercase_x(self):
|
||||
count, qty, unit, _ = parse_case_size("6X500g")
|
||||
assert count == 6
|
||||
|
||||
def test_no_match(self):
|
||||
count, qty, unit, raw = parse_case_size("just text")
|
||||
assert count is None
|
||||
assert qty is None
|
||||
assert unit is None
|
||||
|
||||
def test_empty(self):
|
||||
count, qty, unit, raw = parse_case_size("")
|
||||
assert count is None
|
||||
assert raw == ""
|
||||
|
||||
def test_none_input(self):
|
||||
count, _, _, raw = parse_case_size(None)
|
||||
assert count is None
|
||||
assert raw == ""
|
||||
58
market/tests/test_registry.py
Normal file
58
market/tests/test_registry.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""Unit tests for product registry utilities."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from market.scrape.product.registry import merge_missing
|
||||
|
||||
|
||||
class TestMergeMissing:
|
||||
def test_fills_empty_dict(self):
|
||||
dst = {}
|
||||
merge_missing(dst, {"a": 1, "b": "hello"})
|
||||
assert dst == {"a": 1, "b": "hello"}
|
||||
|
||||
def test_existing_value_not_overwritten(self):
|
||||
dst = {"a": "original"}
|
||||
merge_missing(dst, {"a": "new"})
|
||||
assert dst["a"] == "original"
|
||||
|
||||
def test_none_overwritten(self):
|
||||
dst = {"a": None}
|
||||
merge_missing(dst, {"a": "filled"})
|
||||
assert dst["a"] == "filled"
|
||||
|
||||
def test_empty_string_overwritten(self):
|
||||
dst = {"a": ""}
|
||||
merge_missing(dst, {"a": "filled"})
|
||||
assert dst["a"] == "filled"
|
||||
|
||||
def test_empty_list_overwritten(self):
|
||||
dst = {"a": []}
|
||||
merge_missing(dst, {"a": [1, 2]})
|
||||
assert dst["a"] == [1, 2]
|
||||
|
||||
def test_empty_dict_overwritten(self):
|
||||
dst = {"a": {}}
|
||||
merge_missing(dst, {"a": {"key": "val"}})
|
||||
assert dst["a"] == {"key": "val"}
|
||||
|
||||
def test_zero_not_overwritten(self):
|
||||
dst = {"a": 0}
|
||||
merge_missing(dst, {"a": 42})
|
||||
assert dst["a"] == 0
|
||||
|
||||
def test_false_not_overwritten(self):
|
||||
dst = {"a": False}
|
||||
merge_missing(dst, {"a": True})
|
||||
assert dst["a"] is False
|
||||
|
||||
def test_none_src(self):
|
||||
dst = {"a": 1}
|
||||
merge_missing(dst, None)
|
||||
assert dst == {"a": 1}
|
||||
|
||||
def test_new_keys_added(self):
|
||||
dst = {"a": 1}
|
||||
merge_missing(dst, {"b": 2})
|
||||
assert dst == {"a": 1, "b": 2}
|
||||
45
market/tests/test_slugs.py
Normal file
45
market/tests/test_slugs.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""Unit tests for market slug helpers."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from market.bp.browse.services.slugs import (
|
||||
product_slug_from_href, canonical_html_slug,
|
||||
)
|
||||
|
||||
|
||||
class TestProductSlugFromHref:
|
||||
def test_html_extension(self):
|
||||
result = product_slug_from_href("https://site.com/foo/bar-thing.html")
|
||||
assert result == "bar-thing-html"
|
||||
|
||||
def test_htm_extension(self):
|
||||
result = product_slug_from_href("https://site.com/products/widget.htm")
|
||||
assert result == "widget-html"
|
||||
|
||||
def test_no_extension(self):
|
||||
result = product_slug_from_href("https://site.com/item/cool-product")
|
||||
assert result == "cool-product-html"
|
||||
|
||||
def test_empty_path(self):
|
||||
result = product_slug_from_href("https://site.com/")
|
||||
assert result == ""
|
||||
|
||||
def test_already_has_html_suffix_in_slug(self):
|
||||
result = product_slug_from_href("https://site.com/prod/item-html.html")
|
||||
assert result == "item-html"
|
||||
|
||||
|
||||
class TestCanonicalHtmlSlug:
|
||||
def test_adds_html_suffix(self):
|
||||
assert canonical_html_slug("product-name") == "product-name-html"
|
||||
|
||||
def test_idempotent(self):
|
||||
assert canonical_html_slug("product-name-html") == "product-name-html"
|
||||
|
||||
def test_double_html_kept(self):
|
||||
# canonical_html_slug only appends -html if not already present
|
||||
assert canonical_html_slug("product-name-html-html") == "product-name-html-html"
|
||||
|
||||
def test_strips_htm(self):
|
||||
assert canonical_html_slug("product-name-htm") == "product-name-html"
|
||||
0
orders/tests/__init__.py
Normal file
0
orders/tests/__init__.py
Normal file
32
orders/tests/test_sexp_helpers.py
Normal file
32
orders/tests/test_sexp_helpers.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""Unit tests for orders sexp component helpers."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from orders.sexp.sexp_components import _status_pill_cls
|
||||
|
||||
|
||||
class TestStatusPillCls:
|
||||
def test_paid(self):
|
||||
result = _status_pill_cls("paid")
|
||||
assert "emerald" in result
|
||||
|
||||
def test_Paid_uppercase(self):
|
||||
result = _status_pill_cls("Paid")
|
||||
assert "emerald" in result
|
||||
|
||||
def test_failed(self):
|
||||
result = _status_pill_cls("failed")
|
||||
assert "rose" in result
|
||||
|
||||
def test_cancelled(self):
|
||||
result = _status_pill_cls("cancelled")
|
||||
assert "rose" in result
|
||||
|
||||
def test_pending(self):
|
||||
result = _status_pill_cls("pending")
|
||||
assert "stone" in result
|
||||
|
||||
def test_unknown(self):
|
||||
result = _status_pill_cls("refunded")
|
||||
assert "stone" in result
|
||||
0
relations/tests/__init__.py
Normal file
0
relations/tests/__init__.py
Normal file
66
relations/tests/test_serialize.py
Normal file
66
relations/tests/test_serialize.py
Normal file
@@ -0,0 +1,66 @@
|
||||
"""Unit tests for relations serialization."""
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from relations.bp.data.routes import _serialize_rel
|
||||
|
||||
|
||||
class TestSerializeRel:
|
||||
def test_all_fields(self):
|
||||
rel = SimpleNamespace(
|
||||
id=1,
|
||||
parent_type="page",
|
||||
parent_id=100,
|
||||
child_type="calendar",
|
||||
child_id=200,
|
||||
sort_order=0,
|
||||
label="Main Calendar",
|
||||
relation_type="container",
|
||||
metadata_={"key": "val"},
|
||||
)
|
||||
result = _serialize_rel(rel)
|
||||
assert result == {
|
||||
"id": 1,
|
||||
"parent_type": "page",
|
||||
"parent_id": 100,
|
||||
"child_type": "calendar",
|
||||
"child_id": 200,
|
||||
"sort_order": 0,
|
||||
"label": "Main Calendar",
|
||||
"relation_type": "container",
|
||||
"metadata": {"key": "val"},
|
||||
}
|
||||
|
||||
def test_none_optionals(self):
|
||||
rel = SimpleNamespace(
|
||||
id=2,
|
||||
parent_type="post",
|
||||
parent_id=1,
|
||||
child_type="market",
|
||||
child_id=1,
|
||||
sort_order=1,
|
||||
label=None,
|
||||
relation_type=None,
|
||||
metadata_=None,
|
||||
)
|
||||
result = _serialize_rel(rel)
|
||||
assert result["label"] is None
|
||||
assert result["relation_type"] is None
|
||||
assert result["metadata"] is None
|
||||
|
||||
def test_dict_structure(self):
|
||||
rel = SimpleNamespace(
|
||||
id=3, parent_type="a", parent_id=1,
|
||||
child_type="b", child_id=2,
|
||||
sort_order=0, label="", relation_type="link",
|
||||
metadata_={},
|
||||
)
|
||||
result = _serialize_rel(rel)
|
||||
assert set(result.keys()) == {
|
||||
"id", "parent_type", "parent_id",
|
||||
"child_type", "child_id", "sort_order",
|
||||
"label", "relation_type", "metadata",
|
||||
}
|
||||
11
test/app.py
11
test/app.py
@@ -3,6 +3,7 @@ import path_setup # noqa: F401
|
||||
import sexp.sexp_components as sexp_components # noqa: F401
|
||||
|
||||
from shared.infrastructure.factory import create_base_app
|
||||
from shared.sexp.jinja_bridge import render
|
||||
|
||||
from bp import register_dashboard
|
||||
from services import register_domain_services
|
||||
@@ -14,7 +15,15 @@ async def test_context() -> dict:
|
||||
|
||||
ctx = await base_context()
|
||||
ctx["menu_items"] = []
|
||||
ctx["cart_mini_html"] = ""
|
||||
# Render cart-mini with cart_count=0 to show the logo image
|
||||
blog_url = ctx.get("blog_url", "")
|
||||
if callable(blog_url):
|
||||
blog_url_str = blog_url("")
|
||||
else:
|
||||
blog_url_str = str(blog_url or "")
|
||||
ctx["cart_mini_html"] = render(
|
||||
"cart-mini", cart_count=0, blog_url=blog_url_str, cart_url="",
|
||||
)
|
||||
ctx["auth_menu_html"] = ""
|
||||
ctx["nav_tree_html"] = ""
|
||||
return ctx
|
||||
|
||||
@@ -21,8 +21,14 @@ def register(url_prefix: str = "/") -> Blueprint:
|
||||
result = runner.get_results()
|
||||
running = runner.is_running()
|
||||
csrf = generate_csrf_token()
|
||||
active_filter = request.args.get("filter")
|
||||
active_service = request.args.get("service")
|
||||
|
||||
html = await render_dashboard_page(ctx, result, running, csrf)
|
||||
html = await render_dashboard_page(
|
||||
ctx, result, running, csrf,
|
||||
active_filter=active_filter,
|
||||
active_service=active_service,
|
||||
)
|
||||
return await make_response(html, 200)
|
||||
|
||||
@bp.post("/run")
|
||||
@@ -52,8 +58,14 @@ def register(url_prefix: str = "/") -> Blueprint:
|
||||
result = runner.get_results()
|
||||
running = runner.is_running()
|
||||
csrf = generate_csrf_token()
|
||||
active_filter = request.args.get("filter")
|
||||
active_service = request.args.get("service")
|
||||
|
||||
html = await render_results_partial(result, running, csrf)
|
||||
html = await render_results_partial(
|
||||
result, running, csrf,
|
||||
active_filter=active_filter,
|
||||
active_service=active_service,
|
||||
)
|
||||
|
||||
resp = Response(html, status=200, content_type="text/html")
|
||||
# If still running, tell HTMX to keep polling
|
||||
|
||||
209
test/runner.py
209
test/runner.py
@@ -4,7 +4,9 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from collections import OrderedDict
|
||||
from pathlib import Path
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@@ -13,13 +15,81 @@ log = logging.getLogger(__name__)
|
||||
_last_result: dict | None = None
|
||||
_running: bool = False
|
||||
|
||||
# Paths to test directories (relative to /app in Docker)
|
||||
_TEST_DIRS = [
|
||||
"shared/tests/",
|
||||
"shared/sexp/tests/",
|
||||
# Each service group runs in its own pytest subprocess with its own PYTHONPATH
|
||||
_SERVICE_GROUPS: list[dict] = [
|
||||
{"name": "shared", "dirs": ["shared/tests/", "shared/sexp/tests/"],
|
||||
"pythonpath": None},
|
||||
{"name": "blog", "dirs": ["blog/tests/"], "pythonpath": "/app/blog"},
|
||||
{"name": "market", "dirs": ["market/tests/"], "pythonpath": "/app/market"},
|
||||
{"name": "cart", "dirs": ["cart/tests/"], "pythonpath": "/app/cart"},
|
||||
{"name": "events", "dirs": ["events/tests/"], "pythonpath": "/app/events"},
|
||||
{"name": "account", "dirs": ["account/tests/"], "pythonpath": "/app/account"},
|
||||
{"name": "orders", "dirs": ["orders/tests/"], "pythonpath": "/app/orders"},
|
||||
{"name": "federation", "dirs": ["federation/tests/"],
|
||||
"pythonpath": "/app/federation"},
|
||||
{"name": "relations", "dirs": ["relations/tests/"],
|
||||
"pythonpath": "/app/relations"},
|
||||
{"name": "likes", "dirs": ["likes/tests/"], "pythonpath": "/app/likes"},
|
||||
]
|
||||
|
||||
_REPORT_PATH = "/tmp/test-report.json"
|
||||
_SERVICE_ORDER = [g["name"] for g in _SERVICE_GROUPS]
|
||||
_REPORT_PATH = "/tmp/test-report-{}.json"
|
||||
|
||||
|
||||
def _parse_report(path: str) -> tuple[list[dict], dict]:
|
||||
"""Parse a pytest-json-report file."""
|
||||
rp = Path(path)
|
||||
if not rp.exists():
|
||||
return [], {}
|
||||
try:
|
||||
report = json.loads(rp.read_text())
|
||||
except (json.JSONDecodeError, OSError):
|
||||
return [], {}
|
||||
|
||||
summary = report.get("summary", {})
|
||||
tests_raw = report.get("tests", [])
|
||||
|
||||
tests = []
|
||||
for t in tests_raw:
|
||||
tests.append({
|
||||
"nodeid": t.get("nodeid", ""),
|
||||
"outcome": t.get("outcome", "unknown"),
|
||||
"duration": round(t.get("duration", 0), 4),
|
||||
"longrepr": (t.get("call", {}) or {}).get("longrepr", ""),
|
||||
})
|
||||
return tests, summary
|
||||
|
||||
|
||||
async def _run_group(group: dict) -> tuple[list[dict], dict, str]:
|
||||
"""Run pytest for a single service group."""
|
||||
existing = [d for d in group["dirs"] if Path(f"/app/{d}").is_dir()]
|
||||
if not existing:
|
||||
return [], {}, ""
|
||||
|
||||
report_file = _REPORT_PATH.format(group["name"])
|
||||
cmd = [
|
||||
"python3", "-m", "pytest",
|
||||
*existing,
|
||||
"--json-report",
|
||||
f"--json-report-file={report_file}",
|
||||
"-q",
|
||||
"--tb=short",
|
||||
]
|
||||
env = {**os.environ}
|
||||
if group["pythonpath"]:
|
||||
env["PYTHONPATH"] = group["pythonpath"] + ":" + env.get("PYTHONPATH", "")
|
||||
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.STDOUT,
|
||||
cwd="/app",
|
||||
env=env,
|
||||
)
|
||||
stdout, _ = await proc.communicate()
|
||||
stdout_str = (stdout or b"").decode("utf-8", errors="replace")
|
||||
tests, summary = _parse_report(report_file)
|
||||
return tests, summary, stdout_str
|
||||
|
||||
|
||||
async def run_tests() -> dict:
|
||||
@@ -33,74 +103,48 @@ async def run_tests() -> dict:
|
||||
started_at = time.time()
|
||||
|
||||
try:
|
||||
cmd = [
|
||||
"python3", "-m", "pytest",
|
||||
*_TEST_DIRS,
|
||||
"--json-report",
|
||||
f"--json-report-file={_REPORT_PATH}",
|
||||
"-q",
|
||||
"--tb=short",
|
||||
]
|
||||
tasks = [_run_group(g) for g in _SERVICE_GROUPS]
|
||||
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||
|
||||
all_tests: list[dict] = []
|
||||
total_passed = total_failed = total_errors = total_skipped = total_count = 0
|
||||
all_stdout: list[str] = []
|
||||
|
||||
for i, res in enumerate(results):
|
||||
if isinstance(res, Exception):
|
||||
log.error("Group %s failed: %s", _SERVICE_GROUPS[i]["name"], res)
|
||||
continue
|
||||
tests, summary, stdout_str = res
|
||||
all_tests.extend(tests)
|
||||
total_passed += summary.get("passed", 0)
|
||||
total_failed += summary.get("failed", 0)
|
||||
total_errors += summary.get("error", 0)
|
||||
total_skipped += summary.get("skipped", 0)
|
||||
total_count += summary.get("total", len(tests))
|
||||
if stdout_str.strip():
|
||||
all_stdout.append(stdout_str)
|
||||
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.STDOUT,
|
||||
cwd="/app",
|
||||
)
|
||||
stdout, _ = await proc.communicate()
|
||||
finished_at = time.time()
|
||||
|
||||
# Parse JSON report
|
||||
report_path = Path(_REPORT_PATH)
|
||||
if report_path.exists():
|
||||
try:
|
||||
report = json.loads(report_path.read_text())
|
||||
except (json.JSONDecodeError, OSError):
|
||||
report = {}
|
||||
else:
|
||||
report = {}
|
||||
|
||||
summary = report.get("summary", {})
|
||||
tests_raw = report.get("tests", [])
|
||||
|
||||
tests = []
|
||||
for t in tests_raw:
|
||||
tests.append({
|
||||
"nodeid": t.get("nodeid", ""),
|
||||
"outcome": t.get("outcome", "unknown"),
|
||||
"duration": round(t.get("duration", 0), 4),
|
||||
"longrepr": (t.get("call", {}) or {}).get("longrepr", ""),
|
||||
})
|
||||
|
||||
passed = summary.get("passed", 0)
|
||||
failed = summary.get("failed", 0)
|
||||
errors = summary.get("error", 0)
|
||||
skipped = summary.get("skipped", 0)
|
||||
total = summary.get("total", len(tests))
|
||||
|
||||
if failed > 0 or errors > 0:
|
||||
status = "failed"
|
||||
else:
|
||||
status = "passed"
|
||||
status = "failed" if total_failed > 0 or total_errors > 0 else "passed"
|
||||
|
||||
_last_result = {
|
||||
"status": status,
|
||||
"started_at": started_at,
|
||||
"finished_at": finished_at,
|
||||
"duration": round(finished_at - started_at, 2),
|
||||
"passed": passed,
|
||||
"failed": failed,
|
||||
"errors": errors,
|
||||
"skipped": skipped,
|
||||
"total": total,
|
||||
"tests": tests,
|
||||
"stdout": (stdout or b"").decode("utf-8", errors="replace")[-5000:],
|
||||
"passed": total_passed,
|
||||
"failed": total_failed,
|
||||
"errors": total_errors,
|
||||
"skipped": total_skipped,
|
||||
"total": total_count,
|
||||
"tests": all_tests,
|
||||
"stdout": "\n".join(all_stdout)[-5000:],
|
||||
}
|
||||
|
||||
log.info(
|
||||
"Test run complete: %s (%d passed, %d failed, %d errors, %.1fs)",
|
||||
status, passed, failed, errors, _last_result["duration"],
|
||||
status, total_passed, total_failed, total_errors,
|
||||
_last_result["duration"],
|
||||
)
|
||||
return _last_result
|
||||
|
||||
@@ -130,6 +174,49 @@ def get_results() -> dict | None:
|
||||
return _last_result
|
||||
|
||||
|
||||
def get_test(nodeid: str) -> dict | None:
|
||||
"""Look up a single test by nodeid."""
|
||||
if not _last_result:
|
||||
return None
|
||||
for t in _last_result["tests"]:
|
||||
if t["nodeid"] == nodeid:
|
||||
return t
|
||||
return None
|
||||
|
||||
|
||||
def is_running() -> bool:
|
||||
"""Check if tests are currently running."""
|
||||
return _running
|
||||
|
||||
|
||||
def _service_from_nodeid(nodeid: str) -> str:
|
||||
"""Extract service name from a test nodeid."""
|
||||
parts = nodeid.split("/")
|
||||
return parts[0] if len(parts) >= 2 else "other"
|
||||
|
||||
|
||||
def group_tests_by_service(tests: list[dict]) -> list[dict]:
|
||||
"""Group tests into ordered sections by service."""
|
||||
buckets: dict[str, list[dict]] = OrderedDict()
|
||||
for svc in _SERVICE_ORDER:
|
||||
buckets[svc] = []
|
||||
for t in tests:
|
||||
svc = _service_from_nodeid(t["nodeid"])
|
||||
if svc not in buckets:
|
||||
buckets[svc] = []
|
||||
buckets[svc].append(t)
|
||||
|
||||
sections = []
|
||||
for svc, svc_tests in buckets.items():
|
||||
if not svc_tests:
|
||||
continue
|
||||
sections.append({
|
||||
"service": svc,
|
||||
"tests": svc_tests,
|
||||
"total": len(svc_tests),
|
||||
"passed": sum(1 for t in svc_tests if t["outcome"] == "passed"),
|
||||
"failed": sum(1 for t in svc_tests if t["outcome"] == "failed"),
|
||||
"errors": sum(1 for t in svc_tests if t["outcome"] == "error"),
|
||||
"skipped": sum(1 for t in svc_tests if t["outcome"] == "skipped"),
|
||||
})
|
||||
return sections
|
||||
|
||||
@@ -17,7 +17,20 @@
|
||||
:disabled (if running "true" nil)
|
||||
(if running "Running..." "Run Tests"))))
|
||||
|
||||
(defcomp ~test-summary (&key status passed failed errors skipped total duration last-run running csrf)
|
||||
(defcomp ~test-filter-card (&key href label count colour-border colour-bg colour-text active)
|
||||
(a :href href
|
||||
:hx-get href
|
||||
:hx-target "#main-panel"
|
||||
:hx-select "#main-panel"
|
||||
:hx-swap "outerHTML"
|
||||
:hx-push-url "true"
|
||||
:class (str "block rounded border p-3 text-center transition-colors no-underline hover:opacity-80 "
|
||||
colour-border " " colour-bg " "
|
||||
(if active "ring-2 ring-offset-1 ring-stone-500 " ""))
|
||||
(div :class (str "text-3xl font-bold " colour-text) count)
|
||||
(div :class (str "text-sm " colour-text) label)))
|
||||
|
||||
(defcomp ~test-summary (&key status passed failed errors skipped total duration last-run running csrf active-filter)
|
||||
(div :class "space-y-4"
|
||||
(div :class "flex items-center justify-between flex-wrap gap-3"
|
||||
(div :class "flex items-center gap-3"
|
||||
@@ -26,26 +39,38 @@
|
||||
(~test-run-button :running running :csrf csrf))
|
||||
(when status
|
||||
(div :class "grid grid-cols-2 sm:grid-cols-3 md:grid-cols-6 gap-3"
|
||||
(div :class "rounded border border-stone-200 bg-white p-3 text-center"
|
||||
(div :class "text-3xl font-bold text-stone-800" total)
|
||||
(div :class "text-sm text-stone-500" "Total"))
|
||||
(div :class "rounded border border-emerald-200 bg-emerald-50 p-3 text-center"
|
||||
(div :class "text-3xl font-bold text-emerald-700" passed)
|
||||
(div :class "text-sm text-emerald-600" "Passed"))
|
||||
(div :class "rounded border border-rose-200 bg-rose-50 p-3 text-center"
|
||||
(div :class "text-3xl font-bold text-rose-700" failed)
|
||||
(div :class "text-sm text-rose-600" "Failed"))
|
||||
(div :class "rounded border border-orange-200 bg-orange-50 p-3 text-center"
|
||||
(div :class "text-3xl font-bold text-orange-700" errors)
|
||||
(div :class "text-sm text-orange-600" "Errors"))
|
||||
(div :class "rounded border border-sky-200 bg-sky-50 p-3 text-center"
|
||||
(div :class "text-3xl font-bold text-sky-700" skipped)
|
||||
(div :class "text-sm text-sky-600" "Skipped"))
|
||||
(div :class "rounded border border-stone-200 bg-white p-3 text-center"
|
||||
(div :class "text-3xl font-bold text-stone-800" (str duration "s"))
|
||||
(div :class "text-sm text-stone-500" "Duration")))
|
||||
(~test-filter-card :href "/" :label "Total" :count total
|
||||
:colour-border "border-stone-200" :colour-bg "bg-white"
|
||||
:colour-text "text-stone-800"
|
||||
:active (if (= active-filter nil) "true" nil))
|
||||
(~test-filter-card :href "/?filter=passed" :label "Passed" :count passed
|
||||
:colour-border "border-emerald-200" :colour-bg "bg-emerald-50"
|
||||
:colour-text "text-emerald-700"
|
||||
:active (if (= active-filter "passed") "true" nil))
|
||||
(~test-filter-card :href "/?filter=failed" :label "Failed" :count failed
|
||||
:colour-border "border-rose-200" :colour-bg "bg-rose-50"
|
||||
:colour-text "text-rose-700"
|
||||
:active (if (= active-filter "failed") "true" nil))
|
||||
(~test-filter-card :href "/?filter=errors" :label "Errors" :count errors
|
||||
:colour-border "border-orange-200" :colour-bg "bg-orange-50"
|
||||
:colour-text "text-orange-700"
|
||||
:active (if (= active-filter "errors") "true" nil))
|
||||
(~test-filter-card :href "/?filter=skipped" :label "Skipped" :count skipped
|
||||
:colour-border "border-sky-200" :colour-bg "bg-sky-50"
|
||||
:colour-text "text-sky-700"
|
||||
:active (if (= active-filter "skipped") "true" nil))
|
||||
(~test-filter-card :href "/" :label "Duration" :count (str duration "s")
|
||||
:colour-border "border-stone-200" :colour-bg "bg-white"
|
||||
:colour-text "text-stone-800" :active nil))
|
||||
(div :class "text-sm text-stone-400" (str "Last run: " last-run)))))
|
||||
|
||||
(defcomp ~test-service-header (&key service total passed failed)
|
||||
(tr :class "border-b-2 border-stone-300 bg-stone-100"
|
||||
(td :class "px-3 py-2 text-sm font-bold text-stone-700" :colspan "4"
|
||||
(span service)
|
||||
(span :class "ml-2 text-xs font-normal text-stone-500"
|
||||
(str total " tests, " passed " passed, " failed " failed")))))
|
||||
|
||||
(defcomp ~test-row (&key nodeid outcome duration longrepr)
|
||||
(tr :class (str "border-b border-stone-100 "
|
||||
(if (= outcome "passed") "bg-white"
|
||||
@@ -85,4 +110,4 @@
|
||||
(div :class "flex items-center justify-center py-12 text-stone-400"
|
||||
(div :class "text-center"
|
||||
(div :class "text-4xl mb-2" "?")
|
||||
(div :class "text-sm" "No test results yet. Click \"Run Tests\" to start."))))
|
||||
(div :class "text-sm" "No test results yet. Click Run Tests to start."))))
|
||||
|
||||
@@ -18,6 +18,65 @@ def _format_time(ts: float | None) -> str:
|
||||
return datetime.fromtimestamp(ts).strftime("%-d %b %Y, %H:%M:%S")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Menu / header
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_FILTER_MAP = {
|
||||
"passed": "passed",
|
||||
"failed": "failed",
|
||||
"errors": "error",
|
||||
"skipped": "skipped",
|
||||
}
|
||||
|
||||
|
||||
def _test_header_html(ctx: dict, active_service: str | None = None) -> str:
|
||||
"""Build the Tests menu-row (level 1) with service nav links."""
|
||||
nav = _service_nav_html(ctx, active_service)
|
||||
return render(
|
||||
"menu-row",
|
||||
id="test-row", level=1, colour="sky",
|
||||
link_href="/", link_label="Tests", icon="fa fa-flask",
|
||||
nav_html=nav,
|
||||
child_id="test-header-child",
|
||||
)
|
||||
|
||||
|
||||
def _service_nav_html(ctx: dict, active_service: str | None = None) -> str:
|
||||
"""Render service filter nav links using ~nav-link component."""
|
||||
from runner import _SERVICE_ORDER
|
||||
parts = []
|
||||
# "All" link
|
||||
parts.append(render(
|
||||
"nav-link",
|
||||
href="/",
|
||||
label="all",
|
||||
is_selected="true" if not active_service else None,
|
||||
select_colours="aria-selected:bg-sky-200 aria-selected:text-sky-900",
|
||||
))
|
||||
for svc in _SERVICE_ORDER:
|
||||
parts.append(render(
|
||||
"nav-link",
|
||||
href=f"/?service={svc}",
|
||||
label=svc,
|
||||
is_selected="true" if active_service == svc else None,
|
||||
select_colours="aria-selected:bg-sky-200 aria-selected:text-sky-900",
|
||||
))
|
||||
return "".join(parts)
|
||||
|
||||
|
||||
def _header_stack_html(ctx: dict, active_service: str | None = None) -> str:
|
||||
"""Full header stack: root row + tests child row."""
|
||||
hdr = root_header_html(ctx)
|
||||
inner = _test_header_html(ctx, active_service)
|
||||
hdr += render("header-child", inner_html=inner)
|
||||
return hdr
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Test rows / grouping
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _test_rows_html(tests: list[dict]) -> str:
|
||||
"""Render all test result rows."""
|
||||
parts = []
|
||||
@@ -32,7 +91,43 @@ def _test_rows_html(tests: list[dict]) -> str:
|
||||
return "".join(parts)
|
||||
|
||||
|
||||
def _results_partial_html(result: dict | None, running: bool, csrf: str) -> str:
|
||||
def _grouped_rows_html(tests: list[dict]) -> str:
|
||||
"""Render test rows grouped by service with section headers."""
|
||||
from runner import group_tests_by_service
|
||||
sections = group_tests_by_service(tests)
|
||||
parts = []
|
||||
for sec in sections:
|
||||
parts.append(render(
|
||||
"test-service-header",
|
||||
service=sec["service"],
|
||||
total=str(sec["total"]),
|
||||
passed=str(sec["passed"]),
|
||||
failed=str(sec["failed"]),
|
||||
))
|
||||
parts.append(_test_rows_html(sec["tests"]))
|
||||
return "".join(parts)
|
||||
|
||||
|
||||
def _filter_tests(tests: list[dict], active_filter: str | None,
|
||||
active_service: str | None) -> list[dict]:
|
||||
"""Filter tests by outcome and/or service."""
|
||||
from runner import _service_from_nodeid
|
||||
filtered = tests
|
||||
if active_filter and active_filter in _FILTER_MAP:
|
||||
outcome = _FILTER_MAP[active_filter]
|
||||
filtered = [t for t in filtered if t["outcome"] == outcome]
|
||||
if active_service:
|
||||
filtered = [t for t in filtered if _service_from_nodeid(t["nodeid"]) == active_service]
|
||||
return filtered
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Results partial
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _results_partial_html(result: dict | None, running: bool, csrf: str,
|
||||
active_filter: str | None = None,
|
||||
active_service: str | None = None) -> str:
|
||||
"""Render the results section (summary + table or running indicator)."""
|
||||
if running and not result:
|
||||
summary = render(
|
||||
@@ -40,6 +135,7 @@ def _results_partial_html(result: dict | None, running: bool, csrf: str) -> str:
|
||||
status="running", passed="0", failed="0", errors="0",
|
||||
skipped="0", total="0", duration="...",
|
||||
last_run="in progress", running=True, csrf=csrf,
|
||||
active_filter=active_filter,
|
||||
)
|
||||
return summary + render("test-running-indicator")
|
||||
|
||||
@@ -49,6 +145,7 @@ def _results_partial_html(result: dict | None, running: bool, csrf: str) -> str:
|
||||
status=None, passed="0", failed="0", errors="0",
|
||||
skipped="0", total="0", duration="0",
|
||||
last_run="never", running=running, csrf=csrf,
|
||||
active_filter=active_filter,
|
||||
)
|
||||
return summary + render("test-no-results")
|
||||
|
||||
@@ -65,17 +162,19 @@ def _results_partial_html(result: dict | None, running: bool, csrf: str) -> str:
|
||||
last_run=_format_time(result["finished_at"]) if not running else "in progress",
|
||||
running=running,
|
||||
csrf=csrf,
|
||||
active_filter=active_filter,
|
||||
)
|
||||
|
||||
if running:
|
||||
return summary + render("test-running-indicator")
|
||||
|
||||
tests = result.get("tests", [])
|
||||
tests = _filter_tests(tests, active_filter, active_service)
|
||||
if not tests:
|
||||
return summary + render("test-no-results")
|
||||
|
||||
has_failures = result["failed"] > 0 or result["errors"] > 0
|
||||
rows = _test_rows_html(tests)
|
||||
rows = _grouped_rows_html(tests)
|
||||
table = render("test-results-table", rows_html=rows,
|
||||
has_failures=str(has_failures).lower())
|
||||
return summary + table
|
||||
@@ -90,16 +189,20 @@ def _wrap_results_div(inner_html: str, running: bool) -> str:
|
||||
|
||||
|
||||
async def render_dashboard_page(ctx: dict, result: dict | None,
|
||||
running: bool, csrf: str) -> str:
|
||||
running: bool, csrf: str,
|
||||
active_filter: str | None = None,
|
||||
active_service: str | None = None) -> str:
|
||||
"""Full page: test dashboard."""
|
||||
hdr = root_header_html(ctx)
|
||||
inner = _results_partial_html(result, running, csrf)
|
||||
hdr = _header_stack_html(ctx, active_service)
|
||||
inner = _results_partial_html(result, running, csrf, active_filter, active_service)
|
||||
content = _wrap_results_div(inner, running)
|
||||
return full_page(ctx, header_rows_html=hdr, content_html=content)
|
||||
|
||||
|
||||
async def render_results_partial(result: dict | None, running: bool,
|
||||
csrf: str) -> str:
|
||||
csrf: str,
|
||||
active_filter: str | None = None,
|
||||
active_service: str | None = None) -> str:
|
||||
"""HTMX partial: just the results section (wrapped in polling div)."""
|
||||
inner = _results_partial_html(result, running, csrf)
|
||||
inner = _results_partial_html(result, running, csrf, active_filter, active_service)
|
||||
return _wrap_results_div(inner, running)
|
||||
|
||||
Reference in New Issue
Block a user