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
def auctions_with_id(auction_id): '''This is for requests associated with an Auction ID''' if request.method == 'POST': required_fields = ['bidder_id', 'item_id', 'amount'] input_dict = request.get_json(force=True) _log.info('POST request recieved with body %s', input_dict) try: query_id = int(auction_id) except TypeError as err: input_dict = {} if all(field in input_dict for field in required_fields): new_bidder_id = input_dict['bidder_id'] new_item_id = input_dict['item_id'] new_amount = input_dict['amount'] new_bid = Bid(new_bidder_id, new_item_id, new_amount) create_bid(new_bid, query_id) # response = make_response() return jsonify(new_bid.to_dict()), 201 else: # response = make_response() return request.json, 400 elif request.method == 'PUT': input_dict = request.get_json(force=True) _log.debug(input_dict) target_auction = int(auction_id) if 'numOfDays' in input_dict: updated_auction = auction_start(target_auction, input_dict['numOfDays']) return updated_auction, 200 elif 'bidder_id' in input_dict: auction_end(input_dict['auction_id'], input_dict['bidder_id']) return 'Nothing to say', 204 else: return 'Invalid Request', 400 else: pass
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)
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)
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 setUpClass(cls): cls.bid = Bid()
def setUp(self): self.bid = Bid(-1, -1, 0)
util.insert_one({'_id': 'USERID_COUNTER', 'count': 0}) util.insert_one({'_id': 'AUCTIONID_COUNTER', 'count': 0}) util.insert_one({'_id': 'PRODUCTID_COUNTER', 'count': 0}) users.create_index('username', unique=True) # Bidder bidder = Bidder('bidder', 'password') bidder = create_bidder(bidder) # manager manager = Employee('manager', 'password', 'Manager') create_employee(manager) # curator curator = Employee('curator', 'password', 'Curator') create_employee(curator) # auctioneer auctioneer = Employee('auctioneer', 'password', 'Auctioneer') create_employee(auctioneer) # product product = Product('Egyptian Sarcophagus', 'Egyptian funeral receptacle for a corpse.', 1000) create_product(product) # Bid bid = Bid(bidder.get_id(), product.get_id(), 2000) # auction auction = Auction(product.get_id()) create_auction(auction) create_bid(bid, auction.get_id())