Skip to content

Commit 143cd6b

Browse files
authored
Simplify the exception types and tests (#17)
1 parent d485dc1 commit 143cd6b

2 files changed

Lines changed: 16 additions & 88 deletions

File tree

instaparser/exceptions.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22
Exception classes for the Instaparser Library.
33
"""
44

5+
from typing import Any
6+
57

68
class InstaparserError(Exception):
79
"""Base exception for all Instaparser errors."""
810

9-
pass
10-
1111

1212
class InstaparserAPIError(InstaparserError):
1313
"""Exception raised for API errors."""
1414

15-
def __init__(self, message, status_code=None, response=None):
15+
def __init__(
16+
self,
17+
message: str,
18+
status_code: int | None = None,
19+
response: Any | None = None,
20+
):
1621
super().__init__(message)
1722
self.status_code = status_code
1823
self.response = response
@@ -21,16 +26,10 @@ def __init__(self, message, status_code=None, response=None):
2126
class InstaparserAuthenticationError(InstaparserAPIError):
2227
"""Exception raised for authentication errors (401)."""
2328

24-
pass
25-
2629

2730
class InstaparserRateLimitError(InstaparserAPIError):
2831
"""Exception raised for rate limit errors (429)."""
2932

30-
pass
31-
3233

3334
class InstaparserValidationError(InstaparserAPIError):
3435
"""Exception raised for validation errors (400)."""
35-
36-
pass

tests/test_exceptions.py

Lines changed: 8 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,23 @@
22
Tests for exception classes.
33
"""
44

5-
import pytest
6-
7-
from instaparser.exceptions import (
8-
InstaparserAPIError,
9-
InstaparserAuthenticationError,
10-
InstaparserError,
11-
InstaparserRateLimitError,
12-
InstaparserValidationError,
13-
)
14-
15-
16-
class TestInstaparserError:
17-
"""Tests for base InstaparserError exception."""
18-
19-
def test_instaparser_error_is_exception(self):
20-
"""Test that InstaparserError is an Exception."""
21-
assert issubclass(InstaparserError, Exception)
22-
23-
def test_instaparser_error_can_be_raised(self):
24-
"""Test that InstaparserError can be raised and caught."""
25-
with pytest.raises(InstaparserError):
26-
raise InstaparserError("Test error")
5+
from instaparser.exceptions import InstaparserAPIError
276

287

298
class TestInstaparserAPIError:
30-
"""Tests for InstaparserAPIError exception."""
9+
"""Tests for InstaparserAPIError attributes."""
3110

32-
def test_instaparser_api_error_inherits_from_base(self):
33-
"""Test that InstaparserAPIError inherits from InstaparserError."""
34-
assert issubclass(InstaparserAPIError, InstaparserError)
35-
36-
def test_instaparser_api_error_with_message(self):
37-
"""Test InstaparserAPIError with just a message."""
11+
def test_message(self):
3812
error = InstaparserAPIError("API error occurred")
3913
assert str(error) == "API error occurred"
4014
assert error.status_code is None
4115
assert error.response is None
4216

43-
def test_instaparser_api_error_with_status_code(self):
44-
"""Test InstaparserAPIError with status code."""
17+
def test_status_code(self):
4518
error = InstaparserAPIError("API error", status_code=500)
46-
assert str(error) == "API error"
4719
assert error.status_code == 500
48-
assert error.response is None
49-
50-
def test_instaparser_api_error_with_response(self):
51-
"""Test InstaparserAPIError with response object."""
52-
mock_response = type("MockResponse", (), {"status_code": 500})()
53-
error = InstaparserAPIError("API error", status_code=500, response=mock_response)
54-
assert str(error) == "API error"
55-
assert error.status_code == 500
56-
assert error.response == mock_response
57-
58-
59-
class TestInstaparserAuthenticationError:
60-
"""Tests for InstaparserAuthenticationError exception."""
61-
62-
def test_authentication_error_inherits_from_api_error(self):
63-
"""Test that InstaparserAuthenticationError inherits from InstaparserAPIError."""
64-
assert issubclass(InstaparserAuthenticationError, InstaparserAPIError)
65-
66-
def test_authentication_error_can_be_raised(self):
67-
"""Test that InstaparserAuthenticationError can be raised."""
68-
with pytest.raises(InstaparserAuthenticationError):
69-
raise InstaparserAuthenticationError("Invalid API key", status_code=401)
70-
71-
72-
class TestInstaparserRateLimitError:
73-
"""Tests for InstaparserRateLimitError exception."""
74-
75-
def test_rate_limit_error_inherits_from_api_error(self):
76-
"""Test that InstaparserRateLimitError inherits from InstaparserAPIError."""
77-
assert issubclass(InstaparserRateLimitError, InstaparserAPIError)
78-
79-
def test_rate_limit_error_can_be_raised(self):
80-
"""Test that InstaparserRateLimitError can be raised."""
81-
with pytest.raises(InstaparserRateLimitError):
82-
raise InstaparserRateLimitError("Rate limit exceeded", status_code=429)
83-
84-
85-
class TestInstaparserValidationError:
86-
"""Tests for InstaparserValidationError exception."""
87-
88-
def test_validation_error_inherits_from_api_error(self):
89-
"""Test that InstaparserValidationError inherits from InstaparserAPIError."""
90-
assert issubclass(InstaparserValidationError, InstaparserAPIError)
9120

92-
def test_validation_error_can_be_raised(self):
93-
"""Test that InstaparserValidationError can be raised."""
94-
with pytest.raises(InstaparserValidationError):
95-
raise InstaparserValidationError("Invalid request", status_code=400)
21+
def test_response(self):
22+
response = object()
23+
error = InstaparserAPIError("API error", status_code=500, response=response)
24+
assert error.response is response

0 commit comments

Comments
 (0)