Пример #1
0
    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"))
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)
Пример #3
0
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
Пример #4
0
    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)
    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))
Пример #6
0
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"))