diff --git a/swh/indexer/metadata_dictionary/cff.py b/swh/indexer/metadata_dictionary/cff.py --- a/swh/indexer/metadata_dictionary/cff.py +++ b/swh/indexer/metadata_dictionary/cff.py @@ -42,16 +42,16 @@ author_data: Dict[str, Optional[Union[str, Dict]]] = { "@type": SCHEMA_URI + "Person" } - if "orcid" in author: + if "orcid" in author and isinstance(author["orcid"], str): author_data["@id"] = author["orcid"] - if "affiliation" in author: + if "affiliation" in author and isinstance(author["affiliation"], str): author_data[SCHEMA_URI + "affiliation"] = { "@type": SCHEMA_URI + "Organization", SCHEMA_URI + "name": author["affiliation"], } - if "family-names" in author: + if "family-names" in author and isinstance(author["family-names"], str): author_data[SCHEMA_URI + "familyName"] = author["family-names"] - if "given-names" in author: + if "given-names" in author and isinstance(author["given-names"], str): author_data[SCHEMA_URI + "givenName"] = author["given-names"] result.append(author_data) diff --git a/swh/indexer/tests/test_metadata.py b/swh/indexer/tests/test_metadata.py --- a/swh/indexer/tests/test_metadata.py +++ b/swh/indexer/tests/test_metadata.py @@ -212,6 +212,92 @@ # then assert expected == result + def test_cff_empty_fields(self): + """ + testing CITATION.cff translation + """ + # given + content = """# YAML 1.2 + authors: + - + affiliation: "Hogwarts" + family-names: + given-names: Harry + - + affiliation: "Ministry of Magic" + family-names: Weasley + orcid: + given-names: Arthur + """.encode( + "utf-8" + ) + + expected = { + "@context": "https://doi.org/10.5063/schema/codemeta-2.0", + "type": "SoftwareSourceCode", + "author": [ + { + "type": "Person", + "affiliation": { + "type": "Organization", + "name": "Hogwarts", + }, + "givenName": "Harry", + }, + { + "type": "Person", + "affiliation": { + "type": "Organization", + "name": "Ministry of Magic", + }, + "familyName": "Weasley", + "givenName": "Arthur", + }, + ], + } + + # when + result = self.cff_mapping.translate(content) + # then + assert expected == result + + def test_cff_invalid_fields(self): + """ + testing CITATION.cff translation + """ + # given + content = """# YAML 1.2 + authors: + - + affiliation: "Hogwarts" + family-names: + - Potter + - James + given-names: Harry + """.encode( + "utf-8" + ) + + expected = { + "@context": "https://doi.org/10.5063/schema/codemeta-2.0", + "type": "SoftwareSourceCode", + "author": [ + { + "type": "Person", + "affiliation": { + "type": "Organization", + "name": "Hogwarts", + }, + "givenName": "Harry", + }, + ], + } + + # when + result = self.cff_mapping.translate(content) + # then + assert expected == result + def test_compute_metadata_npm(self): """ testing only computation of metadata with hard_mapping_npm