Source

ebay / tests / test_ebay.py

Full commit

from __future__ import with_statement
from ebay import transport
from ebay.transport import HTTPError
from ebay.shopping import ShoppingAPI
from ebay.exc import EbayHTTPError
from ebay.etree import ET
from nose.tools import eq_, raises
from tests import testconfig
from tests import attr, support_fileobj
from ebay.api import NS
from fudge import with_fakes, patched_context, Fake

@attr(net=1)
def test_find_and_get_item():
    shopping = ShoppingAPI(testconfig.sandbox_appid, sandbox=True)
    one_or_more = False
    already_parsed_item = False
    
    find = shopping.FindItems(QueryKeywords="truck")
    # print ET.tostring(find.tree.getroot())
    eq_( find.Ack, "Success")
    assert find.Build
    assert find.Timestamp
    assert find.Version
    assert find.ItemSearchURL
    assert find.TotalItems
    
    for item_summary in find.Item:
        one_or_more = True
        
        if already_parsed_item:
            # this is just a sanity check; we don't need to parse every item
            continue
        
        item_meta = shopping.GetSingleItem(ItemID=item_summary.ItemID)
        print str(item_meta)
        # sanity checks:
        
        eq_( item_meta.Ack, "Success")
        assert item_meta.Build
        assert item_meta.Timestamp
        assert item_meta.Version
        
        assert item_meta.Item.AutoPay is not None
        assert item_meta.Item.BestOfferEnabled is not None
        assert item_meta.Item.BidCount is not None
        assert item_meta.Item.Country is not None
        assert item_meta.Item.CurrentPrice is not None
        assert item_meta.Item.ConvertedCurrentPrice is not None
        ## not always:
        # item_meta.Item.Description
        assert item_meta.Item.EndTime is not None
        # assert item_meta.Item.GalleryURL is not None
        assert item_meta.Item.ItemID is not None
        assert item_meta.Item.Location is not None
        assert item_meta.Item.ListingType is not None
        assert item_meta.Item.PictureURL is not None
        assert item_meta.Item.Quantity is not None
        assert item_meta.Item.QuantitySold is not None
        assert item_meta.Item.PaymentAllowedSite is not None
        ## not always:
        # assert item_meta.Item.PostalCode is not None
        assert item_meta.Item.PrimaryCategoryID is not None
        assert item_meta.Item.PrimaryCategoryIDPath is not None
        assert item_meta.Item.PrimaryCategoryName is not None
        assert item_meta.Item.StartTime is not None
        assert item_meta.Item.Title is not None
        assert item_meta.Item.ViewItemURLForNaturalSearch is not None
        
        already_parsed_item = True
    
    if not one_or_more:
        raise AssertionError("FindItems() did not return any results")

class TestRetries(object):
    
    def setUp(self):
        pass
    
    @with_fakes
    def test_retry_then_success(self):
        shopping = ShoppingAPI(":appid:")
        item_meta = None
        with patched_context(transport, "urlopen", 
                Fake('urlopen',callable=True)\
                    .raises(HTTPError(None,"502","Proxy Error",None,None))\
                    .next_call()\
                    .returns(support_fileobj("GetSingleItemResponse.xml"))
        ):
            item_meta = shopping.GetSingleItem(ItemID=1234)
        
        eq_(item_meta.Item.Title, 
            u'DJ Technics Vintage Turntable P Funk 45 T-Shirt GRAY2 by Ivan Krsti\u0107')
    
    @raises(EbayHTTPError)
    @with_fakes
    def test_retry_then_failure(self):
        shopping = ShoppingAPI(":appid:", failed_req_sleep_time=0.1)
        item_meta = None
        with patched_context(transport, "urlopen",
                # always returns HTTPError :
                Fake('urlopen',callable=True)\
                            .raises(HTTPError(None,"502","Proxy Error",None,None))
        ):
            item_meta = shopping.GetSingleItem(ItemID=1234)