test_dify_config.py 2.8 KB

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