Skip to content

Commit 4a29087

Browse files
committed
Added requests tests
1 parent c364317 commit 4a29087

2 files changed

Lines changed: 102 additions & 5 deletions

File tree

tests/test_api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ def test_api_init_invalid_argument(self):
1515
def test_api_throttling_disabled(self):
1616
throttling = 0
1717
amazon = AmazonApi("key", "secret", "tag", "ES", throttling)
18-
start = int(time.time())
18+
start = int(time.time() * 10)
1919
amazon._throttle()
2020
amazon._throttle()
2121

22-
self.assertEqual(start, int(time.time()))
22+
self.assertEqual(start, int(time.time() * 10))
2323

2424
def test_api_throttling_sleeps(self):
25-
throttling = 1
25+
throttling = 0.1
2626
amazon = AmazonApi("key", "secret", "tag", "ES", throttling)
27-
start = int(time.time())
27+
start = int(time.time() * 10)
2828
amazon._throttle()
2929
amazon._throttle()
3030

31-
self.assertTrue(start < int(time.time()))
31+
self.assertTrue(start < int(time.time() * 10))
3232

3333
@mock.patch.object(requests, "get_items_response")
3434
def test_get_items(self, mocked_get_items_response):

tests/test_helpers_requests.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import unittest
2+
from unittest.mock import Mock, patch
3+
4+
from amazon_paapi.errors.exceptions import MalformedRequest
5+
from amazon_paapi.helpers import requests
6+
7+
8+
class TestRequests(unittest.TestCase):
9+
@patch.object(requests, "GetItemsRequest")
10+
def test_get_items_request(self, mock_get_items_request):
11+
mock_get_items_request.return_value = "foo"
12+
result = requests.get_items_request(Mock(), ["test"])
13+
14+
self.assertEqual("foo", result)
15+
16+
@patch.object(requests, "GetItemsRequest")
17+
def test_get_items_request_error(self, mock_get_items_request):
18+
mock_get_items_request.side_effect = TypeError()
19+
20+
with self.assertRaises(MalformedRequest):
21+
requests.get_items_request(Mock(), ["test"])
22+
23+
@patch.object(requests, "SearchItemsRequest")
24+
def test_search_items_request(self, mock_search_items_request):
25+
mock_search_items_request.return_value = "foo"
26+
result = requests.get_search_items_request(Mock())
27+
28+
self.assertEqual("foo", result)
29+
30+
@patch.object(requests, "SearchItemsRequest")
31+
def test_search_items_request_error(self, mock_search_items_request):
32+
mock_search_items_request.side_effect = TypeError()
33+
34+
with self.assertRaises(MalformedRequest):
35+
requests.get_search_items_request(Mock())
36+
37+
@patch.object(requests, "GetVariationsRequest")
38+
def test_variations_request(self, mock_variations_request):
39+
mock_variations_request.return_value = "foo"
40+
result = requests.get_variations_request(Mock())
41+
42+
self.assertEqual("foo", result)
43+
44+
@patch.object(requests, "GetVariationsRequest")
45+
def test_variations_request_error(self, mock_variations_request):
46+
mock_variations_request.side_effect = TypeError()
47+
48+
with self.assertRaises(MalformedRequest):
49+
requests.get_variations_request(Mock())
50+
51+
@patch.object(requests, "GetBrowseNodesRequest")
52+
def test_browse_nodes_request(self, mock_browse_nodes_request):
53+
mock_browse_nodes_request.return_value = "foo"
54+
result = requests.get_browse_nodes_request(Mock())
55+
56+
self.assertEqual("foo", result)
57+
58+
@patch.object(requests, "GetBrowseNodesRequest")
59+
def test_browse_nodes_request_error(self, mock_browse_nodes_request):
60+
mock_browse_nodes_request.side_effect = TypeError()
61+
62+
with self.assertRaises(MalformedRequest):
63+
requests.get_browse_nodes_request(Mock())
64+
65+
def test_get_items_response(self):
66+
amazon_api = Mock()
67+
api_result = Mock()
68+
api_result.items_result.items = "foo"
69+
amazon_api.api.get_items.return_value = api_result
70+
response = requests.get_items_response(amazon_api, Mock())
71+
72+
self.assertEqual("foo", response)
73+
74+
def test_search_items_response(self):
75+
amazon_api = Mock()
76+
api_result = Mock(search_result="foo")
77+
amazon_api.api.search_items.return_value = api_result
78+
response = requests.get_search_items_response(amazon_api, Mock())
79+
80+
self.assertEqual("foo", response)
81+
82+
def test_get_variations_response(self):
83+
amazon_api = Mock()
84+
api_result = Mock(variations_result="foo")
85+
amazon_api.api.get_variations.return_value = api_result
86+
response = requests.get_variations_response(amazon_api, Mock())
87+
88+
self.assertEqual("foo", response)
89+
90+
def test_get_browse_nodes_response(self):
91+
amazon_api = Mock()
92+
api_result = Mock()
93+
api_result.browse_nodes_result.browse_nodes = "foo"
94+
amazon_api.api.get_browse_nodes.return_value = api_result
95+
response = requests.get_browse_nodes_response(amazon_api, Mock())
96+
97+
self.assertEqual("foo", response)

0 commit comments

Comments
 (0)