test_dify_config.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from textwrap import dedent
  2. import pytest
  3. from flask import Flask
  4. from configs.app_config import DifyConfig
  5. EXAMPLE_ENV_FILENAME = '.env'
  6. @pytest.fixture
  7. def example_env_file(tmp_path, monkeypatch) -> str:
  8. monkeypatch.chdir(tmp_path)
  9. file_path = tmp_path.joinpath(EXAMPLE_ENV_FILENAME)
  10. file_path.write_text(dedent(
  11. """
  12. CONSOLE_API_URL=https://example.com
  13. """))
  14. return str(file_path)
  15. def test_dify_config_undefined_entry(example_env_file):
  16. # load dotenv file with pydantic-settings
  17. config = DifyConfig(_env_file=example_env_file)
  18. # entries not defined in app settings
  19. with pytest.raises(TypeError):
  20. # TypeError: 'AppSettings' object is not subscriptable
  21. assert config['LOG_LEVEL'] == 'INFO'
  22. def test_dify_config(example_env_file):
  23. # load dotenv file with pydantic-settings
  24. config = DifyConfig(_env_file=example_env_file)
  25. # constant values
  26. assert config.COMMIT_SHA == ''
  27. # default values
  28. assert config.EDITION == 'SELF_HOSTED'
  29. assert config.API_COMPRESSION_ENABLED is False
  30. assert config.SENTRY_TRACES_SAMPLE_RATE == 1.0
  31. def test_flask_configs(example_env_file):
  32. flask_app = Flask('app')
  33. flask_app.config.from_mapping(DifyConfig(_env_file=example_env_file).model_dump())
  34. config = flask_app.config
  35. # configs read from dotenv directly
  36. assert config['LOG_LEVEL'] == 'INFO'
  37. # configs read from pydantic-settings
  38. assert config['COMMIT_SHA'] == ''
  39. assert config['EDITION'] == 'SELF_HOSTED'
  40. assert config['API_COMPRESSION_ENABLED'] is False
  41. assert config['SENTRY_TRACES_SAMPLE_RATE'] == 1.0
  42. # value from env file
  43. assert config['CONSOLE_API_URL'] == 'https://example.com'
  44. # fallback to alias choices value as CONSOLE_API_URL
  45. assert config['FILES_URL'] == 'https://example.com'
  46. assert config['SQLALCHEMY_DATABASE_URI'] == 'postgresql://postgres:@localhost:5432/dify'
  47. assert config['SQLALCHEMY_ENGINE_OPTIONS'] == {
  48. 'connect_args': {
  49. 'options': '-c timezone=UTC',
  50. },
  51. 'max_overflow': 10,
  52. 'pool_pre_ping': False,
  53. 'pool_recycle': 3600,
  54. 'pool_size': 30,
  55. }