You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_dify_config.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import os
  2. from flask import Flask
  3. from yarl import URL
  4. from configs.app_config import DifyConfig
  5. def test_dify_config(monkeypatch):
  6. # clear system environment variables
  7. os.environ.clear()
  8. # Set environment variables using monkeypatch
  9. monkeypatch.setenv("CONSOLE_API_URL", "https://example.com")
  10. monkeypatch.setenv("CONSOLE_WEB_URL", "https://example.com")
  11. monkeypatch.setenv("HTTP_REQUEST_MAX_WRITE_TIMEOUT", "30")
  12. monkeypatch.setenv("DB_USERNAME", "postgres")
  13. monkeypatch.setenv("DB_PASSWORD", "postgres")
  14. monkeypatch.setenv("DB_HOST", "localhost")
  15. monkeypatch.setenv("DB_PORT", "5432")
  16. monkeypatch.setenv("DB_DATABASE", "dify")
  17. monkeypatch.setenv("HTTP_REQUEST_MAX_READ_TIMEOUT", "600")
  18. # load dotenv file with pydantic-settings
  19. config = DifyConfig()
  20. # constant values
  21. assert config.COMMIT_SHA == ""
  22. # default values
  23. assert config.EDITION == "SELF_HOSTED"
  24. assert config.API_COMPRESSION_ENABLED is False
  25. assert config.SENTRY_TRACES_SAMPLE_RATE == 1.0
  26. # annotated field with default value
  27. assert config.HTTP_REQUEST_MAX_READ_TIMEOUT == 600
  28. # annotated field with configured value
  29. assert config.HTTP_REQUEST_MAX_WRITE_TIMEOUT == 30
  30. assert config.WORKFLOW_PARALLEL_DEPTH_LIMIT == 3
  31. # NOTE: If there is a `.env` file in your Workspace, this test might not succeed as expected.
  32. # This is due to `pymilvus` loading all the variables from the `.env` file into `os.environ`.
  33. def test_flask_configs(monkeypatch):
  34. flask_app = Flask("app")
  35. # clear system environment variables
  36. os.environ.clear()
  37. # Set environment variables using monkeypatch
  38. monkeypatch.setenv("CONSOLE_API_URL", "https://example.com")
  39. monkeypatch.setenv("CONSOLE_WEB_URL", "https://example.com")
  40. monkeypatch.setenv("HTTP_REQUEST_MAX_WRITE_TIMEOUT", "30")
  41. monkeypatch.setenv("DB_USERNAME", "postgres")
  42. monkeypatch.setenv("DB_PASSWORD", "postgres")
  43. monkeypatch.setenv("DB_HOST", "localhost")
  44. monkeypatch.setenv("DB_PORT", "5432")
  45. monkeypatch.setenv("DB_DATABASE", "dify")
  46. monkeypatch.setenv("WEB_API_CORS_ALLOW_ORIGINS", "http://127.0.0.1:3000,*")
  47. monkeypatch.setenv("CODE_EXECUTION_ENDPOINT", "http://127.0.0.1:8194/")
  48. flask_app.config.from_mapping(DifyConfig().model_dump()) # pyright: ignore
  49. config = flask_app.config
  50. # configs read from pydantic-settings
  51. assert config["LOG_LEVEL"] == "INFO"
  52. assert config["COMMIT_SHA"] == ""
  53. assert config["EDITION"] == "SELF_HOSTED"
  54. assert config["API_COMPRESSION_ENABLED"] is False
  55. assert config["SENTRY_TRACES_SAMPLE_RATE"] == 1.0
  56. # value from env file
  57. assert config["CONSOLE_API_URL"] == "https://example.com"
  58. # fallback to alias choices value as CONSOLE_API_URL
  59. assert config["FILES_URL"] == "https://example.com"
  60. assert config["SQLALCHEMY_DATABASE_URI"] == "postgresql://postgres:postgres@localhost:5432/dify"
  61. assert config["SQLALCHEMY_ENGINE_OPTIONS"] == {
  62. "connect_args": {
  63. "options": "-c timezone=UTC",
  64. },
  65. "max_overflow": 10,
  66. "pool_pre_ping": False,
  67. "pool_recycle": 3600,
  68. "pool_size": 30,
  69. }
  70. assert config["CONSOLE_WEB_URL"] == "https://example.com"
  71. assert config["CONSOLE_CORS_ALLOW_ORIGINS"] == ["https://example.com"]
  72. assert config["WEB_API_CORS_ALLOW_ORIGINS"] == ["http://127.0.0.1:3000", "*"]
  73. assert str(config["CODE_EXECUTION_ENDPOINT"]) == "http://127.0.0.1:8194/"
  74. assert str(URL(str(config["CODE_EXECUTION_ENDPOINT"])) / "v1") == "http://127.0.0.1:8194/v1"
  75. def test_inner_api_config_exist(monkeypatch):
  76. # Set environment variables using monkeypatch
  77. monkeypatch.setenv("CONSOLE_API_URL", "https://example.com")
  78. monkeypatch.setenv("CONSOLE_WEB_URL", "https://example.com")
  79. monkeypatch.setenv("HTTP_REQUEST_MAX_WRITE_TIMEOUT", "30")
  80. monkeypatch.setenv("DB_USERNAME", "postgres")
  81. monkeypatch.setenv("DB_PASSWORD", "postgres")
  82. monkeypatch.setenv("DB_HOST", "localhost")
  83. monkeypatch.setenv("DB_PORT", "5432")
  84. monkeypatch.setenv("DB_DATABASE", "dify")
  85. monkeypatch.setenv("INNER_API_KEY", "test-inner-api-key")
  86. config = DifyConfig()
  87. assert config.INNER_API is False
  88. assert isinstance(config.INNER_API_KEY, str)
  89. assert len(config.INNER_API_KEY) > 0
  90. def test_db_extras_options_merging(monkeypatch):
  91. """Test that DB_EXTRAS options are properly merged with default timezone setting"""
  92. # Set environment variables
  93. monkeypatch.setenv("DB_USERNAME", "postgres")
  94. monkeypatch.setenv("DB_PASSWORD", "postgres")
  95. monkeypatch.setenv("DB_HOST", "localhost")
  96. monkeypatch.setenv("DB_PORT", "5432")
  97. monkeypatch.setenv("DB_DATABASE", "dify")
  98. monkeypatch.setenv("DB_EXTRAS", "options=-c search_path=myschema")
  99. # Create config
  100. config = DifyConfig()
  101. # Get engine options
  102. engine_options = config.SQLALCHEMY_ENGINE_OPTIONS
  103. # Verify options contains both search_path and timezone
  104. options = engine_options["connect_args"]["options"]
  105. assert "search_path=myschema" in options
  106. assert "timezone=UTC" in options