56 lines
2.1 KiB
Python
56 lines
2.1 KiB
Python
"""
|
||
字典一键初始化脚本测试(seed_dict)
|
||
=============================================================
|
||
|
||
验证 scripts/seed_dict.py 的幂等初始化:
|
||
- 首次执行创建 DICT_SEED 中全部字典(dict + dict_item)
|
||
- 二次执行不重复(按 code 跳过已存在)
|
||
- 种入后 dict_utils 可从字典表取值
|
||
"""
|
||
|
||
import pytest
|
||
|
||
from src.modules.dictionary import dict_utils
|
||
from src.modules.dictionary.dict.repository import DictRepository
|
||
from src.modules.dictionary.dict_item.repository import DictItemRepository
|
||
from scripts.seed_dict import seed_dict
|
||
|
||
|
||
@pytest.fixture(autouse=True)
|
||
def _clear_dict_cache():
|
||
"""每用例前后清空 dict_utils 缓存,避免用例间 sqlite 库重建导致的脏缓存"""
|
||
dict_utils.clear_cache()
|
||
yield
|
||
dict_utils.clear_cache()
|
||
|
||
|
||
class TestSeedDict:
|
||
def test_seed_creates_all_dicts(self, app):
|
||
from scripts.seed_dict import DICT_SEED
|
||
|
||
with app.app_context():
|
||
created = seed_dict(app)
|
||
assert created == len(DICT_SEED)
|
||
for spec in DICT_SEED:
|
||
dict_obj = DictRepository.first_by({"code": spec["code"]})
|
||
assert dict_obj is not None, spec["code"]
|
||
assert len(DictItemRepository.list_by_code(spec["code"])) == len(spec["items"])
|
||
|
||
def test_seed_idempotent(self, app):
|
||
with app.app_context():
|
||
seed_dict(app)
|
||
first_items = len(DictItemRepository.list_by_code("gender"))
|
||
created = seed_dict(app)
|
||
assert created == 0
|
||
# 重跑不重复:item 数不变、dict 仍只有一份
|
||
assert len(DictItemRepository.list_by_code("gender")) == first_items
|
||
assert len(DictRepository.find_list(filters={"code": "gender"})) == 1
|
||
|
||
def test_seed_works_with_dict_utils(self, app):
|
||
with app.app_context():
|
||
seed_dict(app)
|
||
assert dict_utils.get_name("gender", 1) == "男"
|
||
assert dict_utils.get_name("operation_type", 3) == "新增"
|
||
assert dict_utils.get_name("article_status", 0) == "正常"
|
||
assert dict_utils.get_name("operation_source", "0") == "后台系统"
|