| import pytest | import pytest | ||||
| # import monkeypatch | |||||
| from _pytest.monkeypatch import MonkeyPatch | |||||
| from core.plugin.impl.model import PluginModelClient | from core.plugin.impl.model import PluginModelClient | ||||
| from tests.integration_tests.model_runtime.__mock.plugin_model import MockModelClass | from tests.integration_tests.model_runtime.__mock.plugin_model import MockModelClass | ||||
| def mock_plugin_daemon( | def mock_plugin_daemon( | ||||
| monkeypatch: MonkeyPatch, | |||||
| monkeypatch: pytest.MonkeyPatch, | |||||
| ) -> Callable[[], None]: | ) -> Callable[[], None]: | ||||
| """ | """ | ||||
| mock openai module | mock openai module | ||||
| @pytest.fixture | @pytest.fixture | ||||
| def setup_model_mock(monkeypatch): | |||||
| def setup_model_mock(monkeypatch: pytest.MonkeyPatch): | |||||
| if MOCK: | if MOCK: | ||||
| unpatch = mock_plugin_daemon(monkeypatch) | unpatch = mock_plugin_daemon(monkeypatch) | ||||
| import pytest | import pytest | ||||
| import requests | import requests | ||||
| from _pytest.monkeypatch import MonkeyPatch | |||||
| from core.plugin.entities.plugin_daemon import PluginDaemonBasicResponse | from core.plugin.entities.plugin_daemon import PluginDaemonBasicResponse | ||||
| from core.tools.entities.common_entities import I18nObject | from core.tools.entities.common_entities import I18nObject | ||||
| @pytest.fixture | @pytest.fixture | ||||
| def setup_http_mock(request, monkeypatch: MonkeyPatch): | |||||
| def setup_http_mock(request, monkeypatch: pytest.MonkeyPatch): | |||||
| if MOCK_SWITCH: | if MOCK_SWITCH: | ||||
| monkeypatch.setattr(requests, "request", MockedHttp.requests_request) | monkeypatch.setattr(requests, "request", MockedHttp.requests_request) | ||||
| import httpx | import httpx | ||||
| import pytest | import pytest | ||||
| from _pytest.monkeypatch import MonkeyPatch | |||||
| from core.helper import ssrf_proxy | from core.helper import ssrf_proxy | ||||
| @pytest.fixture | @pytest.fixture | ||||
| def setup_http_mock(request, monkeypatch: MonkeyPatch): | |||||
| def setup_http_mock(request, monkeypatch: pytest.MonkeyPatch): | |||||
| monkeypatch.setattr(ssrf_proxy, "make_request", MockedHttp.httpx_request) | monkeypatch.setattr(ssrf_proxy, "make_request", MockedHttp.httpx_request) | ||||
| yield | yield | ||||
| monkeypatch.undo() | monkeypatch.undo() |
| from configs.app_config import DifyConfig | from configs.app_config import DifyConfig | ||||
| def test_dify_config(monkeypatch): | |||||
| def test_dify_config(monkeypatch: pytest.MonkeyPatch): | |||||
| # clear system environment variables | # clear system environment variables | ||||
| os.environ.clear() | os.environ.clear() | ||||
| # NOTE: If there is a `.env` file in your Workspace, this test might not succeed as expected. | # NOTE: If there is a `.env` file in your Workspace, this test might not succeed as expected. | ||||
| # This is due to `pymilvus` loading all the variables from the `.env` file into `os.environ`. | # This is due to `pymilvus` loading all the variables from the `.env` file into `os.environ`. | ||||
| def test_flask_configs(monkeypatch): | |||||
| def test_flask_configs(monkeypatch: pytest.MonkeyPatch): | |||||
| flask_app = Flask("app") | flask_app = Flask("app") | ||||
| # clear system environment variables | # clear system environment variables | ||||
| os.environ.clear() | os.environ.clear() | ||||
| assert str(URL(str(config["CODE_EXECUTION_ENDPOINT"])) / "v1") == "http://127.0.0.1:8194/v1" | assert str(URL(str(config["CODE_EXECUTION_ENDPOINT"])) / "v1") == "http://127.0.0.1:8194/v1" | ||||
| def test_inner_api_config_exist(monkeypatch): | |||||
| def test_inner_api_config_exist(monkeypatch: pytest.MonkeyPatch): | |||||
| # Set environment variables using monkeypatch | # Set environment variables using monkeypatch | ||||
| monkeypatch.setenv("CONSOLE_API_URL", "https://example.com") | monkeypatch.setenv("CONSOLE_API_URL", "https://example.com") | ||||
| monkeypatch.setenv("CONSOLE_WEB_URL", "https://example.com") | monkeypatch.setenv("CONSOLE_WEB_URL", "https://example.com") | ||||
| assert len(config.INNER_API_KEY) > 0 | assert len(config.INNER_API_KEY) > 0 | ||||
| def test_db_extras_options_merging(monkeypatch): | |||||
| def test_db_extras_options_merging(monkeypatch: pytest.MonkeyPatch): | |||||
| """Test that DB_EXTRAS options are properly merged with default timezone setting""" | """Test that DB_EXTRAS options are properly merged with default timezone setting""" | ||||
| # Set environment variables | # Set environment variables | ||||
| monkeypatch.setenv("DB_USERNAME", "postgres") | monkeypatch.setenv("DB_USERNAME", "postgres") | ||||
| ], | ], | ||||
| ) | ) | ||||
| def test_celery_broker_url_with_special_chars_password( | def test_celery_broker_url_with_special_chars_password( | ||||
| monkeypatch, broker_url, expected_host, expected_port, expected_username, expected_password, expected_db | |||||
| monkeypatch: pytest.MonkeyPatch, | |||||
| broker_url, | |||||
| expected_host, | |||||
| expected_port, | |||||
| expected_username, | |||||
| expected_password, | |||||
| expected_db, | |||||
| ): | ): | ||||
| """Test that CELERY_BROKER_URL with various formats are handled correctly.""" | """Test that CELERY_BROKER_URL with various formats are handled correctly.""" | ||||
| from kombu.utils.url import parse_url | from kombu.utils.url import parse_url |
| @pytest.fixture | @pytest.fixture | ||||
| def mock_request_receiver(monkeypatch) -> mock.Mock: | |||||
| def mock_request_receiver(monkeypatch: pytest.MonkeyPatch) -> mock.Mock: | |||||
| mock_log_request_started = mock.Mock() | mock_log_request_started = mock.Mock() | ||||
| monkeypatch.setattr(ext_request_logging, "_log_request_started", mock_log_request_started) | monkeypatch.setattr(ext_request_logging, "_log_request_started", mock_log_request_started) | ||||
| return mock_log_request_started | return mock_log_request_started | ||||
| @pytest.fixture | @pytest.fixture | ||||
| def mock_response_receiver(monkeypatch) -> mock.Mock: | |||||
| def mock_response_receiver(monkeypatch: pytest.MonkeyPatch) -> mock.Mock: | |||||
| mock_log_request_finished = mock.Mock() | mock_log_request_finished = mock.Mock() | ||||
| monkeypatch.setattr(ext_request_logging, "_log_request_finished", mock_log_request_finished) | monkeypatch.setattr(ext_request_logging, "_log_request_finished", mock_log_request_finished) | ||||
| return mock_log_request_finished | return mock_log_request_finished | ||||
| @pytest.fixture | @pytest.fixture | ||||
| def mock_logger(monkeypatch) -> logging.Logger: | |||||
| def mock_logger(monkeypatch: pytest.MonkeyPatch) -> logging.Logger: | |||||
| _logger = mock.MagicMock(spec=logging.Logger) | _logger = mock.MagicMock(spec=logging.Logger) | ||||
| monkeypatch.setattr(ext_request_logging, "logger", _logger) | monkeypatch.setattr(ext_request_logging, "logger", _logger) | ||||
| return _logger | return _logger | ||||
| @pytest.fixture | @pytest.fixture | ||||
| def enable_request_logging(monkeypatch): | |||||
| def enable_request_logging(monkeypatch: pytest.MonkeyPatch): | |||||
| monkeypatch.setattr(dify_config, "ENABLE_REQUEST_LOGGING", True) | monkeypatch.setattr(dify_config, "ENABLE_REQUEST_LOGGING", True) | ||||
| import datetime | import datetime | ||||
| import pytest | |||||
| from libs.datetime_utils import naive_utc_now | from libs.datetime_utils import naive_utc_now | ||||
| def test_naive_utc_now(monkeypatch): | |||||
| def test_naive_utc_now(monkeypatch: pytest.MonkeyPatch): | |||||
| tz_aware_utc_now = datetime.datetime.now(tz=datetime.UTC) | tz_aware_utc_now = datetime.datetime.now(tz=datetime.UTC) | ||||
| def _now_func(tz: datetime.timezone | None) -> datetime.datetime: | def _now_func(tz: datetime.timezone | None) -> datetime.datetime: |
| assert extracted_timestamp == custom_timestamp # Exact match for integer milliseconds | assert extracted_timestamp == custom_timestamp # Exact match for integer milliseconds | ||||
| def test_uuidv7_with_none_timestamp(monkeypatch): | |||||
| def test_uuidv7_with_none_timestamp(monkeypatch: pytest.MonkeyPatch): | |||||
| """Test UUID generation with None timestamp uses current time.""" | """Test UUID generation with None timestamp uses current time.""" | ||||
| mock_time = 1609459200 | mock_time = 1609459200 | ||||
| mock_time_func = mock.Mock(return_value=mock_time) | mock_time_func = mock.Mock(return_value=mock_time) |