def test_dao_create_mentorship_relation_with_mentor_already_in_relation(self): self.mentorship_relation = MentorshipRelationModel( action_user_id=self.admin_user.id, mentor_user=self.admin_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.ACCEPTED, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(self.mentorship_relation) db.session.commit() dao = MentorshipRelationDAO() data = dict( mentor_id=self.second_user.id, mentee_id=self.first_user.id, end_date=self.end_date_example.timestamp(), notes=self.notes_example, tasks_list=TasksListModel(), ) # Use DAO to create a mentorship relation result = dao.create_mentorship_relation(self.second_user.id, data) self.assertEqual((messages.MENTOR_ALREADY_IN_A_RELATION, 400), result) query_mentorship_relations = MentorshipRelationModel.query.all() self.assertTrue(1, len(query_mentorship_relations))
def setUp(self): super(TestListMentorshipRelationsApi, self).setUp() self.notes_example = "description of a good mentorship relation" self.now_datetime = datetime.now() self.past_end_date_example = self.now_datetime - timedelta(weeks=5) self.future_end_date_example = self.now_datetime + timedelta(weeks=5) # create new mentorship relation self.past_mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.past_end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) self.future_pending_mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.future_end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) self.future_accepted_mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.future_end_date_example.timestamp(), state=MentorshipRelationState.ACCEPTED, notes=self.notes_example, tasks_list=TasksListModel(), ) self.admin_only_mentorship_relation = MentorshipRelationModel( action_user_id=self.admin_user.id, mentor_user=self.admin_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.future_end_date_example.timestamp(), state=MentorshipRelationState.REJECTED, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(self.past_mentorship_relation) db.session.add(self.future_pending_mentorship_relation) db.session.add(self.future_accepted_mentorship_relation) db.session.commit()
def setUp(self): super(TestTasksListModel, self).setUp() self.empty_tasks_list = TasksListModel() self.tasks_list_1 = TasksListModel() db.session.add(self.empty_tasks_list) db.session.add(self.tasks_list_1) db.session.commit() self.now_timestamp = datetime.now().timestamp() self.test_description_1 = "test description number one" self.test_description_2 = "test description number two" self.tasks_list_1.add_task(self.test_description_1, self.now_timestamp)
def test_accepted_requests_auth(self): start_date = datetime.now() end_date = start_date + timedelta(weeks=4) start_date = start_date.timestamp() end_date = end_date.timestamp() tasks_list = TasksListModel() mentorship_relation = MentorshipRelationModel( action_user_id=self.user2.id, mentor_user=self.user1, mentee_user=self.user2, creation_date=start_date, end_date=end_date, state=MentorshipRelationState.ACCEPTED, notes="", tasks_list=tasks_list, ) db.session.add(mentorship_relation) db.session.commit() expected_response = { "name": "User1", "pending_requests": 0, "accepted_requests": 1, "rejected_requests": 0, "completed_relations": 0, "cancelled_relations": 0, "achievements": [], } auth_header = get_test_request_header(self.user1.id) actual_response = self.client.get( "/home", follow_redirects=True, headers=auth_header ) self.assertEqual(200, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
def test_recepient_already_in_relation_accept_request(self): # create existing relation between admin_user and second_user mentorship_relation_current = MentorshipRelationModel( action_user_id=self.admin_user.id, mentor_user=self.admin_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.ACCEPTED, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(mentorship_relation_current) db.session.commit() self.assertEqual(MentorshipRelationState.ACCEPTED, mentorship_relation_current.state) # current self.assertEqual(MentorshipRelationState.PENDING, self.mentorship_relation.state) # new with self.client: response = self.client.put( f"/mentorship_relation/{self.mentorship_relation.id}/accept", headers=get_test_request_header(self.second_user.id), ) self.assertEqual(403, response.status_code) self.assertEqual(MentorshipRelationState.ACCEPTED, mentorship_relation_current.state) # current self.assertEqual(MentorshipRelationState.PENDING, self.mentorship_relation.state) # new self.assertDictEqual( messages.USER_IS_INVOLVED_IN_A_MENTORSHIP_RELATION, json.loads(response.data), )
def setUp(self): super(TestMentorshipRelationAcceptRequestDAO, self).setUp() self.notes_example = "description of a good mentorship relation" self.now_datetime = datetime.now() self.end_date_example = self.now_datetime + timedelta(weeks=5) # create new mentorship relation self.mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) self.mentorship_relation2 = MentorshipRelationModel( action_user_id=self.third_user.id, mentor_user=self.first_user, mentee_user=self.third_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) self.mentorship_relation3 = MentorshipRelationModel( action_user_id=self.second_user.id, mentor_user=self.third_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(self.mentorship_relation) db.session.add(self.mentorship_relation2) db.session.add(self.mentorship_relation3) db.session.commit()
def setUp(self): super(TestMentorshipRelationDeleteDAO, self).setUp() self.first_user = UserModel( name=user1["name"], email=user1["email"], username=user1["username"], password=user1["password"], terms_and_conditions_checked=user1["terms_and_conditions_checked"], ) self.second_user = UserModel( name=user2["name"], email=user2["email"], username=user2["username"], password=user2["password"], terms_and_conditions_checked=user2["terms_and_conditions_checked"], ) # making sure both are available to be mentor or mentee self.first_user.need_mentoring = True self.first_user.available_to_mentor = True self.first_user.is_email_verified = True self.second_user.need_mentoring = True self.second_user.available_to_mentor = True self.second_user.is_email_verified = True self.notes_example = "description of a good mentorship relation" self.now_datetime = datetime.now() self.end_date_example = self.now_datetime + timedelta(weeks=5) db.session.add(self.first_user) db.session.add(self.second_user) db.session.commit() # create new mentorship relation self.mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(self.mentorship_relation) db.session.commit()
def create_relationship(self): """Creates relationship between verified and other user.""" relation = MentorshipRelationModel( self.other_user.id, self.other_user, self.verified_user, datetime.now().timestamp(), (datetime.now() + timedelta(weeks=5)).timestamp(), MentorshipRelationState.ACCEPTED, "notes", TasksListModel(), ) db.session.add(relation) db.session.commit()
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 test_dao_create_mentorship_relation_with_good_args_mentor_is_receiver(self): dao = MentorshipRelationDAO() data = dict( mentor_id=self.first_user.id, mentee_id=self.second_user.id, end_date=self.end_date_example.timestamp(), notes=self.notes_example, tasks_list=TasksListModel(), ) # Use DAO to create a mentorship relation result = dao.create_mentorship_relation(self.second_user.id, data) self.assertEqual(messages.MENTORSHIP_RELATION_WAS_SENT_SUCCESSFULLY, result[0]) query_mentorship_relation = MentorshipRelationModel.query.first() self.assertTrue(query_mentorship_relation is not None) self.assertEqual(1, query_mentorship_relation.id) # asserting relation extra fields self.assertEqual(self.second_user.id, query_mentorship_relation.action_user_id) self.assertEqual(self.notes_example, query_mentorship_relation.notes) # asserting dates # asserting dates self.assertIsNone(query_mentorship_relation.start_date) self.assertEqual( self.end_date_example.timestamp(), query_mentorship_relation.end_date ) # asserting mentor and mentees setup self.assertEqual(self.first_user.id, query_mentorship_relation.mentor_id) self.assertEqual(self.second_user.id, query_mentorship_relation.mentee_id) # assert mentors' mentor_relations and mentee_relations self.assertEqual(1, len(self.first_user.mentor_relations)) self.assertEqual(query_mentorship_relation, self.first_user.mentor_relations[0]) self.assertEqual([], self.first_user.mentee_relations) # assert mentees' mentor_relations and mentee_relations self.assertEqual(1, len(self.second_user.mentee_relations)) self.assertEqual( query_mentorship_relation, self.second_user.mentee_relations[0] ) self.assertEqual([], self.second_user.mentor_relations)
def test_dao_create_mentorship_relation_with_good_args_but_invalid_timestamp(self): dao = MentorshipRelationDAO() data = dict( mentor_id=self.first_user.id, mentee_id=self.second_user.id, end_date=1580338800000000, notes=self.notes_example, tasks_list=TasksListModel(), ) # Use DAO to create a mentorship relation result = dao.create_mentorship_relation(self.first_user.id, data) self.assertEqual(messages.INVALID_END_DATE, result[0]) self.assertEqual(400, result[1])
def setUp(self): db.create_all() self.first_user = UserModel( name=user1["name"], email=user1["email"], username=user1["username"], password=user1["password"], terms_and_conditions_checked=user1["terms_and_conditions_checked"], ) self.second_user = UserModel( name=user2["name"], email=user2["email"], username=user2["username"], password=user2["password"], terms_and_conditions_checked=user2["terms_and_conditions_checked"], ) self.notes_example = "description of a good mentorship relation" now_datetime = datetime.now() self.start_date_example = datetime(year=now_datetime.year + 1, month=3, day=1).timestamp() self.end_date_example = datetime(year=now_datetime.year + 1, month=5, day=1).timestamp() self.now_datetime = datetime.now().timestamp() db.session.add(self.first_user) db.session.add(self.second_user) db.session.commit() self.mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime, end_date=self.end_date_example, state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(self.mentorship_relation) db.session.commit()
def test_dao_create_mentorship_relation_with_non_existent_mentee(self): dao = MentorshipRelationDAO() data = dict( mentor_id=self.first_user.id, mentee_id=1234, end_date=self.end_date_example.timestamp(), notes=self.notes_example, tasks_list=TasksListModel(), ) # Use DAO to create a mentorship relation result = dao.create_mentorship_relation(self.first_user.id, data) self.assertDictEqual(messages.MENTEE_DOES_NOT_EXIST, result[0]) query_mentorship_relation = MentorshipRelationModel.query.first() self.assertIsNone(query_mentorship_relation)
def test_achievements(self): start_date = datetime.now() end_date = start_date + timedelta(weeks=4) start_date = start_date.timestamp() end_date = end_date.timestamp() tasks_list = TasksListModel() task_created_time = datetime.now().timestamp() task_completed_time = task_created_time + 100 tasks_list.add_task( description="Test task", created_at=task_created_time, is_done=True, completed_at=task_completed_time, ) tasks_list.add_task( description="Incomplete task: Should not appear in achievements", created_at=task_created_time, is_done=False, completed_at=task_completed_time, ) db.session.add(tasks_list) db.session.commit() mentorship_relation = MentorshipRelationModel( action_user_id=self.user2.id, mentor_user=self.user1, mentee_user=self.user2, creation_date=start_date, end_date=end_date, state=MentorshipRelationState.ACCEPTED, notes="", tasks_list=tasks_list, ) db.session.add(mentorship_relation) db.session.commit() expected_response = { "name": "User1", "pending_requests": 0, "accepted_requests": 1, "rejected_requests": 0, "completed_relations": 0, "cancelled_relations": 0, "achievements": [ { "completed_at": task_completed_time, "created_at": task_created_time, "description": "Test task", "id": 1, # This is the only task in the list "is_done": True, } ], } auth_header = get_test_request_header(self.user1.id) actual_response = self.client.get( "/home", follow_redirects=True, headers=auth_header ) self.assertEqual(200, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
class TasksBaseTestCase(BaseTestCase): # Setup consists of adding 2 users into the database # User 1 is the mentorship relation requester = action user # User 2 is the receiver def setUp(self): super(TasksBaseTestCase, self).setUp() self.first_user = UserModel( name=user1["name"], email=user1["email"], username=user1["username"], password=user1["password"], terms_and_conditions_checked=user1["terms_and_conditions_checked"], ) self.second_user = UserModel( name=user2["name"], email=user2["email"], username=user2["username"], password=user2["password"], terms_and_conditions_checked=user2["terms_and_conditions_checked"], ) self.fourth_user = UserModel( name=user4["name"], email=user4["email"], username=user4["username"], password=user4["password"], terms_and_conditions_checked=user4["terms_and_conditions_checked"], ) # making sure both are available to be mentor or mentee self.first_user.need_mentoring = True self.first_user.available_to_mentor = True self.first_user.is_email_verified = True self.second_user.need_mentoring = True self.second_user.available_to_mentor = True self.second_user.is_email_verified = True self.fourth_user.available_to_mentor = True self.fourth_user.is_email_verified = True self.notes_example = "description of a good mentorship relation" self.now_datetime = datetime.now() self.end_date_example = self.now_datetime + timedelta(weeks=5) self.tasks_list_1 = TasksListModel() self.tasks_list_2 = TasksListModel() self.tasks_list_3 = TasksListModel() db.session.add(self.tasks_list_1) db.session.add(self.tasks_list_2) db.session.add(self.tasks_list_3) db.session.add(self.first_user) db.session.add(self.second_user) db.session.add(self.fourth_user) db.session.commit() # create new mentorship relation self.mentorship_relation_w_second_user = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.ACCEPTED, notes=self.notes_example, tasks_list=self.tasks_list_1, ) self.mentorship_relation_w_admin_user = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.admin_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.ACCEPTED, notes=self.notes_example, tasks_list=self.tasks_list_2, ) self.mentorship_relation_without_first_user = MentorshipRelationModel( action_user_id=self.second_user.id, mentor_user=self.second_user, mentee_user=self.admin_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.COMPLETED, notes=self.notes_example, tasks_list=self.tasks_list_3, ) db.session.add(self.mentorship_relation_w_second_user) db.session.add(self.mentorship_relation_w_admin_user) db.session.add(self.mentorship_relation_without_first_user) db.session.commit() self.description_example = "This is an example of a description" self.tasks_list_1.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), ) self.tasks_list_1.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), is_done=True, completed_at=self.end_date_example.timestamp(), ) self.tasks_list_2.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), ) db.session.add(self.tasks_list_1) db.session.add(self.tasks_list_2) db.session.commit() self.test_description = "testing this description" self.test_is_done = False
def create_mentorship_relation(self, user_id: int, data: Dict[str, str]): """Creates a relationship between two users. Establishes the mentor-mentee relationship. Args: user_id: ID of the user initiating this request. Has to be either the mentor or the mentee. data: List containing the mentor_id, mentee_id, end_date_timestamp and notes. Returns: message: A message corresponding to the completed action; success if mentorship relationship is established, failure if otherwise. """ action_user_id = user_id mentor_id = data["mentor_id"] mentee_id = data["mentee_id"] end_date_timestamp = data["end_date"] notes = data["notes"] # user_id has to match either mentee_id or mentor_id is_valid_user_ids = action_user_id == mentor_id or action_user_id == mentee_id if not is_valid_user_ids: return messages.MATCH_EITHER_MENTOR_OR_MENTEE, HTTPStatus.BAD_REQUEST # mentor_id has to be different from mentee_id if mentor_id == mentee_id: return messages.MENTOR_ID_SAME_AS_MENTEE_ID, HTTPStatus.BAD_REQUEST try: end_date_datetime = datetime.fromtimestamp(end_date_timestamp) except ValueError: return messages.INVALID_END_DATE, HTTPStatus.BAD_REQUEST now_datetime = datetime.now() if end_date_datetime < now_datetime: return messages.END_TIME_BEFORE_PRESENT, HTTPStatus.BAD_REQUEST # business logic constraints max_relation_duration = end_date_datetime - now_datetime if max_relation_duration > self.MAXIMUM_MENTORSHIP_DURATION: return messages.MENTOR_TIME_GREATER_THAN_MAX_TIME, HTTPStatus.BAD_REQUEST if max_relation_duration < self.MINIMUM_MENTORSHIP_DURATION: return messages.MENTOR_TIME_LESS_THAN_MIN_TIME, HTTPStatus.BAD_REQUEST # validate if mentor user exists mentor_user = UserModel.find_by_id(mentor_id) if mentor_user is None: return messages.MENTOR_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND # validate if mentor is available to mentor if not mentor_user.available_to_mentor: return messages.MENTOR_NOT_AVAILABLE_TO_MENTOR, HTTPStatus.BAD_REQUEST # validate if mentee user exists mentee_user = UserModel.find_by_id(mentee_id) if mentee_user is None: return messages.MENTEE_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND # validate if mentee is wants to be mentored if not mentee_user.need_mentoring: return messages.MENTEE_NOT_AVAIL_TO_BE_MENTORED, HTTPStatus.BAD_REQUEST # TODO add tests for this portion all_mentor_relations = ( mentor_user.mentor_relations + mentor_user.mentee_relations ) for relation in all_mentor_relations: if relation.state == MentorshipRelationState.ACCEPTED: return messages.MENTOR_ALREADY_IN_A_RELATION, HTTPStatus.BAD_REQUEST all_mentee_relations = ( mentee_user.mentor_relations + mentee_user.mentee_relations ) for relation in all_mentee_relations: if relation.state == MentorshipRelationState.ACCEPTED: return messages.MENTEE_ALREADY_IN_A_RELATION, HTTPStatus.BAD_REQUEST # All validations were checked tasks_list = TasksListModel() tasks_list.save_to_db() mentorship_relation = MentorshipRelationModel( action_user_id=action_user_id, mentor_user=mentor_user, mentee_user=mentee_user, creation_date=datetime.now().timestamp(), end_date=end_date_timestamp, state=MentorshipRelationState.PENDING, notes=notes, tasks_list=tasks_list, ) mentorship_relation.save_to_db() return messages.MENTORSHIP_RELATION_WAS_SENT_SUCCESSFULLY, HTTPStatus.OK
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)
class TestUserDao(BaseTestCase): def setUp(self): super(TestUserDao, self).setUp() self.first_user = UserModel( name=user1["name"], email=user1["email"], username=user1["username"], password=user1["password"], terms_and_conditions_checked=user1["terms_and_conditions_checked"], ) self.second_user = UserModel( name=user2["name"], email=user2["email"], username=user2["username"], password=user2["password"], terms_and_conditions_checked=user2["terms_and_conditions_checked"], ) # making sure both are available to be mentor or mentee self.first_user.need_mentoring = True self.first_user.available_to_mentor = True self.first_user.is_email_verified = True self.second_user.need_mentoring = True self.second_user.available_to_mentor = True self.second_user.is_email_verified = True self.notes_example = "description of a good mentorship relation" self.now_datetime = datetime.now() self.end_date_example = self.now_datetime + timedelta(weeks=5) self.tasks_list_1 = TasksListModel() self.tasks_list_2 = TasksListModel() self.tasks_list_3 = TasksListModel() db.session.add(self.tasks_list_1) db.session.add(self.tasks_list_2) db.session.add(self.tasks_list_3) db.session.add(self.first_user) db.session.add(self.second_user) db.session.commit() # create new mentorship relation self.mentorship_relation1 = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.ACCEPTED, notes=self.notes_example, tasks_list=self.tasks_list_1, ) self.mentorship_relation2 = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=self.tasks_list_2, ) self.mentorship_relation3 = MentorshipRelationModel( action_user_id=self.second_user.id, mentor_user=self.second_user, mentee_user=self.first_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.CANCELLED, notes=self.notes_example, tasks_list=self.tasks_list_3, ) db.session.add(self.mentorship_relation1) db.session.add(self.mentorship_relation3) db.session.add(self.mentorship_relation2) db.session.commit() self.description_example = "This is an example of a description" self.tasks_list_1.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), ) self.tasks_list_1.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), is_done=True, completed_at=self.end_date_example.timestamp(), ) self.tasks_list_2.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), ) db.session.add(self.tasks_list_1) db.session.add(self.tasks_list_2) db.session.commit() self.test_description = "testing this description" self.test_is_done = False def test_dao_get_user_dashboard(self): expected_response = { "as_mentor": { "sent": { "accepted": [ DashboardRelationResponseModel( self.mentorship_relation1).response ], "rejected": [], "completed": [], "cancelled": [], "pending": [ DashboardRelationResponseModel( self.mentorship_relation2).response ], }, "received": { "accepted": [], "rejected": [], "completed": [], "cancelled": [], "pending": [], }, }, "as_mentee": { "sent": { "accepted": [], "rejected": [], "completed": [], "cancelled": [], "pending": [], }, "received": { "accepted": [], "rejected": [], "completed": [], "cancelled": [ DashboardRelationResponseModel( self.mentorship_relation3).response ], "pending": [], }, }, "tasks_todo": [self.tasks_list_1.find_task_by_id(1)], "tasks_done": [self.tasks_list_1.find_task_by_id(2)], } actual_response = UserDAO.get_user_dashboard(self.first_user.id) self.assertEqual(actual_response, expected_response)
def setUp(self): super(TestUserDao, self).setUp() self.first_user = UserModel( name=user1["name"], email=user1["email"], username=user1["username"], password=user1["password"], terms_and_conditions_checked=user1["terms_and_conditions_checked"], ) self.second_user = UserModel( name=user2["name"], email=user2["email"], username=user2["username"], password=user2["password"], terms_and_conditions_checked=user2["terms_and_conditions_checked"], ) # making sure both are available to be mentor or mentee self.first_user.need_mentoring = True self.first_user.available_to_mentor = True self.first_user.is_email_verified = True self.second_user.need_mentoring = True self.second_user.available_to_mentor = True self.second_user.is_email_verified = True self.notes_example = "description of a good mentorship relation" self.now_datetime = datetime.now() self.end_date_example = self.now_datetime + timedelta(weeks=5) self.tasks_list_1 = TasksListModel() self.tasks_list_2 = TasksListModel() self.tasks_list_3 = TasksListModel() db.session.add(self.tasks_list_1) db.session.add(self.tasks_list_2) db.session.add(self.tasks_list_3) db.session.add(self.first_user) db.session.add(self.second_user) db.session.commit() # create new mentorship relation self.mentorship_relation1 = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.ACCEPTED, notes=self.notes_example, tasks_list=self.tasks_list_1, ) self.mentorship_relation2 = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=self.tasks_list_2, ) self.mentorship_relation3 = MentorshipRelationModel( action_user_id=self.second_user.id, mentor_user=self.second_user, mentee_user=self.first_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.CANCELLED, notes=self.notes_example, tasks_list=self.tasks_list_3, ) db.session.add(self.mentorship_relation1) db.session.add(self.mentorship_relation3) db.session.add(self.mentorship_relation2) db.session.commit() self.description_example = "This is an example of a description" self.tasks_list_1.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), ) self.tasks_list_1.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), is_done=True, completed_at=self.end_date_example.timestamp(), ) self.tasks_list_2.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), ) db.session.add(self.tasks_list_1) db.session.add(self.tasks_list_2) db.session.commit() self.test_description = "testing this description" self.test_is_done = False
class TestTasksListModel(BaseTestCase): def setUp(self): super(TestTasksListModel, self).setUp() self.empty_tasks_list = TasksListModel() self.tasks_list_1 = TasksListModel() db.session.add(self.empty_tasks_list) db.session.add(self.tasks_list_1) db.session.commit() self.now_timestamp = datetime.now().timestamp() self.test_description_1 = "test description number one" self.test_description_2 = "test description number two" self.tasks_list_1.add_task(self.test_description_1, self.now_timestamp) def test_tasks_list_creation(self): tasks_list_one = TasksListModel.query.filter_by(id=1).first() self.assertIsNotNone(tasks_list_one) self.assertEqual([], tasks_list_one.tasks) self.assertEqual(1, tasks_list_one.next_task_id) self.assertEqual(1, tasks_list_one.id) def test_add_task_to_tasks_list(self): tasks_list_one = TasksListModel.query.filter_by(id=1).first() self.assertEqual([], tasks_list_one.tasks) tasks_list_one.add_task(self.test_description_1, self.now_timestamp) expected_task_1 = dict( completed_at=None, created_at=self.now_timestamp, description=self.test_description_1, id=1, is_done=False, ) self.assertEqual([expected_task_1], tasks_list_one.tasks) tasks_list_one.add_task(self.test_description_1, self.now_timestamp) expected_task_2 = dict( completed_at=None, created_at=self.now_timestamp, description=self.test_description_1, id=2, is_done=False, ) self.assertEqual([expected_task_1, expected_task_2], tasks_list_one.tasks) def test_remove_task_from_tasks_list(self): tasks_list_one = TasksListModel.query.filter_by(id=1).first() self.assertEqual([], tasks_list_one.tasks) tasks_list_one.add_task(self.test_description_1, self.now_timestamp) expected_task_1 = dict( completed_at=None, created_at=self.now_timestamp, description=self.test_description_1, id=1, is_done=False, ) self.assertEqual([expected_task_1], tasks_list_one.tasks) tasks_list_one.delete_task(task_id=1) self.assertEqual([], tasks_list_one.tasks) def test_remove_task_from_pre_filled_list(self): tasks_list_two = TasksListModel.query.filter_by(id=2).first() expected_task_1 = dict( completed_at=None, created_at=self.now_timestamp, description=self.test_description_1, id=1, is_done=False, ) self.assertEqual([expected_task_1], tasks_list_two.tasks) self.assertFalse(tasks_list_two.is_empty()) tasks_list_two.delete_task(task_id=1) self.assertEqual([], tasks_list_two.tasks) self.assertTrue(tasks_list_two.is_empty()) def test_empty_tasks_list_function(self): tasks_list_one = TasksListModel.query.filter_by(id=1).first() self.assertTrue(tasks_list_one.is_empty()) def test_find_task_by_id(self): tasks_list_one = TasksListModel.query.filter_by(id=1).first() expected_task_1 = dict( completed_at=None, created_at=self.now_timestamp, description=self.test_description_1, id=1, is_done=False, ) expected_task_2 = dict( completed_at=None, created_at=self.now_timestamp, description=self.test_description_2, id=2, is_done=False, ) tasks_list_one.add_task(self.test_description_1, self.now_timestamp) tasks_list_one.add_task(self.test_description_2, self.now_timestamp) found_task_1 = tasks_list_one.find_task_by_id(task_id=1) self.assertIsNotNone(found_task_1) self.assertEqual(expected_task_1, found_task_1) found_task_2 = tasks_list_one.find_task_by_id(task_id=2) self.assertIsNotNone(found_task_2) self.assertEqual(expected_task_2, found_task_2) found_task_3 = tasks_list_one.find_task_by_id(task_id=3) self.assertIsNone(found_task_3) def test_update_task(self): tasks_list_one = TasksListModel.query.filter_by(id=2).first() self.assertIsNotNone(tasks_list_one) task_1 = tasks_list_one.find_task_by_id(task_id=1) self.assertIsNotNone(task_1) self.assertFalse(task_1.get("is_done")) tasks_list_one.update_task(task_id=1, is_done=True) new_task_1 = tasks_list_one.find_task_by_id(task_id=1) self.assertTrue(new_task_1.get("is_done"))