当前位置: 首页>>代码示例>>Python>>正文


Python AmazonAPI.cart_clear方法代码示例

本文整理汇总了Python中amazon.api.AmazonAPI.cart_clear方法的典型用法代码示例。如果您正苦于以下问题:Python AmazonAPI.cart_clear方法的具体用法?Python AmazonAPI.cart_clear怎么用?Python AmazonAPI.cart_clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在amazon.api.AmazonAPI的用法示例。


在下文中一共展示了AmazonAPI.cart_clear方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestAmazonCart

# 需要导入模块: from amazon.api import AmazonAPI [as 别名]
# 或者: from amazon.api.AmazonAPI import cart_clear [as 别名]
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
#.........这里部分代码省略.........
开发者ID:fmartingr,项目名称:python-amazon-simple-product-api,代码行数:103,代码来源:tests.py


注:本文中的amazon.api.AmazonAPI.cart_clear方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。