def test_find_by_attributes(self): """ Find Recommendation by some attributes """ Recommendation(customer_id=2, product_id=3, recommend_product_id=4, recommend_type="upsell").save() Recommendation(customer_id=2, product_id=3, recommend_product_id=4, recommend_type="downsell").save() Recommendation(customer_id=5, product_id=6, recommend_product_id=7, recommend_type="downsell").save() recommendations = Recommendation.find_by_attributes(3, 2, "downsell") self.assertEqual(recommendations[0].recommend_type, "downsell") self.assertEqual(recommendations[0].product_id, 3) self.assertEqual(recommendations[0].customer_id, 2) recommendations = Recommendation.find_by_attributes(3, 2, None) self.assertEqual(recommendations[0].recommend_type, "upsell") self.assertEqual(recommendations[0].product_id, 3) self.assertEqual(recommendations[0].customer_id, 2) self.assertEqual(recommendations[1].recommend_type, "downsell") self.assertEqual(recommendations[1].product_id, 3) self.assertEqual(recommendations[1].customer_id, 2)
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 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_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_find_by_recommend_type(self): """ Find Recommendation by recommend_type """ Recommendation(customer_id=2, product_id=3, recommend_product_id=4, recommend_type="upsell").save() Recommendation(customer_id=5, product_id=6, recommend_product_id=7, recommend_type="downsell").save() recommendations = Recommendation.find_by_recommend_type("downsell") self.assertEqual(recommendations[0].customer_id, 5) self.assertEqual(recommendations[0].product_id, 6) self.assertEqual(recommendations[0].recommend_product_id, 7) self.assertEqual(recommendations[0].recommend_type, "downsell")
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 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_deserialize_a_recommendation_negative_value(self): """ Test deserialization of a Recommendation with negative value """ data = {"id": 1, "customer_id": -2,\ "product_id": 3, "recommend_product_id": 4, "recommend_type": "upsell"} recommendation = Recommendation() self.assertRaises(DataValidationError, recommendation.deserialize, data)
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_create_a_recommendation(self): recommendation = Recommendation(1, "productId", "suggestionId", "categoryId") self.assertNotEqual(recommendation, None) self.assertEqual(recommendation.id, 1) self.assertEqual(recommendation.productId, "productId") self.assertEqual(recommendation.suggestionId, "suggestionId") self.assertEqual(recommendation.categoryId, "categoryId")
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_create_a_recommendation(self): """ Create a recommendation and assert that it exists """ recommendation = Recommendation() self.assertTrue(recommendation is not None) self.assertEqual(recommendation.id, None) self.assertEqual(recommendation.customer_id, None) self.assertEqual(recommendation.product_id, None) self.assertEqual(recommendation.recommend_product_id, None) self.assertEqual(recommendation.recommend_type, None)
def delete(self, recommendation_id): """ Delete a Recommendation This endpoint will delete a Recommendation based the id specified in the path """ #app.logger.info('Request to Delete a recommendation with id [%s]', recommendation_id) recommendation = Recommendation.find(recommendation_id) if recommendation: recommendation.delete() return '', status.HTTP_204_NO_CONTENT
def test_deserialize_with_no_productId(self): #recommendation = Recommendation() data = { "id": 1, "suggestionId": "Pixel", "categoryId": "Digital Prodct" } recommendation = Recommendation(id=data["id"]) self.assertRaises(DataValidationError, recommendation.deserialize, data)
def get(self): """ Returns all of the Recommendations """ #app.logger.info('Listing recommendations') recommendations = [] categoryId = request.args.get('categoryId') productId = request.args.get('productId') suggestionId = request.args.get('suggestionId') if categoryId: recommendations = Recommendation.find_by_categoryId(categoryId) elif productId: recommendations = Recommendation.find_by_productId(productId) elif suggestionId: recommendations = Recommendation.find_by_suggestionId(suggestionId) else: recommendations = Recommendation.all() #app.logger.info('[%s] Recommendations returned', len(recommendations)) results = [ recommendation.serialize() for recommendation in recommendations ] return results, status.HTTP_200_OK
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)
def put(self, categoryId): """ Update a recommendation category This end point will update a recommendation category for all RELEVANT RECOMMENDATIONS based on the data in the body """ results = Recommendation.find_by_categoryId(categoryId) if (len(results) == 0): # message = {'error' : 'Recommendation with categoryId: %s was not found' % str(categoryId)} return_code = status.HTTP_404_NOT_FOUND return '', return_code data = request.get_json() i = 0 sizeOfResults = len(results) while i < sizeOfResults: recommendation = Recommendation.find(results[i].id) recommendation.categoryId = data['categoryId'] recommendation.update() i += 1 # message = {'success' : 'RECOMMENDATIONS category updated'} return '', status.HTTP_200_OK
def get(self, rec_id): """ Retrieve a single Recommendation This endpoint will return a Recommendation based on it's id """ app.logger.info("Request to Retrieve a recommendation with id [%s]", rec_id) recommendation = Recommendation.find(rec_id) if not recommendation: api.abort( status.HTTP_404_NOT_FOUND, "Recommendation with id '{}' was not found.".format(rec_id)) return recommendation.serialize(), status.HTTP_200_OK
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 """ 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 put(self, rec_id): """ Update a Recommendation This endpoint will update a Recommendation based the body that is posted """ app.logger.info('Request to update recommendation with id: %s', rec_id) check_content_type('application/json') recommendation = Recommendation.find(rec_id) if not recommendation: api.abort( status.HTTP_404_NOT_FOUND, "Recommendation with id '{}' was not found.".format(rec_id)) recommendation.deserialize(request.get_json()) recommendation.id = rec_id recommendation.save() return recommendation.serialize(), status.HTTP_200_OK
def get(self): """ List all or query the Recommendations""" app.logger.info('Request to list Recommendations...') product_id = request.args.get('product-id') customer_id = request.args.get('customer-id') recommend_type = request.args.get('recommend-type') recommendations = Recommendation.find_by_attributes( product_id, customer_id, recommend_type) if not recommendations: raise NotFound( "Recommendation with product_id {}, " "customer_id {}, recommend_type {} was not found.".format( product_id, customer_id, recommend_type)) results = [ recommendation.serialize() for recommendation in recommendations ] return results, status.HTTP_200_OK
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 put(self, rec_id): """ Increment A Recommendation's Success Field This endpoint will increment the success counter based on the recommendtion succeed """ app.logger.info( 'Increment success field for recommendation with id: %s', rec_id) check_content_type('application/json') recommendation = Recommendation.find(rec_id) if not recommendation: api.abort( status.HTTP_404_NOT_FOUND, "Recommendation with id '{}' was not found.".format(rec_id)) count = recommendation.rec_success recommendation.rec_success = count + 1 recommendation.id = rec_id recommendation.save() return recommendation.serialize(), status.HTTP_200_OK
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 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 setUp(self): """Runs before each test""" self.app = app.test_client() Recommendation.init_db("tests") sleep(0.5) Recommendation.remove_all() sleep(0.5) Recommendation(id=1, productId='Infinity Gauntlet', suggestionId='Soul Stone', categoryId='Comics').save() sleep(0.5) Recommendation(id=2, productId='iPhone', suggestionId='iphone Case', categoryId='Electronics').save() sleep(0.5)
def put(self, recommendation_id): """ Update a Recommendation This endpoint will update a Recommendation based the body that is posted """ #app.logger.info('Request to Update a recommendation with id [%s]', recommendation_id) #check_content_type('application/json') recommendation = Recommendation.find(recommendation_id) if not recommendation: abort( status.HTTP_404_NOT_FOUND, "Recommendation with id '{}' was not found.".format( recommendation_id)) payload = request.get_json() try: recommendation.deserialize(payload) except DataValidationError as error: raise BadRequest(str(error)) recommendation.id = recommendation_id recommendation.update() return recommendation.serialize(), status.HTTP_200_OK
def test_deserialize_with_no_data(self): recommendation = Recommendation(0) self.assertRaises(DataValidationError, recommendation.deserialize, None)
def setUp(self): sleep(0.5) Recommendation.init_db() sleep(0.5) Recommendation.remove_all() sleep(0.5)