diff --git a/pytests/test_plugin_runtime.py b/pytests/test_plugin_runtime.py index 004b7292..887628e5 100644 --- a/pytests/test_plugin_runtime.py +++ b/pytests/test_plugin_runtime.py @@ -1832,6 +1832,44 @@ class TestSupervisor: class TestIntegration: """运行时集成层启动/清理测试""" + @pytest.mark.asyncio + async def test_cap_database_get_with_filters_does_not_reference_unbound_key_value(self, monkeypatch): + from src.plugin_runtime import integration as integration_module + import src.common.database.database_model as real_db_models + from src.services import database_service as real_database_service + + captured: dict[str, object] = {} + + class DummyModel: + pass + + async def fake_db_get(model_class, filters=None, limit=None, order_by=None, single_result=False): + captured["model_class"] = model_class + captured["filters"] = filters + captured["limit"] = limit + captured["order_by"] = order_by + captured["single_result"] = single_result + return [{"id": 1}] + + monkeypatch.setattr(real_database_service, "db_get", fake_db_get) + monkeypatch.setattr(real_db_models, "DemoTable", DummyModel, raising=False) + + result = await integration_module.PluginRuntimeManager._cap_database_get( + "plugin_a", + "database.get", + { + "table": "DemoTable", + "filters": {"status": "active"}, + "limit": 5, + }, + ) + + assert result == {"success": True, "result": [{"id": 1}]} + assert captured["model_class"] is DummyModel + assert captured["filters"] == {"status": "active"} + assert captured["limit"] == 5 + assert captured["single_result"] is False + @pytest.mark.asyncio async def test_component_enable_rejects_ambiguous_short_name(self, monkeypatch): from src.plugin_runtime import integration as integration_module diff --git a/src/plugin_runtime/integration.py b/src/plugin_runtime/integration.py index 8e4c2cf4..3eb7cd4a 100644 --- a/src/plugin_runtime/integration.py +++ b/src/plugin_runtime/integration.py @@ -942,9 +942,9 @@ class PluginRuntimeManager: # 兼容 SDK 的 key_field/key_value 参数,自动转换为 filters filters = args.get("filters") + key_value = args.get("key_value") if not filters: key_field = args.get("key_field", "id") - key_value = args.get("key_value") if key_value is not None: filters = {key_field: key_value}