|
| 1 | +""" |
| 2 | +Unit tests for multi-vendor routing logic. |
| 3 | +
|
| 4 | +These tests use mocked vendor implementations and can run without API keys, |
| 5 | +making them suitable for CI/CD environments. |
| 6 | +
|
| 7 | +Run with: pytest tests/test_multi_vendor_routing.py -v |
| 8 | +""" |
| 9 | + |
| 10 | +import pytest |
| 11 | +from unittest.mock import patch, MagicMock |
| 12 | +import sys |
| 13 | +import os |
| 14 | + |
| 15 | +# Add parent directory to path |
| 16 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def mock_vendors(): |
| 21 | + """Create mock vendor functions with __name__ attribute.""" |
| 22 | + mock_a = MagicMock(return_value="Result from Vendor A") |
| 23 | + mock_a.__name__ = "mock_vendor_a" |
| 24 | + |
| 25 | + mock_b = MagicMock(return_value="Result from Vendor B") |
| 26 | + mock_b.__name__ = "mock_vendor_b" |
| 27 | + |
| 28 | + mock_c = MagicMock(return_value="Result from Vendor C") |
| 29 | + mock_c.__name__ = "mock_vendor_c" |
| 30 | + |
| 31 | + return {'a': mock_a, 'b': mock_b, 'c': mock_c} |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def mock_routing(mock_vendors): |
| 36 | + """Set up mocked routing environment.""" |
| 37 | + with patch('tradingagents.dataflows.interface.VENDOR_METHODS', { |
| 38 | + 'test_method': { |
| 39 | + 'vendor_a': mock_vendors['a'], |
| 40 | + 'vendor_b': mock_vendors['b'], |
| 41 | + 'vendor_c': mock_vendors['c'], |
| 42 | + } |
| 43 | + }), \ |
| 44 | + patch('tradingagents.dataflows.interface.get_category_for_method', return_value='test_category'), \ |
| 45 | + patch('tradingagents.dataflows.interface.get_config') as mock_config: |
| 46 | + yield mock_config, mock_vendors |
| 47 | + |
| 48 | + |
| 49 | +def test_single_vendor_stops_after_success(mock_routing): |
| 50 | + """Test that single vendor config stops after first successful vendor.""" |
| 51 | + mock_config, mock_vendors = mock_routing |
| 52 | + |
| 53 | + # Configure single vendor |
| 54 | + mock_config.return_value = { |
| 55 | + 'data_vendors': {'test_category': 'vendor_a'}, |
| 56 | + 'tool_vendors': {} |
| 57 | + } |
| 58 | + |
| 59 | + from tradingagents.dataflows.interface import route_to_vendor |
| 60 | + |
| 61 | + result = route_to_vendor('test_method', 'arg1', 'arg2') |
| 62 | + |
| 63 | + # Assertions |
| 64 | + mock_vendors['a'].assert_called_once_with('arg1', 'arg2') |
| 65 | + mock_vendors['b'].assert_not_called() # Should not try fallback |
| 66 | + mock_vendors['c'].assert_not_called() # Should not try fallback |
| 67 | + assert result == 'Result from Vendor A' |
| 68 | + |
| 69 | + |
| 70 | +def test_multi_vendor_stops_after_all_primaries_success(mock_routing): |
| 71 | + """Test that multi-vendor stops after all primaries when they succeed.""" |
| 72 | + mock_config, mock_vendors = mock_routing |
| 73 | + |
| 74 | + # Configure two primary vendors |
| 75 | + mock_config.return_value = { |
| 76 | + 'data_vendors': {'test_category': 'vendor_a,vendor_b'}, |
| 77 | + 'tool_vendors': {} |
| 78 | + } |
| 79 | + |
| 80 | + from tradingagents.dataflows.interface import route_to_vendor |
| 81 | + |
| 82 | + result = route_to_vendor('test_method', 'arg1') |
| 83 | + |
| 84 | + # Assertions |
| 85 | + mock_vendors['a'].assert_called_once_with('arg1') |
| 86 | + mock_vendors['b'].assert_called_once_with('arg1') |
| 87 | + mock_vendors['c'].assert_not_called() # Should NOT try fallback |
| 88 | + |
| 89 | + # Result should contain both |
| 90 | + assert 'Result from Vendor A' in result |
| 91 | + assert 'Result from Vendor B' in result |
| 92 | + |
| 93 | + |
| 94 | +def test_multi_vendor_stops_after_all_primaries_failure(mock_routing): |
| 95 | + """Test that multi-vendor stops after all primaries even when they fail.""" |
| 96 | + mock_config, mock_vendors = mock_routing |
| 97 | + |
| 98 | + # Configure two primary vendors that will fail |
| 99 | + mock_vendors['a'].side_effect = Exception("Vendor A failed") |
| 100 | + mock_vendors['b'].side_effect = Exception("Vendor B failed") |
| 101 | + |
| 102 | + mock_config.return_value = { |
| 103 | + 'data_vendors': {'test_category': 'vendor_a,vendor_b'}, |
| 104 | + 'tool_vendors': {} |
| 105 | + } |
| 106 | + |
| 107 | + from tradingagents.dataflows.interface import route_to_vendor |
| 108 | + |
| 109 | + # Should raise error after trying all primaries |
| 110 | + with pytest.raises(RuntimeError, match="All vendor implementations failed"): |
| 111 | + route_to_vendor('test_method', 'arg1') |
| 112 | + |
| 113 | + # Assertions |
| 114 | + mock_vendors['a'].assert_called_once_with('arg1') |
| 115 | + mock_vendors['b'].assert_called_once_with('arg1') |
| 116 | + mock_vendors['c'].assert_not_called() # Should NOT try fallback |
| 117 | + |
| 118 | + |
| 119 | +def test_multi_vendor_partial_failure_stops_after_primaries(mock_routing): |
| 120 | + """Test that multi-vendor stops after all primaries even if one fails.""" |
| 121 | + mock_config, mock_vendors = mock_routing |
| 122 | + |
| 123 | + # First vendor fails, second succeeds |
| 124 | + mock_vendors['a'].side_effect = Exception("Vendor A failed") |
| 125 | + |
| 126 | + mock_config.return_value = { |
| 127 | + 'data_vendors': {'test_category': 'vendor_a,vendor_b'}, |
| 128 | + 'tool_vendors': {} |
| 129 | + } |
| 130 | + |
| 131 | + from tradingagents.dataflows.interface import route_to_vendor |
| 132 | + |
| 133 | + result = route_to_vendor('test_method', 'arg1') |
| 134 | + |
| 135 | + # Assertions |
| 136 | + mock_vendors['a'].assert_called_once_with('arg1') |
| 137 | + mock_vendors['b'].assert_called_once_with('arg1') |
| 138 | + mock_vendors['c'].assert_not_called() # Should NOT try fallback |
| 139 | + |
| 140 | + assert result == 'Result from Vendor B' |
| 141 | + |
| 142 | + |
| 143 | +def test_single_vendor_uses_fallback_on_failure(mock_routing): |
| 144 | + """Test that single vendor uses fallback if primary fails.""" |
| 145 | + mock_config, mock_vendors = mock_routing |
| 146 | + |
| 147 | + # Primary vendor fails |
| 148 | + mock_vendors['a'].side_effect = Exception("Vendor A failed") |
| 149 | + |
| 150 | + mock_config.return_value = { |
| 151 | + 'data_vendors': {'test_category': 'vendor_a'}, |
| 152 | + 'tool_vendors': {} |
| 153 | + } |
| 154 | + |
| 155 | + from tradingagents.dataflows.interface import route_to_vendor |
| 156 | + |
| 157 | + result = route_to_vendor('test_method', 'arg1') |
| 158 | + |
| 159 | + # Assertions |
| 160 | + mock_vendors['a'].assert_called_once_with('arg1') |
| 161 | + mock_vendors['b'].assert_called_once_with('arg1') # Should try fallback |
| 162 | + assert result == 'Result from Vendor B' |
| 163 | + |
| 164 | + |
| 165 | +def test_tool_level_override_takes_precedence(mock_routing): |
| 166 | + """Test that tool-level vendor config overrides category-level.""" |
| 167 | + mock_config, mock_vendors = mock_routing |
| 168 | + |
| 169 | + # Category says vendor_a, but tool override says vendor_b |
| 170 | + mock_config.return_value = { |
| 171 | + 'data_vendors': {'test_category': 'vendor_a'}, |
| 172 | + 'tool_vendors': {'test_method': 'vendor_b'} |
| 173 | + } |
| 174 | + |
| 175 | + from tradingagents.dataflows.interface import route_to_vendor |
| 176 | + |
| 177 | + result = route_to_vendor('test_method', 'arg1') |
| 178 | + |
| 179 | + # Assertions |
| 180 | + mock_vendors['a'].assert_not_called() # Category default ignored |
| 181 | + mock_vendors['b'].assert_called_once_with('arg1') # Tool override used |
| 182 | + assert result == 'Result from Vendor B' |
| 183 | + |
0 commit comments