Exemplo n.º 1
0
 def get_by_name(record_name):
     try:
         return LocalId.objects.get(record_name=record_name)
     except mongoengine_errors.DoesNotExist as dne:
         raise exceptions.DoesNotExist(str(dne))
     except Exception as exc:
         raise exceptions.ModelError(str(exc))
Exemplo n.º 2
0
    def get_by_id(data_structure_id):
        """ Returns the object with the given id

        Args:
            data_structure_id:

        Returns:
            Data Structure (obj): DataStructure object with the given id

        """
        # FIXME: temporary solution to query concrete children of abstract class
        # (https://github.com/MongoEngine/mongoengine/issues/741)
        data_structure = None
        # iterate concrete data structure classes
        for subclass in DataStructure.__subclasses__():
            try:
                # get data structure from concrete subclass
                data_structure = subclass.get_by_id(data_structure_id)
                # break if found
                break
            except:
                # data structure not found, continue search
                pass
        # data structure found
        if data_structure is not None:
            # return data structure
            return data_structure
        else:
            # raise exception
            raise exceptions.DoesNotExist("No data structure found for the given id.")
Exemplo n.º 3
0
    def test_list_records_no_set_or_bad_set(
        self,
        mock_get,
        mock_request,
        mock_get_templates_id,
        mock_get_by_metadata_prefix,
        mock_get_template_ids,
        mock_get_by_set_spec,
    ):
        # Arrange
        mock_get.return_value = _create_mock_oai_settings()
        mock_request.return_value = ""
        mock_get_templates_id.return_value = []
        mock_get_by_metadata_prefix.return_value = []
        mock_get_template_ids.return_value = [ObjectId()]
        mock_get_by_set_spec.side_effect = common_exceptions.DoesNotExist("")
        data = {
            "verb": "ListRecords",
            "metadataPrefix": "dummy",
            "set": "dummy_set"
        }

        # Act
        response = RequestMock.do_request_get(OAIProviderView.as_view(),
                                              None,
                                              data=data)

        # Assert
        self.assertTrue(
            isinstance(response.context_data["errors"][0],
                       exceptions.NoRecordsMatch))
        self.check_tag_error_code(response.rendered_content,
                                  exceptions.NO_RECORDS_MATCH)
Exemplo n.º 4
0
 def test_data_get_by_id_raises_api_error_if_not_found(self, mock_get):
     # Arrange
     mock_get.side_effect = exceptions.DoesNotExist("")
     mock_user = create_mock_user("1")
     # Act # Assert
     with self.assertRaises(exceptions.DoesNotExist):
         data_api.get_by_id(1, mock_user)
Exemplo n.º 5
0
def get_by_template_hash(template_hash):
    """Get TemplateXslRendering by its template hash.

    Args:
        template_hash: Template hash.

    Returns:
        The TemplateXslRendering instance.

    """
    instance = None
    # Get list of templates with the given hash
    templates = template_api.get_all_by_hash(template_hash)
    # Check if one of the templates has a xslt. Stop when found.
    # FIXME: Check if this process is okay (no solutions to distinguish templates from same hash)
    for template in templates:
        try:
            instance = TemplateXslRendering.get_by_template_id(template.id)
            break
        except exceptions.DoesNotExist:
            pass

    if instance is None:
        raise exceptions.DoesNotExist(
            "No TemplateXslRendering found with the given template hash")

    return instance
Exemplo n.º 6
0
    def test_list_metadata_format_identifier_does_not_exist(
        self,
        mock_get,
        mock_request,
        mock_get_all,
        mock_check_identifier,
        mock_get_by_id,
    ):
        # Arrange
        mock_get.return_value = _create_mock_oai_settings()
        mock_request.return_value = ""
        mock_get_all.return_value = []
        mock_check_identifier.return_value = ObjectId()
        mock_get_by_id.side_effect = common_exceptions.DoesNotExist("")
        data = {"verb": "ListMetadataFormats", "identifier": "dummy"}

        # Act
        response = RequestMock.do_request_get(OAIProviderView.as_view(),
                                              None,
                                              data=data)

        # Assert
        self.assertTrue(
            isinstance(response.context_data["errors"][0],
                       exceptions.IdDoesNotExist))
        self.check_tag_error_code(response.rendered_content,
                                  exceptions.ID_DOES_NOT_EXIST)
