Example #1
0
    def test_delete_a_recommendation(self):
        """ Delete a Recommendation """
        recommendation = Recommendation(product_id=PS4,
                                        recommended_product_id=CONTROLLER,
                                        recommendation_type="accessory")
        recommendation.save()
        self.assertEqual(len(Recommendation.all()), 1)

        # delete the recommendation and make sure it isn't in the database
        recommendation.delete()
        self.assertEqual(len(Recommendation.all()), 0)
Example #2
0
    def test_add_a_recommendation(self):
        """ Create a recommendation and add it to the database """
        recommendations = Recommendation.all()
        self.assertEqual(recommendations, [])

        recommendation = Recommendation(product_id=PS4,
                                        recommended_product_id=CONTROLLER,
                                        recommendation_type="accessory")

        self.assertNotEqual(recommendation, None)
        self.assertEqual(recommendation.product_id, PS4)
        recommendation.save()

        # Assert that it was assigned an id and shows up in the database
        self.assertEqual(recommendation.id, 1)
        recommendations = Recommendation.all()
        self.assertEqual(len(recommendations), 1)
Example #3
0
    def test_update_a_recommendation(self):
        """ Update a Recommendation """
        recommendation = Recommendation(product_id=PS4,
                                        recommended_product_id=CONTROLLER,
                                        recommendation_type="accessory")
        recommendation.save()

        # Change it an save it
        recommendation.product_id = PS3
        recommendation.save()
        self.assertEqual(recommendation.id, 1)
        self.assertEqual(recommendation.product_id, PS3)

        # Fetch it back and make sure the id hasn't changed
        # but the data did change
        recommendations = Recommendation.all()
        self.assertEqual(len(recommendations), 1)
        self.assertEqual(recommendations[0].product_id, PS3)
Example #4
0
def list_recommendations():
    """
    Retrieves a list of recommendations from the database
    This endpoint will return all recommendations unless it is given
    a query parameter
    ----
    tags:
        - Recommendations
    description: List recommendations from the database
    parameters:
        - in: query
          name: product_id
          type: integer
          descrtiption: query the recommendation that matches the product_id
        - in: query
          name: recommendation_type
          type: string
          description: query the recommendation that matches the reommendation_type
        - in: query
          name: recommended_product_id
          type: integer
          description: query the recommendation that matches the recommended_product_id
    definitions:
        Recommendation:
            type: object
            properties:
                id:
                    type: integer
                    description: the unique id of a recommendation
                recommendation type:
                    type: string
                    description: type of the recommendation
                recommended product id:
                    type: integer
                    description: id of a recommended product
                likes:
                    type: integer
                    description: the count of how many people like this recommendation
    responses:
        200:
            description: A list of recommendations
            schema:
                type: list
        400:
            description: Recommendation was not found

    """
    results = []
    product_id = request.args.get('product_id')
    recommendation_type = request.args.get('recommendation_type')
    recommended_product_id = request.args.get('recommended_product_id')
    if product_id:
        message, return_code = query_recommendations_by_product_id(product_id)
    elif recommended_product_id:
        message, return_code = query_recommendations_by_recommended_product_id(
            recommended_product_id)
    elif recommendation_type:
        message, return_code = query_recommendations_by_recommendation_type(
            recommendation_type)
    else:
        results = Recommendation.all()
        message = [recommendation.serialize() for recommendation in results]
        return_code = HTTP_200_OK

    return jsonify(message), return_code