Skip to content

Commit 140ec82

Browse files
committed
update tools
1 parent 0b76975 commit 140ec82

20 files changed

+259
-1723
lines changed

backend/tests/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55

66
import asyncio
77
import os
8+
import sys
9+
from pathlib import Path
810
from unittest.mock import AsyncMock, MagicMock, Mock, patch
911

1012
import pytest
1113

1214
# Set testing environment variables before any imports
1315
os.environ["TESTING"] = "1"
1416

17+
# Ensure backend package is importable when tests run from project root
18+
BACKEND_ROOT = Path(__file__).resolve().parents[1]
19+
if str(BACKEND_ROOT) not in sys.path:
20+
sys.path.insert(0, str(BACKEND_ROOT))
21+
1522

1623
# Setup async test event loop
1724
@pytest.fixture(scope="session")
@@ -27,12 +34,24 @@ def mock_db_session():
2734
"""Mock database session"""
2835
mock_session = AsyncMock()
2936
mock_session.execute = AsyncMock()
37+
mock_session.execute.return_value = Mock()
38+
mock_session.execute.return_value.scalars.return_value.all.return_value = []
39+
mock_session.execute.return_value.scalar_one_or_none.return_value = None
3040
mock_session.scalar = AsyncMock()
3141
mock_session.add = Mock()
3242
mock_session.flush = AsyncMock()
3343
mock_session.commit = AsyncMock()
3444
mock_session.rollback = AsyncMock()
3545
mock_session.close = AsyncMock()
46+
47+
class _AsyncTransaction:
48+
async def __aenter__(self):
49+
return None
50+
51+
async def __aexit__(self, exc_type, exc, tb):
52+
return False
53+
54+
mock_session.begin = Mock(return_value=_AsyncTransaction())
3655
return mock_session
3756

3857

