Spaces:
Runtime error
Runtime error
Download test_agents.py from Macbath/indicator-modeling: direct link, hf CLI and curl.
- Browser
- Download file 16.9 kB
-
https://huggingface.co/spaces/Macbath/indicator-modeling/resolve/main/test_agents.py
- Command line
-
hf download hf://spaces/Macbath/indicator-modeling/test_agents.py
-
curl -L -o test_agents.py https://huggingface.co/spaces/Macbath/indicator-modeling/resolve/main/test_agents.py
16.9 kB
| """ | |
| 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() | |