|
| 1 | +from typing import Any, Dict, List |
| 2 | + |
1 | 3 | import pytest
|
| 4 | +from httpx import Request, Response |
2 | 5 |
|
3 | 6 | from postgrest_py import AsyncRequestBuilder
|
| 7 | +from postgrest_py.base_request_builder import APIResponse |
4 | 8 | from postgrest_py.types import CountMethod
|
5 | 9 | from postgrest_py.utils import AsyncClient
|
6 | 10 |
|
@@ -114,3 +118,191 @@ def test_delete_with_count(self, request_builder: AsyncRequestBuilder):
|
114 | 118 | ]
|
115 | 119 | assert builder.http_method == "DELETE"
|
116 | 120 | assert builder.json == {}
|
| 121 | + |
| 122 | + |
| 123 | +@pytest.fixture |
| 124 | +def api_response_with_error() -> Dict[str, Any]: |
| 125 | + return { |
| 126 | + "message": "Route GET:/countries?select=%2A not found", |
| 127 | + "error": "Not Found", |
| 128 | + "statusCode": 404, |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | +@pytest.fixture |
| 133 | +def api_response() -> List[Dict[str, Any]]: |
| 134 | + return [ |
| 135 | + { |
| 136 | + "id": 1, |
| 137 | + "name": "Bonaire, Sint Eustatius and Saba", |
| 138 | + "iso2": "BQ", |
| 139 | + "iso3": "BES", |
| 140 | + "local_name": None, |
| 141 | + "continent": None, |
| 142 | + }, |
| 143 | + { |
| 144 | + "id": 2, |
| 145 | + "name": "Curaçao", |
| 146 | + "iso2": "CW", |
| 147 | + "iso3": "CUW", |
| 148 | + "local_name": None, |
| 149 | + "continent": None, |
| 150 | + }, |
| 151 | + ] |
| 152 | + |
| 153 | + |
| 154 | +@pytest.fixture |
| 155 | +def content_range_header_with_count() -> str: |
| 156 | + return "0-1/2" |
| 157 | + |
| 158 | + |
| 159 | +@pytest.fixture |
| 160 | +def content_range_header_without_count() -> str: |
| 161 | + return "0-1" |
| 162 | + |
| 163 | + |
| 164 | +@pytest.fixture |
| 165 | +def prefer_header_with_count() -> str: |
| 166 | + return "count=exact" |
| 167 | + |
| 168 | + |
| 169 | +@pytest.fixture |
| 170 | +def prefer_header_without_count() -> str: |
| 171 | + return "random prefer header" |
| 172 | + |
| 173 | + |
| 174 | +@pytest.fixture |
| 175 | +def request_response_without_prefer_header() -> Response: |
| 176 | + return Response( |
| 177 | + status_code=200, request=Request(method="GET", url="http://example.com") |
| 178 | + ) |
| 179 | + |
| 180 | + |
| 181 | +@pytest.fixture |
| 182 | +def request_response_with_prefer_header_without_count( |
| 183 | + prefer_header_without_count: str, |
| 184 | +) -> Response: |
| 185 | + return Response( |
| 186 | + status_code=200, |
| 187 | + request=Request( |
| 188 | + method="GET", |
| 189 | + url="http://example.com", |
| 190 | + headers={"prefer": prefer_header_without_count}, |
| 191 | + ), |
| 192 | + ) |
| 193 | + |
| 194 | + |
| 195 | +@pytest.fixture |
| 196 | +def request_response_with_prefer_header_with_count_and_content_range( |
| 197 | + prefer_header_with_count: str, content_range_header_with_count: str |
| 198 | +) -> Response: |
| 199 | + return Response( |
| 200 | + status_code=200, |
| 201 | + headers={"content-range": content_range_header_with_count}, |
| 202 | + request=Request( |
| 203 | + method="GET", |
| 204 | + url="http://example.com", |
| 205 | + headers={"prefer": prefer_header_with_count}, |
| 206 | + ), |
| 207 | + ) |
| 208 | + |
| 209 | + |
| 210 | +@pytest.fixture |
| 211 | +def request_response_with_data( |
| 212 | + prefer_header_with_count: str, |
| 213 | + content_range_header_with_count: str, |
| 214 | + api_response: List[Dict[str, Any]], |
| 215 | +) -> Response: |
| 216 | + return Response( |
| 217 | + status_code=200, |
| 218 | + headers={"content-range": content_range_header_with_count}, |
| 219 | + json=api_response, |
| 220 | + request=Request( |
| 221 | + method="GET", |
| 222 | + url="http://example.com", |
| 223 | + headers={"prefer": prefer_header_with_count}, |
| 224 | + ), |
| 225 | + ) |
| 226 | + |
| 227 | + |
| 228 | +class TestApiResponse: |
| 229 | + def test_response_raises_when_api_error( |
| 230 | + self, api_response_with_error: Dict[str, Any] |
| 231 | + ): |
| 232 | + with pytest.raises(ValueError): |
| 233 | + APIResponse(data=api_response_with_error) |
| 234 | + |
| 235 | + def test_parses_valid_response_only_data(self, api_response: List[Dict[str, Any]]): |
| 236 | + result = APIResponse(data=api_response) |
| 237 | + assert result.data == api_response |
| 238 | + |
| 239 | + def test_parses_valid_response_data_and_count( |
| 240 | + self, api_response: List[Dict[str, Any]] |
| 241 | + ): |
| 242 | + count = len(api_response) |
| 243 | + result = APIResponse(data=api_response, count=count) |
| 244 | + assert result.data == api_response |
| 245 | + assert result.count == count |
| 246 | + |
| 247 | + def test_get_count_from_content_range_header_with_count( |
| 248 | + self, content_range_header_with_count: str |
| 249 | + ): |
| 250 | + assert ( |
| 251 | + APIResponse.get_count_from_content_range_header( |
| 252 | + content_range_header_with_count |
| 253 | + ) |
| 254 | + == 2 |
| 255 | + ) |
| 256 | + |
| 257 | + def test_get_count_from_content_range_header_without_count( |
| 258 | + self, content_range_header_without_count: str |
| 259 | + ): |
| 260 | + assert ( |
| 261 | + APIResponse.get_count_from_content_range_header( |
| 262 | + content_range_header_without_count |
| 263 | + ) |
| 264 | + is None |
| 265 | + ) |
| 266 | + |
| 267 | + def test_is_count_in_prefer_header_true(self, prefer_header_with_count: str): |
| 268 | + assert APIResponse.is_count_in_prefer_header(prefer_header_with_count) |
| 269 | + |
| 270 | + def test_is_count_in_prefer_header_false(self, prefer_header_without_count: str): |
| 271 | + assert not APIResponse.is_count_in_prefer_header(prefer_header_without_count) |
| 272 | + |
| 273 | + def test_get_count_from_http_request_response_without_prefer_header( |
| 274 | + self, request_response_without_prefer_header: Response |
| 275 | + ): |
| 276 | + assert ( |
| 277 | + APIResponse.get_count_from_http_request_response( |
| 278 | + request_response_without_prefer_header |
| 279 | + ) |
| 280 | + is None |
| 281 | + ) |
| 282 | + |
| 283 | + def test_get_count_from_http_request_response_with_prefer_header_without_count( |
| 284 | + self, request_response_with_prefer_header_without_count: Response |
| 285 | + ): |
| 286 | + assert ( |
| 287 | + APIResponse.get_count_from_http_request_response( |
| 288 | + request_response_with_prefer_header_without_count |
| 289 | + ) |
| 290 | + is None |
| 291 | + ) |
| 292 | + |
| 293 | + def test_get_count_from_http_request_response_with_count_and_content_range( |
| 294 | + self, request_response_with_prefer_header_with_count_and_content_range: Response |
| 295 | + ): |
| 296 | + assert ( |
| 297 | + APIResponse.get_count_from_http_request_response( |
| 298 | + request_response_with_prefer_header_with_count_and_content_range |
| 299 | + ) |
| 300 | + == 2 |
| 301 | + ) |
| 302 | + |
| 303 | + def test_from_http_request_response_constructor( |
| 304 | + self, request_response_with_data: Response, api_response: List[Dict[str, Any]] |
| 305 | + ): |
| 306 | + result = APIResponse.from_http_request_response(request_response_with_data) |
| 307 | + assert result.data == api_response |
| 308 | + assert result.count == 2 |
0 commit comments