|
| 1 | +"""Test that response_info assignment handles all types correctly""" |
| 2 | +import pytest |
| 3 | +from unittest.mock import Mock, MagicMock |
| 4 | +from pinecone.openapi_support.api_client import ApiClient |
| 5 | +from pinecone.openapi_support.asyncio_api_client import AsyncioApiClient |
| 6 | +from pinecone.config.openapi_configuration import Configuration |
| 7 | + |
| 8 | + |
| 9 | +class TestResponseInfoAssignment: |
| 10 | + """Test that _response_info assignment works for all response types""" |
| 11 | + |
| 12 | + def setup_method(self): |
| 13 | + """Set up test fixtures""" |
| 14 | + self.config = Configuration() |
| 15 | + |
| 16 | + def test_sync_api_client_dict_response(self, mocker): |
| 17 | + """Test that dict responses get _response_info as a key""" |
| 18 | + api_client = ApiClient(self.config) |
| 19 | + |
| 20 | + # Mock the request method to return a dict response |
| 21 | + mock_response = Mock() |
| 22 | + mock_response.data = b'{}' |
| 23 | + mock_response.status = 200 |
| 24 | + mock_response.getheaders = Mock(return_value={'x-pinecone-request-latency-ms': '100'}) |
| 25 | + mock_response.getheader = Mock(side_effect=lambda x: 'application/json' if x == 'content-type' else None) |
| 26 | + |
| 27 | + mocker.patch.object(api_client, 'request', return_value=mock_response) |
| 28 | + |
| 29 | + # Call the API |
| 30 | + result = api_client.call_api( |
| 31 | + resource_path='/test', |
| 32 | + method='POST', |
| 33 | + response_type=(dict,), |
| 34 | + _return_http_data_only=True, |
| 35 | + ) |
| 36 | + |
| 37 | + # Verify _response_info is set as a dict key |
| 38 | + assert isinstance(result, dict) |
| 39 | + assert '_response_info' in result |
| 40 | + |
| 41 | + def test_sync_api_client_string_response(self, mocker): |
| 42 | + """Test that string responses don't cause AttributeError""" |
| 43 | + api_client = ApiClient(self.config) |
| 44 | + |
| 45 | + # Mock the request method to return a string response |
| 46 | + mock_response = Mock() |
| 47 | + mock_response.data = b'"success"' |
| 48 | + mock_response.status = 200 |
| 49 | + mock_response.getheaders = Mock(return_value={'x-pinecone-request-latency-ms': '100'}) |
| 50 | + mock_response.getheader = Mock(side_effect=lambda x: 'application/json' if x == 'content-type' else None) |
| 51 | + |
| 52 | + mocker.patch.object(api_client, 'request', return_value=mock_response) |
| 53 | + |
| 54 | + # This should not raise AttributeError when trying to set _response_info |
| 55 | + try: |
| 56 | + result = api_client.call_api( |
| 57 | + resource_path='/test', |
| 58 | + method='POST', |
| 59 | + response_type=(str,), |
| 60 | + _return_http_data_only=True, |
| 61 | + _check_type=False, |
| 62 | + ) |
| 63 | + # If we get a string back, it should not have _response_info |
| 64 | + # (we don't check what type we get back because it depends on deserializer behavior) |
| 65 | + except AttributeError as e: |
| 66 | + if "'str' object has no attribute '_response_info'" in str(e): |
| 67 | + pytest.fail(f"Should not raise AttributeError for string response: {e}") |
| 68 | + # Other AttributeErrors may be raised by deserializer for invalid types |
| 69 | + |
| 70 | + def test_sync_api_client_none_response(self, mocker): |
| 71 | + """Test that None responses are handled correctly""" |
| 72 | + api_client = ApiClient(self.config) |
| 73 | + |
| 74 | + # Mock the request method to return no content |
| 75 | + mock_response = Mock() |
| 76 | + mock_response.data = b'' |
| 77 | + mock_response.status = 204 |
| 78 | + mock_response.getheaders = Mock(return_value={'x-pinecone-request-latency-ms': '100'}) |
| 79 | + mock_response.getheader = Mock(side_effect=lambda x: None) |
| 80 | + |
| 81 | + mocker.patch.object(api_client, 'request', return_value=mock_response) |
| 82 | + |
| 83 | + # This should not raise AttributeError |
| 84 | + try: |
| 85 | + result = api_client.call_api( |
| 86 | + resource_path='/test', |
| 87 | + method='DELETE', |
| 88 | + response_type=None, |
| 89 | + _return_http_data_only=True, |
| 90 | + ) |
| 91 | + assert result is None |
| 92 | + except AttributeError as e: |
| 93 | + pytest.fail(f"Should not raise AttributeError for None response: {e}") |
| 94 | + |
| 95 | + @pytest.mark.asyncio |
| 96 | + @pytest.mark.skip(reason="Requires asyncio extras") |
| 97 | + async def test_asyncio_api_client_dict_response(self, mocker): |
| 98 | + """Test that dict responses get _response_info as a key in asyncio""" |
| 99 | + api_client = AsyncioApiClient(self.config) |
| 100 | + |
| 101 | + # Mock the request method to return a dict response |
| 102 | + mock_response = Mock() |
| 103 | + mock_response.data = b'{}' |
| 104 | + mock_response.status = 200 |
| 105 | + mock_response.getheaders = Mock(return_value={'x-pinecone-request-latency-ms': '100'}) |
| 106 | + mock_response.getheader = Mock(side_effect=lambda x: 'application/json' if x == 'content-type' else None) |
| 107 | + |
| 108 | + async def mock_request(*args, **kwargs): |
| 109 | + return mock_response |
| 110 | + |
| 111 | + mocker.patch.object(api_client, 'request', side_effect=mock_request) |
| 112 | + |
| 113 | + # Call the API |
| 114 | + result = await api_client.call_api( |
| 115 | + resource_path='/test', |
| 116 | + method='POST', |
| 117 | + response_type=(dict,), |
| 118 | + _return_http_data_only=True, |
| 119 | + ) |
| 120 | + |
| 121 | + # Verify _response_info is set as a dict key |
| 122 | + assert isinstance(result, dict) |
| 123 | + assert '_response_info' in result |
| 124 | + |
| 125 | + await api_client.close() |
| 126 | + |
| 127 | + @pytest.mark.asyncio |
| 128 | + @pytest.mark.skip(reason="Requires asyncio extras") |
| 129 | + async def test_asyncio_api_client_string_response(self, mocker): |
| 130 | + """Test that string responses don't cause AttributeError in asyncio""" |
| 131 | + api_client = AsyncioApiClient(self.config) |
| 132 | + |
| 133 | + # Mock the request method to return a string response |
| 134 | + mock_response = Mock() |
| 135 | + mock_response.data = b'"success"' |
| 136 | + mock_response.status = 200 |
| 137 | + mock_response.getheaders = Mock(return_value={'x-pinecone-request-latency-ms': '100'}) |
| 138 | + mock_response.getheader = Mock(side_effect=lambda x: 'application/json' if x == 'content-type' else None) |
| 139 | + |
| 140 | + async def mock_request(*args, **kwargs): |
| 141 | + return mock_response |
| 142 | + |
| 143 | + mocker.patch.object(api_client, 'request', side_effect=mock_request) |
| 144 | + |
| 145 | + # This should not raise AttributeError when trying to set _response_info |
| 146 | + try: |
| 147 | + result = await api_client.call_api( |
| 148 | + resource_path='/test', |
| 149 | + method='POST', |
| 150 | + response_type=(str,), |
| 151 | + _return_http_data_only=True, |
| 152 | + _check_type=False, |
| 153 | + ) |
| 154 | + # If we get a string back, it should not have _response_info |
| 155 | + except AttributeError as e: |
| 156 | + if "'str' object has no attribute '_response_info'" in str(e): |
| 157 | + pytest.fail(f"Should not raise AttributeError for string response: {e}") |
| 158 | + # Other AttributeErrors may be raised by deserializer for invalid types |
| 159 | + finally: |
| 160 | + await api_client.close() |
| 161 | + |
| 162 | + @pytest.mark.asyncio |
| 163 | + @pytest.mark.skip(reason="Requires asyncio extras") |
| 164 | + async def test_asyncio_api_client_none_response(self, mocker): |
| 165 | + """Test that None responses are handled correctly in asyncio""" |
| 166 | + api_client = AsyncioApiClient(self.config) |
| 167 | + |
| 168 | + # Mock the request method to return no content |
| 169 | + mock_response = Mock() |
| 170 | + mock_response.data = b'' |
| 171 | + mock_response.status = 204 |
| 172 | + mock_response.getheaders = Mock(return_value={'x-pinecone-request-latency-ms': '100'}) |
| 173 | + mock_response.getheader = Mock(side_effect=lambda x: None) |
| 174 | + |
| 175 | + async def mock_request(*args, **kwargs): |
| 176 | + return mock_response |
| 177 | + |
| 178 | + mocker.patch.object(api_client, 'request', side_effect=mock_request) |
| 179 | + |
| 180 | + # This should not raise AttributeError |
| 181 | + try: |
| 182 | + result = await api_client.call_api( |
| 183 | + resource_path='/test', |
| 184 | + method='DELETE', |
| 185 | + response_type=None, |
| 186 | + _return_http_data_only=True, |
| 187 | + ) |
| 188 | + assert result is None |
| 189 | + except AttributeError as e: |
| 190 | + pytest.fail(f"Should not raise AttributeError for None response: {e}") |
| 191 | + finally: |
| 192 | + await api_client.close() |
| 193 | + |
| 194 | + def test_sync_api_client_model_response(self, mocker): |
| 195 | + """Test that OpenAPI model responses get _response_info as an attribute""" |
| 196 | + api_client = ApiClient(self.config) |
| 197 | + |
| 198 | + # Create a mock model class that supports attribute assignment |
| 199 | + class MockModel: |
| 200 | + def __init__(self): |
| 201 | + pass |
| 202 | + |
| 203 | + # Mock the request and deserializer |
| 204 | + mock_response = Mock() |
| 205 | + mock_response.data = b'{"test": "value"}' |
| 206 | + mock_response.status = 200 |
| 207 | + mock_response.getheaders = Mock(return_value={'x-pinecone-request-latency-ms': '100'}) |
| 208 | + mock_response.getheader = Mock(side_effect=lambda x: 'application/json' if x == 'content-type' else None) |
| 209 | + |
| 210 | + mocker.patch.object(api_client, 'request', return_value=mock_response) |
| 211 | + |
| 212 | + # Mock the deserializer to return a model instance |
| 213 | + mock_model_instance = MockModel() |
| 214 | + mocker.patch('pinecone.openapi_support.deserializer.Deserializer.deserialize', |
| 215 | + return_value=mock_model_instance) |
| 216 | + mocker.patch('pinecone.openapi_support.deserializer.Deserializer.decode_response') |
| 217 | + |
| 218 | + # Call the API |
| 219 | + result = api_client.call_api( |
| 220 | + resource_path='/test', |
| 221 | + method='GET', |
| 222 | + response_type=(MockModel,), |
| 223 | + _return_http_data_only=True, |
| 224 | + ) |
| 225 | + |
| 226 | + # Verify _response_info is set as an attribute |
| 227 | + assert hasattr(result, '_response_info') |
0 commit comments