Skip to content

n8n Integration: Fix Test Mocking Issues #20

@MervinPraison

Description

@MervinPraison

Summary

The n8n integration in praisonai-tools is fully implemented and functional. This issue tracks minor test mocking issues that cause 2 test failures.

Implementation Status: ✅ Complete

What's Working

  • N8nWorkflowTool class — executes n8n workflows via Public API
  • n8n_workflow() function — decorated tool for agents
  • n8n_list_workflows() function — lists available workflows
  • ✅ Environment variable support (N8N_URL, N8N_API_KEY)
  • ✅ Lazy httpx import — no import-time overhead
  • ✅ Timeout and error handling
  • ✅ Execution polling with wait_for_completion

Test Results

11 passed, 2 failed, 2 skipped (integration tests)

Test Failures

1. test_n8n_workflow_missing_httpx

File: tests/test_n8n_integration.py:72

The mock patch path is incorrect:

# Current (fails):
with patch('praisonai_tools.n8n.n8n_workflow.httpx', None):

# Fix — use sys.modules approach:
with patch.dict('sys.modules', {'httpx': None}):
    # Force reimport to trigger ImportError

2. test_n8n_workflow_http_error / test_n8n_workflow_timeout_error

File: tests/test_n8n_integration.py:111, 131

The mock fixture mock_httpx doesn't properly mock the lazy import inside the run() method. The httpx module is imported inside the method, so the mock needs to intercept the actual import.

Fix:

@pytest.fixture
def mock_httpx(self):
    """Mock httpx module for testing."""
    mock_module = MagicMock()
    mock_module.TimeoutException = type('TimeoutException', (Exception,), {})
    mock_module.HTTPStatusError = type('HTTPStatusError', (Exception,), {
        '__init__': lambda self, msg, request, response: setattr(self, 'response', response)
    })
    
    with patch.dict('sys.modules', {'httpx': mock_module}):
        yield mock_module

Files Involved

File Lines Purpose
praisonai_tools/n8n/__init__.py 20 Exports
praisonai_tools/n8n/n8n_workflow.py 252 Tool implementation
tests/test_n8n_integration.py 314 Unit tests
docs/n8n_integration.md Documentation
examples/n8n_integration_example.py Example usage

Acceptance Criteria

  • All 14 tests pass (currently 11/14)
  • Mock fixture properly intercepts lazy httpx import
  • Integration tests work with real n8n instance

Labels

bug, tests, good first issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    claudeAuto-trigger Claude analysis

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions