test_dify_config.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. def test_dify_config(example_env_file):
  31. # load dotenv file with pydantic-settings
  32. config = DifyConfig(_env_file=example_env_file)
  33. # constant values
  34. assert config.COMMIT_SHA == ""
  35. # default values
  36. assert config.EDITION == "SELF_HOSTED"
  37. assert config.API_COMPRESSION_ENABLED is False
  38. assert config.SENTRY_TRACES_SAMPLE_RATE == 1.0
  39. # annotated field with default value
  40. assert config.HTTP_REQUEST_MAX_READ_TIMEOUT == 60
  41. # annotated field with configured value
  42. assert config.HTTP_REQUEST_MAX_WRITE_TIMEOUT == 30
  43. # NOTE: If there is a `.env` file in your Workspace, this test might not succeed as expected.
  44. # This is due to `pymilvus` loading all the variables from the `.env` file into `os.environ`.
  45. def test_flask_configs(example_env_file):
  46. flask_app = Flask("app")
  47. # clear system environment variables
  48. os.environ.clear()
  49. flask_app.config.from_mapping(DifyConfig(_env_file=example_env_file).model_dump()) # pyright: ignore
  50. config = flask_app.config
  51. # configs read from pydantic-settings
  52. assert config["LOG_LEVEL"] == "INFO"
  53. assert config["COMMIT_SHA"] == ""
  54. assert config["EDITION"] == "SELF_HOSTED"
  55. assert config["API_COMPRESSION_ENABLED"] is False
  56. assert config["SENTRY_TRACES_SAMPLE_RATE"] == 1.0
  57. assert config["TESTING"] == False
  58. # value from env file
  59. assert config["CONSOLE_API_URL"] == "https://example.com"
  60. # fallback to alias choices value as CONSOLE_API_URL
  61. assert config["FILES_URL"] == "https://example.com"
  62. assert config["SQLALCHEMY_DATABASE_URI"] == "postgresql://postgres:@localhost:5432/dify"
  63. assert config["SQLALCHEMY_ENGINE_OPTIONS"] == {
  64. "connect_args": {
  65. "options": "-c timezone=UTC",
  66. },
  67. "max_overflow": 10,
  68. "pool_pre_ping": False,
  69. "pool_recycle": 3600,
  70. "pool_size": 30,
  71. }
  72. assert config["CONSOLE_WEB_URL"] == "https://example.com"
  73. assert config["CONSOLE_CORS_ALLOW_ORIGINS"] == ["https://example.com"]
  74. assert config["WEB_API_CORS_ALLOW_ORIGINS"] == ["*"]
  75. assert str(config["CODE_EXECUTION_ENDPOINT"]) == "http://sandbox:8194/"
  76. assert str(URL(str(config["CODE_EXECUTION_ENDPOINT"])) / "v1") == "http://sandbox:8194/v1"