Пример #1
0
    def test_bind_pids(self, mocker):
        """Test the bind_pids function end-to-end and ensure that the
        result is that which is anticipated.

        The bind_pids module is responsible for binding persistent identifiers
        to the SIP and the SIP's directories so we only test that here.
        """
        # We might want to return a unique accession number, but we can also
        # test here using the package UUID, the function's fallback position.
        mocker.patch.object(bind_pids,
                            "_get_unique_acc_no",
                            return_value=self.package_uuid)
        mocker.patch.object(bind_pids,
                            "_validate_handle_server_config",
                            return_value=None)
        with vcr_cassettes.use_cassette(
                "test_bind_pids_to_sip_and_dirs.yaml") as cassette:
            # Primary entry-point for the bind_pids microservice job.
            bind_pids.main(self.job, self.package_uuid, "")
        assert cassette.all_played
        sip_mdl = SIP.objects.filter(uuid=self.package_uuid).first()
        assert len(sip_mdl.identifiers.all()) == len(
            self.bound_identifier_types
        ), "Number of SIP identifiers is greater than anticipated"
        dirs = Directory.objects.filter(sip=self.package_uuid).all()
        assert len(dirs) == len(
            self.package_directories), "Number of directories is incorrect"
        for mdl in chain(dirs, (sip_mdl, )):
            bound = [(idfr.type, idfr.value) for idfr in mdl.identifiers.all()]
            assert len(bound) == len(self.bound_identifier_types)
            pid_types = []
            for pid in bound:
                pid_types.append(pid[0])
            assert (
                "hdl" in pid_types
            ), "An expected hdl persistent identifier isn't in the result set"
            assert "URI" in pid_types, "An expected URI isn't in the result set"
            bound_hdl = "{}{}".format(self.bound_hdl, mdl.pk)
            bound_uri = "{}{}".format(self.bound_uri, mdl.pk)
            pids = []
            for pid in bound:
                pids.append(pid[1])
            assert bound_hdl in pids, "Handle PID bound to SIP is incorrect"
            assert bound_uri in pids, "URI PID bound to SIP is incorrect"
            # Once we know we're creating identifiers as expected, test to ensure
            # that those identifiers are output as expected by the METS functions
            # doing that work.
            dir_dmd_sec = create_mets_v2.getDirDmdSec(mdl, "")
            id_type = dir_dmd_sec.xpath("//premis:objectIdentifierType",
                                        namespaces={"premis": ns.premisNS})
            id_value = dir_dmd_sec.xpath("//premis:objectIdentifierValue",
                                         namespaces={"premis": ns.premisNS})
            id_types = [item.text for item in id_type]
            id_values = [item.text for item in id_value]
            identifiers_dict = dict(zip(id_types, id_values))
            for key in identifiers_dict.keys():
                assert key in chain(self.traditional_identifiers,
                                    self.bound_identifier_types)
            assert bound_hdl in identifiers_dict.values()
            assert bound_uri in identifiers_dict.values()
Пример #2
0
    def test_bind_pids_not_set(self, caplog):
        """Test the output of the code without any args.

        bind_pids should return zero, for no-error. It won't have performed
        any actions on the database either.
        """
        assert (
            bind_pids.main(self.job, None, None, None) == 0
        ), "Return from bind_pids is something other than expected."
        assert (
            caplog.records[0].message == self.do_not_bind
        ), "Captured logging message from bind_pids is different than anticipated."
        assert (
            bind_pids.main(self.job, None, None, False) == 0
        ), "Return from bind_pids is something other than expected."
        assert (
            caplog.records[1].message == self.do_not_bind
        ), "Captured logging message from bind_pids is different than anticipated."
Пример #3
0
    def test_bind_pids_no_config(self, caplog):
        """Test the output of the code without any args.

        In this instance, we want bind_pids to think that there is some
        configuration available but we haven't provided any other information.
        We should see the microservice job exit without failing for the user.
        An example scenario might be when the user has bind PIDs on in their
        processing configuration but no handle server information configured.
        """
        DashboardSetting.objects.filter(scope="handle").delete()
        assert (
            bind_pids.main(self.job, None, None, True) == 1
        ), "Incorrect return value for bind_pids with incomplete configuration."
        assert caplog.records[0].message.startswith(self.incomplete_configuration_msg)