Exemplo n.º 7
0
    def test_get_record_metadata_format_does_not_exist(
        self,
        mock_get,
        mock_request,
        mock_check_identifier,
        mock_get_by_data,
        mock_get_by_metadata_prefix,
    ):
        # Arrange
        mock_get.return_value = _create_mock_oai_settings()
        mock_request.return_value = ""
        mock_check_identifier.return_value = ObjectId()
        mock_get_by_data.return_value = object()
        mock_get_by_metadata_prefix.side_effect = common_exceptions.DoesNotExist(
            "")
        data = {
            "verb": "GetRecord",
            "metadataPrefix": "dummy",
            "identifier": "dummy"
        }

        # Act
        response = RequestMock.do_request_get(OAIProviderView.as_view(),
                                              None,
                                              data=data)

        # Assert
        self.assertTrue(
            isinstance(response.context_data["errors"][0],
                       exceptions.CannotDisseminateFormat))
        self.check_tag_error_code(response.rendered_content,
                                  exceptions.DISSEMINATE_FORMAT)
Exemplo n.º 8
0
    def test_get_raises_exception_if_object_does_not_exist(self, mock_get):
        # Arrange
        mock_get.side_effect = exceptions.DoesNotExist("Error")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            settings_api.get()
Exemplo n.º 9
0
 def test_data_structure_element_get_by_id_raises_does_not_exist_error_if_not_found(
         self, mock_get):
     # Arrange
     mock_get.side_effect = exceptions.DoesNotExist('')
     # Act # Assert
     with self.assertRaises(exceptions.DoesNotExist):
         data_structure_element_api.get_by_id(1)
 def test_saved_query_get_by_id_raises_api_error_if_not_found(
         self, mock_get):
     # Arrange
     mock_get.side_effect = exceptions.DoesNotExist("")
     # Act # Assert
     with self.assertRaises(exceptions.DoesNotExist):
         saved_query_api.get_by_id(1)
Exemplo n.º 11
0
    def test_xsl_transformation_get_by_id_raises_api_error_if_does_not_exisst(
            self, mock_get):
        # Arrange
        mock_get.side_effect = exceptions.DoesNotExist("")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            xsl_transformation_api.get_by_id(1)
 def test_saved_query_get_by_name_and_url_raises_api_error_if_not_found(self, mock_get):
     # create query
     query = _create_query()
     # Arrange
     mock_get.side_effect = exceptions.DoesNotExist('')
     # Act # Assert
     with self.assertRaises(exceptions.DoesNotExist):
         query_api.get_data_source_by_name_and_url_query(query, "name", "url")
Exemplo n.º 13
0
 def test_get_absent_slug_or_template_raises_DoesNotExist(
         self, get_custom_resource_by_template_and_slug):
     # Arrange
     get_custom_resource_by_template_and_slug.side_effect = exceptions.DoesNotExist(
         "error")
     # Assert
     with self.assertRaises(exceptions.DoesNotExist):
         custom_resource_api.get_by_current_template_and_slug("test")
Exemplo n.º 14
0
    def test_template_get_raises_exception_if_object_does_not_exist(self, mock_get_by_id):
        # Arrange
        mock_absent_id = ObjectId()
        mock_get_by_id.side_effect = exceptions.DoesNotExist('')

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            type_api.get(mock_absent_id)
Exemplo n.º 15
0
 def get_by_resumption_token(resumption_token):
     try:
         return OaiRequestPage.objects.get(
             resumption_token=resumption_token)
     except mongoengine_errors.DoesNotExist as e:
         raise exceptions.DoesNotExist(str(e))
     except Exception as ex:
         raise exceptions.ModelError(str(ex))
Exemplo n.º 16
0
 def test_curate_data_structure_get_by_user_and_template_and_name_raises_does_not_exist_error_if_not_found(
         self, mock_get):
     # Arrange
     mock_get.side_effect = exceptions.DoesNotExist("")
     # Act # Assert
     with self.assertRaises(exceptions.DoesNotExist):
         curate_data_structure_api.get_by_user_id_and_template_id_and_name(
             1, 1, "name_title_1")
Exemplo n.º 17
0
    def test_get_by_id_raises_exception_if_object_does_not_exist(self, mock_get_by_id):
        # Arrange
        mock_absent_id = ObjectId()

        mock_get_by_id.side_effect = exceptions.DoesNotExist("Error.")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            harvester_metadata_format_api.get_by_id(mock_absent_id)
Exemplo n.º 18
0
    def test_oai_set_get_by_set_spec_raises_exception_if_object_does_not_exist(self, mock_get_by_set_spec):
        # Arrange
        mock_absent_set_spec = "oai_test"

        mock_get_by_set_spec.side_effect = exceptions.DoesNotExist("Error.")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            set_api.get_by_set_spec(mock_absent_set_spec)
