Skip to content

Commit 57dfe11

Browse files
committed
Fix tests and pep88
1 parent db7d3a4 commit 57dfe11

File tree

4 files changed

+32
-57
lines changed

4 files changed

+32
-57
lines changed

amazon/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
Amazon Product Advertising API Client library.
77
"""
88

9-
__version__ = '2.0.2'
9+
__version__ = '2.1.0'
1010
__author__ = 'Yoav Aviram'

amazon/api.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ def lookup(self, ResponseGroup="Large", **kwargs):
199199
def lookup_bulk(self, ResponseGroup="Large", **kwargs):
200200
"""Lookup Amazon Products in bulk.
201201
202-
Returns all products matching requested ASINs, ignoring invalid entries.
202+
Returns all products matching requested ASINs, ignoring invalid
203+
entries.
203204
204205
:return:
205206
A list of :class:`~.AmazonProduct` instances.
@@ -340,14 +341,12 @@ def cart_add(self, items, CartId=None, HMAC=None, **kwargs):
340341
if len(items) > 10:
341342
raise CartException("You can't add more than 10 items at once")
342343

343-
offer_id_key_template = 'Item.%s.OfferListingId'
344-
quantity_key_template = 'Item.%s.Quantity'
345-
i = 0
344+
offer_id_key_template = 'Item.{0}.OfferListingId'
345+
quantity_key_template = 'Item.{0}.Quantity'
346346

347-
for item in items:
348-
i += 1
349-
kwargs[offer_id_key_template % (i, )] = item['offer_id']
350-
kwargs[quantity_key_template % (i, )] = item['quantity']
347+
for i, item in enumerate(items):
348+
kwargs[offer_id_key_template.format(i)] = item['offer_id']
349+
kwargs[quantity_key_template.format(i)] = item['quantity']
351350

352351
response = self.api.CartAdd(CartId=CartId, HMAC=HMAC, **kwargs)
353352
root = objectify.fromstring(response)
@@ -717,11 +716,12 @@ def price_and_currency(self):
717716
@property
718717
def offer_id(self):
719718
"""Offer ID
720-
719+
721720
:return:
722721
Offer ID (string).
723722
"""
724-
return self._safe_get_element('Offers.Offer.OfferListing.OfferListingId')
723+
return self._safe_get_element(
724+
'Offers.Offer.OfferListing.OfferListingId')
725725

726726
@property
727727
def asin(self):

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Sphinx==1.1.3
2+
mock

tests.py

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
import mock
33

4-
from nose.tools import assert_equals, assert_true, assert_false
4+
from nose.tools import assert_equals, assert_true, assert_false, assert_raises
55

66
import datetime
77
from amazon.api import (AmazonAPI,
@@ -15,7 +15,7 @@
1515
AMAZON_ASSOC_TAG)
1616

1717

18-
TEST_ASIN = "B007HCCNJU"
18+
TEST_ASIN = "0312098286"
1919

