Spaces:
Sleeping
Sleeping
Download tests/test_json_parser.py from baljot07/studiq-backend: direct link, hf CLI and curl.
- Browser
- Download file 918 Bytes
-
https://huggingface.co/spaces/baljot07/studiq-backend/resolve/main/tests/test_json_parser.py
- Command line
-
hf download hf://spaces/baljot07/studiq-backend/tests/test_json_parser.py
-
curl -L -o test_json_parser.py https://huggingface.co/spaces/baljot07/studiq-backend/resolve/main/tests/test_json_parser.py
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"} | |