test_dify_config.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. # NOTE: See https://github.com/microsoft/pylance-release/issues/6099 for more details about this type error.
  18. # load dotenv file with pydantic-settings
  19. config = DifyConfig(_env_file=example_env_file)
  20. # entries not defined in app settings
  21. with pytest.raises(TypeError):
  22. # TypeError: 'AppSettings' object is not subscriptable
  23. assert config['LOG_LEVEL'] == 'INFO'
  24. def test_dify_config(example_env_file):
  25. # load dotenv file with pydantic-settings
  26. config = DifyConfig(_env_file=example_env_file)
  27. # constant values
  28. assert config.COMMIT_SHA == ''
  29. # default values
  30. assert config.EDITION == 'SELF_HOSTED'
  31. assert config.API_COMPRESSION_ENABLED is False
  32. assert config.SENTRY_TRACES_SAMPLE_RATE == 1.0
  33. # NOTE: If there is a `.env` file in your Workspace, this test might not succeed as expected.
  34. # This is due to `pymilvus` loading all the variables from the `.env` file into `os.environ`.
  35. def test_flask_configs(example_env_file):
  36. flask_app = Flask('app')
  37. flask_app.config.from_mapping(DifyConfig(_env_file=example_env_file).model_dump())
  38. config = flask_app.config
  39. # configs read from pydantic-settings
  40. assert config['LOG_LEVEL'] == 'INFO'
  41. assert config['COMMIT_SHA'] == ''
  42. assert config['EDITION'] == 'SELF_HOSTED'
  43. assert config['API_COMPRESSION_ENABLED'] is False
  44. assert config['SENTRY_TRACES_SAMPLE_RATE'] == 1.0
  45. assert config['TESTING'] == False
  46. # value from env file
  47. assert config['CONSOLE_API_URL'] == 'https://example.com'
  48. # fallback to alias choices value as CONSOLE_API_URL
  49. assert config['FILES_URL'] == 'https://example.com'
  50. assert config['SQLALCHEMY_DATABASE_URI'] == 'postgresql://postgres:@localhost:5432/dify'
  51. assert config['SQLALCHEMY_ENGINE_OPTIONS'] == {
  52. 'connect_args': {
  53. 'options': '-c timezone=UTC',
  54. },
  55. 'max_overflow': 10,
  56. 'pool_pre_ping': False,
  57. 'pool_recycle': 3600,
  58. 'pool_size': 30,
  59. }
  60. assert config['CONSOLE_WEB_URL']=='https://example.com'
  61. assert config['CONSOLE_CORS_ALLOW_ORIGINS']==['https://example.com']
  62. assert config['WEB_API_CORS_ALLOW_ORIGINS'] == ['*']