feat: implement ERP AI Assistant Phase 1
Backend (FastAPI + SQLAlchemy + Claude API + RAG): - Config management with Pydantic v2 - Database engine with connection pooling and SQL injection prevention - AI engine with Claude API integration (support custom base URL) - RAG engine with ChromaDB and sentence-transformers - Requirement analysis service - Config generation service - Executor engine with SQL validation - REST API endpoints: /analyze, /generate, /execute Frontend (Vue 3 + Element Plus + Pinia): - Complete 3-step workflow: analyze → generate → execute - Step indicator with progress visualization - Analysis result display with field table - SQL preview with monospace font - Execute confirmation dialog with safety warning - Execution result display - State management with Pinia - API service integration Security: - SQL injection prevention with parameterized queries - Dangerous SQL operation blocking - Database password URL encoding - Transaction auto-rollback - Pydantic config validation Features: - Natural language requirement analysis - Automated SQL configuration generation - Safe execution with human review - LAN access support - Custom Claude API endpoint support Documentation: - README with quick start guide - Quick start guide - LAN access configuration - Dependency fixes guide - Claude API configuration - Git operation guide - Implementation report Dependencies fixed: - numpy<2.0.0 for chromadb compatibility - sentence-transformers==2.7.0 for huggingface_hub compatibility Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
94
backend/tests/test_config_service.py
Normal file
94
backend/tests/test_config_service.py
Normal file
@@ -0,0 +1,94 @@
|
||||
"""Tests for Config Service.
|
||||
|
||||
This module tests the ConfigService class for configuration generation.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, AsyncMock, patch
|
||||
from app.services.config_service import ConfigService
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_generate_config():
|
||||
"""Test config generation with mocked dependencies."""
|
||||
with patch('app.services.config_service.ClaudeEngine') as MockClaudeEngine, \
|
||||
patch('app.services.config_service.RAGEngine') as MockRAGEngine, \
|
||||
patch('app.services.config_service.DatabaseEngine') as MockDBEngine:
|
||||
|
||||
# Setup mocks
|
||||
mock_ai_engine = MagicMock()
|
||||
mock_ai_engine.call_claude = AsyncMock(return_value='{"配置方案": {"sql_list": ["INSERT INTO SYS_FORM..."]}}')
|
||||
mock_ai_engine.parse_json_response = MagicMock(return_value={
|
||||
"配置方案": {
|
||||
"sql_list": ["INSERT INTO SYS_FORM VALUES (...)"]
|
||||
}
|
||||
})
|
||||
MockClaudeEngine.return_value = mock_ai_engine
|
||||
|
||||
mock_rag_engine = MagicMock()
|
||||
mock_rag_engine.search = MagicMock(return_value=[
|
||||
{"content": "Sample rule", "metadata": {}}
|
||||
])
|
||||
MockRAGEngine.return_value = mock_rag_engine
|
||||
|
||||
MockDBEngine.return_value = MagicMock()
|
||||
|
||||
# Create service and test
|
||||
service = ConfigService()
|
||||
|
||||
requirements = {
|
||||
"功能名称": "销售订单",
|
||||
"功能号建议": "11-001",
|
||||
"窗体类型": "5",
|
||||
"主表名建议": "SA_ORDER",
|
||||
"主表字段": [
|
||||
{"字段名": "订单号", "字段类型": "varchar(50)", "必填": True}
|
||||
]
|
||||
}
|
||||
|
||||
result = await service.generate(requirements, "test-session")
|
||||
|
||||
assert result is not None
|
||||
assert "配置方案" in result
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_platform_rules():
|
||||
"""Test platform rules retrieval."""
|
||||
with patch('app.services.config_service.ClaudeEngine'), \
|
||||
patch('app.services.config_service.RAGEngine') as MockRAGEngine, \
|
||||
patch('app.services.config_service.DatabaseEngine'):
|
||||
|
||||
mock_rag_engine = MagicMock()
|
||||
mock_rag_engine.search = MagicMock(return_value=[
|
||||
{"content": "Rule 1"},
|
||||
{"content": "Rule 2"}
|
||||
])
|
||||
MockRAGEngine.return_value = mock_rag_engine
|
||||
|
||||
service = ConfigService()
|
||||
rules = service._get_platform_rules("5")
|
||||
|
||||
assert "Rule 1" in rules
|
||||
assert "Rule 2" in rules
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_similar_cases():
|
||||
"""Test similar cases retrieval."""
|
||||
with patch('app.services.config_service.ClaudeEngine'), \
|
||||
patch('app.services.config_service.RAGEngine') as MockRAGEngine, \
|
||||
patch('app.services.config_service.DatabaseEngine'):
|
||||
|
||||
mock_rag_engine = MagicMock()
|
||||
mock_rag_engine.search = MagicMock(return_value=[
|
||||
{"content": "Case 1"},
|
||||
{"content": "Case 2"}
|
||||
])
|
||||
MockRAGEngine.return_value = mock_rag_engine
|
||||
|
||||
service = ConfigService()
|
||||
cases = service._get_similar_cases("销售订单")
|
||||
|
||||
assert "Case 1" in cases
|
||||
assert "Case 2" in cases
|
||||
Reference in New Issue
Block a user