Skip to content

Commit

Permalink
chore: serialized-metadata tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed May 26, 2023
1 parent 7946749 commit b61c00f
Show file tree
Hide file tree
Showing 33 changed files with 2,006 additions and 674 deletions.
2 changes: 2 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def override_settings():
website_settings.BCRYPT_LOG_ROUNDS = 1
# Make sure we don't accidentally send any emails
website_settings.SENDGRID_API_KEY = None
# or try to contact a SHARE
website_settings.SHARE_ENABLED = False
# Set this here instead of in SILENT_LOGGERS, in case developers
# call setLevel in local.py
logging.getLogger('website.mails.mails').setLevel(logging.CRITICAL)
Expand Down
11 changes: 2 additions & 9 deletions osf/metadata/osf_gathering.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,16 +430,8 @@ def gather_versions(focus):
if hasattr(focus.dbmodel, 'versions'): # quacks like BaseFileNode
last_fileversion = focus.dbmodel.versions.last() # just the last version, for now
if last_fileversion is not None: # quacks like OsfStorageFileNode
from api.base.utils import absolute_reverse as apiv2_absolute_reverse
fileversion_iri = rdflib.URIRef(
apiv2_absolute_reverse(
'files:version-detail',
kwargs={
'version': 'v2', # api version
'file_id': focus.dbmodel._id,
'version_id': last_fileversion.identifier,
},
),
f'{focus.iri}?revision={last_fileversion.identifier}'
)
yield (OSF.hasFileVersion, fileversion_iri)
yield from _gather_fileversion(last_fileversion, fileversion_iri)
Expand Down Expand Up @@ -661,6 +653,7 @@ def gather_agents(focus):
# TODO: contributor roles
for user in getattr(focus.dbmodel, 'visible_contributors', ()):
yield (DCTERMS.creator, OsfFocus(user))
# TODO: preserve order via rdflib.Seq


@gather.er(OSF.affiliatedInstitution)
Expand Down
7 changes: 4 additions & 3 deletions osf/metadata/serializers/datacite/datacite_tree_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def _visit_related(self, parent_el):
relation_pairs.add((datacite_relation, related_iri))
related_identifiers_el = self.visit(parent_el, 'relatedIdentifiers', is_list=True)
related_items_el = self.visit(parent_el, 'relatedItems', is_list=True)
for datacite_relation, related_iri in relation_pairs:
for datacite_relation, related_iri in sorted(relation_pairs):
self._visit_related_identifier_and_item(
related_identifiers_el,
related_items_el,
Expand All @@ -396,11 +396,11 @@ def _visit_name_identifiers(self, parent_el, agent_iri):

def _visit_subjects(self, parent_el):
subjects_el = self.visit(parent_el, 'subjects', is_list=True)
for subject in self.basket[DCTERMS.subject]:
for subject in sorted(self.basket[DCTERMS.subject]):
self.visit(subjects_el, 'subject', text=subject, attrib={
'subjectScheme': BEPRESS_SUBJECT_SCHEME,
})
for keyword in self.basket[OSF.keyword]:
for keyword in sorted(self.basket[OSF.keyword]):
self.visit(subjects_el, 'subject', text=keyword)

def _visit_resource_type(self, parent_el):
Expand All @@ -417,6 +417,7 @@ def _visit_resource_type(self, parent_el):
})

