class TestAmazonCart(unittest.TestCase): def setUp(self): self.amazon = AmazonAPI( AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, CacheReader=cache_reader, CacheWriter=cache_writer, MaxQPS=0.5 ) def test_cart_clear_required_params(self): self.assertRaises(CartException, self.amazon.cart_clear, None, None) self.assertRaises(CartException, self.amazon.cart_clear, 'NotNone', None) self.assertRaises(CartException, self.amazon.cart_clear, None, 'NotNone') def build_cart_object(self): product = self.amazon.lookup(ItemId="B0016J8AOC") return self.amazon.cart_create( { 'offer_id': product._safe_get_element('Offers.Offer.OfferListing.OfferListingId'), 'quantity': 1 } ) def test_cart_create_single_item(self): cart = self.build_cart_object() assert_equals(len(cart), 1) def test_cart_create_multiple_item(self): product1 = self.amazon.lookup(ItemId="B0016J8AOC") product2 = self.amazon.lookup(ItemId="B007HCCNJU") asins = [product1.asin, product2.asin] cart = self.amazon.cart_create([ { 'offer_id': product1._safe_get_element('Offers.Offer.OfferListing.OfferListingId'), 'quantity': 1 }, { 'offer_id': product2._safe_get_element('Offers.Offer.OfferListing.OfferListingId'), 'quantity': 1 }, ]) assert_equals(len(cart), 2) for item in cart: assert_true(item.asin in asins) def test_cart_clear(self): cart = self.build_cart_object() new_cart = self.amazon.cart_clear(cart.cart_id, cart.hmac) assert_true(new_cart._safe_get_element('Cart.Request.IsValid')) def test_cart_clear_wrong_hmac(self): cart = self.build_cart_object() # never use urlencoded hmac, as library encodes as well. Just in case hmac = url_encoded_hmac we add some noise hmac = cart.url_encoded_hmac + '%3d' self.assertRaises(CartInfoMismatchException, self.amazon.cart_clear, cart.cart_id, hmac) def test_cart_attributes(self): cart = self.build_cart_object() for attribute in CART_ATTRIBUTES: getattr(cart, attribute) def test_cart_item_attributes(self): cart = self.build_cart_object() for item in cart: for attribute in CART_ITEM_ATTRIBUTES: getattr(item, attribute) def test_cart_get(self): # We need to flush the cache here so we will get a new cart that has not been used in test_cart_clear cache_clear() cart = self.build_cart_object() fetched_cart = self.amazon.cart_get(cart.cart_id, cart.hmac) assert_equals(fetched_cart.cart_id, cart.cart_id) assert_equals(len(fetched_cart), len(cart)) def test_cart_get_wrong_hmac(self): # We need to flush the cache here so we will get a new cart that has not been used in test_cart_clear cache_clear() cart = self.build_cart_object() self.assertRaises(CartInfoMismatchException, self.amazon.cart_get, cart.cart_id, cart.hmac + '%3d') def test_cart_add(self): cart = self.build_cart_object() product = self.amazon.lookup(ItemId="B007HCCNJU") item = { 'offer_id': product._safe_get_element('Offers.Offer.OfferListing.OfferListingId'), 'quantity': 1 } new_cart = self.amazon.cart_add(item, cart.cart_id, cart.hmac) assert_true(len(new_cart) > len(cart)) def test_cart_modify(self): cart = self.build_cart_object() cart_item_id = None for item in cart: cart_item_id = item.cart_item_id item = {'cart_item_id': cart_item_id, 'quantity': 3} new_cart = self.amazon.cart_modify(item, cart.cart_id, cart.hmac) assert_equals(new_cart[cart_item_id].quantity, '3') def test_cart_delete(self): cart = self.build_cart_object() cart_item_id = None for item in cart: cart_item_id = item.cart_item_id item = {'cart_item_id': cart_item_id, 'quantity': 0} new_cart = self.amazon.cart_modify(item, cart.cart_id, cart.hmac) self.assertRaises(KeyError, new_cart.__getitem__, cart_item_id)
class TestAmazonCart(unittest.TestCase): def setUp(self): self.amazon = AmazonAPI( _AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY, _AMAZON_ASSOC_TAG, CacheReader=cache_reader, CacheWriter=cache_writer, MaxQPS=0.5 ) def test_cart_clear_required_params(self): assert_raises(CartException, self.amazon.cart_clear, None, None) assert_raises(CartException, self.amazon.cart_clear, 'NotNone', None) assert_raises(CartException, self.amazon.cart_clear, None, 'NotNone') def build_cart_object(self): product = self.amazon.lookup(ItemId="B0016J8AOC") return self.amazon.cart_create( { 'offer_id': product.offer_id, 'quantity': 1 } ) def test_cart_create_single_item(self): cart = self.build_cart_object() assert_equals(len(cart), 1) def test_cart_create_multiple_item(self): product1 = self.amazon.lookup(ItemId="B0016J8AOC") product2 = self.amazon.lookup(ItemId=TEST_ASIN) asins = [product1.asin, product2.asin] cart = self.amazon.cart_create([ { 'offer_id': product1._safe_get_element( 'Offers.Offer.OfferListing.OfferListingId'), 'quantity': 1 }, { 'offer_id': product2._safe_get_element( 'Offers.Offer.OfferListing.OfferListingId'), 'quantity': 1 }, ]) assert_equals(len(cart), 2) for item in cart: assert_true(item.asin in asins) def test_cart_clear(self): cart = self.build_cart_object() new_cart = self.amazon.cart_clear(cart.cart_id, cart.hmac) assert_true(new_cart._safe_get_element('Cart.Request.IsValid')) def test_cart_clear_wrong_hmac(self): cart = self.build_cart_object() # never use urlencoded hmac, as library encodes as well. Just in case # hmac = url_encoded_hmac we add some noise hmac = cart.url_encoded_hmac + '%3d' assert_raises(CartInfoMismatchException, self.amazon.cart_clear, cart.cart_id, hmac) def test_cart_attributes(self): cart = self.build_cart_object() for attribute in CART_ATTRIBUTES: getattr(cart, attribute) def test_cart_item_attributes(self): cart = self.build_cart_object() for item in cart: for attribute in CART_ITEM_ATTRIBUTES: getattr(item, attribute) def test_cart_get(self): # We need to flush the cache here so we will get a new cart that has # not been used in test_cart_clear cache_clear() cart = self.build_cart_object() fetched_cart = self.amazon.cart_get(cart.cart_id, cart.hmac) assert_equals(fetched_cart.cart_id, cart.cart_id) assert_equals(len(fetched_cart), len(cart)) def test_cart_get_wrong_hmac(self): # We need to flush the cache here so we will get a new cart that has # not been used in test_cart_clear cache_clear() cart = self.build_cart_object() assert_raises(CartInfoMismatchException, self.amazon.cart_get, cart.cart_id, cart.hmac + '%3d') def test_cart_add(self): cart = self.build_cart_object() product = self.amazon.lookup(ItemId=TEST_ASIN) item = { 'offer_id': product._safe_get_element( 'Offers.Offer.OfferListing.OfferListingId'), 'quantity': 1 } new_cart = self.amazon.cart_add(item, cart.cart_id, cart.hmac) assert_true(len(new_cart) > len(cart)) def test_cart_modify(self): cart = self.build_cart_object() cart_item_id = None for item in cart: cart_item_id = item.cart_item_id item = {'cart_item_id': cart_item_id, 'quantity': 3} new_cart = self.amazon.cart_modify(item, cart.cart_id, cart.hmac) assert_equals(new_cart[cart_item_id].quantity, '3') def test_cart_delete(self): cart = self.build_cart_object() cart_item_id = None for item in cart: cart_item_id = item.cart_item_id item = {'cart_item_id': cart_item_id, 'quantity': 0} new_cart = self.amazon.cart_modify(item, cart.cart_id, cart.hmac) assert_raises(KeyError, new_cart.__getitem__, cart_item_id)