test_dify_config.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import os
  2. from textwrap import dedent
  3. import pytest
  4. from flask import Flask
  5. from configs.app_config import DifyConfig
  6. EXAMPLE_ENV_FILENAME = '.env'
  7. @pytest.fixture
  8. def example_env_file(tmp_path, monkeypatch) -> str:
  9. monkeypatch.chdir(tmp_path)
  10. file_path = tmp_path.joinpath(EXAMPLE_ENV_FILENAME)
  11. file_path.write_text(dedent(
  12. """
  13. CONSOLE_API_URL=https://example.com
  14. CONSOLE_WEB_URL=https://example.com
  15. """))
  16. return str(file_path)
  17. def test_dify_config_undefined_entry(example_env_file):
  18. # NOTE: See https://github.com/microsoft/pylance-release/issues/6099 for more details about this type error.
  19. # load dotenv file with pydantic-settings
  20. config = DifyConfig(_env_file=example_env_file)
  21. # entries not defined in app settings
  22. with pytest.raises(TypeError):
  23. # TypeError: 'AppSettings' object is not subscriptable
  24. assert config['LOG_LEVEL'] == 'INFO'
  25. def test_dify_config(example_env_file):
  26. # load dotenv file with pydantic-settings
  27. config = DifyConfig(_env_file=example_env_file)
  28. # constant values
  29. assert config.COMMIT_SHA == ''
  30. # default values
  31. assert config.EDITION == 'SELF_HOSTED'
  32. assert config.API_COMPRESSION_ENABLED is False
  33. assert config.SENTRY_TRACES_SAMPLE_RATE == 1.0
  34. # NOTE: If there is a `.env` file in your Workspace, this test might not succeed as expected.
  35. # This is due to `pymilvus` loading all the variables from the `.env` file into `os.environ`.
  36. def test_flask_configs(example_env_file):
  37. flask_app = Flask('app')
  38. # clear system environment variables
  39. os.environ.clear()
  40. flask_app.config.from_mapping(DifyConfig(_env_file=example_env_file).model_dump()) # pyright: ignore
  41. config = flask_app.config
  42. # configs read from pydantic-settings
  43. assert config['LOG_LEVEL'] == 'INFO'
  44. assert config['COMMIT_SHA'] == ''
  45. assert config['EDITION'] == 'SELF_HOSTED'
  46. assert config['API_COMPRESSION_ENABLED'] is False
  47. assert config['SENTRY_TRACES_SAMPLE_RATE'] == 1.0
  48. assert config['TESTING'] == False
  49. # value from env file
  50. assert config['CONSOLE_API_URL'] == 'https://example.com'
  51. # fallback to alias choices value as CONSOLE_API_URL
  52. assert config['FILES_URL'] == 'https://example.com'
  53. assert config['SQLALCHEMY_DATABASE_URI'] == 'postgresql://postgres:@localhost:5432/dify'
  54. assert config['SQLALCHEMY_ENGINE_OPTIONS'] == {
  55. 'connect_args': {
  56. 'options': '-c timezone=UTC',
  57. },
  58. 'max_overflow': 10,
  59. 'pool_pre_ping': False,
  60. 'pool_recycle': 3600,
  61. 'pool_size': 30,
  62. }
  63. assert config['CONSOLE_WEB_URL']=='https://example.com'
  64. assert config['CONSOLE_CORS_ALLOW_ORIGINS']==['https://example.com']
  65. assert config['WEB_API_CORS_ALLOW_ORIGINS'] == ['*']