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_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 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 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 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 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_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 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_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_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_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_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_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_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 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_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_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_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 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 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)
def test_recommend_not_found(self): """ Test for a Recommend that doesn't exist """ Recommendation(id=0, product_id=PS4).save() recommendation = Recommendation.find(2) self.assertIs(recommendation, None)