def test_find_by_quantity(self): """ Test find by the product quantity """ ProductInformation(prod_id=1234, new_qty=1, used_qty=2, open_boxed_qty=3).save() ProductInformation(prod_id=4321, new_qty=5, used_qty=1, open_boxed_qty=2).save() result = ProductInformation.find_by_quantity(1) self.assertEqual(0, len(result)) result = ProductInformation.find_by_quantity(6) self.assertEqual(1, len(result)) self.assertEqual(1234, result[0].prod_id) result = ProductInformation.find_by_quantity(8) self.assertEqual(1, len(result)) self.assertEqual(4321, result[0].prod_id) result = ProductInformation.find_by_quantity(-1) self.assertEqual(0, len(result))
def query_prod_info(): """ Retrieve a list of all the products in the inventory & query specific entries in the Inventory system This endpoint will return all the details of the products in the inventory unless a query parameter is specificed --- tags: - Inventory description: The inventory endpoint allows you to query the inventory parameters: - name: prod_name in: query description: the name of the product you are looking for required: false type: string - name: quantity in: query description: if you want to check how many products have a specfic quantity required: false type: integer - name: condition in: query description: if you want to find all the products of a certain condition (e.g. new, used, open_boxed) required: false type: string enum: - new - used - open_boxed responses: 400: description: Bad Request (invalid posted data) 200: description: An array of all the products schema: type: array items: schema: $ref: '#/definitions/Product' """ if request.args: app.logger.info("GET received, List all that satisfy {}.".format( request.args.to_dict())) else: app.logger.info("GET received, List all.") all_prod_info = [] if request.args.get('prod_name'): prod_name = request.args.get('prod_name') all_prod_info = ProductInformation.find_by_name(prod_name) elif request.args.get('quantity'): quantity = request.args.get('quantity') try: quantity = int(quantity) all_prod_info = ProductInformation.find_by_quantity(quantity) except ValueError: abort(status.HTTP_400_BAD_REQUEST, INVALID_PARAMETER_MSG) elif request.args.get('condition'): condition = request.args.get('condition') if condition in ['new', 'used', 'open-boxed']: all_prod_info = ProductInformation.find_by_condition(condition) else: abort(status.HTTP_400_BAD_REQUEST, INVALID_PARAMETER_MSG) elif not request.args: all_prod_info = ProductInformation.list_all() else: abort(status.HTTP_400_BAD_REQUEST, INVALID_PARAMETER_MSG) results = [prod_info.serialize() for prod_info in all_prod_info] return jsonify(results), status.HTTP_200_OK