示例#1
0
class TestGet(TestJobQueue):
    @given(services())
    def test_no_jobs_returns_204(self, service: Service) -> None:
        assume(len(service.jobs) == 0)
        endpoint = JobQueueForService(
            self.session, self.request, self.service_list
        )
        response = endpoint.get(service)
        self.assertEqual(204, response.status_code)

    @given(services())
    @settings(perform_health_check=False)
    def test_jobs_registered_returns_200(self, service: Service) -> None:
        assume(len(self._registered_jobs_for_service(service)) > 0)
        endpoint = JobQueueForService(
            self.session, self.request, self.service_list
        )
        response = endpoint.get(service)
        self.assertEqual(200, response.status_code)

    @staticmethod
    def _registered_jobs_for_service(service: Service) -> Sized:
        return set(filter(
            lambda job: job.status is job.JobStatus.REGISTERED, service.jobs
        ))
示例#2
0
class TestSingleServiceSerialization(TestServiceDetail):
    """
    Contains unit tests for the serializer in order to control serialization
    to a single service
    """
    @given(services())
    def test_serialize_service_without_jobs(self, service: Service) -> None:
        assume(not service.jobs)
        _, errors = self.serializer.dump(service, many=False)
        self.assertFalse(errors)

    @given(services())
    def test_serialize_one_service(self, service: Service) -> None:
        """
        Tests that a serializer successfully serializes to one service

        :param service: The randomly-generated service to serialize
        """
        data, errors = self.serializer.dump(service, many=False)
        self.assertFalse(errors)

    @given(service_lists())
    def test_serialize_many_services(self, service_list: ServiceList) -> None:
        """

        :param service_list: A list of randomly-generated services
        """
        _, errors = self.serializer.dump(service_list, many=True)
        self.assertFalse(errors)
示例#3
0
def service_lists(
        draw,
        min_size=None, average_size=None, max_size=None,
        unique_by=None, unique=False
) -> _ServiceList:
    service_generator = lists(
        services(), min_size=min_size, average_size=average_size,
        max_size=max_size, unique_by=unique_by, unique=unique
    )
    return _ServiceList(draw(service_generator))
示例#4
0
class TestGet(TestNextJob):
    @given(
        services(
            service_job_lists=job_lists(min_size=1, jobs=registered_jobs())))
    def test_get_job_available(self, service: Service) -> None:
        endpoint = NextJob(self.session, self.request)
        response = endpoint.get(service)
        self.assertEqual(200, response.status_code)

    @given(services(service_job_lists=just(list())))
    def test_get_job_unavailable(self, service: Service) -> None:
        assume(len(self._registered_jobs(service)) == 0)
        endpoint = NextJob(self.session, self.request)
        response = endpoint.get(service)
        self.assertEqual(204, response.status_code)

    @staticmethod
    def _registered_jobs(service: Service) -> Sequence[Job]:
        return tuple(job for job in service.jobs
                     if job.status == Job.JobStatus.REGISTERED)
示例#5
0
def service_lists(draw,
                  min_size=None,
                  average_size=None,
                  max_size=None,
                  unique_by=None,
                  unique=False) -> _ServiceList:
    service_generator = lists(services(),
                              min_size=min_size,
                              average_size=average_size,
                              max_size=max_size,
                              unique_by=unique_by,
                              unique=unique)
    return _ServiceList(draw(service_generator))
示例#6
0
class TestGet(TestJobsForService):
    """
    Contains unit tests for the getter
    """
    @given(services())
    def test_get(self, service: Service) -> None:
        """

        :param service: The service for which the endpoint is to be tested
        :return:
        """
        endpoint = JobsForServiceEndpoint(
            self.session, self.request, self.service_list
        )
        response = endpoint.get(service)
        self.assertEqual(200, response.status_code)

        serializer = JobDetailSerializer()
        self.assertEqual(
            json.loads(response.data.decode('utf-8'))['data'],
            serializer.dump(service.jobs, many=True).data
        )
示例#7
0
class TestPost(TestJobsForService):
    """
    Contains unit tests for the ``POST`` method of this endpoint, testing
    whether jobs can be made successfully
    """
    @given(dictionaries(text(), text()), services())
    def test_serialization_error(
            self, invalid_parameters: dict,
            service: Service
    ) -> None:
        """

        :param invalid_parameters: The incorrect parameters to test
        :param service: The service for which the job is to be made
        """
        serializer = NewJobSerializer()
        data, errors = serializer.load(invalid_parameters)
        assume(errors)

        self.request.get_json = mock.MagicMock(return_value=data)
        endpoint = JobsForServiceEndpoint(
            self.session, self.request, self.service_list
        )
        with self.assertRaises(endpoint.Abort):
            endpoint.post(service)

    @given(
        fixed_dictionaries({
            "parameters": dictionaries(text(), text())
        }),
        services()
    )
    def test_validation_error(
            self, parameters: dict, service: Service
    ) -> None:
        """

        :param parameters: The parameters that do not satisfy the JSON schema
        :param service: The service to test
        """
        validator = mock.MagicMock(spec=JSONSchemaValidator)
        validator_factory = mock.MagicMock(return_value=validator)
        validator.is_valid = mock.MagicMock(return_value=False)
        validator.iter_errors = mock.MagicMock(
            return_value={'error': 'message'}
        )

        self.request.get_json = mock.MagicMock(return_value=parameters)

        endpoint = JobsForServiceEndpoint(
            self.session, self.request, self.service_list, validator_factory
        )
        with self.assertRaises(endpoint.Abort):
            endpoint.post(service)

        self.assertTrue(endpoint.errors)

    @given(
        fixed_dictionaries({
            'parameters': dictionaries(text(), text())
        }),
        services()
    )
    def test_valid_post(
            self, parameters: dict, service: Service
    ) -> None:
        """
        Test that the job can be correctly created

        :param parameters: The new job parameters
        :param service: The service for which the job is to be created
        """
        validator = mock.MagicMock(spec=JSONSchemaValidator)
        validator_factory = mock.MagicMock(return_value=validator)
        validator.is_valid = mock.MagicMock(return_value=True)

        self.request.get_json = mock.MagicMock(return_value=parameters)

        endpoint = JobsForServiceEndpoint(
            self.session, self.request, self.service_list, validator_factory
        )
        response = endpoint.post(service)

        self.assertEqual(response.status_code, 201)
        self.assertIn('Location', response.headers)