Spaces:
Sleeping
Sleeping
File size: 9,851 Bytes
74e7808 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | # -*- coding: utf-8 -*-
"""
===================================
Chip structure fallback tests (Issue #589)
===================================
Tests for fill_chip_structure_if_needed and related helpers.
"""
import sys
import unittest
from unittest.mock import MagicMock
try:
import litellm # noqa: F401
except ModuleNotFoundError:
sys.modules["litellm"] = MagicMock()
from data_provider.realtime_types import ChipDistribution
from src.analyzer import (
AnalysisResult,
fill_chip_structure_if_needed,
_is_value_placeholder,
_derive_chip_health,
_build_chip_structure_from_data,
)
class TestIsValuePlaceholder(unittest.TestCase):
"""Tests for _is_value_placeholder."""
def test_none_is_placeholder(self) -> None:
self.assertTrue(_is_value_placeholder(None))
def test_zero_is_placeholder(self) -> None:
self.assertTrue(_is_value_placeholder(0))
self.assertTrue(_is_value_placeholder(0.0))
def test_empty_string_is_placeholder(self) -> None:
self.assertTrue(_is_value_placeholder(""))
self.assertTrue(_is_value_placeholder(" "))
def test_na_variants_are_placeholder(self) -> None:
self.assertTrue(_is_value_placeholder("N/A"))
self.assertTrue(_is_value_placeholder("n/a"))
self.assertTrue(_is_value_placeholder("NA"))
self.assertTrue(_is_value_placeholder("na"))
def test_data_missing_is_placeholder(self) -> None:
self.assertTrue(_is_value_placeholder("数据缺失"))
self.assertTrue(_is_value_placeholder("未知"))
def test_valid_values_not_placeholder(self) -> None:
self.assertFalse(_is_value_placeholder(0.5))
self.assertFalse(_is_value_placeholder("50%"))
self.assertFalse(_is_value_placeholder("67.5%"))
self.assertFalse(_is_value_placeholder(25.6))
self.assertFalse(_is_value_placeholder("健康"))
class TestDeriveChipHealth(unittest.TestCase):
"""Tests for _derive_chip_health."""
def test_high_profit_ratio_returns_jingti(self) -> None:
self.assertEqual(_derive_chip_health(0.95, 0.10), "警惕")
self.assertEqual(_derive_chip_health(0.9, 0.05), "警惕")
def test_high_concentration_returns_jingti(self) -> None:
self.assertEqual(_derive_chip_health(0.5, 0.30), "警惕")
self.assertEqual(_derive_chip_health(0.3, 0.25), "警惕")
def test_concentrated_moderate_profit_returns_jiankang(self) -> None:
self.assertEqual(_derive_chip_health(0.5, 0.10), "健康")
self.assertEqual(_derive_chip_health(0.6, 0.12), "健康")
self.assertEqual(_derive_chip_health(0.3, 0.14), "健康")
def test_otherwise_returns_yiban(self) -> None:
self.assertEqual(_derive_chip_health(0.2, 0.20), "一般")
self.assertEqual(_derive_chip_health(0.5, 0.18), "一般")
class TestBuildChipStructureFromData(unittest.TestCase):
"""Tests for _build_chip_structure_from_data."""
def test_from_chip_distribution(self) -> None:
chip = ChipDistribution(
code="600519",
profit_ratio=0.567,
avg_cost=1850.5,
concentration_90=0.12,
)
out = _build_chip_structure_from_data(chip)
self.assertEqual(out["profit_ratio"], "56.7%")
self.assertEqual(out["avg_cost"], 1850.5)
self.assertEqual(out["concentration"], "12.00%")
self.assertEqual(out["chip_health"], "健康")
def test_from_dict(self) -> None:
d = {"profit_ratio": 0.9, "avg_cost": 100.0, "concentration_90": 0.08}
out = _build_chip_structure_from_data(d)
self.assertEqual(out["profit_ratio"], "90.0%")
self.assertEqual(out["avg_cost"], 100.0)
self.assertEqual(out["concentration"], "8.00%")
self.assertEqual(out["chip_health"], "警惕")
def test_dict_with_string_values(self) -> None:
d = {"profit_ratio": "0.5", "avg_cost": "25.6", "concentration_90": "0.15"}
out = _build_chip_structure_from_data(d)
self.assertEqual(out["profit_ratio"], "50.0%")
self.assertEqual(out["avg_cost"], "25.6") # raw value preserved
self.assertEqual(out["concentration"], "15.00%")
def test_avg_cost_zero_shows_na(self) -> None:
chip = ChipDistribution(code="600519", profit_ratio=0.5, avg_cost=0.0, concentration_90=0.1)
out = _build_chip_structure_from_data(chip)
self.assertEqual(out["avg_cost"], "N/A")
def test_avg_cost_none_shows_na(self) -> None:
d = {"profit_ratio": 0.5, "avg_cost": None, "concentration_90": 0.1}
out = _build_chip_structure_from_data(d)
self.assertEqual(out["avg_cost"], "N/A")
class TestFillChipStructureIfNeeded(unittest.TestCase):
"""Tests for fill_chip_structure_if_needed."""
def _make_result(self, dashboard: dict = None) -> AnalysisResult:
return AnalysisResult(
code="600519",
name="贵州茅台",
trend_prediction="看多",
sentiment_score=70,
operation_advice="持有",
analysis_summary="稳健",
decision_type="hold",
dashboard=dashboard,
)
def _make_chip(self) -> ChipDistribution:
return ChipDistribution(
code="600519",
profit_ratio=0.67,
avg_cost=1850.0,
concentration_90=0.11,
)
def test_no_modification_when_chip_data_none(self) -> None:
result = self._make_result(dashboard={"data_perspective": {"chip_structure": {}}})
fill_chip_structure_if_needed(result, None)
self.assertEqual(result.dashboard["data_perspective"]["chip_structure"], {})
def test_no_modification_when_result_none(self) -> None:
chip = self._make_chip()
fill_chip_structure_if_needed(None, chip)
# No crash
def test_full_fill_when_cs_all_empty(self) -> None:
result = self._make_result(
dashboard={"data_perspective": {"chip_structure": {"profit_ratio": 0, "avg_cost": 0, "concentration": 0, "chip_health": ""}}}
)
chip = self._make_chip()
fill_chip_structure_if_needed(result, chip)
cs = result.dashboard["data_perspective"]["chip_structure"]
self.assertEqual(cs["profit_ratio"], "67.0%")
self.assertEqual(cs["avg_cost"], 1850.0)
self.assertEqual(cs["concentration"], "11.00%")
self.assertEqual(cs["chip_health"], "健康")
def test_merge_fill_partial_placeholder(self) -> None:
result = self._make_result(
dashboard={
"data_perspective": {
"chip_structure": {"profit_ratio": "65.0%", "avg_cost": 0, "concentration": 0, "chip_health": ""}
}
}
)
chip = self._make_chip()
fill_chip_structure_if_needed(result, chip)
cs = result.dashboard["data_perspective"]["chip_structure"]
self.assertEqual(cs["profit_ratio"], "65.0%") # LLM value kept
self.assertEqual(cs["avg_cost"], 1850.0) # filled from chip
self.assertEqual(cs["concentration"], "11.00%") # filled from chip
self.assertEqual(cs["chip_health"], "健康") # filled from chip
def test_dashboard_none_initialized(self) -> None:
result = self._make_result(dashboard=None)
chip = self._make_chip()
fill_chip_structure_if_needed(result, chip)
self.assertIsNotNone(result.dashboard)
cs = result.dashboard["data_perspective"]["chip_structure"]
self.assertEqual(cs["profit_ratio"], "67.0%")
self.assertEqual(cs["chip_health"], "健康")
def test_no_overwrite_valid_llm_values(self) -> None:
result = self._make_result(
dashboard={
"data_perspective": {
"chip_structure": {
"profit_ratio": "70.0%",
"avg_cost": 1900.0,
"concentration": "10.00%",
"chip_health": "健康",
}
}
}
)
chip = self._make_chip()
fill_chip_structure_if_needed(result, chip)
cs = result.dashboard["data_perspective"]["chip_structure"]
self.assertEqual(cs["profit_ratio"], "70.0%")
self.assertEqual(cs["avg_cost"], 1900.0)
self.assertEqual(cs["concentration"], "10.00%")
self.assertEqual(cs["chip_health"], "健康")
def test_data_perspective_null_handled(self) -> None:
"""When LLM returns data_perspective: null, fill should still work."""
result = self._make_result(
dashboard={"data_perspective": None, "core_conclusion": {"one_sentence": "观望"}}
)
chip = self._make_chip()
fill_chip_structure_if_needed(result, chip)
self.assertIsNotNone(result.dashboard["data_perspective"])
cs = result.dashboard["data_perspective"]["chip_structure"]
self.assertEqual(cs["profit_ratio"], "67.0%")
def test_extra_keys_in_chip_structure_preserved(self) -> None:
"""Extra keys added by LLM in chip_structure must not be dropped."""
result = self._make_result(
dashboard={
"data_perspective": {
"chip_structure": {
"profit_ratio": 0,
"avg_cost": 0,
"concentration": 0,
"chip_health": "",
"custom_note": "LLM added this",
}
}
}
)
chip = self._make_chip()
fill_chip_structure_if_needed(result, chip)
cs = result.dashboard["data_perspective"]["chip_structure"]
self.assertEqual(cs["profit_ratio"], "67.0%")
self.assertEqual(cs["custom_note"], "LLM added this")
|