test_dify_config.py 3.0 KB

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