-
Notifications
You must be signed in to change notification settings - Fork 3
AP-25704: inactivate empty Agent Chat Widget outputs #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AtR1an
wants to merge
2
commits into
master
Choose a base branch
from
enh/AP-25704-inactive-ports
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import unittest | ||
| from unittest.mock import patch | ||
| import sys | ||
| import types | ||
|
|
||
| import knime.extension as knext | ||
| import knime.extension.nodes as kn | ||
|
|
||
|
|
||
| class _MockBackend: | ||
| def register_port_type(self, name, object_class, spec_class, id=None): | ||
| if id is None: | ||
| id = f"test.{object_class.__module__}.{object_class.__qualname__}" | ||
| return kn.PortType(id, name, object_class, spec_class) | ||
|
|
||
|
|
||
| knime_types_module = types.ModuleType("knime.types") | ||
| knime_types_tool_module = types.ModuleType("knime.types.tool") | ||
| knime_types_tool_module.WorkflowTool = type("WorkflowTool", (), {}) | ||
| knime_types_message_module = types.ModuleType("knime.types.message") | ||
| knime_types_message_module.MessageValue = type("MessageValue", (), {}) | ||
|
|
||
|
|
||
| def setUpModule(): | ||
| """Set up patched KNIME backend and types for this test module.""" | ||
| global _backend_patcher | ||
| global _logical_patcher | ||
| global _sysmodules_patcher | ||
| global _inactiveport_present | ||
| global _inactiveport_original | ||
| global AgentChatWidget | ||
|
|
||
| _backend_patcher = patch.object(kn, "_backend", _MockBackend()) | ||
| _backend_patcher.start() | ||
|
|
||
| _logical_patcher = patch.object( | ||
| knext, "logical", lambda value_type: knext.string() | ||
| ) | ||
| _logical_patcher.start() | ||
|
|
||
| _inactiveport_present = hasattr(knext, "InactivePort") | ||
| _inactiveport_original = getattr(knext, "InactivePort", None) | ||
| knext.InactivePort = object() | ||
|
|
||
| _sysmodules_patcher = patch.dict( | ||
| sys.modules, | ||
| { | ||
| "knime.types": knime_types_module, | ||
| "knime.types.tool": knime_types_tool_module, | ||
| "knime.types.message": knime_types_message_module, | ||
| }, | ||
| clear=False, | ||
| ) | ||
| _sysmodules_patcher.start() | ||
|
|
||
| from agents.base import AgentChatWidget as _AgentChatWidget | ||
|
|
||
| AgentChatWidget = _AgentChatWidget | ||
|
|
||
|
|
||
| def tearDownModule(): | ||
| """Tear down patched KNIME backend and types for this test module.""" | ||
| _backend_patcher.stop() | ||
| _logical_patcher.stop() | ||
|
|
||
| if _inactiveport_present: | ||
| knext.InactivePort = _inactiveport_original | ||
| else: | ||
| delattr(knext, "InactivePort") | ||
|
|
||
| _sysmodules_patcher.stop() | ||
| class _MockContext: | ||
| def __init__(self, view_data, num_data_outputs, combined_tools_workflow): | ||
| self._view_data_value = view_data | ||
| self._num_data_outputs = num_data_outputs | ||
| self._combined_tools_workflow_value = combined_tools_workflow | ||
|
|
||
| def _get_view_data(self): | ||
| return self._view_data_value | ||
|
|
||
| def get_connected_output_port_numbers(self): | ||
| return [1, 1, self._num_data_outputs] | ||
|
|
||
| def _get_combined_tools_workflow(self): | ||
| return self._combined_tools_workflow_value | ||
|
|
||
|
|
||
| class AgentChatWidgetOutputTest(unittest.TestCase): | ||
| def test_execute_returns_inactive_ports_when_no_view_data_exists(self): | ||
| node = AgentChatWidget() | ||
| ctx = _MockContext( | ||
| view_data=None, | ||
| num_data_outputs=2, | ||
| combined_tools_workflow="workflow-port", | ||
| ) | ||
|
|
||
| combined_tools_workflow, conversation_output, data_outputs = node.execute( | ||
| ctx, None, None, [] | ||
| ) | ||
|
|
||
| self.assertEqual("workflow-port", combined_tools_workflow) | ||
| self.assertIs(knext.InactivePort, conversation_output) | ||
| self.assertEqual([knext.InactivePort, knext.InactivePort], data_outputs) | ||
|
|
||
| def test_execute_marks_missing_data_outputs_inactive(self): | ||
| node = AgentChatWidget() | ||
| conversation_table = object() | ||
| data_table = object() | ||
| ctx = _MockContext( | ||
| view_data={ | ||
| "ports": [conversation_table], | ||
| "data": {"data_registry": {"ids": [], "ports": []}}, | ||
| "ports_for_ids": [], | ||
| }, | ||
| num_data_outputs=2, | ||
| combined_tools_workflow="workflow-port", | ||
| ) | ||
|
|
||
| class _MockDataRegistry: | ||
| def get_last_tables(self, num_tables, fill_missing=True): | ||
| self.args = (num_tables, fill_missing) | ||
| return [data_table] | ||
|
|
||
| registry = _MockDataRegistry() | ||
| with patch("agents._data_service.DataRegistry.load", return_value=registry): | ||
| combined_tools_workflow, conversation_output, data_outputs = node.execute( | ||
| ctx, None, None, [] | ||
| ) | ||
|
|
||
| self.assertEqual("workflow-port", combined_tools_workflow) | ||
| self.assertIs(conversation_table, conversation_output) | ||
| self.assertEqual((2, False), registry.args) | ||
| self.assertEqual(2, len(data_outputs)) | ||
| self.assertIs(data_table, data_outputs[0]) | ||
| self.assertIs(knext.InactivePort, data_outputs[1]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_last_tablesnow has afill_missingflag that changes the length/behavior of the returned list (it may return fewer thannum_tableswhenfill_missing=False), but the docstring still implies a fixed-size return. Please update the docstring (and/or parameter description) to document thefill_missingbehavior so callers know what to expect.