def test_serialize_a_recommendation(self): recommendation = Recommendation(1, "iPhone", "Pixel", "Digital Prodct") data = recommendation.serialize() self.assertNotEqual(data, None) self.assertNotIn('_id', data) self.assertEqual(data['id'], 1) self.assertEqual(data['productId'], "iPhone") self.assertEqual(data['suggestionId'], "Pixel") self.assertEqual(data['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 }
def test_serialize_a_recommendation(self): """ Test serialization of a Recommendation """ recommendation = Recommendation(customer_id=2, product_id=3, recommend_product_id=4, recommend_type="upsell", rec_success=9) data = recommendation.serialize() self.assertNotEqual(data, None) self.assertIn('id', data) self.assertEqual(data['id'], None) self.assertIn('customer_id', data) self.assertEqual(data['customer_id'], 2) self.assertIn('product_id', data) self.assertEqual(data['product_id'], 3) self.assertIn('recommend_product_id', data) self.assertEqual(data['recommend_product_id'], 4) self.assertIn('recommend_type', data) self.assertEqual(data['recommend_type'], "upsell") self.assertIn('rec_success', data) self.assertEqual(data['rec_success'], 9)