def test_delete_a_recommendation(self): recommendation = Recommendation(1, "productId", "recommended", "categoryId") recommendation.save() self.assertEqual(len(Recommendation.all()), 1) recommendation.delete() self.assertEqual(len(Recommendation.all()), 0)
def test_update_a_recommendation(self): recommendation = Recommendation(1, "productId", "recommended", "categoryId") recommendation.save() recommendation.categoryId = "newcategoryId" recommendation.save() recommendations = Recommendation.all() self.assertEqual(recommendations[0].categoryId, "newcategoryId")
def test_find_a_recommendation(self): saved_recommendation = Recommendation(1, "productId", "recommended", "categoryId") saved_recommendation.save() recommendation = Recommendation.find(saved_recommendation.id) self.assertEqual(recommendation.productId, "productId") self.assertIsNot(recommendation, None) self.assertEqual(recommendation.id, saved_recommendation.id) self.assertEqual(recommendation.productId, "productId")
def test_reset(self): """ Reset """ recommendation = Recommendation(customer_id=2, product_id=3, recommend_product_id=4, recommend_type="upsell") recommendation.save() self.assertEqual(len(Recommendation.all()), 1) # delete the recommendation and make sure it isn't in the database recommendation.remove_all() self.assertEqual(len(Recommendation.all()), 0)
def test_find_recommendation(self): """ Find a Recommendation by ID """ Recommendation(customer_id=2, product_id=3, recommend_product_id=4,\ recommend_type="upsell").save() recc = Recommendation(customer_id=5, product_id=6,\ recommend_product_id=7, recommend_type="downsell") recc.save() recommendation = Recommendation.find(recc.id) self.assertIsNot(recommendation, None) self.assertEqual(recommendation.id, recc.id) self.assertEqual(recommendation.product_id, 6) self.assertEqual(recommendation.customer_id, 5)
def test_add_a_recommendation(self): """ Create a recommendation and add it to the database """ recommendations = Recommendation.all() self.assertEqual(recommendations, []) recommendation = Recommendation(customer_id=2, product_id=3, recommend_product_id=4, recommend_type="upsell") self.assertTrue(recommendation is not None) self.assertEqual(recommendation.id, None) recommendation.save() # Asert 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)
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 test_update_a_recommendation(self): """ Update a Recommendation """ recommendation = Recommendation(customer_id=2, product_id=3, recommend_product_id=4, recommend_type="upsell") recommendation.save() self.assertEqual(recommendation.id, 1) # Change it an save it recommendation.recommend_product_id = 5 recommendation.save() self.assertEqual(recommendation.id, 1) # 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].recommend_product_id, 5)
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 }