"""JRXML 验证服务的单元测试。""" import pytest from fastapi.testclient import TestClient from validation_service.main import app client = TestClient(app) class TestValidationService: def test_health_endpoint(self): resp = client.get("/health") assert resp.status_code == 200 data = resp.json() assert data["status"] == "ok" def test_empty_jrxml(self): resp = client.post("/validate", json={"jrxml": ""}) assert resp.status_code == 200 assert resp.json()["valid"] is False assert "空" in resp.json()["error"] def test_invalid_xml(self): resp = client.post("/validate", json={"jrxml": "xml<<<"}) assert resp.status_code == 200 data = resp.json() assert data["valid"] is False def test_missing_page_dimensions(self): jrxml = """ <band height="30"/> """ resp = client.post("/validate", json={"jrxml": jrxml}) assert resp.status_code == 200 data = resp.json() assert data["valid"] is False assert "pageWidth" in data["error"] def test_valid_jrxml(self): jrxml = """ <band height="30"> <staticText> <reportElement x="0" y="0" width="555" height="30"/> <text><![CDATA[Report Title]]></text> </staticText> </band> """ resp = client.post("/validate", json={"jrxml": jrxml}) assert resp.status_code == 200 data = resp.json() assert data["valid"] is True, f"验证应该通过,实际错误: {data.get('error')}" def test_missing_field_declaration(self): jrxml = """ """ resp = client.post("/validate", json={"jrxml": jrxml}) assert resp.status_code == 200 data = resp.json() assert data["valid"] is False assert "missing_field" in data["error"]