Exemplo n.º 19
0
    def test_get_by_id_raises_exception_if_object_does_not_exist(self, mock_get_by_id):
        # Arrange
        mock_absent_id = ObjectId()

        mock_get_by_id.side_effect = exceptions.DoesNotExist("Error.")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            template_xsl_rendering_api.get_by_id(mock_absent_id)
Exemplo n.º 20
0
    def test_get_by_template_hash_raises_exception_if_object_does_not_exist(
            self, mock_get_by_template_hash):
        # Arrange
        mock_absent_hash = "dummy_hash"
        mock_get_by_template_hash.side_effect = exceptions.DoesNotExist(
            "Error.")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            template_xsl_rendering_api.get_by_template_hash(mock_absent_hash)
Exemplo n.º 21
0
    def test_bucket_get_by_id_raises_exception_if_object_does_not_exist(
        self, mock_get_by_id
    ):
        # Arrange
        mock_absent_id = ObjectId()
        mock_get_by_id.side_effect = exceptions.DoesNotExist("")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            bucket_api.get_by_id(mock_absent_id)
Exemplo n.º 22
0
 def get_by_class_and_id(record_object_class, record_object_id):
     try:
         return LocalId.objects.get(
             record_object_class=record_object_class,
             record_object_id=record_object_id,
         )
     except mongoengine_errors.DoesNotExist as dne:
         raise exceptions.DoesNotExist(str(dne))
     except Exception as exc:
         raise exceptions.ModelError(str(exc))
    def test_get_by_data_raises_exception_if_object_does_not_exist(
            self, mock_get_by_data):
        # Arrange
        mock_absent_data = Data()

        mock_get_by_data.side_effect = exceptions.DoesNotExist("Error.")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            oai_data_api.get_by_data(mock_absent_data)
    def test_get_by_id_raises_exception_if_object_does_not_exist(
            self, mock_get_by_id):
        # Arrange
        mock_absent_id = ObjectId()

        mock_get_by_id.side_effect = exceptions.DoesNotExist("Error.")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            data_structure_type_element_api.get_by_id(mock_absent_id)
Exemplo n.º 25
0
    def test_get_by_registry_id_raises_exception_if_object_does_not_exist(
            self, mock_get):
        # Arrange
        mock_absent_registry_id = str(ObjectId())

        mock_get.side_effect = exceptions.DoesNotExist("Error.")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            oai_identify_api.get_by_registry_id(mock_absent_registry_id)
Exemplo n.º 26
0
    def test_oai_metadata_format_get_by_metadata_prefix_raises_exception_if_object_does_not_exist(
            self, mock_get_by):
        # Arrange
        mock_absent_metadata_prefix = "oai_test"

        mock_get_by.side_effect = exceptions.DoesNotExist("Error")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            metadata_format_api.get_by_metadata_prefix(
                mock_absent_metadata_prefix)
Exemplo n.º 27
0
    def get_global_workspace():
        """ Get global workspace.

        Returns:
        """
        try:
            return Workspace.objects.get(owner=None, is_public=True)
        except mongoengine_errors.DoesNotExist as e:
            raise exceptions.DoesNotExist(str(e))
        except Exception as ex:
            raise exceptions.ModelError(str(ex))
Exemplo n.º 28
0
    def test_get_by_metadata_prefix_and_registry_id_raises_exception_if_object_does_not_exist(self, mock_get):
        # Arrange
        mock_absent_metadata_prefix = 'oai_test'
        mock_absent_registry_id = str(ObjectId())

        mock_get.side_effect = exceptions.DoesNotExist("Error.")

        # Act + Assert
        with self.assertRaises(exceptions.DoesNotExist):
            harvester_metadata_format_api.get_by_metadata_prefix_and_registry_id(mock_absent_metadata_prefix,
                                                                                 mock_absent_registry_id)
    def test_select_metadata_format_not_found(self, mock_get_by_id):
        # Arrange
        mock_get_by_id.side_effect = exceptions.DoesNotExist("Error")

        # Act
        response = RequestMock.\
            do_request_get(rest_oai_provider_metadata_format.MetadataFormatDetail.as_view(),
                           user=create_mock_user("1", is_staff=True), param=self.param)

        # Assert
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
    def test_add_template_metadata_format_raises_exception_if_template_does_not_exist(self, mock_get_by_id):
        # Arrange
        mock_get_by_id.side_effect = exceptions.DoesNotExist("Error")

        # Act
        response = RequestMock.\
            do_request_post(rest_oai_provider_metadata_format.TemplateAsMetadataFormat.as_view(),
                            user=create_mock_user("1", is_staff=True), data=self.data)

        # Assert
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)