def test_finding_recommendations_by_product_id(self): """ List all recommendations for a particular product id """ data_one = { "product_id": 23, "rec_type_id": 1, "rec_product_id": 45, "weight": .5 } rec = Recommendation() rec.deserialize(data_one) rec.save() data_two = { "product_id": 87, "rec_type_id": 2, "rec_product_id": 51, "weight": .5 } rec = Recommendation() rec.deserialize(data_two) rec.save() # Assuming the client will provide a product id and category as a String rec = Recommendation.find_by_product_id(87)[0] self.assertIsNot(rec, None) self.assertEqual(rec.product_id, 87) self.assertEqual(rec.rec_type_id, 2) self.assertEqual(rec.rec_product_id, 51) self.assertEqual(rec.weight, .5)
def test_find_recommendation(self): """ Find a Recommendation by ID """ data_one = { "product_id": 54, "rec_type_id": 1, "rec_product_id": 45, "weight": .5 } rec = Recommendation() rec.deserialize(data_one) rec.save() data_two = { "product_id": 87, "rec_type_id": 1, "rec_product_id": 51, "weight": .5 } rec = Recommendation() rec.deserialize(data_two) rec.save() rec = Recommendation.find_by_id(2) self.assertIsNot(rec, None) self.assertEqual(rec.product_id, 87) self.assertEqual(rec.rec_type_id, 1) self.assertEqual(rec.rec_product_id, 51) self.assertEqual(rec.weight, .5)
def test_get_recommendations(self): link = Recommendation(url='www.fakeurl.com', count=10) link.save() response = self.client.post(self.get_recommendations, { 'link' : 'www.fakeurl.com' }) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content.decode('utf-8')), {'message' : 'Success', 'content' : 10})
def test_delete_a_recommendation(self): """ Delete a Recommendation """ recommendation = Recommendation(product_id=PS4, recommended_product_id=CONTROLLER, recommendation_type="accessory") recommendation.save() self.assertEqual(len(Recommendation.all()), 1) # delete the recommendation and make sure it isn't in the database recommendation.delete() self.assertEqual(len(Recommendation.all()), 0)
def test_delete_recommendation(self): rec = Recommendation(url="www.fakeurl6.com", count=1) rec.save() response = self.client.post(self.delete_recommendation, { 'link' : 'www.fakeurl6.com' }) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content.decode('utf-8')), {'message' : 'Success'}) self.assertFalse(Recommendation.objects.filter(url='www.fakeurl6.com').exists())
def test_save_recommendations_with_existing_url(self): link = Recommendation(url="www.fakeurl4.com", count=5) link.save() response = self.client.post(self.save_recommendations, { 'link' : 'www.fakeurl4.com', 'recommendationCount' : '6' }) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content.decode('utf-8')), {'message' : 'Success'}) self.assertTrue(Recommendation.objects.filter(url='www.fakeurl4.com', count=11).exists())
def test_create_a_recommendation(self): """ Create a recommendation and assert that it exists """ data = { "product_id": 54, "rec_type_id": 1, "rec_product_id": 45, "weight": .5 } rec = Recommendation() rec.deserialize(data) rec.save() self.assertTrue(rec != None) self.assertEquals(rec.id, 1)
def setUp(self): """ Runs before each test """ server.app.config.from_object('config.%s' % str(APP_SETTING)) self.app = server.app.test_client() server.initialize_logging() server.initialize_db() data = { "product_id": 23, "rec_type_id": 1, "rec_product_id": 45, "weight": .5 } rec = Recommendation() rec.deserialize(data) rec.save() data = { "product_id": 51, "rec_type_id": 2, "rec_product_id": 50, "weight": 1.5 } rec = Recommendation() rec.deserialize(data) rec.save() data = { "product_id": 45, "rec_type_id": 3, "rec_product_id": 4, "weight": 2.5 } rec = Recommendation() rec.deserialize(data) rec.save() data = { "product_id": 33, "rec_type_id": 1, "rec_product_id": 41, "weight": 3.5 } rec = Recommendation() rec.deserialize(data) rec.save()
def create_recommendations(): """ Creates and saves a recommendation --- tags: - Recommendations path: - /recommendations parameters: - in: body name: body required: true schema: required: - product_id - recommended_product_id - recommendation_type - likes properties: id: type: integer description: The unique id of a recommendation product_id: type: integer description: The product id of this recommendation recommended_product_id: type: integer description: The product id of being recommended recommendation_type: type: string description: The type of this recommendation, should be ('up-sell', 'cross-sell', 'accessory') likes: type: integer description: The count of how many people like this recommendation responses: 201: description: Recommendation created """ payload = request.get_json() recommendation = Recommendation() recommendation.deserialize(payload) recommendation.save() message = recommendation.serialize() response = make_response(jsonify(message), HTTP_201_CREATED) response.headers['Location'] = url_for('get_recommendations', id=recommendation.id, _external=True) return response
def test_add_a_recommendation(self): """ Create a recommendation and add it to the database """ recommendations = Recommendation.all() self.assertEqual(recommendations, []) recommendation = Recommendation(product_id=PS4, recommended_product_id=CONTROLLER, recommendation_type="accessory") self.assertNotEqual(recommendation, None) self.assertEqual(recommendation.product_id, PS4) recommendation.save() # Assert 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_delete_a_recommendation(self): """ Delete a Recommendation """ data = { "product_id": 54, "rec_type_id": 1, "rec_product_id": 45, "weight": .5 } rec = Recommendation() rec.deserialize(data) rec.save() self.assertEqual(Recommendation.count(), 1) # delete the recommendation and make sure it isn't in the database rec.delete() self.assertEqual(Recommendation.count(), 0)
def test_update_a_recommendation(self): """ Update a Recommendation """ recommendation = Recommendation(product_id=PS4, recommended_product_id=CONTROLLER, recommendation_type="accessory") recommendation.save() # Change it an save it recommendation.product_id = PS3 recommendation.save() self.assertEqual(recommendation.id, 1) self.assertEqual(recommendation.product_id, PS3) # 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].product_id, PS3)
def test_find_recommendation(self): """ Find a Recommendation by product_id """ Recommendation(product_id=PS3, recommended_product_id=CONTROLLER, recommendation_type="accessory").save() ps4 = Recommendation(product_id=PS4, recommended_product_id=CONTROLLER, recommendation_type="accessory") ps4.save() recommendation = Recommendation.find_by_product_id(ps4.product_id) self.assertIsNot(len(recommendation), 0) self.assertEqual(recommendation[0].id, ps4.id) self.assertEqual(recommendation[0].product_id, PS4) self.assertEqual(recommendation[0].recommended_product_id, ps4.recommended_product_id) self.assertEqual(recommendation[0].recommendation_type, ps4.recommendation_type) self.assertEqual(recommendation[0].likes, ps4.likes)
def test_update_a_recommendation(self): """ Update a Recommendation """ data = { "product_id": 23, "rec_type_id": 1, "rec_product_id": 45, "weight": .5 } rec = Recommendation() rec.deserialize(data) rec.save() # self.assertEqual(rec.id, 1) # Change and save it rec.product_id = 54 rec.save() self.assertEqual(rec.product_id, 54) # Fetch it back and make sure the id hasn't changed # but the data did change rec = Recommendation.find_by_id(1) self.assertEqual(rec.product_id, 54)
def test_serialize_a_recommendation(self): """ Test serialization of a Recommendation """ input_data = {"product_id": 23, \ "id": 1, \ "rec_type_id": 1, \ "rec_product_id": 45, \ "weight": .5} rec = Recommendation() rec.deserialize(input_data) rec.save() data = rec.serialize() self.assertNotEqual(data, None) self.assertIn('product_id', data) self.assertEqual(data['product_id'], 23) self.assertIn('id', data["rec_type"]) self.assertEqual(data["rec_type"]["id"], 1) self.assertIn('rec_product_id', data) self.assertEqual(data['rec_product_id'], 45) self.assertIn('weight', data) self.assertEqual(data['weight'], .5)
def add_recommender(email, first_name, last_name, applicant): applicant = Applicant.objects.get(id = applicant.id) rec_user = User.objects.filter(email = email).first() if not rec_user: if len(email)>30: username = email[0:30] else: username = email rec_user = User(username = username, first_name = first_name, last_name = last_name, email = email) password = generate_password() rec_user.set_password(password) rec_user.save() recommender = Recommender(user = rec_user, role=2) recommender.save() recommendation_requested(applicant.user.id, recommender.id, password) else: recommender = Recommender.objects.filter(user_id = rec_user.id).first() if not recommender: recommender = Recommender(user = rec_user, role=2) recommender.save() recommendation_requested_existing_recommender(applicant.user.id, recommender.id) recommendation = Recommendation(applicant = applicant, recommender = recommender) recommendation.save()
def test_create_recommendation(self): """ Create a Recommendation """ # save the current number of recommendations for later comparison data = { "product_id": 33, "rec_type_id": 1, "rec_product_id": 41, "weight": 3.5 } rec = Recommendation() rec.deserialize(data) rec.save() recommendation_count = self.get_recommendation_count() # add a new recommendation new_recommendation = {"product_id": 17, \ "rec_type_id": 3, \ "rec_product_id": 42, \ "weight": 4.6} data_obj = json.dumps(new_recommendation) resp = self.app.post('/recommendations', data=data_obj, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # Make sure location header is set location = resp.headers.get('Location', None) self.assertIsNotNone(location) # Check the data is correct new_json = json.loads(resp.data) self.assertEqual(new_json['product_id'], 17) # check that count has gone up and includes sammy resp = self.app.get('/recommendations') data = json.loads(resp.data) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(len(data), recommendation_count + 1) self.assertIn(new_json, data)