Skip to content

Commit 55b9049

Browse files
authored
Merge pull request #88 from sergioteula/dev-add-tests
Added basic tests
2 parents 989c5e2 + e2d9137 commit 55b9049

5 files changed

Lines changed: 130 additions & 25 deletions

File tree

tests/test_api_get_items.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import unittest
2+
from unittest import mock
3+
4+
from amazon_paapi import AmazonApi
5+
from amazon_paapi.helpers import requests
6+
7+
8+
class TestGetItems(unittest.TestCase):
9+
@mock.patch.object(requests, "get_items_response")
10+
def test_get_items(self, mocked_get_items_response):
11+
mocked_get_items_response.return_value = []
12+
amazon = AmazonApi("key", "secret", "tag", "ES")
13+
response = amazon.get_items("ABCDEFGHIJ")
14+
self.assertTrue(isinstance(response, list))

tests/test_helpers_arguments.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
1-
from amazon_paapi.exceptions import AsinNotFoundException, InvalidArgumentException
1+
import unittest
2+
3+
from amazon_paapi.errors import AsinNotFoundException, InvalidArgumentException
24
from amazon_paapi.helpers.arguments import get_items_ids
3-
import pytest
45

56

6-
def test_get_items_ids():
7-
amazon_url = 'https://www.amazon.es/gp/product/B07PHPXHQS'
8-
assert get_items_ids('B01N5IB20Q') == ['B01N5IB20Q']
9-
assert get_items_ids('B01N5IB20Q,B01N5IB20Q,B01N5IB20Q') == ['B01N5IB20Q','B01N5IB20Q','B01N5IB20Q']
10-
assert get_items_ids(['B01N5IB20Q','B01N5IB20Q','B01N5IB20Q']) == ['B01N5IB20Q','B01N5IB20Q','B01N5IB20Q']
11-
assert get_items_ids(amazon_url) == ['B07PHPXHQS']
12-
assert get_items_ids([amazon_url,amazon_url]) == ['B07PHPXHQS', 'B07PHPXHQS']
7+
class TestHelpersArguments(unittest.TestCase):
8+
def test_get_items_ids(self):
9+
amazon_url = "https://www.amazon.es/gp/product/B07PHPXHQS"
10+
self.assertEqual(get_items_ids("B01N5IB20Q"), ["B01N5IB20Q"])
11+
self.assertEqual(
12+
get_items_ids("B01N5IB20Q,B01N5IB20Q,B01N5IB20Q"),
13+
["B01N5IB20Q", "B01N5IB20Q", "B01N5IB20Q"],
14+
)
15+
self.assertEqual(
16+
get_items_ids(["B01N5IB20Q", "B01N5IB20Q", "B01N5IB20Q"]),
17+
["B01N5IB20Q", "B01N5IB20Q", "B01N5IB20Q"],
18+
)
19+
self.assertEqual(get_items_ids(amazon_url), ["B07PHPXHQS"])
20+
self.assertEqual(
21+
get_items_ids([amazon_url, amazon_url]), ["B07PHPXHQS", "B07PHPXHQS"]
22+
)
23+
24+
def test_get_items_ids_asin_not_found(self):
25+
with self.assertRaises(AsinNotFoundException):
26+
get_items_ids("https://www.amazon.es/gp/")
1327

14-
with pytest.raises(AsinNotFoundException):
15-
get_items_ids('https://www.amazon.es/gp/')
16-
with pytest.raises(InvalidArgumentException):
17-
get_items_ids(34)
28+
def test_get_items_ids_invalid_argument(self):
29+
with self.assertRaises(InvalidArgumentException):
30+
get_items_ids(34)

tests/test_helpers_generators.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import unittest
2+
3+
from amazon_paapi.helpers.generators import get_list_chunks
4+
5+
6+
class TestHelpersGenerators(unittest.TestCase):
7+
def test_get_list_chunks(self):
8+
mocked_iterable = get_list_chunks(list(range(15)), 10)
9+
chunk = next(mocked_iterable)
10+
self.assertEqual(len(chunk), 10)
11+
chunk = next(mocked_iterable)
12+
self.assertEqual(len(chunk), 5)
13+
14+
mocked_iterable = get_list_chunks(list(range(5)), 10)
15+
chunk = next(mocked_iterable)
16+
self.assertEqual(len(chunk), 5)

