Example #1
0
    def test_app_user_crud(self) -> None:
        """
        Creates and fetch data from db
        :return:
        """
        # Insert one
        app_user = AppUser('*****@*****.**', 'pass', AppUserRole.ADMIN)
        app_user1 = AppUserRepository.create(app_user)
        self.assertEqual(app_user, app_user1)

        app_users = AppUserRepository.get_all()
        self.assertEqual(1, len(app_users))
        self.assertEqual(app_users[0], app_user1)

        # Insert and get many
        AppUserRepository.create_many([
            AppUser('*****@*****.**', 'pass', AppUserRole.SERVICE),
            AppUser('*****@*****.**', 'pass', AppUserRole.ADMIN)
        ])
        app_users = AppUserRepository.get_all()
        self.assertEqual(3, len(app_users))

        # Get by id
        app_user1_from_db = AppUserRepository.get_by_id(app_user1.id)
        self.assertEqual(app_user1, app_user1_from_db)
Example #2
0
 def test_constraints(self) -> None:
     """
     Test constraints
     :return:
     """
     AppUserRepository.create(
         AppUser('*****@*****.**', 'pass', AppUserRole.ADMIN))
     self.assertRaises(
         IntegrityError, AppUserRepository.create,
         AppUser('*****@*****.**', 'pass', AppUserRole.ADMIN))
    def test_get_queued_job_to_process(self) -> None:
        # Should get no job
        no_job = JobRepository.get_queued_job_to_process()
        self.assertIsNone(no_job)

        # Should get job1
        app_user = AppUser('*****@*****.**', 'pass', AppUserRole.ADMIN)
        job1 = JobRepository.create(Job(app_user))
        JobTransitionRepository.create_many([JobTransition(JobStatusEnum.QUEUED, job1)]
                                            )
        queued_job = JobRepository.get_queued_job_to_process()
        self.assertEqual(job1, queued_job)

        # Should get job1 again even job2 is created
        job2 = JobRepository.create(Job(app_user))
        JobTransitionRepository.create_many([JobTransition(JobStatusEnum.QUEUED, job2)])
        queued_job = JobRepository.get_queued_job_to_process()
        self.assertEqual(job1, queued_job)

        # Should not get job1 cause it is finished, but job2
        JobTransitionRepository.create_many([
            JobTransition(JobStatusEnum.IN_PROGRESS, job1),
            JobTransition(JobStatusEnum.FINISHED, job1)
        ])

        finished_job = JobRepository.get_queued_job_to_process()
        self.assertEqual(job2, finished_job)
    def test_job_crud(self) -> None:
        """
        Creates and fetch data from db
        :return:
        """
        # Insert one
        app_user = AppUser('*****@*****.**', 'pass', AppUserRole.ADMIN)
        job = Job(app_user)
        job1 = JobRepository.create(job)
        self.assertEqual(job, job1)

        jobs = JobRepository.get_all()
        self.assertEqual(1, len(jobs))
        self.assertEqual(jobs[0], job1)

        # Insert and get many
        JobRepository.create_many([
            Job(app_user),
            Job(app_user)
        ])
        jobs = JobRepository.get_all()
        self.assertEqual(3, len(jobs))

        # Get by id
        job1_from_db = JobRepository.get_by_id(job1.id)
        self.assertEqual(job1, job1_from_db)
    def test_job_transition_crud(self) -> None:
        """
        Creates and fetch data from db
        :return:
        """
        # Insert one

        app_user = AppUser('*****@*****.**', 'pass', AppUserRole.ADMIN)
        job = Job(app_user)
        job_transition = JobTransition(JobStatusEnum.QUEUED, job)
        job_transition1 = JobTransitionRepository.create(job_transition)
        self.assertEqual(job_transition, job_transition1)

        job_transitions = JobTransitionRepository.get_all()
        self.assertEqual(1, len(job_transitions))
        self.assertEqual(job_transitions[0], job_transition1)

        # Insert and get many
        JobTransitionRepository.create_many([
            JobTransition(JobStatusEnum.IN_PROGRESS, job),
            JobTransition(JobStatusEnum.FINISHED, job)
        ])
        job_transitions = JobTransitionRepository.get_all()
        self.assertEqual(3, len(job_transitions))

        # Get by id
        job_transition_from_db = JobTransitionRepository.get_by_id(
            job_transition1.id)
        self.assertEqual(job_transition1, job_transition_from_db)
 def test_constraints(self) -> None:
     """
     Test constraints
     :return:
     """
     app_user = AppUser('*****@*****.**', 'pass', AppUserRole.ADMIN)
     job = Job(app_user)
     JobTransitionRepository.create(JobTransition(JobStatusEnum.QUEUED,
                                                  job))
     self.assertRaises(IntegrityError, JobTransitionRepository.create,
                       JobTransition(JobStatusEnum.QUEUED, job))
def register_user(username: str, password: str, role: AppUserRole) -> AppUser:
    """
    Registers new user for given email, password and role.
    """
    normalized_username = username.lower()
    _assert_user_registration(normalized_username, password, role)

    user = AppUser(
        username=normalized_username,
        pass_hash=encode_password(password),
        role=role,
    )
    return AppUserRepository.create(user)
Example #8
0
 def _create_app_user(self) -> AppUser:
     return AppUserRepository.create(
         AppUser('*****@*****.**', 'pass', AppUserRole.ADMIN))