def test_query_by_name(self): """ Test return a supplier given a name """ Supplier(supplierName="Walmart", address="NYC", averageRating=5, productIdList=['1', '2', '3']).save() Supplier(supplierName="Costco", address="SF", averageRating=2, productIdList=['1', '3', '4']).save() supplier = Supplier.find_by_name("Walmart") self.assertEqual(supplier.address, "NYC") self.assertEqual(supplier.averageRating, 5) self.assertEqual(supplier.productIdList, ['1', '2', '3']) nowhere = Supplier.find_by_name("NoWhere") self.assertEqual(nowhere, None)
def test_find_by_name(self): """ Find a Supplier by Name """ suppliers = SupplierFactory.create_batch(3) for supplier in suppliers: supplier.create() results = Supplier.find_by_name(suppliers[0].name) self.assertNotEqual(results, []) self.assertEqual(results[0].name, suppliers[0].name)
def test_find_by_name(self): """ Find a Supplier by Name """ Supplier("supplier1", 2, True, [1, 2, 3], 8.5).save() Supplier("supplier2", 4, False, [1, 3, 5, 7], 6.5).save() suppliers = Supplier.find_by_name("supplier1") 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(len(suppliers[0].products), 3) self.assertEqual(suppliers[0].rating, 8.5)
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 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 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