def test_update_supplier_product(self): """ Update an suppliers product """ suppliers = Supplier.all() self.assertEqual(suppliers, []) product = self._create_product() supplier = self._create_supplier(products=[product]) supplier.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(supplier.id, 1) suppliers = Supplier.all() self.assertEqual(len(suppliers), 1) # Fetch it back supplier = Supplier.find(supplier.id) old_product = supplier.products[0] self.assertEqual(old_product.desc, product.desc) old_product.desc = "XX" supplier.save() # Fetch it back again supplier = Supplier.find(supplier.id) product = supplier.products[0] self.assertEqual(product.desc, "XX")
def test_delete_a_supplier(self): """ Delete a Supplier """ supplier = SupplierFactory() supplier.create() self.assertEqual(len(Supplier.all()), 1) # delete the supplier and make sure it isn't in the database supplier.delete() self.assertEqual(len(Supplier.all()), 0)
def test_delete_a_supplier_with_wrong_id(self): """ Delete a Supplier with wrong Id""" supplier = SupplierFactory() supplier.save() self.assertEqual(len(Supplier.all()), 1) # delete the supplier with wrong Id and make sure nothing happens supplier.id = "0" supplier.delete() self.assertEqual(len(Supplier.all()), 1)
def test_add_a_supplier(self): """ Create a Supplier and add it to the database """ suppliers = Supplier.all() self.assertEqual(suppliers, []) supplier = self._create_supplier() supplier.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(supplier.id, 1) suppliers = Supplier.all() self.assertEqual(len(suppliers), 1)
def get(self, product_id): """ Recommend a Supplier This endpoint will recommend top 1 highly-rated supplier based on a given product """ app.logger.info( 'Recommend suppliers containing product with id %s in their products', product_id) product_id = int(product_id) # retrieve all suppliers including this product first suppliers = [ supplier for supplier in Supplier.all() if product_id in supplier.products and supplier.is_active == True ] # get top 1 rated supplier, None if suppliers is empty if suppliers: res_supplier = max(suppliers, key=lambda x: x.rating).serialize() app.logger.info('Recommended supplier is: {}'.format(res_supplier)) else: res_supplier = [] app.logger.info("No Recommended supplier!!") return res_supplier, status.HTTP_200_OK
def test_add_a_supplier(self): """ Test create a supplier and add it to the database """ suppliers = Supplier.all() self.assertEqual(len(suppliers), 0) supplier = Supplier(supplierName="Walmart", address="NYC", averageRating=5, productIdList=['1', '2', '3']) self.assertTrue(supplier != None) self.assertEqual(supplier.id, None) supplier.save() # Asert that it was assigned an id and shows up in the database # self.assertEqual(supplier.id, 1) suppliers = Supplier.all() self.assertEqual(len(suppliers), 1) self.assertNotEqual(supplier.id, None)
def test_add_a_supplier(self): """ Create a supplier and add it to the database """ suppliers = Supplier.all() self.assertEqual(suppliers, []) supplier = Supplier("supplier1", 2, True, [1, 2, 3], 8.5) self.assertNotEqual(supplier, None) self.assertEqual(supplier.id, None) supplier.save() # Asert that it was assigned an id and shows up in the database self.assertNotEqual(supplier.id, None) suppliers = Supplier.all() self.assertEqual(len(suppliers), 1) self.assertEqual(suppliers[0].name, "supplier1") self.assertEqual(suppliers[0].like_count, 2) self.assertEqual(suppliers[0].is_active, True) self.assertEqual(suppliers[0].products, [1, 2, 3]) self.assertEqual(suppliers[0].rating, 8.5)
def get(self): """ Returns all of the suppliers """ app.logger.info('Request to list Suppliers...') name = request.args.get('name') is_active = request.args.get('is_active') rating = request.args.get('rating') product_id = request.args.get('product_id') like_count = request.args.get('like_count') if name: app.logger.info('Find suppliers by name: %s', name) suppliers = Supplier.find_by_name(name) elif like_count: app.logger.info('Find suppliers with rating greater than: %s', rating) like_count = int(like_count) suppliers = Supplier.find_by_greater("like_count", like_count) elif is_active: app.logger.info('Find suppliers by is_active: %s', is_active) is_active = (is_active == 'true') suppliers = Supplier.find_by_is_active(is_active) elif rating: app.logger.info('Find suppliers with rating greater than: %s', rating) rating = float(rating) suppliers = Supplier.find_by_greater("rating", rating) elif product_id: app.logger.info( 'Find suppliers containing product with id %s in their products', product_id) product_id = int(product_id) suppliers = [ supplier for supplier in Supplier.all() if product_id in supplier.products ] else: app.logger.info('Find all suppliers') suppliers = Supplier.all() app.logger.info('[%s] Suppliers returned', len(suppliers)) results = [supplier.serialize() for supplier in suppliers] app.logger.info("Returning %d suppliers", len(results)) return results, status.HTTP_200_OK
def test_update_a_supplier_with_wrong_id(self): """ Update a Supplier with wrong Id""" supplier = SupplierFactory() supplier.id = "0" self.assertEqual(supplier.id, "0") # Call save() with wrong Id, will call update(), nothing will happen supplier.save() # Fetch suppliers and make sure nothing exists in DB suppliers = Supplier.all() self.assertEqual(len(suppliers), 0)
def delete_all_suppliers(): """ Returns IDs of the Suppliers """ app.logger.info("Request for Supplier list") suppliers = [] id = request.args.get("id") if id: suppliers = Supplier.find(id) else: suppliers = Supplier.all() results = [supplier.delete() for supplier in suppliers] return make_response("", status.HTTP_204_NO_CONTENT)
def test_create_a_supplier(self): """ Create a supplier and assert that it exists """ supplier = SupplierFactory() supplier.create() self.assertIsNotNone(supplier.id) suppliers_db = Supplier.all() self.assertEqual(len(suppliers_db), 1) self.assertEqual(supplier.id, suppliers_db[0].id) self.assertEqual(supplier.name, suppliers_db[0].name) self.assertEqual(supplier.like_count, suppliers_db[0].like_count) self.assertEqual(supplier.is_active, suppliers_db[0].is_active) self.assertEqual(supplier.products, suppliers_db[0].products) self.assertEqual(supplier.rating, suppliers_db[0].rating)
def test_update_a_supplier(self): """ Update a Supplier """ supplier = SupplierFactory() supplier.save() self.assertNotEqual(supplier.id, None) # Change it an save it supplier.rating = 9.0 supplier.save() # Fetch it back and make sure the id hasn't changed # but the data did change suppliers = Supplier.all() self.assertEqual(len(suppliers), 1) self.assertEqual(suppliers[0].rating, 9.0) self.assertEqual(suppliers[0].name, supplier.name)
def test_list_all_supplier(self): """ Test return a list of suppliers """ Supplier(supplierName="Walmart", address="NYC", averageRating=5, productIdList=['1', '2', '3']).save() Supplier(supplierName="Costco", address="SF", averageRating=2, productIdList=['1', '3', '4']).save() suppliers = Supplier.all() self.assertEqual(len(suppliers), 2) self.assertEqual(suppliers[0].supplierName, 'Walmart') self.assertEqual(suppliers[1].supplierName, 'Costco')
def test_add_supplier_product(self): """ Create a Supplier with a product and add it to the database """ suppliers = Supplier.all() self.assertEqual(suppliers, []) supplier = self._create_supplier() product = self._create_product() supplier.products.append(product) supplier.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(supplier.id, 1) suppliers = Supplier.all() self.assertEqual(len(suppliers), 1) new_supplier = Supplier.find(supplier.id) self.assertEqual(supplier.products[0].name, product.name) product2 = self._create_product() supplier.products.append(product2) supplier.save() new_supplier = Supplier.find(supplier.id) self.assertEqual(len(supplier.products), 2) self.assertEqual(supplier.products[1].name, product2.name)
def list_suppliers(): """ Returns all of the suppliers """ app.logger.info("Request for Supplier list") suppliers = [] name = request.args.get("name") category = request.args.get("category") if name: suppliers = Supplier.find_by_name(name) elif category: suppliers = Supplier.find_by_category(category) else: suppliers = Supplier.all() results = [supplier.serialize() for supplier in suppliers] return make_response(jsonify(results), status.HTTP_200_OK)
def test_find_supplier(self): """ Find a Supplier by ID """ suppliers = SupplierFactory.create_batch(3) for supplier in suppliers: supplier.create() logging.debug(suppliers) # make sure they got saved self.assertEqual(len(Supplier.all()), 3) # find the 2nd supplier in the list supplier = Supplier.find(suppliers[1].id) self.assertIsNot(supplier, None) self.assertEqual(supplier.id, suppliers[1].id) self.assertEqual(supplier.name, suppliers[1].name) self.assertEqual(supplier.category, suppliers[1].category) self.assertEqual(supplier.email, suppliers[1].email) self.assertEqual(supplier.address, suppliers[1].address) self.assertEqual(supplier.phone_number, suppliers[1].phone_number)
def test_update_a_supplier(self): """ Test update a supplier """ supplier = Supplier(supplierName="Walmart", address="NYC", averageRating=5, productIdList=['1', '2', '3']) supplier.save() self.assertNotEqual(supplier.id, None) old_id = supplier.id # Change it an save it supplier.supplierName = "Costco" supplier.save() self.assertEqual(supplier.id, old_id) # Fetch it back and make sure the id hasn't changed # but the data did change suppliers = Supplier.all() self.assertEqual(len(suppliers), 1) self.assertEqual(suppliers[0].supplierName, "Costco")
def test_update_a_supplier(self): """ Update a Supplier """ supplier = SupplierFactory() logging.debug(supplier) supplier.create() logging.debug(supplier) self.assertEqual(supplier.id, 1) # Change it an save it supplier.category = "suppliers" original_id = supplier.id supplier.save() self.assertEqual(supplier.id, original_id) self.assertEqual(supplier.category, "suppliers") # Fetch it back and make sure the id hasn't changed # but the data did change suppliers = Supplier.all() self.assertEqual(len(suppliers), 1) self.assertEqual(suppliers[0].id, 1) self.assertEqual(suppliers[0].category, "suppliers")
def get(self): """ Returns all of the Suppliers """ app.logger.info('Request to list Suppliers...') suppliers = [] args = supplier_args.parse_args() if args['category']: app.logger.info('Filtering by category: %s', args['category']) suppliers = Supplier.find_by_category(args['category']) elif args['name']: app.logger.info('Filtering by name: %s', args['name']) suppliers = Supplier.find_by_name(args['name']) elif args['preferred'] is not None: app.logger.info('Filtering by preferred: %s', args['preferred']) suppliers = Supplier.find_by_preferred(args['preferred']) else: suppliers = Supplier.all() app.logger.info('[%s] Suppliers returned', len(suppliers)) results = [supplier.serialize() for supplier in suppliers] return results, status.HTTP_200_OK
def get(self): """ Returns all of the Suppliers """ app.logger.info('Request to list Suppliers...') suppliers = [] # args = supplier_args.parse_args() rating = request.args.get('rating') averageRating = request.args.get('averageRating') if rating: app.logger.info('Filtering by rating: %s', rating) suppliers = Supplier.find_by_rating(rating) if len(suppliers) == 0: return '', status.HTTP_400_BAD_REQUEST elif averageRating: app.logger.info('Filtering by rating: %s', averageRating) suppliers = Supplier.find_by_equals_to_rating(averageRating) if len(suppliers) == 0: return '', status.HTTP_400_BAD_REQUEST else: suppliers = Supplier.all() app.logger.info('[%s] suppliers returned', len(suppliers)) results = [json.loads(supplier.to_json()) for supplier in suppliers] return results, status.HTTP_200_OK