Exemplo n.º 1
0
    def test_get_xml_metadata(self, validate_mods_3_7, ddc, abstract_required,
                              upload_data):
        """
        Validates against mods 3.7 schema
        """
        self.protocol.paper = upload_data['paper']

        # Set POST data for form
        data = dict()
        if upload_data['oairecord'].description is not None:
            data['abstract'] = upload_data['oairecord'].description
        elif abstract_required:
            data['abstract'] = upload_data['abstract']

        if ddc is not None:
            data['ddc'] = [
                ddc
                for ddc in DDC.objects.filter(number__in=upload_data['ddc'])
            ]

        form = SWORDMETSForm(ddcs=ddc,
                             abstract_required=abstract_required,
                             data=data)
        form.is_valid()

        xml = self.protocol._get_xml_metadata(form)

        print("")
        print(
            etree.tostring(xml,
                           pretty_print=True,
                           encoding='utf-8',
                           xml_declaration=True).decode())

        validate_mods_3_7(xml)
Exemplo n.º 2
0
    def test_get_xml_dissemin_metadata(self, db, monkeypatch_paper_is_owned,
                                       dissemin_xsd_1_0, depositing_user,
                                       license_chooser, load_json, oairecord):
        """
        Tests for dissemin metadata
        """
        upload_data = load_json.load_upload(oairecord)
        self.protocol.paper = upload_data['paper']
        self.protocol.user = depositing_user

        # Set POST data for form
        data = dict()

        if license_chooser:
            data['license'] = license_chooser.pk
        data['email'] = depositing_user.email

        form = SWORDMETSForm(licenses=LicenseChooser.objects.by_repository(
            repository=self.protocol.repository),
                             data=data)
        form.is_valid()

        xml = self.protocol._get_xml_dissemin_metadata(form)

        # When using pytest -s, show resulting xml
        print("")
        print(
            etree.tostring(xml,
                           pretty_print=True,
                           encoding='utf-8',
                           xml_declaration=True).decode())

        dissemin_xsd_1_0.assertValid(xml)
Exemplo n.º 3
0
    def test_write_mets_metadata_examples(self, db, upload_data, user_leibniz):
        """
        This is not really a test. It just outputs metadata examples that the protocol generates.
        Ususally this test is omitted, you can run it explicetely with "-m write_mets_examples".
        For any subclass, make sure to set ``path_metadata_examples``. This is the place where the file will created. You can then include them in the documentation.
        In case of changes of the protocol or repository, you should run this function, but make sure it's up to date
        """
        self.protocol.paper = upload_data['paper']
        self.protocol.user = user_leibniz

        Researcher.create_by_name(
            user=user_leibniz,
            first=user_leibniz.first_name,
            last=user_leibniz.last_name,
            orcid="2543-2454-2345-234X",
        )

        data = dict()
        data['email'] = user_leibniz.email

        if upload_data['oairecord'].description is not None:
            data['abstract'] = upload_data['oairecord'].description
        else:
            data['abstract'] = upload_data['abstract']

        ddcs = DDC.objects.all()
        data['ddc'] = [
            ddc for ddc in ddcs.filter(number__in=upload_data['ddc'])
        ]

        l = License.objects.get(
            uri="https://creativecommons.org/licenses/by/4.0/")
        lc = LicenseChooser.objects.create(license=l,
                                           repository=self.protocol.repository,
                                           transmit_id='cc_by-40')
        licenses = LicenseChooser.objects.by_repository(
            repository=self.protocol.repository)
        data['license'] = lc.pk

        form = SWORDMETSForm(ddcs=ddcs, licenses=licenses, data=data)

        valid_form = form.is_valid()
        if not valid_form:
            print(form.errors)
        assert valid_form == True

        dissemin_xml = self.protocol._get_xml_dissemin_metadata(form)
        metadata_xml = self.protocol._get_xml_metadata(form)
        mets_xml = self.protocol._get_mets(metadata_xml, dissemin_xml)

        # Here we write the file. We take the base path, extend with protocl specific path and write it out
        f_path = os.path.join(BASE_DIR, 'doc', 'sphinx', 'examples',
                              self.path_metadata_examples)
        f_name = os.path.join(f_path, upload_data['load_name'] + '.xml')
        os.makedirs(f_path, exist_ok=True)
        with open(f_name, 'w') as fout:
            fout.write(mets_xml)
Exemplo n.º 4
0
    def test_get_mets_integration(self, mets_xsd, depositing_user, upload_data,
                                  ddc, license_chooser, abstract_required,
                                  embargo):
        """
        Integration test running all possible metadata cases and validating against mets schema
        """
        self.protocol.paper = upload_data['paper']
        self.protocol.user = depositing_user

        # Set POST data for form
        data = dict()
        data['email'] = depositing_user.email
        if upload_data['oairecord'].description is not None:
            data['abstract'] = upload_data['oairecord'].description
        elif abstract_required:
            data['abstract'] = upload_data['abstract']

        if ddc is not None:
            data['ddc'] = [
                ddc
                for ddc in DDC.objects.filter(number__in=upload_data['ddc'])
            ]

        if embargo == 'optional':
            data['embargo'] = upload_data.get('embargo', None)
        if embargo == 'required':
            data['embargo'] = '2019-10-10'

        licenses = None
        if license_chooser:
            data['license'] = license_chooser.pk
            licenses = LicenseChooser.objects.by_repository(
                repository=self.protocol.repository)

        form = SWORDMETSForm(ddcs=ddc,
                             licenses=licenses,
                             abstract_required=abstract_required,
                             embargo=embargo,
                             data=data)
        valid_form = form.is_valid()
        if not valid_form:
            print(form.errors)
        assert valid_form == True

        dissemin_xml = self.protocol._get_xml_dissemin_metadata(form)
        metadata_xml = self.protocol._get_xml_metadata(form)
        mets_xml = self.protocol._get_mets(metadata_xml, dissemin_xml)

        # Because of the xml declaration we have to convert to a bytes object
        mets_xsd.assertValid(
            etree.fromstring(bytes(mets_xml, encoding='utf-8')))