test_dify_settings.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from textwrap import dedent
  2. import pytest
  3. from flask import Flask
  4. from configs.app_configs import DifyConfigs
  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_configs_undefined_entry(example_env_file):
  16. # load dotenv file with pydantic-settings
  17. settings = DifyConfigs(_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 settings['LOG_LEVEL'] == 'INFO'
  22. def test_dify_configs(example_env_file):
  23. # load dotenv file with pydantic-settings
  24. settings = DifyConfigs(_env_file=example_env_file)
  25. # constant values
  26. assert settings.COMMIT_SHA == ''
  27. # default values
  28. assert settings.EDITION == 'SELF_HOSTED'
  29. assert settings.API_COMPRESSION_ENABLED is False
  30. assert settings.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(DifyConfigs(_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'