106 lines
4.1 KiB
Python
106 lines
4.1 KiB
Python
|
|
"""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": "<not>xml<<<"})
|
||
|
|
assert resp.status_code == 200
|
||
|
|
data = resp.json()
|
||
|
|
assert data["valid"] is False
|
||
|
|
|
||
|
|
def test_missing_page_dimensions(self):
|
||
|
|
jrxml = """<?xml version="1.0" encoding="UTF-8"?>
|
||
|
|
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
|
||
|
|
name="TestReport" columnWidth="555"
|
||
|
|
leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
|
||
|
|
<queryString><![CDATA[SELECT * FROM test]]></queryString>
|
||
|
|
<field name="col1" class="java.lang.String"/>
|
||
|
|
<title><band height="30"/></title>
|
||
|
|
<detail>
|
||
|
|
<band height="20">
|
||
|
|
<textField>
|
||
|
|
<reportElement x="0" y="0" width="100" height="20"/>
|
||
|
|
<textFieldExpression><![CDATA[$F{col1}]]></textFieldExpression>
|
||
|
|
</textField>
|
||
|
|
</band>
|
||
|
|
</detail>
|
||
|
|
</jasperReport>"""
|
||
|
|
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 = """<?xml version="1.0" encoding="UTF-8"?>
|
||
|
|
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
|
||
|
|
name="ValidReport" pageWidth="595" pageHeight="842" columnWidth="555"
|
||
|
|
leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
|
||
|
|
<queryString><![CDATA[SELECT emp_id, emp_name FROM employees]]></queryString>
|
||
|
|
<field name="emp_id" class="java.lang.Integer"/>
|
||
|
|
<field name="emp_name" class="java.lang.String"/>
|
||
|
|
<title><band height="30">
|
||
|
|
<staticText>
|
||
|
|
<reportElement x="0" y="0" width="555" height="30"/>
|
||
|
|
<text><![CDATA[Report Title]]></text>
|
||
|
|
</staticText>
|
||
|
|
</band></title>
|
||
|
|
<detail>
|
||
|
|
<band height="20">
|
||
|
|
<textField>
|
||
|
|
<reportElement x="0" y="0" width="100" height="20"/>
|
||
|
|
<textFieldExpression><![CDATA[$F{emp_id}]]></textFieldExpression>
|
||
|
|
</textField>
|
||
|
|
<textField>
|
||
|
|
<reportElement x="110" y="0" width="200" height="20"/>
|
||
|
|
<textFieldExpression><![CDATA[$F{emp_name}]]></textFieldExpression>
|
||
|
|
</textField>
|
||
|
|
</band>
|
||
|
|
</detail>
|
||
|
|
</jasperReport>"""
|
||
|
|
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 = """<?xml version="1.0" encoding="UTF-8"?>
|
||
|
|
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
|
||
|
|
name="BadReport" pageWidth="595" pageHeight="842" columnWidth="555"
|
||
|
|
leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
|
||
|
|
<queryString><![CDATA[SELECT id FROM t]]></queryString>
|
||
|
|
<field name="id" class="java.lang.Integer"/>
|
||
|
|
<detail>
|
||
|
|
<band height="20">
|
||
|
|
<textField>
|
||
|
|
<reportElement x="0" y="0" width="100" height="20"/>
|
||
|
|
<textFieldExpression><![CDATA[$F{missing_field}]]></textFieldExpression>
|
||
|
|
</textField>
|
||
|
|
</band>
|
||
|
|
</detail>
|
||
|
|
</jasperReport>"""
|
||
|
|
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"]
|