2024-07-22 15:29:39 +08:00
|
|
|
import os
|
2024-06-19 13:41:12 +08:00
|
|
|
from textwrap import dedent
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from flask import Flask
|
2024-08-27 19:38:33 +08:00
|
|
|
from yarl import URL
|
2024-06-19 13:41:12 +08:00
|
|
|
|
2024-06-22 09:54:25 +08:00
|
|
|
from configs.app_config import DifyConfig
|
2024-06-19 13:41:12 +08:00
|
|
|
|
2024-08-23 23:52:25 +08:00
|
|
|
EXAMPLE_ENV_FILENAME = ".env"
|
2024-06-19 13:41:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def example_env_file(tmp_path, monkeypatch) -> str:
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
file_path = tmp_path.joinpath(EXAMPLE_ENV_FILENAME)
|
2024-08-23 23:52:25 +08:00
|
|
|
file_path.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
2024-06-19 13:41:12 +08:00
|
|
|
CONSOLE_API_URL=https://example.com
|
2024-06-25 15:48:02 +08:00
|
|
|
CONSOLE_WEB_URL=https://example.com
|
2024-08-30 19:09:10 +08:00
|
|
|
HTTP_REQUEST_MAX_WRITE_TIMEOUT=30
|
2024-08-23 23:52:25 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2024-06-19 13:41:12 +08:00
|
|
|
return str(file_path)
|
|
|
|
|
|
|
|
|
2024-06-22 09:54:25 +08:00
|
|
|
def test_dify_config_undefined_entry(example_env_file):
|
2024-07-07 12:18:15 +08:00
|
|
|
# NOTE: See https://github.com/microsoft/pylance-release/issues/6099 for more details about this type error.
|
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-08-23 23:52: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-08-23 23:52:25 +08:00
|
|
|
assert config.COMMIT_SHA == ""
|
2024-06-19 13:41:12 +08:00
|
|
|
|
|
|
|
# default values
|
2024-08-23 23:52:25 +08:00
|
|
|
assert config.EDITION == "SELF_HOSTED"
|
2024-06-22 09:54:25 +08:00
|
|
|
assert config.API_COMPRESSION_ENABLED is False
|
|
|
|
assert config.SENTRY_TRACES_SAMPLE_RATE == 1.0
|
2024-06-19 13:41:12 +08:00
|
|
|
|
2024-08-30 19:09:10 +08:00
|
|
|
# annotated field with default value
|
|
|
|
assert config.HTTP_REQUEST_MAX_READ_TIMEOUT == 60
|
|
|
|
|
|
|
|
# annotated field with configured value
|
|
|
|
assert config.HTTP_REQUEST_MAX_WRITE_TIMEOUT == 30
|
|
|
|
|
2024-06-19 13:41:12 +08:00
|
|
|
|
2024-07-07 12:18:15 +08:00
|
|
|
# NOTE: If there is a `.env` file in your Workspace, this test might not succeed as expected.
|
|
|
|
# This is due to `pymilvus` loading all the variables from the `.env` file into `os.environ`.
|
2024-06-19 13:41:12 +08:00
|
|
|
def test_flask_configs(example_env_file):
|
2024-08-23 23:52:25 +08:00
|
|
|
flask_app = Flask("app")
|
2024-07-22 15:29:39 +08:00
|
|
|
# clear system environment variables
|
|
|
|
os.environ.clear()
|
|
|
|
flask_app.config.from_mapping(DifyConfig(_env_file=example_env_file).model_dump()) # pyright: ignore
|
2024-06-19 13:41:12 +08:00
|
|
|
config = flask_app.config
|
|
|
|
|
|
|
|
# configs read from pydantic-settings
|
2024-08-23 23:52:25 +08:00
|
|
|
assert config["LOG_LEVEL"] == "INFO"
|
|
|
|
assert config["COMMIT_SHA"] == ""
|
|
|
|
assert config["EDITION"] == "SELF_HOSTED"
|
|
|
|
assert config["API_COMPRESSION_ENABLED"] is False
|
|
|
|
assert config["SENTRY_TRACES_SAMPLE_RATE"] == 1.0
|
|
|
|
assert config["TESTING"] == False
|
2024-06-19 13:41:12 +08:00
|
|
|
|
|
|
|
# value from env file
|
2024-08-23 23:52:25 +08:00
|
|
|
assert config["CONSOLE_API_URL"] == "https://example.com"
|
2024-06-19 13:41:12 +08:00
|
|
|
# fallback to alias choices value as CONSOLE_API_URL
|
2024-08-23 23:52:25 +08:00
|
|
|
assert config["FILES_URL"] == "https://example.com"
|
2024-06-22 10:29:56 +08:00
|
|
|
|
2024-08-23 23:52:25 +08:00
|
|
|
assert config["SQLALCHEMY_DATABASE_URI"] == "postgresql://postgres:@localhost:5432/dify"
|
|
|
|
assert config["SQLALCHEMY_ENGINE_OPTIONS"] == {
|
|
|
|
"connect_args": {
|
|
|
|
"options": "-c timezone=UTC",
|
2024-06-22 10:29:56 +08:00
|
|
|
},
|
2024-08-23 23:52:25 +08:00
|
|
|
"max_overflow": 10,
|
|
|
|
"pool_pre_ping": False,
|
|
|
|
"pool_recycle": 3600,
|
|
|
|
"pool_size": 30,
|
2024-06-22 10:29:56 +08:00
|
|
|
}
|
2024-06-25 15:48:02 +08:00
|
|
|
|
2024-08-23 23:52:25 +08:00
|
|
|
assert config["CONSOLE_WEB_URL"] == "https://example.com"
|
|
|
|
assert config["CONSOLE_CORS_ALLOW_ORIGINS"] == ["https://example.com"]
|
|
|
|
assert config["WEB_API_CORS_ALLOW_ORIGINS"] == ["*"]
|
2024-08-27 19:38:33 +08:00
|
|
|
|
|
|
|
assert str(config["CODE_EXECUTION_ENDPOINT"]) == "http://sandbox:8194/"
|
|
|
|
assert str(URL(str(config["CODE_EXECUTION_ENDPOINT"])) / "v1") == "http://sandbox:8194/v1"
|