def _get_resource_type_general(self, focus_iri):
# return just the first recognized type, preferably from dcterms:type
type_terms = itertools.chain(
self.basket[focus_iri:DCTERMS.type],
self.basket[focus_iri:RDF.type],
Expand Down
6 changes: 3 additions & 3 deletions osf/metadata/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def pls_gather_metadata_file(osf_item, format_key, serializer_config=None) -> Se
basket = pls_get_magic_metadata_basket(osfguid.referent)
serializer = get_metadata_serializer(format_key, basket, serializer_config)
return SerializedMetadataFile(
serializer.mediatype,
serializer.filename_for_itemid(osfguid._id),
serializer.serialize(),
mediatype=serializer.mediatype,
filename=serializer.filename_for_itemid(osfguid._id),
serialized_metadata=serializer.serialize(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"alternateIdentifiers": [
{
"alternateIdentifier": "http://localhost:5000/w3ibb",
"alternateIdentifierType": "URL"
}
],
"contributors": [
{
"contributorName": {
"contributorName": "Center for Open Science",
"nameType": "Organizational"
},
"contributorType": "HostingInstitution",
"nameIdentifier": {
"nameIdentifier": "https://cos.io/",
"nameIdentifierScheme": "URL"
}
}
],
"creators": [
{
"creatorName": {
"creatorName": "Person McNamington",
"nameType": "Personal"
},
"nameIdentifier": {
"nameIdentifier": "http://localhost:5000/w1ibb",
"nameIdentifierScheme": "URL"
}
}
],
"dates": [
{
"date": "2123-05-04",
"dateType": "Created"
},
{
"date": "2123-05-04",
"dateType": "Updated"
}
],
"descriptions": [],
"fundingReferences": [],
"identifier": {
"identifier": "http://localhost:5000/w3ibb",
"identifierType": "URL"
},
"publicationYear": "2123",
"publisher": "OSF",
"relatedIdentifiers": [
{
"relatedIdentifier": "10.70102/FK2osf.io/w2ibb",
"relatedIdentifierType": "DOI",
"relationType": "IsPartOf"
}
],
"relatedItems": [
{
"publicationYear": "2252",
"publisher": "OSF",
"relatedItemIdentifier": {
"relatedItemIdentifier": "10.70102/FK2osf.io/w2ibb",
"relatedItemIdentifierType": "DOI"
},
"relatedItemType": "Text",
"relationType": "IsPartOf",
"titles": [
{
"title": "this is a project title!"
}
]
}
],
"resourceType": {
"resourceType": "File",
"resourceTypeGeneral": "Text"
},
"rightsList": [],
"subjects": [],
"titles": [
{
"title": "my-file.blarg"
}
]
}
47 changes: 47 additions & 0 deletions osf_tests/metadata/expected_metadata_files/file_basic.datacite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version='1.0' encoding='utf-8'?>
<resource xmlns="http://datacite.org/schema/kernel-4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4.4/metadata.xsd">
<identifier identifierType="URL">http://localhost:5000/w3ibb</identifier>
<creators>
<creator>
<creatorName nameType="Personal">Person McNamington</creatorName>
<nameIdentifier nameIdentifierScheme="URL">http://localhost:5000/w1ibb</nameIdentifier>
</creator>
</creators>
<titles>
<title>my-file.blarg</title>
</titles>
<publisher>OSF</publisher>
<publicationYear>2123</publicationYear>
<subjects/>
<contributors>
<contributor contributorType="HostingInstitution">
<contributorName nameType="Organizational">Center for Open Science</contributorName>
<nameIdentifier nameIdentifierScheme="ROR">https://ror.org/05d5mza29</nameIdentifier>
<nameIdentifier nameIdentifierScheme="URL">https://cos.io/</nameIdentifier>
</contributor>
</contributors>
<dates>
<date dateType="Created">2123-05-04</date>
<date dateType="Updated">2123-05-04</date>
</dates>
<resourceType resourceTypeGeneral="Text">File</resourceType>
<alternateIdentifiers>
<alternateIdentifier alternateIdentifierType="URL">http://localhost:5000/w3ibb</alternateIdentifier>
</alternateIdentifiers>
<rightsList/>
<descriptions/>
<fundingReferences/>
<relatedIdentifiers>
<relatedIdentifier relatedIdentifierType="DOI" relationType="IsPartOf">10.70102/FK2osf.io/w2ibb</relatedIdentifier>
</relatedIdentifiers>
<relatedItems>
<relatedItem relationType="IsPartOf" relatedItemType="Text">
<relatedItemIdentifier relatedItemIdentifierType="DOI">10.70102/FK2osf.io/w2ibb</relatedItemIdentifier>
<titles>
<title>this is a project title!</title>
</titles>
<publicationYear>2252</publicationYear>
<publisher>OSF</publisher>
</relatedItem>
</relatedItems>
</resource>
41 changes: 41 additions & 0 deletions osf_tests/metadata/expected_metadata_files/file_basic.turtle
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix osf: <https://osf.io/vocab/2022/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .

<http://localhost:5000/w3ibb> a osf:File ;
dcterms:created "2123-05-04" ;
dcterms:identifier "http://localhost:5000/w3ibb" ;
dcterms:modified "2123-05-04" ;
osf:fileName "my-file.blarg" ;
osf:filePath "/my-file.blarg" ;
osf:hasFileVersion <http://localhost:5000/w3ibb?revision=1> ;
osf:isContainedBy <http://localhost:5000/w2ibb> .

<http://localhost:5000/w2ibb> a osf:Project ;
dcterms:created "2123-05-04" ;
dcterms:creator <http://localhost:5000/w1ibb> ;
dcterms:identifier "http://localhost:5000/w2ibb",
"https://doi.org/10.70102/FK2osf.io/w2ibb" ;
dcterms:publisher <http://localhost:5000> ;
dcterms:title "this is a project title!" ;
owl:sameAs <https://doi.org/10.70102/FK2osf.io/w2ibb> .

<http://localhost:5000/w3ibb?revision=1> a osf:FileVersion ;
dcterms:created "2123-05-04" ;
dcterms:creator <http://localhost:5000/w1ibb> ;
dcterms:extent "0.000007 MB" ;
dcterms:format "img/png" ;
dcterms:modified "2123-05-04" ;
dcterms:requires <urn:checksum:sha-256::6ac3c336e4094835293a3fed8a4b5fedde1b5e2626d9838fed50693bba00af0e> ;
osf:versionNumber "1" .

<http://localhost:5000/w1ibb> a osf:Agent ;
dcterms:identifier "http://localhost:5000/w1ibb" ;
dcterms:type foaf:Person ;
foaf:name "Person McNamington" .

<http://localhost:5000> a osf:Agent ;
dcterms:identifier "http://localhost:5000" ;
dcterms:type foaf:Organization ;
foaf:name "OSF" .
87 changes: 87 additions & 0 deletions osf_tests/metadata/expected_metadata_files/file_full.datacite.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"alternateIdentifiers": [
{
"alternateIdentifier": "http://localhost:5000/w3ibb",
"alternateIdentifierType": "URL"
}
],
"contributors": [
{
"contributorName": {
"contributorName": "Center for Open Science",
"nameType": "Organizational"
},
"contributorType": "HostingInstitution",
"nameIdentifier": {
"nameIdentifier": "https://cos.io/",
"nameIdentifierScheme": "URL"
}
}
],
"creators": [
{
"creatorName": {
"creatorName": "Person McNamington",
"nameType": "Personal"
},
"nameIdentifier": {
"nameIdentifier": "http://localhost:5000/w1ibb",
"nameIdentifierScheme": "URL"
}
}
],
"dates": [
{
"date": "2123-05-04",
"dateType": "Created"
},
{
"date": "2123-05-04",
"dateType": "Updated"
}
],
"descriptions": [],
"fundingReferences": [],
"identifier": {
"identifier": "http://localhost:5000/w3ibb",
"identifierType": "URL"
},
"publicationYear": "2123",
"publisher": "OSF",
"relatedIdentifiers": [
{
"relatedIdentifier": "10.70102/FK2osf.io/w2ibb",
"relatedIdentifierType": "DOI",
"relationType": "IsPartOf"
}
],
"relatedItems": [
{
"publicationYear": "2123",
"publisher": "OSF",
"relatedItemIdentifier": {
"relatedItemIdentifier": "10.70102/FK2osf.io/w2ibb",
"relatedItemIdentifierType": "DOI"
},
"relatedItemType": "Dataset",
"relationType": "IsPartOf",
"titles": [
{
"lang": "en",
"title": "this is a project title!"
}
]
}
],
"resourceType": {
"resourceType": "File",
"resourceTypeGeneral": "Text"
},
"rightsList": [],
"subjects": [],
"titles": [
{
"title": "my-file.blarg"
}
]
}
47 changes: 47 additions & 0 deletions osf_tests/metadata/expected_metadata_files/file_full.datacite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version='1.0' encoding='utf-8'?>
<resource xmlns="http://datacite.org/schema/kernel-4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4.4/metadata.xsd">
<identifier identifierType="URL">http://localhost:5000/w3ibb</identifier>
<creators>
<creator>
<creatorName nameType="Personal">Person McNamington</creatorName>
<nameIdentifier nameIdentifierScheme="URL">http://localhost:5000/w1ibb</nameIdentifier>
</creator>
</creators>
<titles>
<title>my-file.blarg</title>
</titles>
<publisher>OSF</publisher>
<publicationYear>2123</publicationYear>
<subjects/>
<contributors>
<contributor contributorType="HostingInstitution">
<contributorName nameType="Organizational">Center for Open Science</contributorName>
<nameIdentifier nameIdentifierScheme="ROR">https://ror.org/05d5mza29</nameIdentifier>
<nameIdentifier nameIdentifierScheme="URL">https://cos.io/</nameIdentifier>
</contributor>
</contributors>
<dates>
<date dateType="Created">2123-05-04</date>
<date dateType="Updated">2123-05-04</date>
</dates>
<resourceType resourceTypeGeneral="Text">File</resourceType>
<alternateIdentifiers>
<alternateIdentifier alternateIdentifierType="URL">http://localhost:5000/w3ibb</alternateIdentifier>
</alternateIdentifiers>
<rightsList/>
<descriptions/>
<fundingReferences/>
<relatedIdentifiers>
<relatedIdentifier relatedIdentifierType="DOI" relationType="IsPartOf">10.70102/FK2osf.io/w2ibb</relatedIdentifier>
</relatedIdentifiers>
<relatedItems>
<relatedItem relationType="IsPartOf" relatedItemType="Dataset">
<relatedItemIdentifier relatedItemIdentifierType="DOI">10.70102/FK2osf.io/w2ibb</relatedItemIdentifier>
<titles>
<title xml:lang="en">this is a project title!</title>
</titles>
<publicationYear>2123</publicationYear>
<publisher>OSF</publisher>
</relatedItem>
</relatedItems>
</resource>
Loading

0 comments on commit b61c00f

Please sign in to comment.