Files
gsfdc/demo-dify/dify/api/tests/helpers/unit/config_override.py
T
wuzilongandCursor 6519682885 chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-18 13:59:25 +08:00

27 lines
930 B
Python

"""Typed config override support shared by unit-test fixtures and helpers."""
from collections.abc import Generator
from contextlib import contextmanager
import pytest
from configs import dify_config
def apply_config_overrides(monkeypatch: pytest.MonkeyPatch, **values: object) -> None:
"""Override known DifyConfig fields for the lifetime of ``monkeypatch``."""
unknown_fields = values.keys() - type(dify_config).model_fields.keys()
if unknown_fields:
raise ValueError(f"Unknown DifyConfig fields: {sorted(unknown_fields)}")
for name, value in values.items():
monkeypatch.setattr(dify_config, name, value)
@contextmanager
def config_overrides_context(**values: object) -> Generator[None]:
"""Apply validated config overrides as a context manager or decorator."""
with pytest.MonkeyPatch.context() as monkeypatch:
apply_config_overrides(monkeypatch, **values)
yield