Exemple #1
0
    def test_find_by_name(self):
        """ Test find by the product name """
        ProductInformation(prod_id=1234, prod_name="foo").save()
        ProductInformation(prod_id=4321, prod_name="bar").save()

        result = ProductInformation.find_by_name("foo")
        self.assertEqual(1, len(result))
        self.assertEqual(1234, result[0].prod_id)

        ProductInformation(prod_id=5678, prod_name="foo").save()

        result = ProductInformation.find_by_name("foo")
        self.assertEqual(2, len(result))
Exemple #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