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_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_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_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_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 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 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_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 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_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_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_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_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 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 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 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 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)
def test_deserialize_with_no_data(self): recommendation = Recommendation(0) self.assertRaises(DataValidationError, recommendation.deserialize, None)
def test_find_by_productId(self): Recommendation(1, "productId1", "suggestionId1", "categoryId1").save() Recommendation(2, "productId2", "suggestionId2", "categoryId2").save() recommendations = Recommendation.find_by_productId("productId1") self.assertEqual(len(recommendations), 1) self.assertEqual(recommendations[0].productId, "productId1")
def test_deserialize_with_bad_data(self): recommendation = Recommendation(0) self.assertRaises(DataValidationError, recommendation.deserialize, "string data")
def test_deserialize_missing_data(self): """ Test deserialization of a Recommendation with missing data""" data = {"id": 1} recommendation = Recommendation() self.assertRaises(DataValidationError, recommendation.deserialize, data)
def test_deserialize_no_data(self): """ Test deserialization of no data """ data = None recommendation = Recommendation() self.assertRaises(DataValidationError, recommendation.deserialize, data)
def test_deserialize_bad_data(self): """ Test deserialization of bad data """ data = "this is not a dictionary" recommendation = Recommendation() self.assertRaises(DataValidationError, recommendation.deserialize, data)