tests/test_helpers_items.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import unittest
2+
from operator import imod
3+
from unittest import mock
4+
5+
from amazon_paapi.helpers.items import sort_items
6+
7+
8+
class MockedItem(mock.MagicMock):
9+
def __init__(self, asin):
10+
super().__init__()
11+
self.asin = asin
12+
13+
14+
class TestHelpersItems(unittest.TestCase):
15+
def setUp(self):
16+
self.mocked_items = [
17+
MockedItem("B"),
18+
MockedItem("C"),
19+
MockedItem("A"),
20+
MockedItem("D"),
21+
]
22+
self.mocked_items_ids = ["B", "A", "D", "C", "E", "A"]
23+
24+
def test_sort_items(self):
25+
sorted_items = sort_items(self.mocked_items, self.mocked_items_ids, False)
26+
self.assertEqual(sorted_items[0].asin, "B")
27+
self.assertEqual(sorted_items[1].asin, "A")
28+
self.assertEqual(sorted_items[2].asin, "D")
29+
self.assertEqual(sorted_items[3].asin, "C")
30+
31+
def test_sort_items_include_unavailable(self):
32+
sorted_items = sort_items(self.mocked_items, self.mocked_items_ids, True)
33+
self.assertEqual(sorted_items[4].asin, "E")
34+
self.assertEqual(len(sorted_items), 6)
35+
36+
def test_sort_items_not_include_unavailable(self):
37+
sorted_items = sort_items(self.mocked_items, self.mocked_items_ids, False)
38+
self.assertEqual(sorted_items[4].asin, "A")
39+
self.assertEqual(len(sorted_items), 5)
40+
41+
def test_sort_items_include_repeated(self):
42+
sorted_items = sort_items(self.mocked_items, self.mocked_items_ids, True)
43+
self.assertEqual(sorted_items[1].asin, sorted_items[5].asin)

tests/test_tools.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
1-
from amazon_paapi.exceptions import AsinNotFoundException
1+
import unittest
2+
3+
from amazon_paapi.errors import AsinNotFoundException
24
from amazon_paapi.tools import get_asin
3-
import pytest
45

56

6-
def test_get_asin():
7-
assert get_asin('B01N5IB20Q') == 'B01N5IB20Q'
8-
assert get_asin('https://www.amazon.es/gp/product/B07PHPXHQS') == 'B07PHPXHQS'
9-
assert get_asin('https://www.amazon.es/gp/product/B07PHPXHQS?pf_rd_r=3FXDZDV1W6KY83KEE2Z4&pf_rd_p=c6fa5af0-ec7c-40de-8332-fd1421de4244&pd_rd_r=58786171-de0f-4fe1-a2df-ee335d6715ee&pd_rd_w=KND7A&pd_rd_wg=kIr5z&ref_=pd_gw_unk') == 'B07PHPXHQS'
10-
assert get_asin('https://www.amazon.es/dp/B07PKW4CKF') == 'B07PKW4CKF'
11-
assert get_asin('https://www.amazon.es/dp/B07PKW4CKF?_encoding=UTF8&ref_=pocs_dp_m_sp_multi_c_more_nooffers_B08D1G2XVX') == 'B07PKW4CKF'
7+
class TestTools(unittest.TestCase):
8+
def test_get_asin(self):
9+
self.assertEqual(get_asin("B01N5IB20Q"), "B01N5IB20Q")
10+
self.assertEqual(
11+
get_asin("https://www.amazon.es/gp/product/B07PHPXHQS"), "B07PHPXHQS"
12+
)
13+
self.assertEqual(
14+
get_asin(
15+
"https://www.amazon.es/gp/product/B07PHPXHQS?pf_rd_r3FXDZDV1W6KY83KEE2Z"
16+
"4&pf_rd_p=c6fa5af0-ec7c-40de-8332-fd1421de4244&pd_rd_r=58786171-de0f-4"
17+
"fe1-a2df-ee335d6715ee&pd_rd_w=KND7A&pd_rd_wg=kIr5z&ref_=pd_gw_unk"
18+
),
19+
"B07PHPXHQS",
20+
)
21+
self.assertEqual(get_asin("https://www.amazon.es/dp/B07PKW4CKF"), "B07PKW4CKF")
22+
self.assertEqual(
23+
get_asin(
24+
"https://www.amazon.es/dp/B07PKW4CKF?_encoding=UTF8&ref_=pocs_dp_m_sp_m"
25+
"ulti_c_more_nooffers_B08D1G2XVX"
26+
),
27+
"B07PKW4CKF",
28+
)
29+
30+
def test_asin_not_found(self):
31+
with self.assertRaises(AsinNotFoundException):
32+
get_asin("https://www.amazon.es/gp/")
1233

13-
with pytest.raises(AsinNotFoundException):
14-
get_asin('https://www.amazon.es/gp/')
15-
with pytest.raises(AsinNotFoundException):
16-
get_asin('this is not even a URL')
34+
with self.assertRaises(AsinNotFoundException):
35+
get_asin("this is not even a URL")

0 commit comments

Comments
 (0)