def test_find_by_category(self): """ Find Products by Category """ products = ProductFactory.create_batch(3) for product in products: product.create() category = products[0].category test_products = Product.find_by_category(category) self.assertEqual(test_products[0].category, category) self.assertIsNot(test_products[0].category, "kitty") self.assertEqual(test_products[0].id, products[0].id)
def test_find_by_category(self): """ Find Products by Category """ Product(name="iPhone X", description="Black iPhone", category="Technology", price=9999.99).create() Product(name="Cake", description="Chocolate Cake", category="Food", price=10.50).create() products = Product.find_by_category("technology") self.assertEqual(products[0].category, "Technology") self.assertEqual(products[0].name, "iPhone X") self.assertEqual(products[0].description, "Black iPhone") self.assertEqual(products[0].price, 9999.99)
def list_products(): """ Returns all of the Products """ app.logger.info("Request for Product list") products = [] category = request.args.get("category") name = request.args.get("name") if category: products = Product.find_by_category(category) elif name: products = Product.find_by_name(name) else: products = Product.all() results = [product.serialize() for product in products] return make_response(jsonify(results), status.HTTP_200_OK)
def get(self): """ Returns all of the queried Products """ app.logger.info("Request for product list") products = [] args = product_args.parse_args() category = args.get('category') name = args.get('name') description = args.get('description') minimum = args.get('minimum') maximum = args.get('maximum') if minimum and maximum: if name and category and description: products = Product.find_by_name_category_description_price( name, category, description, minimum, maximum) elif name and category: products = Product.find_by_name_category_price( name, category, minimum, maximum) elif name and description: products = Product.find_by_name_description_price( name, description, minimum, maximum) elif name: products = Product.find_by_name_price(name, minimum, maximum) elif category and description: products = Product.find_by_category_description_price( category, description, minimum, maximum) elif category: products = Product.find_by_category_price( category, minimum, maximum) elif description: products = Product.find_by_description_price( description, minimum, maximum) else: products = Product.query_by_price(minimum, maximum) elif minimum is None and maximum is None: if name and category and description: products = Product.find_by_name_category_description( name, category, description) elif name and category: products = Product.find_by_name_category(name, category) elif name and description: products = Product.find_by_name_description(name, description) elif name: products = Product.find_by_name(name) elif category and description: products = Product.find_by_category_description( category, description) elif category: products = Product.find_by_category(category) elif description: products = Product.find_by_description(description) else: products = Product.all() else: app.logger.info("Minimum and Maximum cannot be empty.") return api.abort(status.HTTP_400_BAD_REQUEST, "Minimum and Maximum cannot be empty.") results = [product.serialize() for product in products] app.logger.info("Returning %d products.", len(results)) return results, status.HTTP_200_OK