def test_get_property_should_return_value_for_property_if_already_answered( app): # Given obj_to_save = [] user = create_user(email='*****@*****.**', username='******', password='******') obj_to_save.append(user) question = Question() question.question_name = 'question_1' obj_to_save.append(question) BaseObject.check_and_save(*obj_to_save) user_property_obj = UserProperty() user_property_obj.user_id = user.id user_property_obj.question_id = question.id user_property_obj.value = float(0.5) BaseObject.check_and_save(user_property_obj) # When property_request = req_with_auth(email='*****@*****.**', password='******') \ .get(API_URL + '/property') # Then assert property_request.status_code == 200 content = property_request.json() assert 'question_1' in content assert content['question_1'] == 0.5
def test_save_property_set_value_if_property_and_user_property_exist(app): # Given obj_to_save = [] user = create_user(email='*****@*****.**', username='******', password='******') obj_to_save.append(user) question = Question() question.question_name = 'question_1' obj_to_save.append(question) BaseObject.check_and_save(*obj_to_save) user_property_obj = UserProperty() user_property_obj.user_id = user.id user_property_obj.question_id = question.id user_property_obj.value = float(12) BaseObject.check_and_save(user_property_obj) data = dict() data['question_1'] = "0.5" # When property_request = req_with_auth(email='*****@*****.**', password='******') \ .post(API_URL + '/property', json=data) # Then assert property_request.status_code == 200 user_property_obj = UserProperty.query. \ filter_by(user_id=user.id). \ filter_by(question_id=question.id). \ first() assert user_property_obj is not None assert user_property_obj.value == 0.5
def execute(self, data: dict, user_id: int): object_to_save = [] for key, value in data.items(): question = Question.query.filter_by(question_name=key).first() if question is not None \ and value != "": user_property = UserProperty.query. \ filter_by(user_id=user_id). \ filter_by(question_id=question.id). \ first() if user_property is None: user_property = UserProperty() user_property.user_id = user_id user_property.question_id = question.id user_property.value = float(value) object_to_save.append(user_property) else: user_property.value = float(value) object_to_save.append(user_property) BaseObject.check_and_save(*object_to_save) for user_property in object_to_save: HistoryUserProperties().execute(user_property.id, user_property.value)