From d8b8983c0ae308d80c5697f408ca678f79bbba26 Mon Sep 17 00:00:00 2001 From: Stelios Voutsinas Date: Fri, 17 Jan 2025 13:56:06 -0700 Subject: [PATCH] Add some missing tests --- pyvo/dal/tests/test_tap.py | 112 +++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/pyvo/dal/tests/test_tap.py b/pyvo/dal/tests/test_tap.py index d3e803f7..076edf91 100644 --- a/pyvo/dal/tests/test_tap.py +++ b/pyvo/dal/tests/test_tap.py @@ -793,6 +793,118 @@ def test_get_udf(self, tapservice): assert func.form == "ivo_hasword(haystack TEXT, needle TEXT) -> INTEGER" +def test_invalid_service_url_format(): + service = TAPService('http://[invalid-url') + + with pytest.raises(DALServiceError) as excinfo: + _ = service.capabilities + + error_message = str(excinfo.value) + assert "Invalid service URL" in error_message + assert "Please provide a valid URL in the format 'http(s)://domain/path'" in error_message + + +def test_timeout_error(): + service = TAPService('http://example.com/tap') + + with requests_mock.Mocker() as rm: + rm.register_uri( + 'GET', + 'http://example.com/tap/capabilities', + exc=requests.Timeout("Request timed out") + ) + rm.register_uri( + 'GET', + 'http://example.com/capabilities', + exc=requests.Timeout("Request timed out") + ) + + with pytest.raises(DALServiceError) as excinfo: + _ = service.capabilities + + error_message = str(excinfo.value) + assert "Request timed out" in error_message + + +def test_generic_request_exception(): + service = TAPService('http://example.com/tap') + + with requests_mock.Mocker() as rm: + rm.register_uri( + 'GET', + 'http://example.com/tap/capabilities', + exc=requests.RequestException("Some request error") + ) + rm.register_uri( + 'GET', + 'http://example.com/capabilities', + exc=requests.RequestException("Some request error") + ) + + with pytest.raises(DALServiceError) as excinfo: + _ = service.capabilities + + error_message = str(excinfo.value) + assert "Some request error" in error_message + + +def test_unexpected_exception(): + service = TAPService('http://example.com/tap') + + class CustomException(Exception): + pass + + with requests_mock.Mocker() as rm: + rm.register_uri( + 'GET', + 'http://example.com/tap/capabilities', + exc=CustomException("Unexpected error occurred") + ) + rm.register_uri( + 'GET', + 'http://example.com/capabilities', + exc=CustomException("Unexpected error occurred") + ) + + with pytest.raises(DALServiceError) as excinfo: + _ = service.capabilities + + error_message = str(excinfo.value) + assert "Unexpected error: Unexpected error occurred" in error_message + + +def test_tap_service_initialization_error(): + with requests_mock.Mocker() as rm: + rm.register_uri( + 'GET', + 'http://example.com/tap/capabilities', + status_code=404 + ) + rm.register_uri( + 'GET', + 'http://example.com/capabilities', + status_code=404 + ) + + service = TAPService('http://example.com/tap') + with pytest.raises(DALServiceError) as excinfo: + _ = service.capabilities + + error_message = str(excinfo.value) + assert "Unable to access the capabilities endpoint at:" in error_message + assert "404" in error_message + + +def test_tap_service_invalid_url_error(): + service = TAPService('http://[invalid-url') + with pytest.raises(DALServiceError) as excinfo: + _ = service.capabilities + + error_message = str(excinfo.value) + assert "Invalid service URL" in error_message + assert "Please provide a valid URL in the format 'http(s)://domain/path'" in error_message + + def test_endpoint_connection_errors(): service = TAPService('http://example.com/tap')