34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
"""scripts/init_db.py 的 Oracle 自增 SQL 生成器单测(与 migrate_db 对齐)。
|
|
|
|
Oracle create_all 不生成 Integer 主键自增,init_db 会补 sequence + BEFORE INSERT 触发器;
|
|
这里验证生成的 DDL 字符串正确(与 scripts/migrate_db.py 的既有实现一致)。
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# scripts/ 下无 __init__.py,把 scripts 目录加进 sys.path 以便直接 import
|
|
_SCRIPTS_DIR = Path(__file__).resolve().parents[3] / "scripts"
|
|
|
|
|
|
def _setup_scripts_path():
|
|
if str(_SCRIPTS_DIR) not in sys.path:
|
|
sys.path.insert(0, str(_SCRIPTS_DIR))
|
|
|
|
|
|
_setup_scripts_path()
|
|
|
|
import init_db
|
|
|
|
|
|
def test_oracle_sequence_sql():
|
|
seq = init_db.oracle_sequence_sql("fastapi_user", 27)
|
|
assert seq == "CREATE SEQUENCE fastapi_user_id_seq START WITH 27 INCREMENT BY 1 NOCACHE"
|
|
|
|
|
|
def test_oracle_trigger_sql():
|
|
trg = init_db.oracle_trigger_sql("fastapi_user")
|
|
assert "BEFORE INSERT ON fastapi_user FOR EACH ROW" in trg
|
|
assert "WHEN (NEW.id IS NULL)" in trg
|
|
assert "SELECT fastapi_user_id_seq.NEXTVAL INTO :NEW.id FROM dual" in trg
|