Files
ewoooc/tests/test_sales_chart_data_contract.py
ogt 911393190d
Some checks failed
CD Pipeline / deploy (push) Has been cancelled
fix(analytics): enforce trustworthy chart rendering
2026-07-16 15:55:22 +08:00

38 lines
1.1 KiB
Python

import pandas as pd
from routes.sales_routes import _build_top_product_chart_data
def test_top_product_chart_aggregates_sku_rows_before_ranking():
rows = pd.DataFrame(
[
{'pid': 'P1', 'name': '同名商品', 'amount': 100},
{'pid': 'P1', 'name': '同名商品', 'amount': 50},
{'pid': 'P2', 'name': '同名商品', 'amount': 90},
{'pid': 'P3', 'name': '獨立商品', 'amount': 200},
{'pid': 'P4', 'name': '零業績商品', 'amount': 0},
]
)
result = _build_top_product_chart_data(rows, 'pid', 'name', 'amount', 'amount')
assert result['chart_values'] == [200.0, 150.0, 90.0]
assert result['labels'] == ['獨立商品', '同名商品 [P1]', '同名商品 [P2]']
assert result['metric_label'] == '銷售金額 ($)'
def test_top_product_chart_returns_explicit_empty_contract():
result = _build_top_product_chart_data(
pd.DataFrame(columns=['name', 'amount']),
None,
'name',
'amount',
'qty',
)
assert result == {
'labels': [],
'chart_values': [],
'metric_label': '銷售數量',
}