def createDummyRecommendations(num): for i in range(0, num): rec = Recommendation() rec.location = "POINT( " + str( round(random.uniform(-87.958428, -87.503532), 6)) + " " + str( round(random.uniform(41.640071, 42.029866), 6)) + " )" rec.recommendation = "Create a grocery store here." db.session.add(rec) db.session.commit() print("Created " + str(num) + " dummy recommendations.")
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_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 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_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 test_find_by_recommend_type(self): """ Test find by recommend_type """ Recommendation(product_id=PS3, recommended_product_id=CONTROLLER, recommendation_type="accessory").save() Recommendation(product_id=PS4, recommended_product_id=CONTROLLER, recommendation_type="accessory").save() Recommendation(product_id=PS4, recommended_product_id=MONSTER_HUNTER, recommendation_type="cross-sell").save() recommendations = Recommendation.find_by_recommend_type("accessory") self.assertEqual(len(recommendations), 2) self.assertEqual(recommendations[0].recommendation_type, "accessory") self.assertEqual(recommendations[1].recommendation_type, "accessory")
def get_recommendations(id): """ Retrieves a Recommendation with a specific id --- tags: - Recommendations path: - /recommendations/<int:id> parameters: - name: id in: path description: The unique id of a recommendation type: integer required: true responses: 200: description: Recommendation returned 404: description: Recommendation not found """ recommendation = Recommendation.find(id) if recommendation: message = recommendation.serialize() return_code = HTTP_200_OK else: message = { 'error': 'Recommendation with id: %s was not found' % str(id) } return_code = HTTP_404_NOT_FOUND return jsonify(message), return_code
def index(): form = RecommendationForm() if form.validate_on_submit(): recommendation = Recommendation(book_title=form.book_title.data, book_author=form.book_author.data, book_category=form.book_category.data, book_summary=form.book_summary.data, author=current_user) db.session.add(recommendation) db.session.commit() flash('Awesome, your recommendation is now posted!') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) recommendations = current_user.followed_posts().paginate( page, app.config['RECOMMENDATIONS_PER_PAGE'], False) next_url = url_for('index', page=recommendations.next_num) \ if recommendations.has_next else None prev_url = url_for('index', page = recommendations.prev_num) \ if recommendations.has_prev else None return render_template('index.html', title='Home', form=form, recommendations=recommendations.items, next_url=next_url, prev_url=prev_url)
def update_recommendations(id): """ Updates a recommendation with a specific id --- tags: - Recommendations path: - /recommendations/<int:id> produces: - application/json parameters: - name: id in: path description: The unique id of a recommendation type: integer required: true responses: 200: description: Recommendation updated 404: description: Recommendation not found """ recommendation = Recommendation.find(id) if recommendation: payload = request.get_json() recommendation.deserialize(payload) recommendation.save() message = recommendation.serialize() return_code = HTTP_200_OK else: message = { 'error': 'Recommendation with id: %s was not found' % str(id) } return_code = HTTP_404_NOT_FOUND return jsonify(message), return_code
def like_recommendation(id): """ Increase the number of likes for a recommendation with a specific id --- tags: - Recommendations path: - /recommendations/<int:id>/likes produces: - application/json parameters: - name: id in: path description: The unique id of a recommendation type: integer required: true responses: 200: description: Recommendation updated 404: description: Recommendation not found """ recommendation = Recommendation.find(id) if not recommendation: message = { 'error': 'Recommendation with product_id: %s was not found' % str(id) } return_code = HTTP_404_NOT_FOUND else: recommendation.likes += 1 recommendation.save() message = recommendation.serialize() return_code = HTTP_200_OK return jsonify(message), return_code
def init_db(): db.drop_all() db.create_all() # Init tag db.session.add(Tag(id=1, name="渣三维")) db.session.add(Tag(id=2, name="转专业")) db.session.add(Tag(id=3, name="高GT")) db.session.add(Tag(id=4, name="高GPA")) # Init project db.session.add(Project(id=1, name="无相关实习经历,有个人项目", value=2)) db.session.add(Project(id=2, name="国内小公司实习", value=2)) db.session.add(Project(id=3, name="国内大公司实习", value=3)) db.session.add(Project(id=4, name="BAT实习", value=4)) db.session.add(Project(id=5, name="外企实习", value=5)) # Init Recommendation db.session.add(Recommendation(id=1, name="无推荐信", value=1)) db.session.add(Recommendation(id=2, name="国内普通推", value=2)) db.session.add(Recommendation(id=3, name="海外普通推", value=3)) db.session.add(Recommendation(id=4, name="国内牛推", value=4)) db.session.add(Recommendation(id=5, name="海外牛推", value=5)) # Init Research db.session.add(Research(id=1, name="无科研经历", value=1)) db.session.add(Research(id=2, name="初步的科研经历", value=2)) db.session.add(Research(id=3, name="大学实验室做过较深入的研究", value=3)) db.session.add(Research(id=4, name="1~3个月的海外研究经历", value=4)) db.session.add(Research(id=5, name="大于3个月的海外研究经历", value=5)) # Init Country db.session.add(Country(id=1, name="美国")) db.session.add(Country(id=2, name="英国")) db.session.add(Country(id=3, name="加拿大")) db.session.add(Country(id=4, name="澳大利亚")) db.session.add(Country(id=5, name="德国")) db.session.add(Country(id=6, name="法国")) db.session.add(Country(id=7, name="香港")) db.session.add(Country(id=8, name="日本")) db.session.add(Country(id=9, name="新加坡")) db.session.commit() # https://api.github.com/search/repositories?q=tetris+language:assembly&sort=stars&order=desc
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_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_serialize_a_recommendation(self): """ Test serialization of a Recommendation """ recommendation = Recommendation(product_id=PS4, recommended_product_id=CONTROLLER, recommendation_type="accessory", likes=10) data = recommendation.serialize() self.assertNotEqual(data, None) self.assertIn('id', data) self.assertEqual(data['id'], 0) self.assertIn('product_id', data) self.assertEqual(data['product_id'], PS4) self.assertIn('recommended_product_id', data) self.assertEqual(data['recommended_product_id'], CONTROLLER) self.assertIn('recommendation_type', data) self.assertEqual(data['recommendation_type'], "accessory") self.assertIn('likes', data) self.assertEqual(data['likes'], 10)
def test_deserialize_with_no_product_id(self): """ Deserialize a Recommend without a name """ recommendation = Recommendation() data = { "id": 0, 'recommended_product_id': CONTROLLER, 'recommendation_type': "accessory", 'likes': 10 } self.assertRaises(DataValidationError, recommendation.deserialize, data)
def test_db_error_on_save(self, db_error_mock): """ Test Rollback on save """ db_error_mock.side_effect = OperationalError() data = { 'product_id': 23, 'rec_type_id': "up-sell", 'rec_product_id': 45, 'weight': .5 } rec = Recommendation() self.assertRaises(DataValidationError, rec.deserialize, data)
def test_create_a_recommendation(self): """ Create a recommendation and assert that it exists """ recommendation = Recommendation(product_id=PS4, recommended_product_id=CONTROLLER, recommendation_type="accessory") self.assertNotEqual(recommendation, None) self.assertEqual(recommendation.id, 0) self.assertEqual(recommendation.product_id, PS4) self.assertEqual(recommendation.recommended_product_id, CONTROLLER) self.assertEqual(recommendation.recommendation_type, "accessory") self.assertEqual(recommendation.likes, 0)
def test_find_by_likes(self): """ Test find by likes """ Recommendation(product_id=PS3, recommended_product_id=CONTROLLER, recommendation_type="accessory", likes=1).save() Recommendation(product_id=PS4, recommended_product_id=CONTROLLER, recommendation_type="accessory", likes=5).save() Recommendation(product_id=PS4, recommended_product_id=MONSTER_HUNTER, recommendation_type="accessory", likes=10).save() # Test query for nothing recommendations = Recommendation.find_by_likes(100) self.assertEqual(len(recommendations), 0) # Test query for something recommendations = Recommendation.find_by_likes(5) self.assertEqual(len(recommendations), 1) self.assertEqual(recommendations[0].recommended_product_id, CONTROLLER) recommendations = Recommendation.find_by_likes(10) self.assertEqual(len(recommendations), 1) self.assertEqual(recommendations[0].recommended_product_id, MONSTER_HUNTER)
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 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_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 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 query_recommendations_by_recommended_product_id(recommended_product_id): """ Query a recommendation from the database that have the same recommended product id """ recommendations = Recommendation.find_by_recommend_product_id( int(recommended_product_id)) if len(recommendations) > 0: message = [ recommendation.serialize() for recommendation in recommendations ] return_code = HTTP_200_OK else: message = { 'error': 'Recommendation with product_id: \ %s was not found' % str(recommended_product_id) } return_code = HTTP_404_NOT_FOUND return message, return_code
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 new_recommendation(): form = RecommendationForm() if form.validate_on_submit(): recommendation = Recommendation(book_title=form.book_title.data, book_author=form.book_author.data, book_category=form.book_category.data, book_summary=form.book_summary.data, author=current_user) db.session.add(recommendation) db.session.commit() flash('Awesome, your recommendation is now posted!') # # I wanted to redirect the user to their profile page, so that they could see their new recommendation # on top of their other recommendations, but I need to keep things simple for now. # return redirect(url_for('user', username=current_user.username)) return redirect(url_for('index')) # if the form has not been filled out yet, then it will be come to this return function to be rendered return render_template('new_recommendation.html', title='New Recommendation', form=form)
def delete_recommendations(id): """ Removes a recommendation from the database that matches the id This endpoint will delete a recommendation based on its id --- tags: - Recommendations description: Delete a recommendation from the database parameters: - name: id in: path description: ID of a recommendation to delete type: integer required: true responses: 204: description: Recommendation deleted """ recommendation = Recommendation.find(id) if recommendation: recommendation.delete() return make_response('', HTTP_204_NO_CONTENT)
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_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_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)