11import unittest
22from 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+ )
512from amazon_paapi .helpers import requests
13+ from amazon_paapi .sdk .rest import ApiException
614
715
816class 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