backend/tests/test_system_config.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
Test cases for system configuration batch operations.
33
"""
44

5+
from contextlib import asynccontextmanager
56
from unittest.mock import AsyncMock, Mock, patch
67

78
import pytest
89
from fastapi.testclient import TestClient
910

1011
from app import app
12+
from utils.tools import mask_config_value
1113

1214

1315
@pytest.mark.asyncio
@@ -35,8 +37,13 @@ async def test_batch_upsert_system_configs(mock_db_session, mock_request):
3537
},
3638
]
3739

38-
# Mock the database operations
39-
with patch("service.system_service.get_db_session", return_value=mock_db_session):
40+
@asynccontextmanager
41+
async def mock_session_factory():
42+
yield mock_db_session
43+
44+
# Mock the database session factory used by middleware
45+
with patch("middleware.db_middleware.async_session_factory") as mock_factory:
46+
mock_factory.return_value = mock_session_factory()
4047
with TestClient(app) as client:
4148
response = client.post("/api/system/batch", json={"configs": test_configs})
4249
assert response.status_code == 200
@@ -53,7 +60,10 @@ async def test_batch_upsert_system_configs(mock_db_session, mock_request):
5360
]
5461
# Sensitive values should be masked
5562
if config["config_key"] == "test_api_key":
56-
assert config["config_value"] == "••••••••••••••••"
63+
expected_value = mask_config_value(
64+
config["config_key"], "sk-test-key"
65+
)
66+
assert config["config_value"] == expected_value
5767

5868

5969
@pytest.mark.asyncio
@@ -76,8 +86,12 @@ async def test_batch_upsert_mixed_operations(mock_db_session, mock_request):
7686
},
7787
]
7888

79-
# Mock the database operations
80-
with patch("service.system_service.get_db_session", return_value=mock_db_session):
89+
@asynccontextmanager
90+
async def mock_session_factory():
91+
yield mock_db_session
92+
93+
with patch("middleware.db_middleware.async_session_factory") as mock_factory:
94+
mock_factory.return_value = mock_session_factory()
8195
with TestClient(app) as client:
8296
response = client.post("/api/system/batch", json={"configs": mixed_configs})
8397
assert response.status_code == 200
Lines changed: 45 additions & 117 deletions
Large diffs are not rendered by default.

frontend/public/locales/en/translation.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@
401401
"selectDatasetFile": "Select Dataset File",
402402
"jsonlFormatDescription": "Required format: .jsonl file with each line containing {\"id\": \"...\", \"prompt\": \"...\"}",
403403
"datasetFileFormatDescription": "Supports JSON (ShareGPT format) and JSONL formats. ShareGPT: [{\"id\": \"...\", \"conversations\": [...]}], JSONL: one JSON object per line {\"id\": \"...\", \"prompt\": \"...\"}",
404-
"datasetImageMountWarning": "⚠️ If dataset contains images, ensure image files are mounted to the container, see deployment docs",
404+
"datasetImageMountWarning": "⚠️ If the dataset contains images, ensure image files are mounted to the container. See DATASET_GUIDE for details.",
405405
"jsonlData": "JSONL Data",
406406
"jsonlDataTooltip": "Each line must be a valid JSON object with \"id\" and \"prompt\" fields.",
407407
"testDuration": "Test Duration (seconds)",

frontend/public/locales/zh/translation.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@
401401
"selectDatasetFile": "选择数据集文件",
402402
"jsonlFormatDescription": "必需格式:.jsonl文件,每行包含{\"id\": \"...\", \"prompt\": \"...\"}",
403403
"datasetFileFormatDescription": "支持 JSON (ShareGPT格式) 和 JSONL 格式。ShareGPT格式: [{\"id\": \"...\", \"conversations\": [...]}],JSONL格式: 每行一个JSON对象 {\"id\": \"...\", \"prompt\": \"...\"}",
404-
"datasetImageMountWarning": "⚠️ 若数据集包含图片,请确保图片文件已挂载到容器,详见部署文档",
404+
"datasetImageMountWarning": "⚠️ 若数据集包含图片,请确保图片文件已挂载到容器对应目录下,详见 DATASET_GUIDE。",
405405
"jsonlData": "JSONL数据",
406406
"jsonlDataTooltip": "每行必须是具有id和prompt字段的有效JSON对象。",
407407
"testDuration": "测试持续时间(秒)",

st_engine/config/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"LOG_DIR",
2121
"LOG_TASK_DIR",
2222
"DATA_DIR",
23-
"PROMPTS_DIR",
2423
"IMAGES_DIR",
2524
"UPLOAD_FOLDER",
2625
"HTTP_OK",

st_engine/config/base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525

2626
# === LOG PATHS ===
2727
LOG_TASK_DIR = os.path.join(LOG_DIR, "task")
28-
# === DATA PATHS ===
29-
PROMPTS_DIR = os.path.join(DATA_DIR, "prompts")
28+
# === IMAGES PATHS ===
3029
IMAGES_DIR = os.path.join(DATA_DIR, "pic")
3130

3231
# === HTTP CONSTANTS ===
@@ -57,7 +56,6 @@
5756
"LOG_DIR",
5857
"LOG_TASK_DIR",
5958
"DATA_DIR",
60-
"PROMPTS_DIR",
6159
"IMAGES_DIR",
6260
"UPLOAD_FOLDER",
6361
# http

st_engine/tests/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Shared pytest configuration for st_engine tests.
3+
"""
4+
5+
import os
6+
import sys
7+
from pathlib import Path
8+
9+
# Ensure tests run in test mode
10+
os.environ.setdefault("TESTING", "1")
11+
12+
# Make the st_engine package importable when tests run from repository root
13+
ST_ENGINE_ROOT = Path(__file__).resolve().parents[1]
14+
if str(ST_ENGINE_ROOT) not in sys.path:
15+
sys.path.insert(0, str(ST_ENGINE_ROOT))

0 commit comments

Comments
 (0)