cthoyt / zenodo-client

A tool for automated uploading and version management of scientific data to Zenodo
MIT License
25 stars 5 forks source link

More tests for combined publishing and metadata update #30

Open nuest opened 10 months ago

nuest commented 10 months ago

The following test cases cover some more variants of creating records with publishing and updating, I'd like to revisit these after merging #28 and #29.

     def test_update_with_metadata_publish(self):
        """Test seperating creation, uploading, updating, and publishing."""
        data = Metadata(
            title="Test Upload",
            upload_type="dataset",
            description="test description",
            creators=[
                Creator(
                    name="Hoyt, Charles Tapley",
                    affiliation="Harvard Medical School",
                    orcid="0000-0003-4423-4370",
                ),
            ],
        )

        res = self.zenodo.create(data=data, paths=[], publish=False)
        res_create_json = res.json()
        deposition_id = res_create_json["id"]

        self.assertEqual(False, res_create_json["submitted"])
        self.assertEqual("unsubmitted", res_create_json["state"])
        self.assertEqual(0, len(res_create_json["files"]))

        path = self.directory.joinpath("test.txt")
        expected_doi = res_create_json["metadata"]["prereserve_doi"]["doi"]
        path.write_text("DOI: https://doi.org/%s" % expected_doi)

        # update files before publish
        res = self.zenodo.update(deposition_id=deposition_id, paths=[path], publish=False)
        res_update_json = res.json()
        self.assertEqual(False, res_update_json["submitted"])
        self.assertEqual("unsubmitted", res_update_json["state"])
        self.assertEqual(data.title, res_update_json["metadata"]["title"])
        self.assertEqual(data.version, res_update_json["metadata"]["version"])
        self.assertEqual(1, len(res_update_json["files"]))
        self.assertEqual("test.txt", res_update_json["files"][0]["filename"])

        data.title = "Test Publication with Metadata Update"
        res = self.zenodo.update_metadata(deposition_id=deposition_id, data=data, publish=True)
        res_update_metadata_json = res.json()
        self.assertEqual(True, res_update_metadata_json["submitted"])
        self.assertEqual("done", res_update_metadata_json["state"])
        self.assertEqual(expected_doi, res_update_metadata_json["doi"])
        self.assertEqual(data.title, res_update_metadata_json["metadata"]["title"])

    def test_multi_step_publish(self):
        """Test separate steps of creation, upload, and publishing."""
        data = Metadata(
            title="Test Upload",
            upload_type="dataset",
            description="test description",
            creators=[
                Creator(
                    name="Hoyt, Charles Tapley",
                    affiliation="Harvard Medical School",
                    orcid="0000-0003-4423-4370",
                ),
            ],
        )
        path = self.directory.joinpath("test.txt")
        path.write_text("it's just the beginning")

        res = self.zenodo.create(data=data, paths=[path], publish=False)
        res_create_json = res.json()
        deposition_id = res_create_json["id"]
        path_hash = hashlib.md5(path.read_bytes()).hexdigest()  # noqa:S324,S303
        self.assertEqual(path_hash, res_create_json["files"][0]["checksum"])

        data.title = "Test Upload with an Update to Unpublished Deposition"

        path_2 = self.directory.joinpath("test2.txt")
        path_2.write_text("it's all metadata after this")
        res = self.zenodo.update(deposition_id=deposition_id, paths=[path_2], publish=False)
        res_update_json = res.json()

        self.assertEqual(False, res_update_json["submitted"])
        self.assertEqual("unsubmitted", res_update_json["state"])
        self.assertEqual(2, len(res_update_json["files"]))
        self.assertEqual("test.txt", res_update_json["files"][1]["filename"])
        self.assertEqual("test2.txt", res_update_json["files"][0]["filename"])

        path_hash_2 = hashlib.md5(path_2.read_bytes()).hexdigest()  # noqa:S324,S303
        self.assertEqual(path_hash_2, res_update_json["files"][0]["checksum"])

        data.title = "Test Publication with Metadata Update"
        res = self.zenodo.update_metadata(deposition_id=deposition_id, data=data, publish=True)
        res_update_metadata_json = res.json()

        self.assertEqual(True, res_update_metadata_json["submitted"])
        self.assertEqual("done", res_update_metadata_json["state"])
        self.assertEqual(data.title, res_update_metadata_json["metadata"]["title"])