2024-06-19 13:41:12 +08:00
|
|
|
from textwrap import dedent
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from flask import Flask
|
|
|
|
|
2024-06-22 09:54:25 +08:00
|
|
|
from configs.app_config import DifyConfig
|
2024-06-19 13:41:12 +08:00
|
|
|
|
|
|
|
EXAMPLE_ENV_FILENAME = '.env'
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def example_env_file(tmp_path, monkeypatch) -> str:
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
file_path = tmp_path.joinpath(EXAMPLE_ENV_FILENAME)
|
|
|
|
file_path.write_text(dedent(
|
|
|
|
"""
|
|
|
|
CONSOLE_API_URL=https://example.com
|
|
|
|
"""))
|
|
|
|
return str(file_path)
|
|
|
|
|
|
|
|
|
2024-06-22 09:54:25 +08:00
|
|
|
def test_dify_config_undefined_entry(example_env_file):
|
2024-06-19 13:41:12 +08:00
|
|
|
# load dotenv file with pydantic-settings
|
2024-06-22 09:54:25 +08:00
|
|
|
config = DifyConfig(_env_file=example_env_file)
|
2024-06-19 13:41:12 +08:00
|
|
|
|
|
|
|
# entries not defined in app settings
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
# TypeError: 'AppSettings' object is not subscriptable
|
2024-06-22 09:54:25 +08:00
|
|
|
assert config['LOG_LEVEL'] == 'INFO'
|
2024-06-19 13:41:12 +08:00
|
|
|
|
|
|
|
|
2024-06-22 09:54:25 +08:00
|
|
|
def test_dify_config(example_env_file):
|
2024-06-19 13:41:12 +08:00
|
|
|
# load dotenv file with pydantic-settings
|
2024-06-22 09:54:25 +08:00
|
|
|
config = DifyConfig(_env_file=example_env_file)
|
2024-06-19 13:41:12 +08:00
|
|
|
|
|
|
|
# constant values
|
2024-06-22 09:54:25 +08:00
|
|
|
assert config.COMMIT_SHA == ''
|
2024-06-19 13:41:12 +08:00
|
|
|
|
|
|
|
# default values
|
2024-06-22 09:54:25 +08:00
|
|
|
assert config.EDITION == 'SELF_HOSTED'
|
|
|
|
assert config.API_COMPRESSION_ENABLED is False
|
|
|
|
assert config.SENTRY_TRACES_SAMPLE_RATE == 1.0
|
2024-06-19 13:41:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_flask_configs(example_env_file):
|
|
|
|
flask_app = Flask('app')
|
2024-06-22 09:54:25 +08:00
|
|
|
flask_app.config.from_mapping(DifyConfig(_env_file=example_env_file).model_dump())
|
2024-06-19 13:41:12 +08:00
|
|
|
config = flask_app.config
|
|
|
|
|
|
|
|
# configs read from dotenv directly
|
|
|
|
assert config['LOG_LEVEL'] == 'INFO'
|
|
|
|
|
|
|
|
# configs read from pydantic-settings
|
|
|
|
assert config['COMMIT_SHA'] == ''
|
|
|
|
assert config['EDITION'] == 'SELF_HOSTED'
|
|
|
|
assert config['API_COMPRESSION_ENABLED'] is False
|
|
|
|
assert config['SENTRY_TRACES_SAMPLE_RATE'] == 1.0
|
|
|
|
|
|
|
|
# value from env file
|
|
|
|
assert config['CONSOLE_API_URL'] == 'https://example.com'
|
|
|
|
# fallback to alias choices value as CONSOLE_API_URL
|
|
|
|
assert config['FILES_URL'] == 'https://example.com'
|