From 5123a0b8200121877abfc2ac7458c6ae45b23562 Mon Sep 17 00:00:00 2001 From: Tom Morrell Date: Sun, 2 Feb 2025 18:40:24 -0800 Subject: [PATCH] schema31: remove depreciated schema 31 --- datacite/schema31.py | 320 -------------------- datacite/schemas/datacite-v3.1.json | 445 --------------------------- docs/index.rst | 6 - tests/test_schema31.py | 453 ---------------------------- 4 files changed, 1224 deletions(-) delete mode 100644 datacite/schema31.py delete mode 100644 datacite/schemas/datacite-v3.1.json delete mode 100644 tests/test_schema31.py diff --git a/datacite/schema31.py b/datacite/schema31.py deleted file mode 100644 index acc0354..0000000 --- a/datacite/schema31.py +++ /dev/null @@ -1,320 +0,0 @@ -# -*- coding: utf-8 -*- -# -# This file is part of DataCite. -# -# Copyright (C) 2016 CERN. -# -# DataCite is free software; you can redistribute it and/or modify it -# under the terms of the Revised BSD License; see LICENSE file for -# more details. - -"""DataCite v3.1 JSON to XML transformations.""" - -import importlib.resources as importlib_resources - -from lxml import etree -from lxml.builder import E - -from .jsonutils import validator_factory -from .xmlutils import ( - Rules, - dump_etree_helper, - etree_to_string, - set_elem_attr, - set_non_empty_attr, -) - -rules = Rules() - -ns = { - None: "http://datacite.org/schema/kernel-3", - "xsi": "http://www.w3.org/2001/XMLSchema-instance", - "xml": "xml", -} - -root_attribs = { - "{http://www.w3.org/2001/XMLSchema-instance}schemaLocation": "http://datacite.org/schema/kernel-3 " - "http://schema.datacite.org/meta/kernel-3/metadata.xsd", -} - -validator = validator_factory( - importlib_resources.files("datacite") / "schemas/datacite-v3.1.json" -) - - -def dump_etree(data): - """Convert JSON dictionary to DataCite v3.1 XML as ElementTree.""" - return dump_etree_helper(data, rules, ns, root_attribs) - - -def tostring(data, **kwargs): - """Convert JSON dictionary to DataCite v3.1 XML as string.""" - return etree_to_string(dump_etree(data), **kwargs) - - -def validate(data): - """Validate DataCite v3.1 JSON dictionary.""" - return validator.is_valid(data) - - -@rules.rule("identifier") -def identifier(path, value): - """Transform identifier.""" - return E.identifier(value["identifier"], identifierType=value["identifierType"]) - - -def affiliation(root, value): - """Extract affiliation.""" - val = value.get("affiliation") - if val: - root.append(E.affiliation(val)) - - -def nameidentifier(root, value): - """Extract nameidentifier.""" - val = value.get("nameIdentifier", {}) - if val.get("nameIdentifier"): - elem = E.nameIdentifier(value["nameIdentifier"]["nameIdentifier"]) - elem.set("nameIdentifierScheme", val["nameIdentifierScheme"]) - set_elem_attr(elem, "schemeURI", val) - root.append(elem) - - -@rules.rule("creators") -def creators(path, values): - """Transform creators.""" - if not values: - return - - root = E.creators() - for value in values: - creator = E.creator(E.creatorName(value["creatorName"])) - - nameidentifier(creator, value) - affiliation(creator, value) - root.append(creator) - - return root - - -@rules.rule("titles") -def titles(path, values): - """Transform titles.""" - if not values: - return - root = E.titles() - - for value in values: - elem = etree.Element("title", nsmap=ns) - elem.text = value["title"] - set_non_empty_attr(elem, "{xml}lang", value.get("lang")) - set_non_empty_attr(elem, "titleType", value.get("titleType")) - root.append(elem) - - return root - - -@rules.rule("publisher") -def publisher(path, value): - """Transform publisher.""" - if not value: - return - return E.publisher(value) - - -@rules.rule("publicationYear") -def publication_year(path, value): - """Transform publicationYear.""" - if not value: - return - return E.publicationYear(str(value)) - - -@rules.rule("subjects") -def subjects(path, values): - """Transform subjects.""" - if not values: - return - - root = E.subjects() - for value in values: - elem = E.subject(value["subject"]) - set_non_empty_attr(elem, "{xml}lang", value.get("lang")) - set_elem_attr(elem, "schemeURI", value) - set_elem_attr(elem, "subjectScheme", value) - root.append(elem) - return root - - -@rules.rule("contributors") -def contributors(path, values): - """Transform contributors.""" - if not values: - return - - root = E.contributors() - for value in values: - contributor = E.contributor( - E.contributorName(value["contributorName"]), - contributorType=value["contributorType"], - ) - nameidentifier(contributor, value) - affiliation(contributor, value) - root.append(contributor) - - return root - - -@rules.rule("dates") -def dates(path, values): - """Transform dates.""" - if not values: - return - - root = E.dates() - for value in values: - root.append(E.date(value["date"], dateType=value["dateType"])) - - return root - - -@rules.rule("language") -def language(path, value): - """Transform language.""" - if not value: - return - return E.language(value) - - -@rules.rule("resourceType") -def resource_type(path, value): - """Transform resourceType.""" - if not value: - return - elem = E.resourceType() - elem.set("resourceTypeGeneral", value["resourceTypeGeneral"]) - if value.get("resourceType"): - elem.text = value["resourceType"] - return elem - - -@rules.rule("alternateIdentifiers") -def alternate_identifiers(path, values): - """Transform alternateIdenftifiers.""" - if not values: - return - - root = E.alternateIdentifiers() - for value in values: - elem = E.alternateIdentifier(value["alternateIdentifier"]) - elem.set("alternateIdentifierType", value["alternateIdentifierType"]) - root.append(elem) - - return root - - -@rules.rule("relatedIdentifiers") -def related_identifiers(path, values): - """Transform relatedIdentifiers.""" - if not values: - return - - root = E.relatedIdentifiers() - for value in values: - elem = E.relatedIdentifier() - elem.text = value["relatedIdentifier"] - elem.set("relatedIdentifierType", value["relatedIdentifierType"]) - elem.set("relationType", value["relationType"]) - set_elem_attr(elem, "relatedMetadataScheme", value) - set_elem_attr(elem, "schemeURI", value) - set_elem_attr(elem, "schemeType", value) - root.append(elem) - return root - - -def free_text_list(plural, singular, values): - """List of elements with free text.""" - if not values: - return - root = etree.Element(plural) - for value in values: - etree.SubElement(root, singular).text = value - return root - - -@rules.rule("sizes") -def sizes(path, values): - """Transform sizes.""" - return free_text_list("sizes", "size", values) - - -@rules.rule("formats") -def formats(path, values): - """Transform sizes.""" - return free_text_list("formats", "format", values) - - -@rules.rule("version") -def version(path, value): - """Transform version.""" - if not value: - return - return E.version(value) - - -@rules.rule("rightsList") -def rights(path, values): - """Transform rights.""" - if not values: - return - - root = E.rightsList() - for value in values: - elem = E.rights(value["rights"]) - set_elem_attr(elem, "rightsURI", value) - root.append(elem) - - return root - - -@rules.rule("descriptions") -def descriptions(path, values): - """Transform descriptions.""" - if not values: - return - - root = E.descriptions() - for value in values: - elem = E.description( - value["description"], descriptionType=value["descriptionType"] - ) - set_non_empty_attr(elem, "{xml}lang", value.get("language")) - root.append(elem) - - return root - - -@rules.rule("geoLocations") -def geolocations(path, values): - """Transform geolocations.""" - if not values: - return - - root = E.geoLocations() - for value in values: - elem = E.geoLocation() - - point = value.get("geoLocationPoint") - if point: - elem.append(E.geoLocationPoint(point)) - - box = value.get("geoLocationBox") - if box: - elem.append(E.geoLocationBox(box)) - - place = value.get("geoLocationPlace") - if place: - elem.append(E.geoLocationPlace(place)) - - root.append(elem) - return root diff --git a/datacite/schemas/datacite-v3.1.json b/datacite/schemas/datacite-v3.1.json deleted file mode 100644 index b8f9045..0000000 --- a/datacite/schemas/datacite-v3.1.json +++ /dev/null @@ -1,445 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "id": "datacite-v3.1.json", - "title": "DataCite v3.1", - "description": "JSON representation of the DataCite v3.1 schema.", - "additionalProperties": false, - "definitions": { - "doi": { - "description": "Digital object identifier.", - "type": "string", - "pattern": "10\\..+/.+" - }, - "year": { - "type": "string" - }, - "language": { - "description": "Allowed values are taken from IETF BCP 47, ISO 639-1 language codes.", - "type": "string" - }, - "uri": { - "description": "For adding future URI validation.", - "type": "string" - }, - "nameIdentifier": { - "type": "object", - "properties": { - "nameIdentifier": { - "type": "string" - }, - "nameIdentifierScheme": { - "type": "string" - }, - "schemeURI": { - "$ref": "#/definitions/uri" - } - }, - "required": [ - "nameIdentifier", - "nameIdentifierScheme" - ] - }, - "person": { - "type": "object", - "properties": { - - }, - "required": [ - "name" - ] - } - }, - "type": "object", - "properties": { - "identifier": { - "description": "A persistent identifier that identifies a resource. Currently, only DOI is allowed.", - "type": "object", - "properties": { - "identifier": { - "$ref": "#/definitions/doi" - }, - "identifierType": { - "enum": [ - "DOI" - ] - } - }, - "required": [ - "identifier", - "identifierType" - ] - }, - "creators": { - "description": "The main researchers involved working on the data, or the authors of the publication in priority order. May be a corporate/institutional or personal name. Format: Family, Given", - "type": "array", - "items": { - "creatorName": { - "type": "string" - }, - "nameIdentifier": { - "$ref": "#/definitions/nameIdentifier" - }, - "affiliation": { - "type": "string" - } - }, - "required": [ - "creatorName" - ] - }, - "titles": { - "type": "array", - "items": { - "description": "A name or title by which a resource is known.", - "type": "object", - "properties": { - "title": { - "type": "string", - "minLength": 1 - }, - "type": { - "description": "WARNING: This field has been superseded by 'titleType'.", - "enum": [ - "AlternativeTitle", - "Subtitle", - "TranslatedTitle" - ] - }, - "titleType": { - "enum": [ - "AlternativeTitle", - "Subtitle", - "TranslatedTitle" - ] - }, - "lang": { - "$ref": "#/definitions/language" - } - }, - "required": [ - "title" - ] - } - }, - "publisher": { - "description": "The name of the entity that holds, archives, publishes prints, distributes, releases, issues, or produces the resource. This property will be used to formulate the citation, so consider the prominence of the role. In the case of datasets, \"publish\" is understood to mean making the data available to the community of researchers.", - "type": "string", - "minLength": 1 - }, - "publicationYear": { - "description": "Year when the data is made publicly available. If an embargo period has been in effect, use the date when the embargo period ends. In the case of datasets, \"publish\" is understood to mean making the data available on a specific date to the community of researchers. If there is no standard publication year value, use the date that would be preferred from a citation perspective.", - "$ref": "#/definitions/year" - }, - "subjects": { - "type": "array", - "items": { - "description": "Subject, keywords, classification codes, or key phrases describing the resource.", - "type": "object", - "properties": { - "subject": { - "type": "string" - }, - "subjectScheme": { - "type": "string" - }, - "schemeURI": { - "$ref": "#/definitions/uri" - }, - "lang": { - "$ref": "#/definitions/language" - } - } - } - }, - "contributors": { - "description": "The institution or person responsible for collecting, creating, or otherwise contributing to the developement of the dataset. The personal name format should be: Family, Given.", - "type": "array", - "items": { - "type": "object", - "properties": { - "contributorType": { - "enum": [ - "ContactPerson", - "DataCollector", - "DataCurator", - "DataManager", - "Distributor", - "Editor", - "Funder", - "HostingInstitution", - "Other", - "Producer", - "ProjectLeader", - "ProjectManager", - "ProjectMember", - "RegistrationAgency", - "RegistrationAuthority", - "RelatedPerson", - "ResearchGroup", - "RightsHolder", - "Researcher", - "Sponsor", - "Supervisor", - "WorkPackageLeader" - ] - }, - "contributorName": { - "type": "string" - }, - "nameIdentifier": { - "$ref": "#/definitions/nameIdentifier" - }, - "affiliation": { - "type": "string" - } - }, - "required": [ - "contributorName", - "contributorType" - ] - } - }, - "dates": { - "description": "Different dates relevant to the work.", - "type": "array", - "items": { - "type": "object", - "properties": { - "date": { - "description": "YYYY,YYYY-MM-DD, YYYY-MM-DDThh:mm:ssTZD or any other format or level of granularity described in W3CDTF. Use RKMS-ISO8601 standard for depicting date ranges.", - "type": "string" - }, - "dateType": { - "enum": [ - "Accepted", - "Available", - "Collected", - "Copyrighted", - "Created", - "Issued", - "Submitted", - "Updated", - "Valid" - ] - } - } - } - }, - "language": { - "description": "Primary language of the resource.", - "$ref": "#/definitions/language" - }, - "resourceType": { - "description": "The type of a resource. You may enter an additional free text description. The format is open, but the preferred format is a single term of some detail so that a pair can be formed with the sub-property.", - "type": "object", - "properties": { - "resourceType": { - "type": "string" - }, - "resourceTypeGeneral": { - "description": "The general type of a resource.", - "enum": [ - "Audiovisual", - "Collection", - "Dataset", - "Event", - "Image", - "InteractiveResource", - "Model", - "PhysicalObject", - "Service", - "Software", - "Sound", - "Text", - "Workflow", - "Other" - ] - } - }, - "required": [ - "resourceTypeGeneral" - ] - }, - "alternateIdentifiers": { - "description": "An identifier or identifiers other than the primary Identifier applied to the resource being registered. This may be any alphanumeric string which is unique within its domain of issue. May be used for local identifiers. AlternateIdentifier should be used for another identifier of the same instance (same location, same file).", - "type": "array", - "items": { - "type": "object", - "properties": { - "alternateIdentifier": { - "type": "string" - }, - "alternateIdentifierType": { - "type": "string" - } - }, - "required": [ - "alternateIdentifier", - "alternateIdentifierType" - ] - } - }, - "relatedIdentifiers": { - "description": "Identifiers of related resources. Use this property to indicate subsets of properties, as appropriate.", - "type": "array", - "items": { - "type": "object", - "properties": { - "relatedIdentifier": { - "type": "string" - }, - "relatedIdentifierType": { - "enum": [ - "ARK", - "arXiv", - "bibcode", - "DOI", - "EAN13", - "EISSN", - "Handle", - "ISBN", - "ISSN", - "ISTC", - "LISSN", - "LSID", - "PMID", - "PURL", - "UPC", - "URL", - "URN" - ] - }, - "relationType": { - "enum": [ - "IsCitedBy", - "Cites", - "IsSupplementTo", - "IsSupplementedBy", - "IsContinuedBy", - "Continues", - "IsNewVersionOf", - "IsPreviousVersionOf", - "IsPartOf", - "HasPart", - "IsReferencedBy", - "References", - "IsDocumentedBy", - "Documents", - "IsCompiledBy", - "Compiles", - "IsVariantFormOf", - "IsOriginalFormOf", - "IsIdenticalTo", - "HasMetadata", - "IsMetadataFor", - "Reviews", - "IsReviewedBy", - "IsDerivedFrom", - "IsSourceOf" - ] - }, - "relatedMetadataScheme": { - "type": "string" - }, - "schemeURI": { - "$ref": "#/definitions/uri" - } - }, - "required": [ - "relatedIdentifier", - "relationType", - "relatedIdentifierType" - ] - } - }, - "sizes": { - "description": "Unstructures size information about the resource.", - "type": "array", - "items": { - "type": "string" - } - }, - "formats": { - "description": "Technical format of the resource. Use file extension or MIME type where possible.", - "type": "array", - "items": { - "type": "string" - } - }, - "version": { - "description": "Version number of the resource. If the primary resource has changed the version number increases. Register a new identifier for a major version change. Individual stewards need to determine which are major vs. minor versions. May be used in conjunction with properties 11 and 12 (AlternateIdentifier and RelatedIdentifier) to indicate various information updates. May be used in conjunction with property 17 (Description) to indicate the nature and file/record range of version.", - "type": "string" - }, - "rightsList": { - "description": "Any rights information for this resource. Provide a rights management statement for the resource or reference a service providing such information. Include embargo information if applicable. Use the complete title of a license and include version information if applicable.", - "type": "array", - "items": { - "type": "object", - "properties": { - "rightsURI": { - "$ref": "#/definitions/uri" - }, - "rights": { - "type": "string" - } - } - } - }, - "descriptions": { - "description": "All additional information that does not fit in any of the other categories. May be used for technical information. It is a best practice to supply a description.", - "type": "array", - "items": { - "type": "object", - "properties": { - "description": { - "type": "string", - "minLength": 1 - }, - "descriptionType": { - "type": "string", - "description": "The type of the description.", - "enum": [ - "Abstract", - "Methods", - "SeriesInformation", - "TableOfContents", - "Other" - ] - }, - "lang": { - "$ref": "#/definitions/language" - } - }, - "required": [ - "description", - "descriptionType" - ] - } - }, - "geoLocations": { - "description": "Spatial region or named place where the data was gathered or about which the data is focused.", - "type": "array", - "items": { - "type": "object", - "properties": { - "geoLocationPoint": { - "type": "string" - }, - "geoLocationBox": { - "type": "string" - }, - "geoLocationPlace": { - "type": "string" - } - } - } - } - }, - "required": [ - "identifier", - "creators", - "titles", - "publisher", - "publicationYear" - ] -} diff --git a/docs/index.rst b/docs/index.rst index fa9be70..3430efd 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -95,12 +95,6 @@ DataCite v4.0 Metadata Management .. automodule:: datacite.schema40 :members: dump_etree, tostring, validate -DataCite v3.1 Metadata Management -================================== - -.. automodule:: datacite.schema31 - :members: dump_etree, tostring, validate - .. include:: ../CHANGES.rst .. include:: ../CONTRIBUTING.rst diff --git a/tests/test_schema31.py b/tests/test_schema31.py deleted file mode 100644 index 220673b..0000000 --- a/tests/test_schema31.py +++ /dev/null @@ -1,453 +0,0 @@ -# -*- coding: utf-8 -*- -# -# This file is part of DataCite. -# -# Copyright (C) 2016 CERN. -# -# DataCite is free software; you can redistribute it and/or modify it -# under the terms of the Revised BSD License; see LICENSE file for -# more details. - -"""Tests for format transformations.""" - -import xml.etree.ElementTree as ET - -import pytest -from lxml import etree - -from datacite.schema31 import dump_etree, tostring, validate -from datacite.xmlutils import Rules - - -def test_rules(): - """Test rules.""" - rules = Rules() - - rules.rule("a")(lambda x: "a") - pytest.raises(ValueError, rules.rule("a"), lambda x: "b") - - -def test_example_json_validates(example_json): - """Test the example file validates against the JSON schema.""" - assert validate(example_json) - - -def test_json_to_xml(example_xml_file, example_json, xsd31): - """Test that example XML converts to example JSON.""" - xsd31.assertValid(etree.XML(example_xml_file.encode("utf8"))) - xsd31.assertValid(etree.XML(tostring(example_json).encode("utf8"))) - - -def test_identifier(): - """Test that example XML converts to example JSON.""" - tree = dump_etree( - { - "identifier": { - "identifierType": "DOI", - "identifier": "10.1234/foo.bar", - } - } - ) - elem = tree.xpath("/resource/identifier")[0] - assert elem.text == "10.1234/foo.bar" - assert elem.get("identifierType") == "DOI" - - -def test_creators(): - """Test creators.""" - pytest.raises(TypeError, dump_etree, {"creators": {"invalid": "data"}}) - - tree = dump_etree({"creators": []}) - assert len(tree.xpath("/resource/creators")) == 0 - - tree = dump_etree({"creators": [{"creatorName": "Smith, John"}]}) - assert len(tree.xpath("/resource/creators/creator")) == 1 - assert len(tree.xpath("/resource/creators/creator/creatorName")) == 1 - assert len(tree.xpath("/resource/creators/creator/nameIdentifier")) == 0 - assert len(tree.xpath("/resource/creators/creator/affiliation")) == 0 - - tree = dump_etree( - { - "creators": [ - { - "creatorName": "Smith, John", - "affiliation": "CERN", - "nameIdentifier": { - "nameIdentifier": "1234", - "schemeURI": "http://orcid.org", - "nameIdentifierScheme": "orcid", - }, - } - ] - } - ) - assert len(tree.xpath("/resource/creators/creator/creatorName")) == 1 - assert len(tree.xpath("/resource/creators/creator/nameIdentifier")) == 1 - assert len(tree.xpath("/resource/creators/creator/affiliation")) == 1 - - -def test_titles(): - """Test titles.""" - pytest.raises(TypeError, dump_etree, {"titles": {"invalid": "data"}}) - - tree = dump_etree({"titles": []}) - assert len(tree.xpath("/resource/titles")) == 0 - - tree = dump_etree({"titles": [{"title": "Test"}]}) - assert len(tree.xpath("/resource/titles")) == 1 - assert len(tree.xpath("/resource/titles/title")) == 1 - - elem = dump_etree({"titles": [{"title": "Test", "titleType": "Subtitle"}]}).xpath( - "/resource/titles/title" - )[0] - assert elem.text == "Test" - assert elem.get("titleType") == "Subtitle" - - elem = dump_etree({"titles": [{"title": "Test", "lang": "en"}]}).xpath( - "/resource/titles/title" - )[0] - assert elem.get("{xml}lang") == "en" - - -def test_publisher(): - """Test publisher.""" - tree = dump_etree({"publisher": "test"}) - assert tree.xpath("/resource/publisher")[0].text == "test" - - tree = dump_etree({"publisher": ""}) - assert len(tree.xpath("/resource/publisher")) == 0 - - -def test_publicationyear(): - """Test publisher.""" - tree = dump_etree({"publicationYear": 2002}) - assert tree.xpath("/resource/publicationYear")[0].text == "2002" - - tree = dump_etree({"publicationYear": None}) - assert len(tree.xpath("/resource/publicationYear")) == 0 - - -def test_subjects(): - """Test creators.""" - pytest.raises(TypeError, dump_etree, {"subjects": {"invalid": "data"}}) - - tree = dump_etree({"subjects": []}) - assert len(tree.xpath("/resource/subjects")) == 0 - - tree = dump_etree({"subjects": [{"subject": "test"}]}) - assert len(tree.xpath("/resource/subjects/subject")) == 1 - - elem = dump_etree( - { - "subjects": [ - { - "subject": "test", - "subjectScheme": "dewey", - "schemeURI": "dewey-uri", - } - ] - } - ).xpath("/resource/subjects/subject")[0] - assert elem.get("subjectScheme") == "dewey" - assert elem.get("schemeURI") == "dewey-uri" - - -def test_contributors(): - """Test creators.""" - pytest.raises(TypeError, dump_etree, {"contributors": {"invalid": "data"}}) - - tree = dump_etree({"contributors": []}) - assert len(tree.xpath("/resource/contributors")) == 0 - - tree = dump_etree( - { - "contributors": [ - { - "contributorName": "Smith, John", - "contributorType": "Funder", - } - ] - } - ) - assert len(tree.xpath("/resource/contributors/contributor")) == 1 - assert len(tree.xpath("/resource/contributors/contributor/contributorName")) == 1 - assert len(tree.xpath("/resource/contributors/contributor/nameIdentifier")) == 0 - assert len(tree.xpath("/resource/contributors/contributor/affiliation")) == 0 - - tree = dump_etree( - { - "contributors": [ - { - "contributorName": "Smith, John", - "contributorType": "Funder", - "affiliation": "CERN", - "nameIdentifier": { - "nameIdentifier": "1234", - "schemeURI": "http://orcid.org", - "nameIdentifierScheme": "orcid", - }, - } - ] - } - ) - assert len(tree.xpath("/resource/contributors/contributor/contributorName")) == 1 - assert len(tree.xpath("/resource/contributors/contributor/nameIdentifier")) == 1 - assert len(tree.xpath("/resource/contributors/contributor/affiliation")) == 1 - - -def test_dates(): - """Test publisher.""" - tree = dump_etree({"dates": []}) - assert len(tree.xpath("/resource/dates")) == 0 - - pytest.raises(KeyError, dump_etree, {"dates": [{"date": "2011-01-01"}]}) - - elem = dump_etree( - {"dates": [{"date": "2011-01-01", "dateType": "Accepted"}]} - ).xpath("/resource/dates/date")[0] - assert elem.text == "2011-01-01" - assert elem.get("dateType") == "Accepted" - - -def test_language(): - """Test language.""" - tree = dump_etree({"language": "en"}) - assert tree.xpath("/resource/language")[0].text == "en" - - tree = dump_etree({"language": ""}) - assert len(tree.xpath("/resource/language")) == 0 - - -def test_resourcetype(): - """Test resource type.""" - tree = dump_etree({"resourceType": {}}) - assert len(tree.xpath("/resource/resourceType")) == 0 - - elem = dump_etree({"resourceType": {"resourceTypeGeneral": "Software"}}).xpath( - "/resource/resourceType" - )[0] - assert elem.get("resourceTypeGeneral") == "Software" - assert elem.text is None - - pytest.raises( - KeyError, dump_etree, {"resourceType": {"resourceType": "Science Software"}} - ) - - elem = dump_etree( - { - "resourceType": { - "resourceTypeGeneral": "Software", - "resourceType": "Science Software", - } - } - ).xpath("/resource/resourceType")[0] - assert elem.get("resourceTypeGeneral") == "Software" - assert elem.text == "Science Software" - - -def test_alternateidentifiers(): - """Test publisher.""" - pytest.raises(TypeError, dump_etree, {"alternateIdentifiers": {"invalid": "data"}}) - - tree = dump_etree({"alternateIdentifiers": []}) - assert len(tree.xpath("/resource/alternateIdentifiers")) == 0 - - elem = dump_etree( - { - "alternateIdentifiers": [ - { - "alternateIdentifier": "10.1234/foo", - "alternateIdentifierType": "DOI", - }, - ] - } - ).xpath("/resource/alternateIdentifiers/alternateIdentifier")[0] - assert elem.get("alternateIdentifierType") == "DOI" - assert elem.text == "10.1234/foo" - - -def test_relatedidentifiers(): - """Test publisher.""" - tree = dump_etree({"relatedIdentifiers": []}) - assert len(tree.xpath("/resource/relatedIdentifiers")) == 0 - - elem = dump_etree( - { - "relatedIdentifiers": [ - { - "relatedIdentifier": "10.1234/foo", - "relatedIdentifierType": "DOI", - "relationType": "Cites", - }, - ] - } - ).xpath("/resource/relatedIdentifiers/relatedIdentifier")[0] - assert elem.get("relatedIdentifierType") == "DOI" - assert elem.get("relationType") == "Cites" - assert elem.text == "10.1234/foo" - - elem = dump_etree( - { - "relatedIdentifiers": [ - { - "relatedIdentifier": "10.1234/foo", - "relatedIdentifierType": "DOI", - "relationType": "HasMetadata", - "relatedMetadataScheme": "MARC21", - "schemeURI": "http://loc.gov", - "schemeType": "XSD", - }, - ] - } - ).xpath("/resource/relatedIdentifiers/relatedIdentifier")[0] - assert elem.get("relatedMetadataScheme") == "MARC21" - assert elem.get("schemeURI") == "http://loc.gov" - assert elem.get("schemeType") == "XSD" - - -def test_sizes(): - """Test sizes.""" - tree = dump_etree({"sizes": []}) - assert len(tree.xpath("/resource/sizes")) == 0 - - elem = dump_etree({"sizes": ["123"]}).xpath("/resource/sizes/size")[0] - assert elem.text == "123" - - -def test_formats(): - """Test formats.""" - tree = dump_etree({"formats": []}) - assert len(tree.xpath("/resource/formats")) == 0 - - elem = dump_etree({"formats": ["abc"]}).xpath("/resource/formats/format")[0] - assert elem.text == "abc" - - -def test_version(): - """Test formats.""" - tree = dump_etree({"version": ""}) - assert len(tree.xpath("/resource/version")) == 0 - - elem = dump_etree({"version": "v3.1"}).xpath("/resource/version")[0] - assert elem.text == "v3.1" - - -def test_rights(): - """Test publisher.""" - tree = dump_etree({"rightsList": []}) - assert len(tree.xpath("/resource/rightsList")) == 0 - - elem = dump_etree( - { - "rightsList": [ - { - "rights": "CC", - "rightsURI": "http://cc.org", - }, - ] - } - ).xpath("/resource/rightsList/rights")[0] - assert elem.get("rightsURI") == "http://cc.org" - assert elem.text == "CC" - - -def test_descriptions(): - """Test publisher.""" - tree = dump_etree({"descriptions": []}) - assert len(tree.xpath("/resource/descriptions")) == 0 - - elem = dump_etree( - { - "descriptions": [ - { - "description": "Test", - "descriptionType": "Abstract", - }, - ] - } - ).xpath("/resource/descriptions/description")[0] - assert elem.get("descriptionType") == "Abstract" - assert elem.text == "Test" - - -def test_geolocations(): - """Test publisher.""" - tree = dump_etree({"geoLocations": []}) - assert len(tree.xpath("/resource/geoLocations")) == 0 - - elem = dump_etree( - { - "geoLocations": [ - { - "geoLocationPoint": "31 67", - "geoLocationBox": "31 67 32 68", - "geoLocationPlace": "Atlantic Ocean", - }, - ] - } - ).xpath("/resource/geoLocations/geoLocation")[0] - point = elem.xpath("geoLocationPoint")[0] - assert point.text == "31 67" - box = elem.xpath("geoLocationBox")[0] - assert box.text == "31 67 32 68" - place = elem.xpath("geoLocationPlace")[0] - assert place.text == "Atlantic Ocean" - - -def test_minimal_xsd(xsd31): - """Test that example XML converts to example JSON.""" - xsd31.assertValid( - etree.XML( - tostring( - { - "identifier": { - "identifierType": "DOI", - "identifier": "10.1234/foo.bar", - }, - "creators": [ - { - "creatorName": "Nielsen, Lars Holm", - }, - { - "creatorName": "Nielsen, Lars Holm", - "nameIdentifier": { - "nameIdentifier": "1234", - "schemeURI": "http://orcid.org", - "nameIdentifierScheme": "ORCID", - }, - }, - ], - "titles": [ - { - "title": "Minimal Test Case", - } - ], - "publisher": "Invenio Software", - "publicationYear": "2016", - } - ).encode("utf8") - ) - ) - - -def test_minimal_xml(xsd31): - """Test minimal xml.""" - from lxml import etree - - xml = """ - - 10.1234/foo.bar - - Nielsen, Lars Holm - - - Minimal Test Case - - Invenio Software - 2016 - """ - xsd31.assertValid(etree.XML(xml))