def test_deserialize_a_recommendation(self): """ Test deserialization of a Recommendation """ data = {"id": 1, "customer_id": 2,\ "product_id": 3, "recommend_product_id": 4, "recommend_type": "upsell"} recommendation = Recommendation() recommendation.deserialize(data) self.assertNotEqual(recommendation, None) self.assertEqual(recommendation.id, None) self.assertEqual(recommendation.customer_id, 2) self.assertEqual(recommendation.product_id, 3) self.assertEqual(recommendation.recommend_product_id, 4) self.assertEqual(recommendation.recommend_type, "upsell")
def test_deserialize_a_recommendation(self): data = { "id": 1, "productId": "iPhone", "suggestionId": "Pixel", "categoryId": "Digital Prodct" } recommendation = Recommendation(id=data["id"]) recommendation.deserialize(data) self.assertNotEqual(recommendation, None) self.assertEqual(recommendation.id, 1) self.assertEqual(recommendation.productId, "iPhone") self.assertEqual(recommendation.suggestionId, "Pixel") self.assertEqual(recommendation.categoryId, "Digital Prodct")
def post(self): """ Creates a Recommendation This endpoint will create a Recommendation based the data in the body that is posted """ app.logger.info('Request to create a Recommendation') check_content_type('application/json') recommendation = Recommendation() recommendation.deserialize(request.get_json()) recommendation.rec_success = 0 recommendation.save() message = recommendation.serialize() location_url = api.url_for(RecommendationResource, rec_id=recommendation.id, _external=True) return message, status.HTTP_201_CREATED, {'Location': location_url}
def post(self): """ Creates a Recommendation This endpoint will create a Recommendation based the data in the body that is posted or data that is sent via an html form post. """ #app.logger.info('Request to Create a Recommendation') content_type = request.headers.get('Content-Type') if not content_type: abort(status.HTTP_400_BAD_REQUEST, "No Content-Type set") data = {} # Check for form submission data if content_type == 'application/x-www-form-urlencoded': #app.logger.info('Processing FORM data') #app.logger.info(type(request.form)) #app.logger.info(request.form) data = { 'productId': request.form['productId'], 'suggestionId': request.form['suggestionId'], 'categoryId': request.form['categoryId'], } elif content_type == 'application/json': #app.logger.info('Processing JSON data') data = request.get_json() else: message = 'Unsupported Content-Type: {}'.format(content_type) abort(status.HTTP_400_BAD_REQUEST, message) recommendation = Recommendation(data["id"]) try: recommendation.deserialize(data) except DataValidationError as error: raise BadRequest(str(error)) recommendation.save() #app.logger.info('Recommendation with new id [%s] saved!', recommendation.id) location_url = api.url_for(RecommendationResource, recommendation_id=recommendation.id, _external=True) #app.logger.info('Location url [%s]', location_url) return recommendation.serialize(), status.HTTP_201_CREATED, { 'Location': location_url }