91 lines
3.0 KiB
Python
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()
|