Ejemplo n.º 1
0
 def test_validation_no_parameter_field(self):
     """Validation must fail if the parameter field is not present"""
     serializer = serializers.JobSerializer(data={'action': 'download'})
     with self.assertRaises(ValidationError) as assert_raises:
         self.assertFalse(serializer.is_valid(raise_exception=True))
     self.assertDictEqual(
         assert_raises.exception.detail, {
             'parameters': [
                 ErrorDetail(string='This field is required.',
                             code='required')
             ]
         })
Ejemplo n.º 2
0
    def test_finished_job_representation_no_success(self):
        """
        The representation of a job which has finished executing
        should have the following fields:
          - id
          - task_id
          - status
          - date_created
          - date_done
          - result if the status is success
        """
        expected_base_dict = {
            "id": 1,
            "task_id": "df2bfb58-7d2e-4f83-9dc2-bac95a421c72",
            "status": 'PLACEHOLDER',
            "date_created": '2020-07-16T13:53:30Z',
            "date_done": 'PLACEHOLDER'
        }
        with mock.patch.object(models.Job,
                               'get_current_task_result') as mock_get_result:
            mock_result = mock.Mock()
            mock_result.state = 'PLACEHOLDER'
            mock_result.date_done = 'PLACEHOLDER'
            mock_result.result = 'PLACEHOLDER'
            mock_get_result.return_value = (mock_result, True)
            self.assertDictEqual(
                serializers.JobSerializer().to_representation(
                    models.Job.objects.get(id=1)), expected_base_dict)

            mock_result.state = 'SUCCESS'
            expected_base_dict['status'] = 'SUCCESS'
            self.assertDictEqual(
                serializers.JobSerializer().to_representation(
                    models.Job.objects.get(id=1)), {
                        **expected_base_dict, 'result': 'PLACEHOLDER'
                    })
Ejemplo n.º 3
0
 def test_launch_download(self):
     """The download task must be called with the right parameters"""
     validated_data = {
         'action': 'download',
         'parameters': {
             'dataset_id': 1
         }
     }
     serializer = serializers.JobSerializer()
     with mock.patch.object(models.DownloadJob,
                            'signature') as mock_signature:
         mock_signature.delay.return_value.task_id = 1
         serializer.create(validated_data)
         mock_signature.delay.assert_called_with(
             (validated_data['parameters']['dataset_id'], ))
Ejemplo n.º 4
0
 def test_launch_idf_conversion(self):
     """The convert_to_idf task must be called with the right parameters"""
     validated_data = {
         'action': 'convert',
         'parameters': {
             'dataset_id': 1,
             'format': 'idf'
         }
     }
     serializer = serializers.JobSerializer()
     with mock.patch.object(models.ConvertJob,
                            'signature') as mock_signature:
         mock_signature.delay.return_value.task_id = 1
         serializer.create(validated_data)
         mock_signature.delay.assert_called_with(
             (validated_data['parameters']['dataset_id'], ))
Ejemplo n.º 5
0
 def test_validation_empty_data(self):
     """Validation must fail if the data is an empty dict"""
     serializer = serializers.JobSerializer(data={})
     with self.assertRaises(ValidationError) as assert_raises:
         self.assertFalse(serializer.is_valid(raise_exception=True))
     self.assertDictEqual(
         assert_raises.exception.detail, {
             'action': [
                 ErrorDetail(string='This field is required.',
                             code='required')
             ],
             'parameters': [
                 ErrorDetail(string='This field is required.',
                             code='required')
             ]
         })
Ejemplo n.º 6
0
 def test_validation_parameter_field_not_dict(self):
     """Validation must fail if the parameter field is not a dictionary"""
     serializer = serializers.JobSerializer(data={
         'action': 'download',
         'parameters': ''
     })
     with self.assertRaises(ValidationError) as assert_raises:
         self.assertFalse(serializer.is_valid(raise_exception=True))
     self.assertDictEqual(
         assert_raises.exception.detail, {
             'parameters': [
                 ErrorDetail(
                     string=
                     'Expected a dictionary of items but got type "str".',
                     code='not_a_dict')
             ]
         })
Ejemplo n.º 7
0
 def test_unfinished_job_representation(self):
     """
     The representation of a job which has not finished executing
     should have the following fields:
       - id
       - task_id
       - status
       - date_created
     """
     with mock.patch.object(models.Job,
                            'get_current_task_result') as mock_get_result:
         mock_result = mock.Mock()
         mock_result.state = 'PLACEHOLDER'
         mock_get_result.return_value = (mock_result, False)
         self.assertDictEqual(
             serializers.JobSerializer().to_representation(
                 models.Job.objects.get(id=1)), {
                     "id": 1,
                     "task_id": "df2bfb58-7d2e-4f83-9dc2-bac95a421c72",
                     "status": 'PLACEHOLDER',
                     "date_created": '2020-07-16T13:53:30Z',
                 })
Ejemplo n.º 8
0
 def test_validation_return_value(self):
     """Test tha the validate() method returns the validated data"""
     request_data = {'action': 'download', 'parameters': {'dataset_id': 1}}
     serializer = serializers.JobSerializer()
     self.assertDictEqual(serializer.validate(request_data), request_data)