Underground-Digital/Workflow-Engine
0
1import os2from textwrap import dedent3 4import pytest5from flask import Flask6from yarl import URL7 8from configs.app_config import DifyConfig9 10EXAMPLE_ENV_FILENAME = ".env"11 12 13@pytest.fixture14def example_env_file(tmp_path, monkeypatch) -> str:15 monkeypatch.chdir(tmp_path)16 file_path = tmp_path.joinpath(EXAMPLE_ENV_FILENAME)17 file_path.write_text(18 dedent(19 """20 CONSOLE_API_URL=https://example.com21 CONSOLE_WEB_URL=https://example.com22 HTTP_REQUEST_MAX_WRITE_TIMEOUT=3023 """24 )25 )26 return str(file_path)27 28 29def test_dify_config_undefined_entry(example_env_file):30 # NOTE: See https://github.com/microsoft/pylance-release/issues/6099 for more details about this type error.31 # load dotenv file with pydantic-settings32 config = DifyConfig(_env_file=example_env_file)33 34 # entries not defined in app settings35 with pytest.raises(TypeError):36 # TypeError: 'AppSettings' object is not subscriptable37 assert config["LOG_LEVEL"] == "INFO"38 39 40def test_dify_config(example_env_file):41 # load dotenv file with pydantic-settings42 config = DifyConfig(_env_file=example_env_file)43 44 # constant values45 assert config.COMMIT_SHA == ""46 47 # default values48 assert config.EDITION == "SELF_HOSTED"49 assert config.API_COMPRESSION_ENABLED is False50 assert config.SENTRY_TRACES_SAMPLE_RATE == 1.051 52 # annotated field with default value53 assert config.HTTP_REQUEST_MAX_READ_TIMEOUT == 6054 55 # annotated field with configured value56 assert config.HTTP_REQUEST_MAX_WRITE_TIMEOUT == 3057 58 59# NOTE: If there is a `.env` file in your Workspace, this test might not succeed as expected.60# This is due to `pymilvus` loading all the variables from the `.env` file into `os.environ`.61def test_flask_configs(example_env_file):62 flask_app = Flask("app")63 # clear system environment variables64 os.environ.clear()65 flask_app.config.from_mapping(DifyConfig(_env_file=example_env_file).model_dump()) # pyright: ignore66 config = flask_app.config67 68 # configs read from pydantic-settings69 assert config["LOG_LEVEL"] == "INFO"70 assert config["COMMIT_SHA"] == ""71 assert config["EDITION"] == "SELF_HOSTED"72 assert config["API_COMPRESSION_ENABLED"] is False73 assert config["SENTRY_TRACES_SAMPLE_RATE"] == 1.074 assert config["TESTING"] == False75 76 # value from env file77 assert config["CONSOLE_API_URL"] == "https://example.com"78 # fallback to alias choices value as CONSOLE_API_URL79 assert config["FILES_URL"] == "https://example.com"80 81 assert config["SQLALCHEMY_DATABASE_URI"] == "postgresql://postgres:@localhost:5432/dify"82 assert config["SQLALCHEMY_ENGINE_OPTIONS"] == {83 "connect_args": {84 "options": "-c timezone=UTC",85 },86 "max_overflow": 10,87 "pool_pre_ping": False,88 "pool_recycle": 3600,89 "pool_size": 30,90 }91 92 assert config["CONSOLE_WEB_URL"] == "https://example.com"93 assert config["CONSOLE_CORS_ALLOW_ORIGINS"] == ["https://example.com"]94 assert config["WEB_API_CORS_ALLOW_ORIGINS"] == ["*"]95 96 assert str(config["CODE_EXECUTION_ENDPOINT"]) == "http://sandbox:8194/"97 assert str(URL(str(config["CODE_EXECUTION_ENDPOINT"])) / "v1") == "http://sandbox:8194/v1"98 