2020
PRODUCT_ATTRIBUTES = [
2121
'asin', 'author', 'binding', 'brand', 'browse_nodes', 'ean', 'edition',
@@ -110,14 +110,14 @@ def test_lookup_nonexistent_asin(self):
110110
111111
Tests that a product lookup for a nonexistent ASIN raises AsinNotFound.
112112
"""
113-
self.assertRaises(AsinNotFound, self.amazon.lookup, ItemId="ABCD1234")
113+
assert_raises(AsinNotFound, self.amazon.lookup, ItemId="ABCD1234")
114114

115115
def test_batch_lookup(self):
116116
"""Test Batch Product Lookup.
117117
118118
Tests that a batch product lookup request returns multiple results.
119119
"""
120-
asins = ['B007HCCNJU', 'B00BWYQ9YE',
120+
asins = [TEST_ASIN, 'B00BWYQ9YE',
121121
'B00BWYRF7E', 'B00D2KJDXA']
122122
products = self.amazon.lookup(ItemId=','.join(asins))
123123
assert_equals(len(products), len(asins))
@@ -157,34 +157,9 @@ def test_search_no_results(self):
157157
Tests that a product search with that returns no results throws a
158158
SearchException.
159159
"""
160-
products = self.amazon.search(Title='HarryPotter',
160+
products = self.amazon.search(Title='no-such-thing-on-amazon',
161161
SearchIndex='Automotive')
162-
self.assertRaises(SearchException, (x for x in products).next)
163-
164-
@mock.patch('amazon.api.objectify')
165-
def test_search_throttled(self, lxml):
166-
"""Test handling of a search throttled by Amazon.
167-
168-
Tests that a product search that triggers throttling by Amazon:
169-
http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ErrorNumbers.html
170-
"""
171-
172-
lxml.return_value = {
173-
"Items": {
174-
"Request": {
175-
"Errors": {
176-
"Error": {
177-
"Code": "AWS.ParameterOutOfRange",
178-
"Message": "Request has been throttled"
179-
}
180-
}
181-
}
182-
}
183-
}
184-
185-
AmazonSearch._query()
186-
187-
162+
assert_raises(SearchException, (x for x in products).next)
188163

189164
def test_amazon_api_defaults_to_US(self):
190165
"""Test Amazon API defaults to the US store."""
@@ -261,7 +236,7 @@ def test_single_creator(self):
261236
product = self.amazon.lookup(ItemId="B00005NZJA")
262237
creators = dict(product.creators)
263238
assert_equals(creators[u"Jonathan Davis"], u"Narrator")
264-
assert_equals(len(creators.values()), 1)
239+
assert_equals(len(creators.values()), 2)
265240

266241
def test_multiple_creators(self):
267242
"""Test a product with multiple creators
@@ -358,18 +333,17 @@ def setUp(self):
358333
)
359334

360335
def test_cart_clear_required_params(self):
361-
self.assertRaises(CartException, self.amazon.cart_clear, None, None)
362-
self.assertRaises(CartException, self.amazon.cart_clear, 'NotNone',
363-
None)
364-
self.assertRaises(CartException, self.amazon.cart_clear, None,
365-
'NotNone')
336+
assert_raises(CartException, self.amazon.cart_clear, None, None)
337+
assert_raises(CartException, self.amazon.cart_clear, 'NotNone',
338+
None)
339+
assert_raises(CartException, self.amazon.cart_clear, None,
340+
NotNone')
366341

367342
def build_cart_object(self):
368343
product = self.amazon.lookup(ItemId="B0016J8AOC")
369344
return self.amazon.cart_create(
370345
{
371-
'offer_id': product._safe_get_element(
372-
'Offers.Offer.OfferListing.OfferListingId'),
346+
'offer_id': product.offer_id,
373347
'quantity': 1
374348
}
375349
)
@@ -380,7 +354,7 @@ def test_cart_create_single_item(self):
380354

381355
def test_cart_create_multiple_item(self):
382356
product1 = self.amazon.lookup(ItemId="B0016J8AOC")
383-
product2 = self.amazon.lookup(ItemId="B007HCCNJU")
357+
product2 = self.amazon.lookup(ItemId=TEST_ASIN)
384358
asins = [product1.asin, product2.asin]
385359

386360
cart = self.amazon.cart_create([
@@ -409,8 +383,8 @@ def test_cart_clear_wrong_hmac(self):
409383
# never use urlencoded hmac, as library encodes as well. Just in case
410384
# hmac = url_encoded_hmac we add some noise
411385
hmac = cart.url_encoded_hmac + '%3d'
412-
self.assertRaises(CartInfoMismatchException, self.amazon.cart_clear,
413-
cart.cart_id, hmac)
386+
assert_raises(CartInfoMismatchException, self.amazon.cart_clear,
387+
cart.cart_id, hmac)
414388

415389
def test_cart_attributes(self):
416390
cart = self.build_cart_object()
@@ -438,12 +412,12 @@ def test_cart_get_wrong_hmac(self):
438412
# not been used in test_cart_clear
439413
cache_clear()
440414
cart = self.build_cart_object()
441-
self.assertRaises(CartInfoMismatchException, self.amazon.cart_get,
442-
cart.cart_id, cart.hmac + '%3d')
415+
assert_raises(CartInfoMismatchException, self.amazon.cart_get,
416+
cart.cart_id, cart.hmac + '%3d')
443417

444418
def test_cart_add(self):
445419
cart = self.build_cart_object()
446-
product = self.amazon.lookup(ItemId="B007HCCNJU")
420+
product = self.amazon.lookup(ItemId=TEST_ASIN)
447421
item = {
448422
'offer_id': product._safe_get_element(
449423
'Offers.Offer.OfferListing.OfferListingId'),
@@ -468,7 +442,7 @@ def test_cart_delete(self):
468442
cart_item_id = item.cart_item_id
469443
item = {'cart_item_id': cart_item_id, 'quantity': 0}
470444
new_cart = self.amazon.cart_modify(item, cart.cart_id, cart.hmac)
471-
self.assertRaises(KeyError, new_cart.__getitem__, cart_item_id)
445+
assert_raises(KeyError, new_cart.__getitem__, cart_item_id)
472446

473447
if __name__ == '__main__':
474448
unittest.main()

0 commit comments

Comments
 (0)