Spaces:
Runtime error
Runtime error
File size: 16,942 Bytes
ce06eb4 | 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | """
Test cases for Agents (RequirementAgent, RecommendationAgent, ModelingAgent)
P0 Testing - Agent Layer (Mock Mode)
"""
import sys
from pathlib import Path
import json
import logging
# Add project root to path
PROJECT_ROOT = Path(__file__).parent
sys.path.insert(0, str(PROJECT_ROOT))
from models.indicator import Indicator
from models.indicator_set import IndicatorSet
from agents.requirement_agent import RequirementAgent
from agents.recommendation_agent import RecommendationAgent
from agents.modeling_agent import ModelingAgent
# Configure logging
logging.basicConfig(level=logging.WARNING)
class TestRequirementAgent:
"""Test cases for RequirementAgent (Mock Mode)"""
def setup_method(self):
"""Setup for each test"""
self.agent = RequirementAgent(mock_mode=True)
def test_create_agent_success(self):
"""Test creating RequirementAgent in mock mode"""
assert self.agent.mock_mode == True
assert self.agent.name == "RequirementAgent"
print("✓ Test 1.1: Create RequirementAgent (Mock Mode) - PASSED")
def test_run_with_ecommerce_keywords(self):
"""Test run() with e-commerce keywords"""
result = self.agent.run({
"user_input": "我想看每天的订单金额和GMV"
})
assert result["status"] == "success"
assert "data" in result
assert "indicator_set" in result["data"]
assert "business_domain" in result["data"]
indicator_set = result["data"]["indicator_set"]
assert indicator_set.business_domain == "电商"
assert len(indicator_set.indicators) > 0
print("✓ Test 1.2: Run with e-commerce keywords - PASSED")
def test_run_with_toc_keywords(self):
"""Test run() with ToC keywords"""
result = self.agent.run({
"user_input": "分析用户活跃度和转化率"
})
assert result["status"] == "success"
assert result["data"]["business_domain"] == "ToC业务"
print("✓ Test 1.3: Run with ToC keywords - PASSED")
def test_run_with_business_type(self):
"""Test run() with business_type parameter"""
result = self.agent.run({
"user_input": "我想看销售数据",
"business_type": "电商"
})
assert result["status"] == "success"
assert result["data"]["business_domain"] == "电商"
print("✓ Test 1.4: Run with business_type - PASSED")
def test_run_missing_user_input(self):
"""Test run() with missing user_input (should return error)"""
result = self.agent.run({
"user_input": "" # Empty input
})
assert result["status"] == "error"
assert "error" in result
print("✓ Test 1.5: Run with missing user_input - PASSED")
def test_run_empty_input_dict(self):
"""Test run() with empty dict (should return error)"""
result = self.agent.run({})
assert result["status"] == "error"
print("✓ Test 1.6: Run with empty dict - PASSED")
def test_mock_indicators_count(self):
"""Test that mock mode generates reasonable number of indicators"""
result = self.agent.run({
"user_input": "我想看电商的GMV、订单量、客单价"
})
assert result["status"] == "success"
indicator_set = result["data"]["indicator_set"]
# Should generate 3 indicators for 电商
assert len(indicator_set.indicators) >= 1
assert len(indicator_set.indicators) <= 10 # Reasonable upper limit
print("✓ Test 1.7: Mock indicators count is reasonable - PASSED")
def test_process_file_metadata(self):
"""Test process_file_metadata method"""
# Create a mock uploaded file object
class MockUploadedFile:
def __init__(self, name):
self.name = name
self.type = "text/csv"
mock_file = MockUploadedFile("test.csv")
# The current implementation may fail because parse_file is not implemented
try:
result = self.agent.process_file_metadata(mock_file)
# If it succeeds
if result["status"] == "success":
assert "data" in result
print("✓ Test 1.8: process_file_metadata - PASSED")
else:
print("⚠ Test 1.8: process_file_metadata returned error (expected - parse_file not fully implemented)")
except Exception as e:
print(f"⚠ Test 1.8: process_file_metadata raised exception (expected): {type(e).__name__}")
class TestRecommendationAgent:
"""Test cases for RecommendationAgent (Mock Mode)"""
def setup_method(self):
"""Setup for each test"""
self.agent = RecommendationAgent(vector_store=None, mock_mode=True)
def test_create_agent_success(self):
"""Test creating RecommendationAgent in mock mode"""
assert self.agent.mock_mode == True
assert self.agent.name == "RecommendationAgent"
print("✓ Test 2.1: Create RecommendationAgent (Mock Mode) - PASSED")
def test_run_with_indicator_set(self):
"""Test run() with IndicatorSet input"""
# Create a test IndicatorSet
ind = Indicator(
indicator_id="ind_001",
indicator_name="GMV",
business_meaning="商品交易总额",
calculation_logic="SUM(order_amount)"
)
indicator_set = IndicatorSet(
set_id="test_001",
set_name="测试指标集",
business_domain="电商",
analysis_purpose="测试",
indicators=[ind]
)
result = self.agent.run({
"indicator_set": indicator_set
})
assert result["status"] == "success"
assert "data" in result
assert "categories" in result["data"]
assert "total_count" in result["data"]
print("✓ Test 2.2: Run with IndicatorSet - PASSED")
def test_run_with_dict_input(self):
"""Test run() with dict input (instead of IndicatorSet object)"""
indicator_set_dict = {
"set_id": "test_002",
"set_name": "测试指标集2",
"business_domain": "电商",
"analysis_purpose": "测试",
"indicators": [
{
"indicator_id": "ind_001",
"indicator_name": "GMV",
"business_meaning": "商品交易总额",
"calculation_logic": "SUM(order_amount)"
}
]
}
result = self.agent.run({
"indicator_set": indicator_set_dict
})
assert result["status"] == "success"
print("✓ Test 2.3: Run with dict input - PASSED")
def test_run_missing_indicator_set(self):
"""Test run() with missing indicator_set (should return error)"""
result = self.agent.run({})
assert result["status"] == "error"
print("✓ Test 2.4: Run with missing indicator_set - PASSED")
def test_mock_tree_structure(self):
"""Test that mock mode returns tree structure (categories + indicators)"""
ind = Indicator(
indicator_id="ind_001",
indicator_name="GMV",
business_meaning="商品交易总额",
calculation_logic="SUM(order_amount)"
)
indicator_set = IndicatorSet(
set_id="test_003",
set_name="电商指标集",
business_domain="电商",
indicators=[ind]
)
result = self.agent.run({
"indicator_set": indicator_set
})
assert result["status"] == "success"
data = result["data"]
# Check tree structure
assert "categories" in data
assert isinstance(data["categories"], list)
if len(data["categories"]) > 0:
category = data["categories"][0]
assert "category_name" in category
assert "indicators" in category
print("✓ Test 2.5: Mock returns tree structure - PASSED")
def test_recommend_indicator_set_alias(self):
"""Test recommend_indicator_set() alias method"""
ind = Indicator(
indicator_id="ind_001",
indicator_name="GMV",
business_meaning="商品交易总额",
calculation_logic="SUM(order_amount)"
)
indicator_set = IndicatorSet(
set_id="test_004",
set_name="测试指标集",
business_domain="电商",
indicators=[ind]
)
result = self.agent.recommend_indicator_set({
"indicator_set": indicator_set
})
assert result["status"] == "success"
print("✓ Test 2.6: recommend_indicator_set alias - PASSED")
class TestModelingAgent:
"""Test cases for ModelingAgent (Mock Mode)"""
def setup_method(self):
"""Setup for each test"""
self.agent = ModelingAgent(mock_mode=True)
def test_create_agent_success(self):
"""Test creating ModelingAgent in mock mode"""
assert self.agent.mock_mode == True
assert self.agent.name == "ModelingAgent"
print("✓ Test 3.1: Create ModelingAgent (Mock Mode) - PASSED")
def test_run_with_selected_indicators(self):
"""Test run() with selected indicators"""
# Create IndicatorSet with multiple indicators
ind1 = Indicator(
indicator_id="ind_001",
indicator_name="GMV",
business_meaning="商品交易总额",
calculation_logic="SUM(order_amount)"
)
ind2 = Indicator(
indicator_id="ind_002",
indicator_name="订单量",
business_meaning="订单总数",
calculation_logic="COUNT(order_id)"
)
indicator_set = IndicatorSet(
set_id="test_005",
set_name="测试指标集",
business_domain="电商",
indicators=[ind1, ind2]
)
result = self.agent.run({
"indicator_set": indicator_set,
"selected_indicators": ["ind_001", "ind_002"]
})
assert result["status"] == "success"
assert "data" in result
assert "schemes" in result["data"]
assert len(result["data"]["schemes"]) == 2
print("✓ Test 3.2: Run with multiple selected indicators - PASSED")
def test_run_with_single_indicator(self):
"""Test run() with single selected indicator"""
ind = Indicator(
indicator_id="ind_001",
indicator_name="GMV",
business_meaning="商品交易总额",
calculation_logic="SUM(order_amount)"
)
indicator_set = IndicatorSet(
set_id="test_006",
set_name="测试指标集",
business_domain="电商",
indicators=[ind]
)
result = self.agent.run({
"indicator_set": indicator_set,
"selected_indicators": ["ind_001"]
})
assert result["status"] == "success"
assert len(result["data"]["schemes"]) == 1
# Check scheme structure
scheme = result["data"]["schemes"][0]
assert "indicator_id" in scheme
assert "indicator_name" in scheme
assert "markdown" in scheme
assert "sql" in scheme
assert "table_structure" in scheme
print("✓ Test 3.3: Run with single indicator - PASSED")
def test_run_missing_selected_indicators(self):
"""Test run() with missing selected_indicators (should return error)"""
ind = Indicator(
indicator_id="ind_001",
indicator_name="GMV",
business_meaning="商品交易总额",
calculation_logic="SUM(order_amount)"
)
indicator_set = IndicatorSet(
set_id="test_007",
set_name="测试指标集",
business_domain="电商",
indicators=[ind]
)
result = self.agent.run({
"indicator_set": indicator_set,
"selected_indicators": [] # Empty list
})
assert result["status"] == "error"
print("✓ Test 3.4: Run with empty selected_indicators - PASSED")
def test_run_missing_indicator_set(self):
"""Test run() with missing indicator_set (should return error)"""
result = self.agent.run({
"selected_indicators": ["ind_001"]
})
assert result["status"] == "error"
print("✓ Test 3.5: Run with missing indicator_set - PASSED")
def test_run_with_invalid_indicator_id(self):
"""Test run() with invalid indicator ID (should skip or error gracefully)"""
ind = Indicator(
indicator_id="ind_001",
indicator_name="GMV",
business_meaning="商品交易总额",
calculation_logic="SUM(order_amount)"
)
indicator_set = IndicatorSet(
set_id="test_008",
set_name="测试指标集",
business_domain="电商",
indicators=[ind]
)
result = self.agent.run({
"indicator_set": indicator_set,
"selected_indicators": ["non_existing_id"] # Invalid ID
})
# Should either return error or skip invalid ID
if result["status"] == "success":
assert len(result["data"]["schemes"]) == 0
print("✓ Test 3.6: Run with invalid indicator ID (skipped) - PASSED")
else:
print("✓ Test 3.6: Run with invalid indicator ID (returned error) - PASSED")
def test_mock_scheme_content(self):
"""Test that mock schemes contain valid content"""
ind = Indicator(
indicator_id="ind_001",
indicator_name="GMV",
business_meaning="商品交易总额",
calculation_logic="SUM(order_amount)"
)
indicator_set = IndicatorSet(
set_id="test_009",
set_name="测试指标集",
business_domain="电商",
indicators=[ind]
)
result = self.agent.run({
"indicator_set": indicator_set,
"selected_indicators": ["ind_001"]
})
assert result["status"] == "success"
scheme = result["data"]["schemes"][0]
# Check content is not empty
assert scheme["markdown"] != ""
assert scheme["sql"] != ""
assert scheme["table_structure"] != ""
print("✓ Test 3.7: Mock scheme content is valid - PASSED")
def run_all_tests():
"""Run all Agent tests"""
print("\n" + "="*60)
print("Running P0 Tests: Agents (Mock Mode)")
print("="*60 + "\n")
# Test RequirementAgent
print("--- Test RequirementAgent (Mock Mode) ---")
test_req = TestRequirementAgent()
test_req.setup_method()
test_req.test_create_agent_success()
test_req.test_run_with_ecommerce_keywords()
test_req.test_run_with_toc_keywords()
test_req.test_run_with_business_type()
test_req.test_run_missing_user_input()
test_req.test_run_empty_input_dict()
test_req.test_mock_indicators_count()
test_req.test_process_file_metadata()
# Test RecommendationAgent
print("\n--- Test RecommendationAgent (Mock Mode) ---")
test_rec = TestRecommendationAgent()
test_rec.setup_method()
test_rec.test_create_agent_success()
test_rec.test_run_with_indicator_set()
test_rec.test_run_with_dict_input()
test_rec.test_run_missing_indicator_set()
test_rec.test_mock_tree_structure()
test_rec.test_recommend_indicator_set_alias()
# Test ModelingAgent
print("\n--- Test ModelingAgent (Mock Mode) ---")
test_mod = TestModelingAgent()
test_mod.setup_method()
test_mod.test_create_agent_success()
test_mod.test_run_with_selected_indicators()
test_mod.test_run_with_single_indicator()
test_mod.test_run_missing_selected_indicators()
test_mod.test_run_missing_indicator_set()
test_mod.test_run_with_invalid_indicator_id()
test_mod.test_mock_scheme_content()
print("\n" + "="*60)
print("✅ All Agent Tests Completed (Mock Mode)!")
print("="*60 + "\n")
if __name__ == "__main__":
run_all_tests()
|