Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenliang123 committed Jun 19, 2024
1 parent 67bb428 commit 3af0064
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import dotenv

dotenv.load_dotenv()

DEFAULTS = {
'DB_USERNAME': 'postgres',
'DB_PASSWORD': '',
Expand Down Expand Up @@ -67,6 +65,8 @@ class Config:
"""Application configuration class."""

def __init__(self):
dotenv.load_dotenv()

self.TESTING = False

# cors settings
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
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):
# TypeError: 'AppSettings' object is not subscriptable
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 == ''
Expand All @@ -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
Expand All @@ -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'

0 comments on commit 3af0064

Please sign in to comment.