feat(phase6): 测试补全 + 集成测试 + repository 落地示范
- tests/unit/modules/test_repository.py:base_repository 在 SQLite 内存库的 CRUD 测试 - tests/integration/api/test_smoke.py:登录链路 / 只读接口 / 未授权拦截(标记 integration,默认跳过) - conftest 提供 sqlite_db(隔离 DB)与 app/client/admin_headers(集成)fixture - pytest.ini 默认跳过 integration,Makefile test-integration 可显式运行 - 修复 base_repository.get_by_id/get_by_ids 对字符串 ID 的兼容(URL 路径参数) - 示范 modules/system/level service 改用 level_repo,CRUD 全链路实测通过 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -42,8 +42,14 @@ class BaseRepository(Generic[M]):
|
||||
# ============================================================
|
||||
|
||||
def get_by_id(self, obj_id: Optional[int]) -> Optional[M]:
|
||||
"""根据ID获取未删除记录。"""
|
||||
if obj_id is None or obj_id <= 0:
|
||||
"""根据ID获取未删除记录(兼容字符串ID,如 URL 路径参数)。"""
|
||||
if obj_id is None or obj_id == '':
|
||||
return None
|
||||
try:
|
||||
obj_id = int(obj_id)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
if obj_id <= 0:
|
||||
return None
|
||||
return self.db.query(self.model).filter(
|
||||
self.model.id == obj_id, self._soft_col() == 0
|
||||
@@ -51,7 +57,14 @@ class BaseRepository(Generic[M]):
|
||||
|
||||
def get_by_ids(self, ids) -> list:
|
||||
"""根据ID列表批量获取未删除记录。"""
|
||||
valid = [i for i in ids if i and i > 0]
|
||||
valid = []
|
||||
for i in ids:
|
||||
try:
|
||||
i = int(i)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if i > 0:
|
||||
valid.append(i)
|
||||
if not valid:
|
||||
return []
|
||||
return self.db.query(self.model).filter(
|
||||
|
||||
Reference in New Issue
Block a user