Files
group_xinghuo_jinrong/tests/test_wave6_analyst_chart.py
T
zhanghongyu_0626 a0f550646e feat(analyst): Add analyze endpoint and chart specification validation
- Introduced a new `/analyze` endpoint in the analyst API to process analysis requests, allowing users to receive textual interpretations and chart specifications based on provided prompts.
- Enhanced `analyst_schemas.py` with `AnalyzeRequest` and `ChartSpec` models to structure analysis requests and validate chart specifications.
- Implemented chart validation logic in a new `analyst_chart.py` service, ensuring that chart types and fields are correctly specified and conform to allowed values.
- Updated `AnalystAgent` to handle analysis requests, integrating the new logic for generating responses based on user prompts and data availability.
- Added unit tests to verify the functionality of the new endpoint and validation mechanisms, ensuring robustness and reliability.

This update significantly enhances the analytical capabilities of the application, providing users with improved tools for data interpretation and visualization.
2026-09-12 12:33:37 +08:00

91 lines
3.0 KiB
Python

"""analyst_chart ChartSpec 校验(问数可视化)。"""
import unittest
from app.model.analyst_schemas import ChartSpec
from app.service.analyst_chart import classify_analysis_kind, parse_analyze_json, validate_chart_spec
class TestAnalystChart(unittest.TestCase):
def test_validate_line_ok(self):
spec, err = validate_chart_spec(
{
"chart_type": "line",
"title": "趋势",
"x_field": "nav_date",
"y_fields": ["nav"],
"series_field": "product_name",
},
["nav_date", "nav", "product_name"],
)
self.assertIsNone(err)
self.assertIsNotNone(spec)
self.assertEqual(spec.chart_type, "line")
def test_validate_unknown_column(self):
_, err = validate_chart_spec(
{"chart_type": "line", "x_field": "bad", "y_fields": ["nav"]},
["nav_date", "nav"],
)
self.assertIsNotNone(err)
def test_validate_pie_single_y(self):
spec, err = validate_chart_spec(
{
"chart_type": "pie",
"title": "占比",
"x_field": "product_name",
"y_fields": ["nav"],
},
["product_name", "nav"],
)
self.assertIsNone(err)
self.assertEqual(spec.chart_type, "pie")
def test_validate_none_returns_no_chart(self):
spec, err = validate_chart_spec({"chart_type": "none"}, ["a"])
self.assertIsNone(err)
self.assertIsNone(spec)
def test_classify_both(self):
cs = ChartSpec(chart_type="line", title="t", x_field="d", y_fields=["v"])
self.assertEqual(classify_analysis_kind("有文字", cs), "both")
def test_validate_chart_type_non_string_coerced(self):
spec, err = validate_chart_spec(
{"chart_type": 123, "x_field": "nav_date", "y_fields": ["nav"]},
["nav_date", "nav"],
)
self.assertIsNotNone(err)
self.assertIsNone(spec)
spec2, err2 = validate_chart_spec(
{"chart_type": " LINE ", "x_field": "nav_date", "y_fields": ["nav"]},
["nav_date", "nav"],
)
self.assertIsNone(err2)
self.assertEqual(spec2.chart_type, "line")
def test_validate_y_fields_single_string(self):
spec, err = validate_chart_spec(
{
"chart_type": "line",
"x_field": "nav_date",
"y_fields": "nav",
},
["nav_date", "nav"],
)
self.assertIsNone(err)
self.assertEqual(spec.y_fields, ["nav"])
def test_classify_analysis_kind_non_string_answer(self):
cs = ChartSpec(chart_type="line", title="t", x_field="d", y_fields=["v"])
self.assertEqual(classify_analysis_kind(123, cs), "both") # type: ignore[arg-type]
def test_parse_json_strips_fence(self):
raw = parse_analyze_json('```json\n{"answer":"ok","chart_type":"none"}\n```')
self.assertEqual(raw["answer"], "ok")
if __name__ == "__main__":
unittest.main()