feat(shipping-notice): 新增临时库存管理功能,支持创建会话、获取分析结果及字段映射建议
This commit is contained in:
1 parent
46eb63f5bb
commit
e9bdd81f1a
320 files changed
+28361
-10487
No files matched your search
@@ -0,0 +1,78 @@
|
||||
"""
|
||||
tests/test_init_local_db.py
|
||||
|
||||
用途:验证本地 SQLite 初始化脚本只操作显式目标,并安全区分空库、版本库与未知旧库。
|
||||
依赖:init_local_db.py、SQLite、Alembic 迁移目录、subprocess。
|
||||
逻辑:使用临时路径执行真实脚本;检查全新库版本标记和种子数据,并确认未知旧库被拒绝且字节不变。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
BACKEND_DIR = Path(__file__).resolve().parents[1]
|
||||
SCRIPT_PATH = BACKEND_DIR / 'init_local_db.py'
|
||||
|
||||
|
||||
def _run_initializer(database_path: Path) -> subprocess.CompletedProcess[str]:
|
||||
"""
|
||||
在隔离 SQLite 路径运行真实初始化脚本。
|
||||
|
||||
Args:
|
||||
database_path: 测试专用数据库文件,禁止指向仓库默认 local_test.db。
|
||||
|
||||
Returns:
|
||||
捕获标准输出和错误输出的子进程结果。
|
||||
"""
|
||||
env = os.environ.copy()
|
||||
env['HXOPS_LOCAL_DATABASE_PATH'] = str(database_path)
|
||||
env['FIXED_ASSETS_SYNC_ENABLED'] = 'false'
|
||||
return subprocess.run(
|
||||
[sys.executable, str(SCRIPT_PATH)],
|
||||
cwd=BACKEND_DIR,
|
||||
env=env,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=120,
|
||||
check=False,
|
||||
)
|
||||
|
||||
|
||||
def test_init_local_db_creates_isolated_current_schema(tmp_path):
|
||||
"""全新隔离库应初始化、标记 Alembic head,并写入最小本地账号。"""
|
||||
database_path = tmp_path / 'isolated-local.db'
|
||||
|
||||
result = _run_initializer(database_path)
|
||||
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert database_path.is_file()
|
||||
with sqlite3.connect(database_path) as connection:
|
||||
version = connection.execute('SELECT version_num FROM alembic_version').fetchone()
|
||||
admin = connection.execute(
|
||||
"SELECT username, role FROM users WHERE username = 'admin'",
|
||||
).fetchone()
|
||||
agent_count = connection.execute('SELECT COUNT(*) FROM agents').fetchone()[0]
|
||||
assert version is not None and version[0]
|
||||
assert admin == ('admin', 'admin')
|
||||
assert int(agent_count) == 1
|
||||
|
||||
|
||||
def test_init_local_db_refuses_unversioned_database_without_modifying_it(tmp_path):
|
||||
"""已有业务表但无 Alembic 版本的数据库必须原样拒绝,不能静默 stamp 或删库。"""
|
||||
database_path = tmp_path / 'legacy-local.db'
|
||||
with sqlite3.connect(database_path) as connection:
|
||||
connection.execute('CREATE TABLE legacy_orders (id INTEGER PRIMARY KEY, code TEXT NOT NULL)')
|
||||
connection.execute("INSERT INTO legacy_orders (code) VALUES ('KEEP-ME')")
|
||||
connection.commit()
|
||||
before = database_path.read_bytes()
|
||||
|
||||
result = _run_initializer(database_path)
|
||||
|
||||
assert result.returncode == 2
|
||||
assert 'Refusing to modify legacy unversioned database' in result.stderr
|
||||
assert database_path.read_bytes() == before
|
||||
Reference in new issue
Block a user