From 0e6ce1dc8fee21c3b45381961a8902d1ca1ed933 Mon Sep 17 00:00:00 2001 From: Andre Luis Anastacio Date: Fri, 23 Aug 2024 23:14:31 -0300 Subject: [PATCH] Fix table_exists behavior in the rest catalog (#1096) --- pyiceberg/catalog/rest.py | 13 ++++++++++++- tests/catalog/test_rest.py | 23 ++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/pyiceberg/catalog/rest.py b/pyiceberg/catalog/rest.py index c22b614d6e..d2acc70136 100644 --- a/pyiceberg/catalog/rest.py +++ b/pyiceberg/catalog/rest.py @@ -834,4 +834,15 @@ def table_exists(self, identifier: Union[str, Identifier]) -> bool: response = self._session.head( self.url(Endpoints.load_table, prefixed=True, **self._split_identifier_for_path(identifier_tuple)) ) - return response.status_code in (200, 204) + + if response.status_code == 404: + return False + elif response.status_code == 204: + return True + + try: + response.raise_for_status() + except HTTPError as exc: + self._handle_non_200_response(exc, {}) + + return False diff --git a/tests/catalog/test_rest.py b/tests/catalog/test_rest.py index 54239ce3f4..bb609b4142 100644 --- a/tests/catalog/test_rest.py +++ b/tests/catalog/test_rest.py @@ -32,6 +32,7 @@ NoSuchNamespaceError, NoSuchTableError, OAuthError, + ServerError, TableAlreadyExistsError, ) from pyiceberg.io import load_file_io @@ -701,17 +702,17 @@ def test_load_table_404(rest_mock: Mocker) -> None: assert "Table does not exist" in str(e.value) -def test_table_exist_200(rest_mock: Mocker) -> None: +def test_table_exists_200(rest_mock: Mocker) -> None: rest_mock.head( f"{TEST_URI}v1/namespaces/fokko/tables/table", status_code=200, request_headers=TEST_HEADERS, ) catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN) - assert catalog.table_exists(("fokko", "table")) + assert not catalog.table_exists(("fokko", "table")) -def test_table_exist_204(rest_mock: Mocker) -> None: +def test_table_exists_204(rest_mock: Mocker) -> None: rest_mock.head( f"{TEST_URI}v1/namespaces/fokko/tables/table", status_code=204, @@ -721,16 +722,28 @@ def test_table_exist_204(rest_mock: Mocker) -> None: assert catalog.table_exists(("fokko", "table")) -def test_table_exist_500(rest_mock: Mocker) -> None: +def test_table_exists_404(rest_mock: Mocker) -> None: rest_mock.head( f"{TEST_URI}v1/namespaces/fokko/tables/table", - status_code=500, + status_code=404, request_headers=TEST_HEADERS, ) catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN) assert not catalog.table_exists(("fokko", "table")) +def test_table_exists_500(rest_mock: Mocker) -> None: + rest_mock.head( + f"{TEST_URI}v1/namespaces/fokko/tables/table", + status_code=500, + request_headers=TEST_HEADERS, + ) + catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN) + + with pytest.raises(ServerError): + catalog.table_exists(("fokko", "table")) + + def test_drop_table_404(rest_mock: Mocker) -> None: rest_mock.delete( f"{TEST_URI}v1/namespaces/fokko/tables/does_not_exists",