示例#1
0
def create_bid(new_bid: Bid, auction_id):
    '''Create a Bid in the database'''
    query_string = {'_id': auction_id}
    auct = Auction.from_dict(read_auction_by_id(auction_id))
    prod = Product.from_dict(read_product_by_id(auct.get_item_id()))
    if new_bid.amount >= prod.get_start_bid():
        bid_list = auct.get_bids()
        if any(d['bidder_id'] == new_bid.get_bidder_id() for d in bid_list):
            for bid in bid_list:
                if bid['bidder_id'] == new_bid.get_bidder_id():
                    ind = bid_list.index(bid)
                    bid_list[ind] = new_bid.to_dict()
        else:
            bid_list.append(new_bid.to_dict())
        try:
            auctions.update_one(query_string, {'$set': {'bids': bid_list}})
            op_success = new_bid
            _log.info('Added new bid to auction %s', auction_id)
        except:
            op_success = None
            _log.info('Could not add new bid to auction %s', auction_id)
    else:
        op_success = None
        _log.info('Could not add new bid to auction %s', auction_id)
    return op_success
class BidTestSuite(unittest.TestCase):
    '''Test suite for User class'''
    bid = None

    def setUp(self):
        self.bid = Bid(-1, -1, 0)

    def tearDown(self):
        self.bid = None

    @classmethod
    def setUpClass(cls):
        cls.bid = Bid()

    @classmethod
    def tearDownClass(cls):
        cls.bid = None

    def test_get_id(self):
        '''Test for the get_id function.'''
        self.assertEqual(self.bid._id, self.bid.get_id())

    def test_get_ammount(self):
        '''Test for the get_amount function.'''
        self.assertEqual(self.bid.amount, self.bid.get_amount())

    def test_get_bidder(self):
        '''Test for the get_bidder function.'''
        self.assertEqual(self.bid.bidder_id, self.bid.get_bidder_id())

    def test_get_item(self):
        '''Test for the get_item function.'''
        self.assertEqual(self.bid.item_id, self.bid.get_item_id())

    def test_set_id(self):
        '''Test for the set_id function.'''
        self.bid.set_id(10)
        self.assertEqual(10, self.bid._id)

    def test_to_dict(self):
        '''Test for the to_dict function.'''
        self.bid = Bid(1, 1, 1)
        self.assertIs(type(self.bid.to_dict()), dict)

    def test_from_dict(self):
        '''Test for the from_dict function.'''
        test_dict = {'bidder_id': 1, 'item_id': 1, 'amount': 1}
        self.bid = Bid().from_dict(test_dict)
        self.assertIs(type(self.bid), Bid)