Пример #1
0
def create_recommendations():
    """ Creates and saves a recommendation
    ---
    tags:
      - Recommendations
    path:
      - /recommendations
    parameters:
      - in: body
        name: body
        required: true
        schema:
          required:
            - product_id
            - recommended_product_id
            - recommendation_type
            - likes
          properties:
            id:
              type: integer
              description: The unique id of a recommendation
            product_id:
              type: integer
              description: The product id of this recommendation
            recommended_product_id:
              type: integer
              description: The product id of being recommended
            recommendation_type:
              type: string
              description: The type of this recommendation, should be ('up-sell', 'cross-sell', 'accessory')
            likes:
              type: integer
              description: The count of how many people like this recommendation
    responses:
      201:
        description: Recommendation created
    """
    payload = request.get_json()
    recommendation = Recommendation()
    recommendation.deserialize(payload)
    recommendation.save()
    message = recommendation.serialize()
    response = make_response(jsonify(message), HTTP_201_CREATED)
    response.headers['Location'] = url_for('get_recommendations',
                                           id=recommendation.id,
                                           _external=True)
    return response
Пример #2
0
    def test_serialize_a_recommendation(self):
        """ Test serialization of a Recommendation """
        recommendation = Recommendation(product_id=PS4,
                                        recommended_product_id=CONTROLLER,
                                        recommendation_type="accessory",
                                        likes=10)
        data = recommendation.serialize()

        self.assertNotEqual(data, None)
        self.assertIn('id', data)
        self.assertEqual(data['id'], 0)
        self.assertIn('product_id', data)
        self.assertEqual(data['product_id'], PS4)
        self.assertIn('recommended_product_id', data)
        self.assertEqual(data['recommended_product_id'], CONTROLLER)
        self.assertIn('recommendation_type', data)
        self.assertEqual(data['recommendation_type'], "accessory")
        self.assertIn('likes', data)
        self.assertEqual(data['likes'], 10)
Пример #3
0
    def test_serialize_a_recommendation(self):
        """ Test serialization of a Recommendation """

        input_data = {"product_id": 23, \
                      "id": 1, \
                      "rec_type_id": 1, \
                      "rec_product_id": 45, \
                      "weight": .5}
        rec = Recommendation()
        rec.deserialize(input_data)
        rec.save()

        data = rec.serialize()

        self.assertNotEqual(data, None)
        self.assertIn('product_id', data)
        self.assertEqual(data['product_id'], 23)
        self.assertIn('id', data["rec_type"])
        self.assertEqual(data["rec_type"]["id"], 1)
        self.assertIn('rec_product_id', data)
        self.assertEqual(data['rec_product_id'], 45)
        self.assertIn('weight', data)
        self.assertEqual(data['weight'], .5)