def create_product(new_product: Product): '''Create a Product in the database''' new_product.set_id(_get_product_id_counter()) try: products.insert_one(new_product.to_dict()) op_success = new_product except pymongo.errors.DuplicateKeyError as err: _log.error(err) op_success = None _log.info('Added %s product. ID: %s.', new_product.get_name(), new_product.get_id()) return op_success
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 products_main(): '''This is the main /products routes.''' required_fields = ['name', 'description', 'start_bid'] if request.method == 'POST': input_dict = request.get_json(force=True) _log.debug('Product POST request received with body %s', input_dict) if all(field in input_dict for field in required_fields): name = input_dict['name'] description = input_dict['description'] start = input_dict['start_bid'] new_product = Product(name, description, start) create_product(new_product) return jsonify(new_product.to_dict()), 201 else: return request.json, 400 elif request.method == 'GET': _log.debug('GET request for products') _log.debug(db.read_all_products()) return jsonify(db.read_all_products()), 200 else: pass
class ProductTestSuite(unittest.TestCase): '''Test suite for Product class''' product = None def setUp(self): self.product = Product('name', 'description', 0) def tearDown(self): self.product = None @classmethod def setUpClass(cls): cls.product = Product() @classmethod def tearDownClass(cls): cls.product = None def test_get_id(self): '''Test for the get_id function.''' self.assertEqual(self.product._id, self.product.get_id()) def test_get_name(self): '''Test for the get_name function.''' self.assertEqual(self.product.name, self.product.get_name()) def test_get_description(self): '''Test for the get_description function.''' self.assertEqual(self.product.description, self.product.get_description()) def test_get_start_bid(self): '''Test for the get_start_bid function.''' self.assertEqual(self.product.start_bid, self.product.get_start_bid()) def test_get_status(self): '''Test for the get_status function.''' self.assertEqual(self.product.status, self.product.get_status()) def test_set_id(self): '''Test for the set_id function.''' self.product.set_id(10) self.assertEqual(10, self.product._id) def test_set_name(self): '''Test for the set_name function.''' self.product.set_name("named") self.assertEqual(self.product.name, "named") def test_set_description(self): '''Test for the set_description function.''' self.product.set_description("desc") self.assertEqual(self.product.description, "desc") def test_set_start_bid(self): '''Test for the start_bid function.''' self.product.set_start_bid(1) self.assertEqual(self.product.start_bid, 1) def test_set_status(self): '''Test for the set_status function.''' self.product.set_status("Approved") self.assertEqual(self.product.status, "Approved") def test_to_dict(self): '''Test for the t0_dict function.''' self.product = Product("name", "descript", 1) self.assertIs(type(self.product.to_dict()), dict) def test_from_dict(self): '''Test for the from_dict function.''' test_dict = {'name': 'name', 'description': 'desc', 'start_bid': 1} self.product = Product().from_dict(test_dict) self.assertIs(type(self.product), Product)
def test_from_dict(self): '''Test for the from_dict function.''' test_dict = {'name': 'name', 'description': 'desc', 'start_bid': 1} self.product = Product().from_dict(test_dict) self.assertIs(type(self.product), Product)
def test_to_dict(self): '''Test for the t0_dict function.''' self.product = Product("name", "descript", 1) self.assertIs(type(self.product.to_dict()), dict)
def setUpClass(cls): cls.product = Product()
def setUp(self): self.product = Product('name', 'description', 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())