From 3af00649ba7b47252a77fd3300cee51cf56d46e7 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 19 Jun 2024 09:33:23 +0800 Subject: [PATCH] update --- api/config.py | 4 +-- ..._app_settings.py => test_dify_settings.py} | 33 ++++++++++++++----- 2 files changed, 26 insertions(+), 11 deletions(-) rename api/tests/unit_tests/settings/{test_app_settings.py => test_dify_settings.py} (52%) diff --git a/api/config.py b/api/config.py index 7efe9c5bc1e70e..66537f8ceee1cd 100644 --- a/api/config.py +++ b/api/config.py @@ -2,8 +2,6 @@ import dotenv -dotenv.load_dotenv() - DEFAULTS = { 'DB_USERNAME': 'postgres', 'DB_PASSWORD': '', @@ -67,6 +65,8 @@ class Config: """Application configuration class.""" def __init__(self): + dotenv.load_dotenv() + self.TESTING = False # cors settings diff --git a/api/tests/unit_tests/settings/test_app_settings.py b/api/tests/unit_tests/settings/test_dify_settings.py similarity index 52% rename from api/tests/unit_tests/settings/test_app_settings.py rename to api/tests/unit_tests/settings/test_dify_settings.py index de4fd57db6edee..2dba2cdce60d3f 100644 --- a/api/tests/unit_tests/settings/test_app_settings.py +++ b/api/tests/unit_tests/settings/test_dify_settings.py @@ -1,13 +1,28 @@ +from textwrap import dedent + import pytest from flask import Flask from config import Config from configs.app_configs import DifyConfigs +EXAMPLE_YAML_FILE = '.env' + + +@pytest.fixture +def prepare_example_env_file(tmp_path, monkeypatch) -> str: + monkeypatch.chdir(tmp_path) + file_path = tmp_path.joinpath(EXAMPLE_YAML_FILE) + file_path.write_text(dedent( + """\ + CONSOLE_API_URL=https://example.com + """)) + return str(file_path) + -def test_dify_configs_undefined_entry(): +def test_dify_configs_undefined_entry(prepare_example_env_file): # load dotenv file with pydantic-settings - settings = DifyConfigs() + settings = DifyConfigs(_env_file=prepare_example_env_file) # entries not defined in app settings with pytest.raises(TypeError): @@ -15,9 +30,9 @@ def test_dify_configs_undefined_entry(): assert settings['LOG_LEVEL'] == 'INFO' -def test_dify_configs(): +def test_dify_configs(prepare_example_env_file): # load dotenv file with pydantic-settings - settings = DifyConfigs() + settings = DifyConfigs(_env_file=prepare_example_env_file) # constant values assert settings.COMMIT_SHA == '' @@ -27,10 +42,9 @@ def test_dify_configs(): assert settings.API_COMPRESSION_ENABLED is False -def test_flask_configs(): +def test_flask_configs(prepare_example_env_file): flask_app = Flask('app') - flask_app.config.from_object(Config()) - flask_app.config.from_mapping(DifyConfigs().dict()) + flask_app.config.from_mapping(DifyConfigs(_env_file=prepare_example_env_file).dict()) config = flask_app.config # configs read from dotenv directly @@ -41,6 +55,7 @@ def test_flask_configs(): assert config['EDITION'] == 'SELF_HOSTED' assert config['API_COMPRESSION_ENABLED'] is False - assert config['CONSOLE_API_URL'] == 'https://cloud.dify.ai' + # 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://cloud.dify.ai' + assert config['FILES_URL'] == 'https://example.com'