Files
cleverswarm-python-client/tests/test_exceptions.py
T
2025-09-27 20:55:31 +01:00

202 lines
7.2 KiB
Python

"""
Unit tests for exceptions module.
Copyright (c) 2016 - present Syncleus, Inc.
Copyright (c) 2016 - present CleverThis, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import pytest
from cleverswarm_python_client.libs.exceptions import (
BaseUrlMissingException,
ClientException,
ConnectionErrorException,
ConnectionTimeoutException,
InvalidFileException,
InvalidUsernameOrPasswordException,
JobNotFoundException,
RequestErrorException,
UnauthorizedException,
UnexpectedConditionException,
)
class TestClientException:
"""Test cases for ClientException base class."""
def test_client_exception_inheritance(self):
"""Test that ClientException inherits from Exception."""
assert issubclass(ClientException, Exception)
def test_client_exception_instantiation(self):
"""Test that ClientException can be instantiated with a message."""
message = "Test client exception"
exc = ClientException(message)
assert str(exc) == message
def test_client_exception_no_message(self):
"""Test that ClientException can be instantiated without a message."""
exc = ClientException()
assert str(exc) == ""
class TestBaseUrlMissingException:
"""Test cases for BaseUrlMissingException."""
def test_inheritance(self):
"""Test that BaseUrlMissingException inherits from ClientException."""
assert issubclass(BaseUrlMissingException, ClientException)
def test_instantiation(self):
"""Test that BaseUrlMissingException can be instantiated."""
exc = BaseUrlMissingException("Base URL is missing")
assert str(exc) == "Base URL is missing"
class TestInvalidUsernameOrPasswordException:
"""Test cases for InvalidUsernameOrPasswordException."""
def test_inheritance(self):
"""Test that InvalidUsernameOrPasswordException inherits from ClientException."""
assert issubclass(InvalidUsernameOrPasswordException, ClientException)
def test_instantiation(self):
"""Test that InvalidUsernameOrPasswordException can be instantiated."""
exc = InvalidUsernameOrPasswordException("Invalid credentials")
assert str(exc) == "Invalid credentials"
class TestUnexpectedConditionException:
"""Test cases for UnexpectedConditionException."""
def test_inheritance(self):
"""Test that UnexpectedConditionException inherits from ClientException."""
assert issubclass(UnexpectedConditionException, ClientException)
def test_instantiation(self):
"""Test that UnexpectedConditionException can be instantiated."""
exc = UnexpectedConditionException("Unexpected condition occurred")
assert str(exc) == "Unexpected condition occurred"
class TestRequestErrorException:
"""Test cases for RequestErrorException."""
def test_inheritance(self):
"""Test that RequestErrorException inherits from ClientException."""
assert issubclass(RequestErrorException, ClientException)
def test_instantiation(self):
"""Test that RequestErrorException can be instantiated."""
exc = RequestErrorException("Request error occurred")
assert str(exc) == "Request error occurred"
class TestConnectionErrorException:
"""Test cases for ConnectionErrorException."""
def test_inheritance(self):
"""Test that ConnectionErrorException inherits from ClientException."""
assert issubclass(ConnectionErrorException, ClientException)
def test_instantiation(self):
"""Test that ConnectionErrorException can be instantiated."""
exc = ConnectionErrorException("Connection failed")
assert str(exc) == "Connection failed"
class TestConnectionTimeoutException:
"""Test cases for ConnectionTimeoutException."""
def test_inheritance(self):
"""Test that ConnectionTimeoutException inherits from ClientException."""
assert issubclass(ConnectionTimeoutException, ClientException)
def test_instantiation(self):
"""Test that ConnectionTimeoutException can be instantiated."""
exc = ConnectionTimeoutException("Connection timed out")
assert str(exc) == "Connection timed out"
class TestJobNotFoundException:
"""Test cases for JobNotFoundException."""
def test_inheritance(self):
"""Test that JobNotFoundException inherits from ClientException."""
assert issubclass(JobNotFoundException, ClientException)
def test_instantiation(self):
"""Test that JobNotFoundException can be instantiated."""
exc = JobNotFoundException("Job not found")
assert str(exc) == "Job not found"
class TestUnauthorizedException:
"""Test cases for UnauthorizedException."""
def test_inheritance(self):
"""Test that UnauthorizedException inherits from ClientException."""
assert issubclass(UnauthorizedException, ClientException)
def test_instantiation(self):
"""Test that UnauthorizedException can be instantiated."""
exc = UnauthorizedException("Unauthorized access")
assert str(exc) == "Unauthorized access"
class TestInvalidFileException:
"""Test cases for InvalidFileException."""
def test_inheritance(self):
"""Test that InvalidFileException inherits from ClientException."""
assert issubclass(InvalidFileException, ClientException)
def test_instantiation(self):
"""Test that InvalidFileException can be instantiated."""
exc = InvalidFileException("Invalid file")
assert str(exc) == "Invalid file"
class TestExceptionHierarchy:
"""Test cases for exception hierarchy and behavior."""
def test_exception_catching(self):
"""Test that specific exceptions can be caught by their parent."""
with pytest.raises(ClientException):
raise BaseUrlMissingException("Test")
with pytest.raises(ClientException):
raise InvalidUsernameOrPasswordException("Test")
with pytest.raises(ClientException):
raise UnexpectedConditionException("Test")
def test_exception_chain(self):
"""Test that exceptions can be chained properly."""
try:
try:
raise ConnectionErrorException("Connection failed")
except ConnectionErrorException as e:
raise ClientException("Higher level error") from e
except ClientException as e:
assert isinstance(e.__cause__, ConnectionErrorException)
assert str(e.__cause__) == "Connection failed"
def test_exception_with_custom_attributes(self):
"""Test that exceptions can have custom attributes."""
exc = ClientException("Test message")
exc.custom_attr = "custom_value"
assert exc.custom_attr == "custom_value"