From b27193ccc95870dcb0a012fa4b8468e735c1f3e7 Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Wed, 1 Apr 2026 11:01:46 -0400 Subject: [PATCH] Simplify the exceptions types and tests --- instaparser/exceptions.py | 17 ++++---- tests/test_exceptions.py | 87 ++++----------------------------------- 2 files changed, 16 insertions(+), 88 deletions(-) diff --git a/instaparser/exceptions.py b/instaparser/exceptions.py index cab996f..df970bf 100644 --- a/instaparser/exceptions.py +++ b/instaparser/exceptions.py @@ -2,17 +2,22 @@ Exception classes for the Instaparser Library. """ +from typing import Any + class InstaparserError(Exception): """Base exception for all Instaparser errors.""" - pass - class InstaparserAPIError(InstaparserError): """Exception raised for API errors.""" - def __init__(self, message, status_code=None, response=None): + def __init__( + self, + message: str, + status_code: int | None = None, + response: Any | None = None, + ): super().__init__(message) self.status_code = status_code self.response = response @@ -21,16 +26,10 @@ def __init__(self, message, status_code=None, response=None): class InstaparserAuthenticationError(InstaparserAPIError): """Exception raised for authentication errors (401).""" - pass - class InstaparserRateLimitError(InstaparserAPIError): """Exception raised for rate limit errors (429).""" - pass - class InstaparserValidationError(InstaparserAPIError): """Exception raised for validation errors (400).""" - - pass diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index f9c04de..29134ea 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -2,94 +2,23 @@ Tests for exception classes. """ -import pytest - -from instaparser.exceptions import ( - InstaparserAPIError, - InstaparserAuthenticationError, - InstaparserError, - InstaparserRateLimitError, - InstaparserValidationError, -) - - -class TestInstaparserError: - """Tests for base InstaparserError exception.""" - - def test_instaparser_error_is_exception(self): - """Test that InstaparserError is an Exception.""" - assert issubclass(InstaparserError, Exception) - - def test_instaparser_error_can_be_raised(self): - """Test that InstaparserError can be raised and caught.""" - with pytest.raises(InstaparserError): - raise InstaparserError("Test error") +from instaparser.exceptions import InstaparserAPIError class TestInstaparserAPIError: - """Tests for InstaparserAPIError exception.""" + """Tests for InstaparserAPIError attributes.""" - def test_instaparser_api_error_inherits_from_base(self): - """Test that InstaparserAPIError inherits from InstaparserError.""" - assert issubclass(InstaparserAPIError, InstaparserError) - - def test_instaparser_api_error_with_message(self): - """Test InstaparserAPIError with just a message.""" + def test_message(self): error = InstaparserAPIError("API error occurred") assert str(error) == "API error occurred" assert error.status_code is None assert error.response is None - def test_instaparser_api_error_with_status_code(self): - """Test InstaparserAPIError with status code.""" + def test_status_code(self): error = InstaparserAPIError("API error", status_code=500) - assert str(error) == "API error" assert error.status_code == 500 - assert error.response is None - - def test_instaparser_api_error_with_response(self): - """Test InstaparserAPIError with response object.""" - mock_response = type("MockResponse", (), {"status_code": 500})() - error = InstaparserAPIError("API error", status_code=500, response=mock_response) - assert str(error) == "API error" - assert error.status_code == 500 - assert error.response == mock_response - - -class TestInstaparserAuthenticationError: - """Tests for InstaparserAuthenticationError exception.""" - - def test_authentication_error_inherits_from_api_error(self): - """Test that InstaparserAuthenticationError inherits from InstaparserAPIError.""" - assert issubclass(InstaparserAuthenticationError, InstaparserAPIError) - - def test_authentication_error_can_be_raised(self): - """Test that InstaparserAuthenticationError can be raised.""" - with pytest.raises(InstaparserAuthenticationError): - raise InstaparserAuthenticationError("Invalid API key", status_code=401) - - -class TestInstaparserRateLimitError: - """Tests for InstaparserRateLimitError exception.""" - - def test_rate_limit_error_inherits_from_api_error(self): - """Test that InstaparserRateLimitError inherits from InstaparserAPIError.""" - assert issubclass(InstaparserRateLimitError, InstaparserAPIError) - - def test_rate_limit_error_can_be_raised(self): - """Test that InstaparserRateLimitError can be raised.""" - with pytest.raises(InstaparserRateLimitError): - raise InstaparserRateLimitError("Rate limit exceeded", status_code=429) - - -class TestInstaparserValidationError: - """Tests for InstaparserValidationError exception.""" - - def test_validation_error_inherits_from_api_error(self): - """Test that InstaparserValidationError inherits from InstaparserAPIError.""" - assert issubclass(InstaparserValidationError, InstaparserAPIError) - def test_validation_error_can_be_raised(self): - """Test that InstaparserValidationError can be raised.""" - with pytest.raises(InstaparserValidationError): - raise InstaparserValidationError("Invalid request", status_code=400) + def test_response(self): + response = object() + error = InstaparserAPIError("API error", status_code=500, response=response) + assert error.response is response