From 7995ff1410a1b02afbd1080a70042a891a43bde2 Mon Sep 17 00:00:00 2001 From: Asuka Minato Date: Thu, 28 Aug 2025 10:36:39 +0900 Subject: [PATCH] Typing test (#24651) --- .../model_runtime/__mock/plugin_daemon.py | 7 ++----- .../integration_tests/plugin/__mock/http.py | 3 +-- api/tests/integration_tests/tools/__mock/http.py | 3 +-- api/tests/unit_tests/configs/test_dify_config.py | 16 +++++++++++----- .../extensions/test_ext_request_logging.py | 8 ++++---- api/tests/unit_tests/libs/test_datetime_utils.py | 4 +++- api/tests/unit_tests/libs/test_uuid_utils.py | 2 +- 7 files changed, 23 insertions(+), 20 deletions(-) diff --git a/api/tests/integration_tests/model_runtime/__mock/plugin_daemon.py b/api/tests/integration_tests/model_runtime/__mock/plugin_daemon.py index e3c592b583..c8cb7528e1 100644 --- a/api/tests/integration_tests/model_runtime/__mock/plugin_daemon.py +++ b/api/tests/integration_tests/model_runtime/__mock/plugin_daemon.py @@ -3,15 +3,12 @@ from collections.abc import Callable import pytest -# import monkeypatch -from _pytest.monkeypatch import MonkeyPatch - from core.plugin.impl.model import PluginModelClient from tests.integration_tests.model_runtime.__mock.plugin_model import MockModelClass def mock_plugin_daemon( - monkeypatch: MonkeyPatch, + monkeypatch: pytest.MonkeyPatch, ) -> Callable[[], None]: """ mock openai module @@ -34,7 +31,7 @@ MOCK = os.getenv("MOCK_SWITCH", "false").lower() == "true" @pytest.fixture -def setup_model_mock(monkeypatch): +def setup_model_mock(monkeypatch: pytest.MonkeyPatch): if MOCK: unpatch = mock_plugin_daemon(monkeypatch) diff --git a/api/tests/integration_tests/plugin/__mock/http.py b/api/tests/integration_tests/plugin/__mock/http.py index 25177274c6..8f8988899b 100644 --- a/api/tests/integration_tests/plugin/__mock/http.py +++ b/api/tests/integration_tests/plugin/__mock/http.py @@ -3,7 +3,6 @@ from typing import Literal import pytest import requests -from _pytest.monkeypatch import MonkeyPatch from core.plugin.entities.plugin_daemon import PluginDaemonBasicResponse from core.tools.entities.common_entities import I18nObject @@ -53,7 +52,7 @@ MOCK_SWITCH = os.getenv("MOCK_SWITCH", "false").lower() == "true" @pytest.fixture -def setup_http_mock(request, monkeypatch: MonkeyPatch): +def setup_http_mock(request, monkeypatch: pytest.MonkeyPatch): if MOCK_SWITCH: monkeypatch.setattr(requests, "request", MockedHttp.requests_request) diff --git a/api/tests/integration_tests/tools/__mock/http.py b/api/tests/integration_tests/tools/__mock/http.py index de9711ab38..fb2e3abcee 100644 --- a/api/tests/integration_tests/tools/__mock/http.py +++ b/api/tests/integration_tests/tools/__mock/http.py @@ -3,7 +3,6 @@ from typing import Literal import httpx import pytest -from _pytest.monkeypatch import MonkeyPatch from core.helper import ssrf_proxy @@ -30,7 +29,7 @@ class MockedHttp: @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) yield monkeypatch.undo() diff --git a/api/tests/unit_tests/configs/test_dify_config.py b/api/tests/unit_tests/configs/test_dify_config.py index b95ed431b4..0c7473019a 100644 --- a/api/tests/unit_tests/configs/test_dify_config.py +++ b/api/tests/unit_tests/configs/test_dify_config.py @@ -8,7 +8,7 @@ from yarl import URL from configs.app_config import DifyConfig -def test_dify_config(monkeypatch): +def test_dify_config(monkeypatch: pytest.MonkeyPatch): # clear system environment variables os.environ.clear() @@ -48,7 +48,7 @@ def test_dify_config(monkeypatch): # 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`. -def test_flask_configs(monkeypatch): +def test_flask_configs(monkeypatch: pytest.MonkeyPatch): flask_app = Flask("app") # clear system environment variables os.environ.clear() @@ -101,7 +101,7 @@ def test_flask_configs(monkeypatch): 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 monkeypatch.setenv("CONSOLE_API_URL", "https://example.com") monkeypatch.setenv("CONSOLE_WEB_URL", "https://example.com") @@ -119,7 +119,7 @@ def test_inner_api_config_exist(monkeypatch): 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""" # Set environment variables monkeypatch.setenv("DB_USERNAME", "postgres") @@ -164,7 +164,13 @@ def test_db_extras_options_merging(monkeypatch): ], ) 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.""" from kombu.utils.url import parse_url diff --git a/api/tests/unit_tests/extensions/test_ext_request_logging.py b/api/tests/unit_tests/extensions/test_ext_request_logging.py index 5508f8e7e6..cf6e172e4d 100644 --- a/api/tests/unit_tests/extensions/test_ext_request_logging.py +++ b/api/tests/unit_tests/extensions/test_ext_request_logging.py @@ -43,28 +43,28 @@ def _get_test_app(): @pytest.fixture -def mock_request_receiver(monkeypatch) -> mock.Mock: +def mock_request_receiver(monkeypatch: pytest.MonkeyPatch) -> mock.Mock: mock_log_request_started = mock.Mock() monkeypatch.setattr(ext_request_logging, "_log_request_started", mock_log_request_started) return mock_log_request_started @pytest.fixture -def mock_response_receiver(monkeypatch) -> mock.Mock: +def mock_response_receiver(monkeypatch: pytest.MonkeyPatch) -> mock.Mock: mock_log_request_finished = mock.Mock() monkeypatch.setattr(ext_request_logging, "_log_request_finished", mock_log_request_finished) return mock_log_request_finished @pytest.fixture -def mock_logger(monkeypatch) -> logging.Logger: +def mock_logger(monkeypatch: pytest.MonkeyPatch) -> logging.Logger: _logger = mock.MagicMock(spec=logging.Logger) monkeypatch.setattr(ext_request_logging, "logger", _logger) return _logger @pytest.fixture -def enable_request_logging(monkeypatch): +def enable_request_logging(monkeypatch: pytest.MonkeyPatch): monkeypatch.setattr(dify_config, "ENABLE_REQUEST_LOGGING", True) diff --git a/api/tests/unit_tests/libs/test_datetime_utils.py b/api/tests/unit_tests/libs/test_datetime_utils.py index e7781a5821..e914ca4816 100644 --- a/api/tests/unit_tests/libs/test_datetime_utils.py +++ b/api/tests/unit_tests/libs/test_datetime_utils.py @@ -1,9 +1,11 @@ import datetime +import pytest + 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) def _now_func(tz: datetime.timezone | None) -> datetime.datetime: diff --git a/api/tests/unit_tests/libs/test_uuid_utils.py b/api/tests/unit_tests/libs/test_uuid_utils.py index 7dbda95f45..9e040efb62 100644 --- a/api/tests/unit_tests/libs/test_uuid_utils.py +++ b/api/tests/unit_tests/libs/test_uuid_utils.py @@ -143,7 +143,7 @@ def test_uuidv7_with_custom_timestamp(): 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.""" mock_time = 1609459200 mock_time_func = mock.Mock(return_value=mock_time)