|
| 1 | +"""Tests for execution.py""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 5 | +from uuid import uuid4 |
| 6 | + |
| 7 | +from app.services.execution import ExecutionService |
| 8 | +from app.models import SemanticContext, RelationshipContext |
| 9 | + |
| 10 | + |
| 11 | +class TestExecutionService: |
| 12 | + """Test ExecutionService class""" |
| 13 | + |
| 14 | + @pytest.fixture |
| 15 | + def mock_user(self): |
| 16 | + """Create mock user""" |
| 17 | + user = MagicMock() |
| 18 | + user.id = uuid4() |
| 19 | + return user |
| 20 | + |
| 21 | + @pytest.fixture |
| 22 | + def mock_db(self): |
| 23 | + """Create mock database session""" |
| 24 | + return AsyncMock() |
| 25 | + |
| 26 | + def test_init(self, mock_user, mock_db): |
| 27 | + """Test service initialization""" |
| 28 | + service = ExecutionService( |
| 29 | + user=mock_user, |
| 30 | + db=mock_db, |
| 31 | + model_name="gpt-4", |
| 32 | + connection_id=uuid4(), |
| 33 | + language="zh", |
| 34 | + ) |
| 35 | + assert service.user == mock_user |
| 36 | + assert service.db == mock_db |
| 37 | + assert service.model_name == "gpt-4" |
| 38 | + assert service.language == "zh" |
| 39 | + |
| 40 | + def test_init_defaults(self, mock_user, mock_db): |
| 41 | + """Test service initialization with defaults""" |
| 42 | + service = ExecutionService(user=mock_user, db=mock_db) |
| 43 | + assert service.model_name is None |
| 44 | + assert service.connection_id is None |
| 45 | + assert service.language == "zh" |
| 46 | + |
| 47 | + def test_build_system_prompt_zh(self, mock_user, mock_db): |
| 48 | + """Test Chinese system prompt generation""" |
| 49 | + service = ExecutionService(user=mock_user, db=mock_db, language="zh") |
| 50 | + prompt = service._build_system_prompt(None) |
| 51 | + |
| 52 | + assert "QueryGPT" in prompt |
| 53 | + assert "自然语言" in prompt or "数据分析" in prompt |
| 54 | + assert "[thinking:" in prompt |
| 55 | + assert "```sql" in prompt |
| 56 | + assert "```python" in prompt |
| 57 | + |
| 58 | + def test_build_system_prompt_en(self, mock_user, mock_db): |
| 59 | + """Test English system prompt generation""" |
| 60 | + service = ExecutionService(user=mock_user, db=mock_db, language="en") |
| 61 | + prompt = service._build_system_prompt(None) |
| 62 | + |
| 63 | + assert "QueryGPT" in prompt |
| 64 | + assert "[thinking:" in prompt |
| 65 | + assert "```sql" in prompt |
| 66 | + |
| 67 | + def test_build_system_prompt_with_db_config(self, mock_user, mock_db): |
| 68 | + """Test system prompt with database config""" |
| 69 | + service = ExecutionService(user=mock_user, db=mock_db, language="zh") |
| 70 | + |
| 71 | + db_config = { |
| 72 | + "driver": "mysql", |
| 73 | + "host": "localhost", |
| 74 | + "port": 3306, |
| 75 | + "database": "testdb", |
| 76 | + } |
| 77 | + |
| 78 | + prompt = service._build_system_prompt(db_config) |
| 79 | + |
| 80 | + assert "mysql" in prompt |
| 81 | + assert "localhost" in prompt |
| 82 | + assert "3306" in prompt |
| 83 | + assert "testdb" in prompt |
| 84 | + |
| 85 | + def test_build_system_prompt_with_semantic_context(self, mock_user, mock_db): |
| 86 | + """Test system prompt with semantic terms""" |
| 87 | + service = ExecutionService(user=mock_user, db=mock_db, language="zh") |
| 88 | + |
| 89 | + # Create mock semantic context |
| 90 | + semantic_context = MagicMock(spec=SemanticContext) |
| 91 | + semantic_context.terms = [MagicMock()] |
| 92 | + semantic_context.to_prompt.return_value = "月活用户 = COUNT(DISTINCT user_id)" |
| 93 | + |
| 94 | + prompt = service._build_system_prompt(None, semantic_context=semantic_context) |
| 95 | + |
| 96 | + assert "月活用户" in prompt |
| 97 | + |
| 98 | + def test_build_system_prompt_with_relationship_context(self, mock_user, mock_db): |
| 99 | + """Test system prompt with table relationships""" |
| 100 | + service = ExecutionService(user=mock_user, db=mock_db, language="zh") |
| 101 | + |
| 102 | + # Create mock relationship context |
| 103 | + relationship_context = MagicMock(spec=RelationshipContext) |
| 104 | + relationship_context.relationships = [MagicMock()] |
| 105 | + relationship_context.to_prompt.return_value = "users.id -> orders.user_id" |
| 106 | + |
| 107 | + prompt = service._build_system_prompt(None, relationship_context=relationship_context) |
| 108 | + |
| 109 | + assert "users.id" in prompt |
| 110 | + |
| 111 | + def test_build_system_prompt_python_instructions(self, mock_user, mock_db): |
| 112 | + """Test that Python instructions are in prompt""" |
| 113 | + service = ExecutionService(user=mock_user, db=mock_db, language="zh") |
| 114 | + prompt = service._build_system_prompt(None) |
| 115 | + |
| 116 | + # Check Python-related instructions |
| 117 | + assert "python" in prompt.lower() |
| 118 | + assert "matplotlib" in prompt.lower() or "plt" in prompt.lower() |
| 119 | + assert "df" in prompt # DataFrame reference |
| 120 | + |
| 121 | + |
| 122 | +class TestSemanticContext: |
| 123 | + """Test SemanticContext model""" |
| 124 | + |
| 125 | + def test_empty_context(self): |
| 126 | + """Test empty semantic context""" |
| 127 | + context = SemanticContext(terms=[]) |
| 128 | + assert len(context.terms) == 0 |
| 129 | + |
| 130 | + def test_to_prompt_zh(self): |
| 131 | + """Test Chinese prompt generation""" |
| 132 | + from datetime import datetime |
| 133 | + from app.models import SemanticTermResponse |
| 134 | + |
| 135 | + term = SemanticTermResponse( |
| 136 | + id=uuid4(), |
| 137 | + term="月活用户", |
| 138 | + expression="COUNT(DISTINCT user_id)", |
| 139 | + term_type="metric", |
| 140 | + description="月度活跃用户数", |
| 141 | + is_active=True, |
| 142 | + created_at=datetime.now(), |
| 143 | + ) |
| 144 | + context = SemanticContext(terms=[term]) |
| 145 | + prompt = context.to_prompt("zh") |
| 146 | + |
| 147 | + assert "月活用户" in prompt |
| 148 | + assert "COUNT(DISTINCT user_id)" in prompt |
| 149 | + |
| 150 | + def test_to_prompt_en(self): |
| 151 | + """Test English prompt generation""" |
| 152 | + from datetime import datetime |
| 153 | + from app.models import SemanticTermResponse |
| 154 | + |
| 155 | + term = SemanticTermResponse( |
| 156 | + id=uuid4(), |
| 157 | + term="MAU", |
| 158 | + expression="COUNT(DISTINCT user_id)", |
| 159 | + term_type="metric", |
| 160 | + description="Monthly Active Users", |
| 161 | + is_active=True, |
| 162 | + created_at=datetime.now(), |
| 163 | + ) |
| 164 | + context = SemanticContext(terms=[term]) |
| 165 | + prompt = context.to_prompt("en") |
| 166 | + |
| 167 | + assert "MAU" in prompt |
| 168 | + |
| 169 | + |
| 170 | +class TestRelationshipContext: |
| 171 | + """Test RelationshipContext model""" |
| 172 | + |
| 173 | + def test_empty_context(self): |
| 174 | + """Test empty relationship context""" |
| 175 | + context = RelationshipContext(relationships=[]) |
| 176 | + assert len(context.relationships) == 0 |
| 177 | + |
| 178 | + def test_to_prompt_zh(self): |
| 179 | + """Test Chinese prompt generation""" |
| 180 | + from datetime import datetime |
| 181 | + from app.models import TableRelationshipResponse |
| 182 | + |
| 183 | + rel = TableRelationshipResponse( |
| 184 | + id=uuid4(), |
| 185 | + connection_id=uuid4(), |
| 186 | + source_table="users", |
| 187 | + source_column="id", |
| 188 | + target_table="orders", |
| 189 | + target_column="user_id", |
| 190 | + relationship_type="1:N", |
| 191 | + join_type="LEFT", |
| 192 | + is_active=True, |
| 193 | + created_at=datetime.now(), |
| 194 | + ) |
| 195 | + context = RelationshipContext(relationships=[rel]) |
| 196 | + prompt = context.to_prompt("zh") |
| 197 | + |
| 198 | + assert "users" in prompt |
| 199 | + assert "orders" in prompt |
| 200 | + |
| 201 | + def test_to_prompt_en(self): |
| 202 | + """Test English prompt generation""" |
| 203 | + from datetime import datetime |
| 204 | + from app.models import TableRelationshipResponse |
| 205 | + |
| 206 | + rel = TableRelationshipResponse( |
| 207 | + id=uuid4(), |
| 208 | + connection_id=uuid4(), |
| 209 | + source_table="products", |
| 210 | + source_column="id", |
| 211 | + target_table="order_items", |
| 212 | + target_column="product_id", |
| 213 | + relationship_type="1:N", |
| 214 | + join_type="INNER", |
| 215 | + is_active=True, |
| 216 | + created_at=datetime.now(), |
| 217 | + ) |
| 218 | + context = RelationshipContext(relationships=[rel]) |
| 219 | + prompt = context.to_prompt("en") |
| 220 | + |
| 221 | + assert "products" in prompt |
| 222 | + assert "order_items" in prompt |
0 commit comments