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_recommendation_not_found_with_data(self): """ Test for a Recommendation that doesn't exist """ data_one = { "product_id": 23, "rec_type_id": 1, "rec_product_id": 45, "weight": .5 } rec = Recommendation() rec.deserialize(data_one) rec = Recommendation.find_by_id(2) self.assertIs(rec, None)
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 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 test_deserialize_a_recommendation(self): """ Test deserialization of a Recommendation """ data = { "product_id": 54, "rec_type_id": 1, "rec_product_id": 45, "weight": .5 } rec = Recommendation() rec.deserialize(data) self.assertNotEqual(rec, None) self.assertEqual(rec.product_id, 54) self.assertEqual(rec.rec_type_id, 1) self.assertEqual(rec.rec_product_id, 45) self.assertEqual(rec.weight, .5)
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_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_deserialize_a_recommendation(self): """ Test deserialization of a Recommendation """ data = { 'id': 1, 'product_id': PS4, 'recommended_product_id': CONTROLLER, 'recommendation_type': "accessory", 'likes': 10 } recommendation = Recommendation() recommendation.deserialize(data) self.assertNotEqual(recommendation, None) # self.assertEqual(recommendation.id, 1) self.assertEqual(recommendation.product_id, PS4) self.assertEqual(recommendation.recommended_product_id, CONTROLLER) self.assertEqual(recommendation.recommendation_type, "accessory") self.assertEqual(recommendation.likes, 10)
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 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)