示例#1
0
    def test_find_by_condition(self):
        """ Test find by the product condition """
        ProductInformation(prod_id=1234,
                           new_qty=1,
                           used_qty=0,
                           open_boxed_qty=0).save()
        ProductInformation(prod_id=4321,
                           new_qty=0,
                           used_qty=2,
                           open_boxed_qty=0).save()
        ProductInformation(prod_id=5678,
                           new_qty=1,
                           used_qty=0,
                           open_boxed_qty=3).save()

        result = ProductInformation.find_by_condition("new")
        self.assertEqual(2, len(result))

        result = ProductInformation.find_by_condition("used")
        self.assertEqual(1, len(result))
        self.assertEqual(4321, result[0].prod_id)

        result = ProductInformation.find_by_condition("open-boxed")
        self.assertEqual(1, len(result))
        self.assertEqual(5678, result[0].prod_id)
示例#2
0
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