def test_get_achievements(self): dao = UserDAO() mentor = UserModel("Test mentor", "test_mentor", "test_password", "*****@*****.**", True) mentee = UserModel("Test mentee", "test_mentee", "test_password", "*****@*****.**", True) mentor.is_email_verified = True mentor.available_to_mentor = True mentee.is_email_verified = True mentee.need_mentoring = True db.session.add(mentor) db.session.add(mentee) db.session.commit() start_date = datetime.datetime.now() end_date = start_date + datetime.timedelta(weeks=4) tasks_list = TasksListModel() tasks_list.add_task( description="Test Task", created_at=start_date.timestamp(), is_done=True, completed_at=end_date.timestamp(), ) tasks_list.add_task( description="Test Task 2", created_at=start_date.timestamp(), is_done=True, completed_at=end_date.timestamp(), ) relation = MentorshipRelationModel( action_user_id=mentee.id, mentor_user=mentor, mentee_user=mentee, creation_date=start_date.timestamp(), end_date=end_date.timestamp(), state=MentorshipRelationState.ACCEPTED, tasks_list=tasks_list, notes="Test Notes", ) db.session.add(tasks_list) db.session.add(relation) db.session.commit() achievements = dao.get_achievements(mentee.id) self.assertEqual(2, len(achievements)) for achievement in achievements: self.assertTrue(achievement.get("is_done"))
def create_user(data: Dict[str, str]): """Creates a new user. Creates a new user with provided data. Arguments: data: A list containing the user's name, username, password, and email, as well as recognition that they have read and agree to the Terms and Conditions. Returns: A tuple with two elements. The first element is a dictionary containing a key 'message' containing a string which indicates whether or not the user was created successfully. The second is the HTTP response code. """ name = data["name"] username = data["username"] password = data["password"] email = data["email"] terms_and_conditions_checked = data["terms_and_conditions_checked"] existing_user = UserModel.find_by_username(data["username"]) if existing_user: return messages.USER_USES_A_USERNAME_THAT_ALREADY_EXISTS, HTTPStatus.CONFLICT else: existing_user = UserModel.find_by_email(data["email"]) if existing_user: return messages.USER_USES_AN_EMAIL_ID_THAT_ALREADY_EXISTS, HTTPStatus.CONFLICT user = UserModel(name, username, password, email, terms_and_conditions_checked) if "need_mentoring" in data: user.need_mentoring = data["need_mentoring"] if "available_to_mentor" in data: user.available_to_mentor = data["available_to_mentor"] user.save_to_db() return messages.USER_WAS_CREATED_SUCCESSFULLY, HTTPStatus.CREATED
def test_get_user_statistics(self): dao = UserDAO() mentor = UserModel("Test mentor", "test_mentor", "__test__", "*****@*****.**", True) mentee = UserModel("Test mentee", "test_mentee", "__test__", "*****@*****.**", True) mentor.is_email_verified = True mentor.available_to_mentor = True mentee.is_email_verified = True mentee.need_mentoring = True db.session.add(mentor) db.session.add(mentee) db.session.commit() start_date = datetime.datetime.now() end_date = start_date + datetime.timedelta(weeks=4) tasks_list = TasksListModel() pending_relation = MentorshipRelationModel( action_user_id=mentee.id, mentor_user=mentor, mentee_user=mentee, creation_date=start_date.timestamp(), end_date=end_date.timestamp(), state=MentorshipRelationState.PENDING, tasks_list=tasks_list, notes="Test Notes", ) db.session.add(tasks_list) db.session.add(pending_relation) db.session.commit() # We first test pending relations expected_response = { "name": mentor.name, "pending_requests": 1, "accepted_requests": 0, "rejected_requests": 0, "completed_relations": 0, "cancelled_relations": 0, "achievements": [], } actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response) # We now test accepted relations pending_relation.state = MentorshipRelationState.ACCEPTED expected_response["pending_requests"] = 0 expected_response["accepted_requests"] = 1 actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response) # We now test completed relations pending_relation.state = MentorshipRelationState.COMPLETED expected_response["accepted_requests"] = 0 expected_response["completed_relations"] = 1 actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response) # We now test rejected relations pending_relation.state = MentorshipRelationState.REJECTED expected_response["completed_relations"] = 0 expected_response["rejected_requests"] = 1 actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response) # We now test cancelled relations pending_relation.state = MentorshipRelationState.CANCELLED expected_response["rejected_requests"] = 0 expected_response["cancelled_relations"] = 1 actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response) # We now test achievements pending_relation.state = MentorshipRelationState.ACCEPTED tasks_list.add_task( description="Test Task", created_at=start_date.timestamp(), is_done=True, completed_at=end_date.timestamp(), ) tasks_list.add_task( description="Test Task 2", created_at=start_date.timestamp(), is_done=True, completed_at=end_date.timestamp(), ) expected_response["cancelled_relations"] = 0 expected_response["accepted_requests"] = 1 expected_response["achievements"] = [ { "completed_at": end_date.timestamp(), "created_at": start_date.timestamp(), "description": "Test Task", "id": 1, # This is the first task in the list "is_done": True, }, { "completed_at": end_date.timestamp(), "created_at": start_date.timestamp(), "description": "Test Task 2", "id": 2, # This is the second task in the list "is_done": True, }, ] actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response)