Spaces:
Sleeping

studiq-backend / tests /test_json_parser.py
StudIQ Deploy Bot
Deploy StudIQ — 2026-09-07 18:07 UTC
2026abe
Raw History Blame Contribute Delete
918 Bytes
"""Unit tests for the clean_json_text and safe_parse_json utilities."""
import pytest
from app.utils.json_parser import clean_json_text, safe_parse_json
def test_clean_json_markdown_blocks():
"""Test extracting JSON from markdown code block fences."""
raw = "```json\n{\"key\": \"value\"}\n```"
cleaned = clean_json_text(raw)
assert cleaned == '{"key": "value"}'
def test_clean_json_trailing_commas():
"""Test removing trailing commas from objects and arrays."""
raw = '{"items": [1, 2, 3,], "name": "test",}'
cleaned = clean_json_text(raw)
parsed = safe_parse_json(cleaned)
assert parsed["items"] == [1, 2, 3]
assert parsed["name"] == "test"
def test_safe_parse_fallback():
"""Test safe fallback on invalid JSON text."""
invalid = "This is not json"
result = safe_parse_json(invalid, default={"error": "fallback"})
assert result == {"error": "fallback"}