Пример #4
0
 def test_pid_declaration(self, mocker):
     """Test that the overall functionality of the PID declaration functions
     work as expected.
     """
     job = self.job
     files_no = 2
     dirs_no = 2
     example_ulid = "EXAMPLE0RE60SW7SVM2C8EGQAD"
     example_uri = "https://éxample.com/"
     expected_identifiers = 2
     identifers_file = "identifiers.json"
     identifiers_loc = os.path.join(
         THIS_DIR, "fixtures", self.pid_declaration_dir, identifers_file
     )
     all_identifier_types = (
         self.traditional_identifiers
         + self.bound_identifier_types
         + self.declared_identifier_types
     )
     mocker.patch.object(
         DeclarePIDs, "_retrieve_identifiers_path", return_value=identifiers_loc
     )
     DeclarePIDs(job).pid_declaration(unit_uuid=self.package_uuid, sip_directory="")
     # Declare PIDs allows us to assign PIDs to very specific objects in a
     # transfer.
     sip_mdl = SIP.objects.filter(uuid=self.package_uuid).first()
     files = File.objects.filter(sip=self.package_uuid, filegrpuse="original").all()
     dir_mdl = Directory.objects.filter(
         currentlocation__contains="%SIPDirectory%objects/"
     )
     assert len(files) == files_no, "Number of files returned is incorrect"
     assert len(dir_mdl) == dirs_no, "Number of directories returned is incorrect"
     for mdl in chain((sip_mdl,), files, dir_mdl):
         bound = {idfr.type: idfr.value for idfr in mdl.identifiers.all()}
         assert (
             len(bound) == expected_identifiers
         ), "Number of identifiers is incorrect"
         assert set(bound.keys()) == set(
             self.declared_identifier_types
         ), "Returned keys are not in expected list"
         for key, value in bound.items():
             assert value, "Returned an empty value for an identifier"
             if key == self.pid_exid:
                 assert example_uri in value, "Example URI type not preserved"
             if key == self.pid_ulid:
                 assert len(example_ulid) == len(value)
     # Use the previous PID binding vcr cassettes to ensure declared PIDs can
     # co-exist with bound ones.
     mocker.patch.object(
         bind_pids, "_get_unique_acc_no", return_value=self.package_uuid
     )
     mocker.patch.object(
         bind_pids, "_validate_handle_server_config", return_value=None
     )
     with vcr_cassettes.use_cassette(
         "test_bind_pids_to_sip_and_dirs.yaml"
     ) as cassette:
         # Primary entry-point for the bind_pids microservice job.
         bind_pids.main(self.job, self.package_uuid, "", True)
     for mdl in chain((sip_mdl,), dir_mdl):
         dir_dmd_sec = create_mets_v2.getDirDmdSec(mdl, "")
         id_type = dir_dmd_sec.xpath(
             "//premis:objectIdentifierType", namespaces={"premis": ns.premisNS}
         )
         id_value = dir_dmd_sec.xpath(
             "//premis:objectIdentifierValue", namespaces={"premis": ns.premisNS}
         )
         assert len(id_type) == len(
             all_identifier_types
         ), "Identifier type count is incorrect"
         assert len(id_value) == len(
             all_identifier_types
         ), "Identifier value count is incorrect"
         for key, value in dict(zip(id_type, id_value)).items():
             if key == self.pid_exid:
                 assert example_uri in value, "Example URI not preserved"
             if key == self.pid_ulid:
                 assert len(example_ulid) == len(value)
     for file_ in files:
         with vcr_cassettes.use_cassette("test_bind_pid_to_files.yaml") as cassette:
             bind_pid.main(self.job, file_.pk, True)
         assert cassette.all_played
     for file_mdl in files:
         file_level_premis = create_mets_v2.create_premis_object(file_mdl.pk)
         id_type = file_level_premis.xpath(
             "//premis:objectIdentifierType", namespaces={"premis": ns.premisNS}
         )
         id_value = file_level_premis.xpath(
             "//premis:objectIdentifierValue", namespaces={"premis": ns.premisNS}
         )
         assert len(id_type) == len(
             all_identifier_types
         ), "Identifier type count is incorrect"
         assert len(id_value) == len(
             all_identifier_types
         ), "Identifier value count is incorrect"
         for key, value in dict(zip(id_type, id_value)).items():
             if key == self.pid_exid:
                 assert example_uri in value, "Example URI not preserved"
             if key == self.pid_ulid:
                 assert len(example_ulid) == len(value)