test_dify_config.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. CONSOLE_WEB_URL=https://example.com
  14. """))
  15. return str(file_path)
  16. def test_dify_config_undefined_entry(example_env_file):
  17. # load dotenv file with pydantic-settings
  18. config = DifyConfig(_env_file=example_env_file)
  19. # entries not defined in app settings
  20. with pytest.raises(TypeError):
  21. # TypeError: 'AppSettings' object is not subscriptable
  22. assert config['LOG_LEVEL'] == 'INFO'
  23. def test_dify_config(example_env_file):
  24. # load dotenv file with pydantic-settings
  25. config = DifyConfig(_env_file=example_env_file)
  26. # constant values
  27. assert config.COMMIT_SHA == ''
  28. # default values
  29. assert config.EDITION == 'SELF_HOSTED'
  30. assert config.API_COMPRESSION_ENABLED is False
  31. assert config.SENTRY_TRACES_SAMPLE_RATE == 1.0
  32. def test_flask_configs(example_env_file):
  33. flask_app = Flask('app')
  34. flask_app.config.from_mapping(DifyConfig(_env_file=example_env_file).model_dump())
  35. config = flask_app.config
  36. # configs read from pydantic-settings
  37. assert config['LOG_LEVEL'] == 'INFO'
  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. assert config['TESTING'] == False
  43. # value from env file
  44. assert config['CONSOLE_API_URL'] == 'https://example.com'
  45. # fallback to alias choices value as CONSOLE_API_URL
  46. assert config['FILES_URL'] == 'https://example.com'
  47. assert config['SQLALCHEMY_DATABASE_URI'] == 'postgresql://postgres:@localhost:5432/dify'
  48. assert config['SQLALCHEMY_ENGINE_OPTIONS'] == {
  49. 'connect_args': {
  50. 'options': '-c timezone=UTC',
  51. },
  52. 'max_overflow': 10,
  53. 'pool_pre_ping': False,
  54. 'pool_recycle': 3600,
  55. 'pool_size': 30,
  56. }
  57. assert config['CONSOLE_WEB_URL']=='https://example.com'
  58. assert config['CONSOLE_CORS_ALLOW_ORIGINS']==['https://example.com']
  59. assert config['WEB_API_CORS_ALLOW_ORIGINS'] == ['*']