Spaces:
Runtime error
Runtime error
Download test_p1.py from Macbath/indicator-modeling: direct link, hf CLI and curl.
- Browser
- Download file 10.8 kB
-
https://huggingface.co/spaces/Macbath/indicator-modeling/resolve/main/test_p1.py
- Command line
-
hf download hf://spaces/Macbath/indicator-modeling/test_p1.py
-
curl -L -o test_p1.py https://huggingface.co/spaces/Macbath/indicator-modeling/resolve/main/test_p1.py
10.8 kB
| """ | |
| Test cases for P1 tests: | |
| 1. BIDesignAgent (Mock Mode) | |
| 2. Edge Cases | |
| 3. Mock Data Validation | |
| """ | |
| import sys | |
| from pathlib import Path | |
| import json | |
| import os | |
| # 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.bi_design_agent import BIDesignAgent | |
| class TestBIDesignAgent: | |
| """Test cases for BIDesignAgent (Mock Mode)""" | |
| def setup_method(self): | |
| """Setup for each test""" | |
| self.agent = BIDesignAgent(mock_mode=True) | |
| def test_create_agent_success(self): | |
| """Test creating BIDesignAgent in mock mode""" | |
| assert self.agent.mock_mode == True | |
| assert "BIDesignAgent" in str(type(self.agent)) | |
| print("✓ P1 Test 1.1: Create BIDesignAgent (Mock Mode) - PASSED") | |
| def test_run_with_indicator_set(self): | |
| """Test run() with IndicatorSet input""" | |
| 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="电商", | |
| indicators=[ind] | |
| ) | |
| result = self.agent.run({ | |
| "indicator_set": indicator_set | |
| }) | |
| assert result["status"] == "success" | |
| assert "data" in result | |
| assert "bi_design" in result["data"] | |
| bi_design = result["data"]["bi_design"] | |
| assert "charts" in bi_design | |
| assert "dashboard_layout" in bi_design | |
| assert "markdown" in bi_design | |
| print("✓ P1 Test 1.2: Run with IndicatorSet - PASSED") | |
| def test_run_with_schemes(self): | |
| """Test run() with schemes (from ModelingAgent)""" | |
| ind = Indicator( | |
| indicator_id="ind_001", | |
| indicator_name="GMV", | |
| business_meaning="商品交易总额", | |
| calculation_logic="SUM(order_amount)" | |
| ) | |
| indicator_set = IndicatorSet( | |
| set_id="test_002", | |
| set_name="测试指标集", | |
| business_domain="电商", | |
| indicators=[ind] | |
| ) | |
| schemes = [{ | |
| "indicator_id": "ind_001", | |
| "indicator_name": "GMV", | |
| "sql": "SELECT SUM(amount) FROM orders", | |
| "markdown": "# GMV\n\nThis is a test.", | |
| "table_structure": {"columns": []} | |
| }] | |
| result = self.agent.run({ | |
| "indicator_set": indicator_set, | |
| "schemes": schemes | |
| }) | |
| assert result["status"] == "success" | |
| print("✓ P1 Test 1.3: Run with schemes - 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("✓ P1 Test 1.4: Run with missing indicator_set - PASSED") | |
| def test_mock_charts_structure(self): | |
| """Test that mock mode returns valid charts structure""" | |
| 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" | |
| bi_design = result["data"]["bi_design"] | |
| charts = bi_design["charts"] | |
| assert isinstance(charts, list) | |
| if len(charts) > 0: | |
| chart = charts[0] | |
| assert "chart_type" in chart | |
| assert "title" in chart | |
| print("✓ P1 Test 1.5: Mock charts structure - PASSED") | |
| class TestEdgeCases: | |
| """Test cases for edge cases""" | |
| def test_empty_user_input(self): | |
| """Test RequirementAgent with empty user input""" | |
| from agents.requirement_agent import RequirementAgent | |
| agent = RequirementAgent(mock_mode=True) | |
| result = agent.run({"user_input": ""}) | |
| assert result["status"] == "error" | |
| print("✓ P1 Test 2.1: Empty user input - PASSED") | |
| def test_no_indicators_selected(self): | |
| """Test ModelingAgent with no indicators selected""" | |
| from agents.modeling_agent import ModelingAgent | |
| 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] | |
| ) | |
| agent = ModelingAgent(mock_mode=True) | |
| result = agent.run({ | |
| "indicator_set": indicator_set, | |
| "selected_indicators": [] # Empty list | |
| }) | |
| assert result["status"] == "error" | |
| print("✓ P1 Test 2.2: No indicators selected - PASSED") | |
| def test_invalid_indicator_id(self): | |
| """Test ModelingAgent with invalid indicator ID""" | |
| from agents.modeling_agent import ModelingAgent | |
| ind = Indicator( | |
| indicator_id="ind_001", | |
| indicator_name="GMV", | |
| business_meaning="商品交易总额", | |
| calculation_logic="SUM(order_amount)" | |
| ) | |
| indicator_set = IndicatorSet( | |
| set_id="test_005", | |
| set_name="测试指标集", | |
| business_domain="电商", | |
| indicators=[ind] | |
| ) | |
| agent = ModelingAgent(mock_mode=True) | |
| result = agent.run({ | |
| "indicator_set": indicator_set, | |
| "selected_indicators": ["non_existing_id"] | |
| }) | |
| # Should return success but with 0 schemes (skipped invalid ID) | |
| assert result["status"] == "success" | |
| assert len(result["data"]["schemes"]) == 0 | |
| print("✓ P1 Test 2.3: Invalid indicator ID - PASSED") | |
| def test_empty_indicator_set(self): | |
| """Test RecommendationAgent with empty indicators list""" | |
| from agents.recommendation_agent import RecommendationAgent | |
| indicator_set = IndicatorSet( | |
| set_id="test_006", | |
| set_name="空指标集", | |
| business_domain="通用", | |
| indicators=[] # Empty indicators | |
| ) | |
| agent = RecommendationAgent(vector_store=None, mock_mode=True) | |
| result = agent.run({ | |
| "indicator_set": indicator_set | |
| }) | |
| # Should handle empty indicators gracefully | |
| assert result["status"] == "success" | |
| print("⚠ P1 Test 2.4: Empty indicator set - Check if handled gracefully") | |
| class TestMockData: | |
| """Test cases for mock data files""" | |
| def test_mock_data_files_exist(self): | |
| """Test that all mock data files exist""" | |
| mock_dir = PROJECT_ROOT / "data" / "mock" | |
| expected_files = [ | |
| "ecommerce_indicators.json", | |
| "finance_indicators.json", | |
| "public_resource_indicators.json", | |
| "retail_indicators.json", | |
| "toc_standard_indicators.json", | |
| "design_patterns.json" | |
| ] | |
| for filename in expected_files: | |
| file_path = mock_dir / filename | |
| assert file_path.exists(), f"Mock file not found: {filename}" | |
| print("✓ P1 Test 3.1: Mock data files exist - PASSED") | |
| def test_mock_data_valid_json(self): | |
| """Test that mock data files are valid JSON""" | |
| mock_dir = PROJECT_ROOT / "data" / "mock" | |
| for filename in os.listdir(mock_dir): | |
| if filename.endswith(".json"): | |
| file_path = mock_dir / filename | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| data = json.load(f) | |
| # Should be dict or list | |
| assert isinstance(data, (dict, list)), f"Expected dict or list, got {type(data)}" | |
| except json.JSONDecodeError as e: | |
| assert False, f"Invalid JSON in {filename}: {e}" | |
| print("✓ P1 Test 3.2: Mock data is valid JSON - PASSED") | |
| def test_mock_data_structure(self): | |
| """Test that mock data has expected structure""" | |
| mock_dir = PROJECT_ROOT / "data" / "mock" | |
| # Test ecommerce_indicators.json | |
| ec_file = mock_dir / "ecommerce_indicators.json" | |
| if ec_file.exists(): | |
| with open(ec_file, 'r', encoding='utf-8') as f: | |
| data = json.load(f) | |
| # Should have 'indicators' key | |
| assert "indicators" in data, "Missing 'indicators' key" | |
| assert isinstance(data["indicators"], list) | |
| # Each indicator should have required fields | |
| if len(data["indicators"]) > 0: | |
| ind = data["indicators"][0] | |
| assert "indicator_id" in ind | |
| assert "indicator_name" in ind | |
| print("✓ P1 Test 3.3: Mock data structure is valid - PASSED") | |
| def run_all_tests(): | |
| """Run all P1 tests""" | |
| print("\n" + "="*60) | |
| print("Running P1 Tests: BIDesignAgent, Edge Cases, Mock Data") | |
| print("="*60 + "\n") | |
| # Test BIDesignAgent | |
| print("--- Test BIDesignAgent (Mock Mode) ---") | |
| test_bi = TestBIDesignAgent() | |
| test_bi.setup_method() | |
| test_bi.test_create_agent_success() | |
| test_bi.test_run_with_indicator_set() | |
| test_bi.test_run_with_schemes() | |
| test_bi.test_run_missing_indicator_set() | |
| test_bi.test_mock_charts_structure() | |
| # Test Edge Cases | |
| print("\n--- Test Edge Cases ---") | |
| test_edge = TestEdgeCases() | |
| test_edge.test_empty_user_input() | |
| test_edge.test_no_indicators_selected() | |
| test_edge.test_invalid_indicator_id() | |
| test_edge.test_empty_indicator_set() | |
| # Test Mock Data | |
| print("\n--- Test Mock Data ---") | |
| test_mock = TestMockData() | |
| test_mock.test_mock_data_files_exist() | |
| test_mock.test_mock_data_valid_json() | |
| test_mock.test_mock_data_structure() | |
| print("\n" + "="*60) | |
| print("✅ All P1 Tests Completed!") | |
| print("="*60 + "\n") | |
| if __name__ == "__main__": | |
| run_all_tests() | |