From 64cbda4e4a04397c6b976d1fe72554ba8643c304 Mon Sep 17 00:00:00 2001 From: Yuhuai Liu Date: Wed, 31 Jan 2024 01:00:18 -0500 Subject: [PATCH] add test_gather_cedar_templates --- osf/models/cedar_metadata.py | 2 +- osf_tests/factories.py | 8 +++++ osf_tests/metadata/test_osf_gathering.py | 37 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/osf/models/cedar_metadata.py b/osf/models/cedar_metadata.py index 2c7fa604136..1f7a19cbf8f 100644 --- a/osf/models/cedar_metadata.py +++ b/osf/models/cedar_metadata.py @@ -18,7 +18,7 @@ def __unicode__(self): return f'(name=[{self.schema_name}], version=[{self.template_version}], id=[{self.cedar_id}])' def get_semantic_iri(self): - return self.template['@id'] + return self.cedar_id class CedarMetadataRecord(ObjectIDMixin, BaseModel): diff --git a/osf_tests/factories.py b/osf_tests/factories.py index 3a0dbc361d1..f05effa3d8a 100644 --- a/osf_tests/factories.py +++ b/osf_tests/factories.py @@ -1207,3 +1207,11 @@ class Meta: class RegistrationBulkUploadRowFactory(DjangoModelFactory): class Meta: model = models.RegistrationBulkUploadRow + +class CedarMetadataRecordFactory(DjangoModelFactory): + class Meta: + model = models.CedarMetadataRecord + +class CedarMetadataTemplateFactory(DjangoModelFactory): + class Meta: + model = models.CedarMetadataTemplate diff --git a/osf_tests/metadata/test_osf_gathering.py b/osf_tests/metadata/test_osf_gathering.py index a0b95d51d94..26ca1de2dd1 100644 --- a/osf_tests/metadata/test_osf_gathering.py +++ b/osf_tests/metadata/test_osf_gathering.py @@ -42,6 +42,13 @@ def setUpTestData(cls): 'baiduScholar': 'blarg', }, ) + # cedar metadata template + cls.cedar_template = factories.CedarMetadataTemplateFactory( + cedar_id='https://repo.metadatacenter.org/templates/this-is-a-cedar-id', + schema_name='Hype Boy', + active=True, + template_version=1, + ) # project (with components): cls.project = factories.ProjectFactory(creator=cls.user__admin, is_public=True) cls.project.add_contributor(cls.user__readwrite, permissions=permissions.WRITE) @@ -49,6 +56,11 @@ def setUpTestData(cls): cls.component = factories.ProjectFactory(parent=cls.project, creator=cls.user__admin, is_public=True) cls.sibcomponent = factories.ProjectFactory(parent=cls.project, creator=cls.user__admin, is_public=True) cls.subcomponent = factories.ProjectFactory(parent=cls.component, creator=cls.user__admin, is_public=True) + cls.project_cedar_record = factories.CedarMetadataRecordFactory( + template=cls.cedar_template, + is_published=True, + guid=cls.project.guids.first() + ) # file: cls.file_sha256 = '876b99ba1225de6b7f55ef52b068d0da3aa2ec4271875954c3b87b6659ae3823' cls.file = create_test_file( @@ -58,6 +70,11 @@ def setUpTestData(cls): filename='blarg.txt', sha256=cls.file_sha256, ) + cls.file_cedar_reford = factories.CedarMetadataRecordFactory( + template=cls.cedar_template, + is_published=True, + guid=cls.file.get_guid() + ) # registration: cls.registration = factories.RegistrationFactory( project=cls.project, @@ -73,6 +90,11 @@ def setUpTestData(cls): ) cls.preprint.add_contributor(cls.user__readwrite, permissions=permissions.WRITE) cls.preprint.add_contributor(cls.user__readonly, permissions=permissions.READ, visible=False) + cls.registration_cedar_record = factories.CedarMetadataRecordFactory( + template=cls.cedar_template, + is_published=True, + guid=cls.registration.guids.first() + ) # "focus" objects: cls.projectfocus = osf_gathering.OsfFocus(cls.project) cls.componentfocus = osf_gathering.OsfFocus(cls.component) @@ -701,3 +723,18 @@ def test_gather_preprint_withdrawal(self): (_withdrawal_bnode, DCTERMS.dateAccepted, Literal(str(_withdrawal_request.date_last_transitioned.date()))), (_withdrawal_bnode, DCTERMS.creator, osf_gathering.OsfFocus(_withdrawal_request.creator)), }) + + def test_gather_cedar_templates(self): + cedar_template_iri = rdflib.URIRef(self.cedar_template.cedar_id) + assert_triples(osf_gathering.gather_cedar_templates(self.projectfocus), { + (self.projectfocus.iri, OSF.hasCedarTemplate, cedar_template_iri), + (cedar_template_iri, DCTERMS.title, Literal(self.cedar_template.schema_name)) + }) + assert_triples(osf_gathering.gather_cedar_templates(self.registrationfocus), { + (self.registrationfocus.iri, OSF.hasCedarTemplate, cedar_template_iri), + (cedar_template_iri, DCTERMS.title, Literal(self.cedar_template.schema_name)) + }) + assert_triples(osf_gathering.gather_cedar_templates(self.filefocus), { + (self.filefocus.iri, OSF.hasCedarTemplate, cedar_template_iri), + (cedar_template_iri, DCTERMS.title, Literal(self.cedar_template.schema_name)) + })