Skip to content

Commit eb8f274

Browse files
committed
Finished tests for requests
1 parent 4a29087 commit eb8f274

3 files changed

Lines changed: 109 additions & 21 deletions

File tree

amazon_paapi/helpers/requests.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,26 @@ def _get_request_resources(resources) -> List[str]:
150150

151151

152152
def _manage_response_exceptions(error) -> None:
153-
if isinstance(error, ApiException):
154-
if error.status == 429:
155-
raise TooManyRequests(
156-
"Requests limit reached, try increasing throttling or wait before"
157-
" trying again"
158-
)
159-
160-
if "InvalidParameterValue" in error.body:
161-
raise InvalidArgument(
162-
"The value provided in the request for atleast one parameter is"
163-
" invalid."
164-
)
165-
166-
if "InvalidPartnerTag" in error.body:
167-
raise InvalidArgument("The partner tag is invalid or not present.")
168-
169-
if "InvalidAssociate" in error.body:
170-
raise AssociateValidationError(
171-
"Used credentials are not valid for the selected country."
172-
)
153+
error_status = getattr(error, "status", None)
154+
error_body = getattr(error, "body", "") or ""
155+
156+
if error_status == 429:
157+
raise TooManyRequests(
158+
"Requests limit reached, try increasing throttling or wait before"
159+
" trying again"
160+
)
161+
162+
if "InvalidParameterValue" in error_body:
163+
raise InvalidArgument(
164+
"The value provided in the request for atleast one parameter is invalid."
165+
)
166+
167+
if "InvalidPartnerTag" in error_body:
168+
raise InvalidArgument("The partner tag is invalid or not present.")
169+
170+
if "InvalidAssociate" in error_body:
171+
raise AssociateValidationError(
172+
"Used credentials are not valid for the selected country."
173+
)
173174

174175
raise RequestError("Request failed: " + str(error.reason))

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ skip_glob = "*/sdk/*"
1717
"too-many-arguments",
1818
"too-many-instance-attributes",
1919
"too-many-locals",
20+
"too-many-public-methods",
2021
]
2122
ignored-argument-names = "args|kwargs"
23+
[tool.pylint.similarities]
24+
ignore-imports = true

tests/test_helpers_requests.py

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import unittest
22
from unittest.mock import Mock, patch
33

4-
from amazon_paapi.errors.exceptions import MalformedRequest
4+
from amazon_paapi.errors import (
5+
AssociateValidationError,
6+
InvalidArgument,
7+
ItemsNotFound,
8+
MalformedRequest,
9+
RequestError,
10+
TooManyRequests,
11+
)
512
from amazon_paapi.helpers import requests
13+
from amazon_paapi.sdk.rest import ApiException
614

715

816
class TestRequests(unittest.TestCase):
@@ -71,6 +79,20 @@ def test_get_items_response(self):
7179

7280
self.assertEqual("foo", response)
7381

82+
def test_get_items_response_api_exception(self):
83+
amazon_api = Mock()
84+
amazon_api.api.get_items.side_effect = ApiException()
85+
86+
with self.assertRaises(RequestError):
87+
requests.get_items_response(amazon_api, Mock())
88+
89+
def test_get_items_response_items_not_found_exception(self):
90+
amazon_api = Mock()
91+
amazon_api.api.get_items.return_value = Mock(items_result=None)
92+
93+
with self.assertRaises(ItemsNotFound):
94+
requests.get_items_response(amazon_api, Mock())
95+
7496
def test_search_items_response(self):
7597
amazon_api = Mock()
7698
api_result = Mock(search_result="foo")
@@ -79,6 +101,20 @@ def test_search_items_response(self):
79101

80102
self.assertEqual("foo", response)
81103

104+
def test_get_search_items_response_api_exception(self):
105+
amazon_api = Mock()
106+
amazon_api.api.search_items.side_effect = ApiException()
107+
108+
with self.assertRaises(RequestError):
109+
requests.get_search_items_response(amazon_api, Mock())
110+
111+
def test_get_search_items_response_items_not_found_exception(self):
112+
amazon_api = Mock()
113+
amazon_api.api.search_items.return_value = Mock(search_result=None)
114+
115+
with self.assertRaises(ItemsNotFound):
116+
requests.get_search_items_response(amazon_api, Mock())
117+
82118
def test_get_variations_response(self):
83119
amazon_api = Mock()
84120
api_result = Mock(variations_result="foo")
@@ -87,6 +123,20 @@ def test_get_variations_response(self):
87123

88124
self.assertEqual("foo", response)
89125

126+
def test_get_variations_response_api_exception(self):
127+
amazon_api = Mock()
128+
amazon_api.api.get_variations.side_effect = ApiException()
129+
130+
with self.assertRaises(RequestError):
131+
requests.get_variations_response(amazon_api, Mock())
132+
133+
def test_get_variations_response_items_not_found_exception(self):
134+
amazon_api = Mock()
135+
amazon_api.api.get_variations.return_value = Mock(variations_result=None)
136+
137+
with self.assertRaises(ItemsNotFound):
138+
requests.get_variations_response(amazon_api, Mock())
139+
90140
def test_get_browse_nodes_response(self):
91141
amazon_api = Mock()
92142
api_result = Mock()
@@ -95,3 +145,37 @@ def test_get_browse_nodes_response(self):
95145
response = requests.get_browse_nodes_response(amazon_api, Mock())
96146

97147
self.assertEqual("foo", response)
148+
149+
def test_get_browse_nodes_response_api_exception(self):
150+
amazon_api = Mock()
151+
amazon_api.api.get_browse_nodes.side_effect = ApiException()
152+
153+
with self.assertRaises(RequestError):
154+
requests.get_browse_nodes_response(amazon_api, Mock())
155+
156+
def test_get_browse_nodes_response_items_not_found_exception(self):
157+
amazon_api = Mock()
158+
amazon_api.api.get_browse_nodes.return_value = Mock(browse_nodes_result=None)
159+
160+
with self.assertRaises(ItemsNotFound):
161+
requests.get_browse_nodes_response(amazon_api, Mock())
162+
163+
def test_manage_response_exceptions_too_many_requests(self):
164+
error = Mock(spec=ApiException, status=429)
165+
with self.assertRaises(TooManyRequests):
166+
requests._manage_response_exceptions(error)
167+
168+
def test_manage_response_exceptions_invalid_parameter_value(self):
169+
error = Mock(spec=ApiException, body="InvalidParameterValue", status=200)
170+
with self.assertRaises(InvalidArgument):
171+
requests._manage_response_exceptions(error)
172+
173+
def test_manage_response_exceptions_invalid_partner_tag(self):
174+
error = Mock(spec=ApiException, body="InvalidPartnerTag", status=200)
175+
with self.assertRaises(InvalidArgument):
176+
requests._manage_response_exceptions(error)
177+
178+
def test_manage_response_exceptions_invalid_associate(self):
179+
error = Mock(spec=ApiException, body="InvalidAssociate", status=200)
180+
with self.assertRaises(AssociateValidationError):
181+
requests._manage_response_exceptions(error)

0 commit comments

Comments
 (0)