test_dify_config.py 3.5 KB

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