Skip to content

Commit 3561ed0

Browse files
added more tests, make delete cedar record function working without cedar record existence
1 parent b1630a1 commit 3561ed0

2 files changed

Lines changed: 216 additions & 20 deletions

File tree

api/share/utils.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ def cedar_record_to_turtle(referent, cedar_record):
103103

104104

105105
@celery_app.task(bind=True)
106-
def share_update_cedar_metadata_record(self, guid_id, cedar_record_pk):
107-
from osf.models import CedarMetadataRecord, Guid
106+
def share_update_cedar_metadata_record(self, referent_id, cedar_record_pk):
107+
from osf.models import Guid, CedarMetadataRecord
108108

109-
guid = Guid.load(guid_id)
109+
guid = Guid.load(referent_id)
110110
referent = guid.referent
111111
cedar_record = CedarMetadataRecord.objects.filter(pk=cedar_record_pk).first()
112112
if not cedar_record:
@@ -117,7 +117,7 @@ def share_update_cedar_metadata_record(self, guid_id, cedar_record_pk):
117117
shtrove_ingest_url(),
118118
params={
119119
'focus_iri': referent.get_semantic_iri(),
120-
'record_identifier': _shtrove_cedar_record_identifier(cedar_record),
120+
'record_identifier': _shtrove_cedar_record_identifier(cedar_record._id, cedar_record.template.cedar_id),
121121
'is_supplementary': True,
122122
},
123123
headers={
@@ -130,18 +130,18 @@ def share_update_cedar_metadata_record(self, guid_id, cedar_record_pk):
130130

131131

132132
@celery_app.task(bind=True)
133-
def share_delete_cedar_metadata_record(self, guid_id, cedar_record_pk):
134-
from osf.models import CedarMetadataRecord, Guid
135-
guid = Guid.load(guid_id)
136-
referent = guid.referent
137-
cedar_record = CedarMetadataRecord.objects.filter(pk=cedar_record_pk).first()
138-
if not cedar_record:
139-
return
140-
133+
def share_delete_cedar_metadata_record(
134+
self,
135+
cedar_referent___id,
136+
cedar_record___id,
137+
cedar_template_cedar_id,
138+
):
139+
from osf.models import Guid
140+
referent = Guid.load(cedar_referent___id).referent
141141
response = requests.delete(
142142
shtrove_ingest_url(),
143143
params={
144-
'record_identifier': _shtrove_cedar_record_identifier(cedar_record),
144+
'record_identifier': _shtrove_cedar_record_identifier(cedar_record___id, cedar_template_cedar_id),
145145
},
146146
headers=_shtrove_auth_headers(referent),
147147
)
@@ -200,7 +200,13 @@ def task__update_share(self, guid: str, is_backfill=False, osfmap_partition_name
200200
for cedar_record in _osfid_instance.cedar_metadata_records.filter(
201201
Q(is_published=False) | Q(template__should_index_for_search=False),
202202
):
203-
enqueue_task(share_delete_cedar_metadata_record.s(_osfid_instance._id, cedar_record.pk))
203+
enqueue_task(
204+
share_delete_cedar_metadata_record.s(
205+
cedar_record.guid._id,
206+
cedar_record._id,
207+
cedar_record.template.cedar_id,
208+
),
209+
)
204210

205211

206212
def pls_send_trove_record(osf_item, *, is_backfill: bool, osfmap_partition: OsfmapPartition):
@@ -256,8 +262,8 @@ def _shtrove_record_identifier(osf_item, osfmap_partition: OsfmapPartition):
256262
)
257263

258264

259-
def _shtrove_cedar_record_identifier(cedar_record) -> str:
260-
return f'{cedar_record.guid._id}/CedarMetadataRecord:{cedar_record.template.cedar_id}'
265+
def _shtrove_cedar_record_identifier(cedar_record___id, template_cedar_id) -> str:
266+
return f'{cedar_record___id}/CedarMetadataRecord:{template_cedar_id}'
261267

262268

263269
def _shtrove_auth_headers(osf_item):

osf_tests/test_collection_submission.py

Lines changed: 194 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from osf.utils.workflows import CollectionSubmissionStates
1414
from framework.exceptions import PermissionsError
1515
from api_tests.utils import UserRoles
16+
from api.share.utils import cedar_record_to_turtle, _shtrove_cedar_record_identifier
1617
from django.utils import timezone
1718
from website import settings
1819

@@ -630,7 +631,7 @@ def test_unindexable_template_and_unpublished_record_calls_records_deletion(
630631
):
631632
cedar_template.should_index_for_search = False
632633
cedar_template.save()
633-
CedarMetadataRecord.objects.create(
634+
record = CedarMetadataRecord.objects.create(
634635
guid=unmoderated_collection_submission_public.guid,
635636
template=cedar_template,
636637
metadata=cedar_template_json,
@@ -643,6 +644,11 @@ def test_unindexable_template_and_unpublished_record_calls_records_deletion(
643644

644645
assert not mock_create.s.called
645646
assert mock_delete.s.called
647+
mock_delete.s.assert_called_with(
648+
record.guid._id,
649+
record._id,
650+
record.template.cedar_id
651+
)
646652

647653
@mock.patch('api.share.utils.pls_send_trove_record')
648654
@mock.patch('api.share.utils.share_update_cedar_metadata_record')
@@ -658,7 +664,7 @@ def test_indexable_template_and_unpublished_record_calls_records_deletion(
658664
):
659665
cedar_template.should_index_for_search = True
660666
cedar_template.save()
661-
CedarMetadataRecord.objects.create(
667+
record = CedarMetadataRecord.objects.create(
662668
guid=unmoderated_collection_submission_public.guid,
663669
template=cedar_template,
664670
metadata=cedar_template_json,
@@ -671,6 +677,11 @@ def test_indexable_template_and_unpublished_record_calls_records_deletion(
671677

672678
assert not mock_create.s.called
673679
assert mock_delete.s.called
680+
mock_delete.s.assert_called_with(
681+
record.guid._id,
682+
record._id,
683+
record.template.cedar_id
684+
)
674685

675686
@mock.patch('api.share.utils.pls_send_trove_record')
676687
@mock.patch('api.share.utils.share_update_cedar_metadata_record')
@@ -686,7 +697,7 @@ def test_unindexable_template_and_published_record_calls_records_deletion(
686697
):
687698
cedar_template.should_index_for_search = False
688699
cedar_template.save()
689-
CedarMetadataRecord.objects.create(
700+
record = CedarMetadataRecord.objects.create(
690701
guid=unmoderated_collection_submission_public.guid,
691702
template=cedar_template,
692703
metadata=cedar_template_json,
@@ -699,6 +710,11 @@ def test_unindexable_template_and_published_record_calls_records_deletion(
699710

700711
assert not mock_create.s.called
701712
assert mock_delete.s.called
713+
mock_delete.s.assert_called_with(
714+
record.guid._id,
715+
record._id,
716+
record.template.cedar_id
717+
)
702718

703719
@mock.patch('api.share.utils.pls_send_trove_record')
704720
@mock.patch('api.share.utils.share_update_cedar_metadata_record')
@@ -714,7 +730,7 @@ def test_indexable_template_and_published_record_call_shtrove(
714730
):
715731
cedar_template.should_index_for_search = True
716732
cedar_template.save()
717-
CedarMetadataRecord.objects.create(
733+
record = CedarMetadataRecord.objects.create(
718734
guid=unmoderated_collection_submission_public.guid,
719735
template=cedar_template,
720736
metadata=cedar_template_json,
@@ -726,4 +742,178 @@ def test_indexable_template_and_published_record_call_shtrove(
726742
unmoderated_collection_submission_public.save()
727743

728744
assert mock_create.s.called
745+
mock_create.s.assert_called_with(unmoderated_collection_submission_public.guid._id, record.pk)
729746
assert not mock_delete.s.called
747+
748+
def test_share_update_cedar_metadata_record(self, unmoderated_collection_submission_public, cedar_template):
749+
metadata = {
750+
'@context': {
751+
'pav': 'http://purl.org/pav/',
752+
'url': 'http://schema.org/url',
753+
'xsd': 'http://www.w3.org/2001/XMLSchema#',
754+
'name': 'http://schema.org/name',
755+
'oslc': 'http://open-services.net/ns/core#',
756+
'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
757+
'skos': 'http://www.w3.org/2004/02/skos/core#',
758+
'author': 'http://schema.org/author',
759+
'funder': 'https://schema.metadatacenter.org/properties/c35f0660-2072-46a3-8e0d-532e40d94919',
760+
'schema': 'http://schema.org/',
761+
'license': 'http://schema.org/license',
762+
'citation': 'http://schema.org/citation',
763+
'keywords': 'http://schema.org/keywords',
764+
'identifier': 'http://schema.org/identifier',
765+
'rdfs:label': {
766+
'@type': 'xsd:string'
767+
},
768+
'description': 'http://schema.org/description',
769+
'schema:name': {
770+
'@type': 'xsd:string'
771+
},
772+
'pav:createdBy': {
773+
'@type': '@id'
774+
},
775+
'pav:createdOn': {
776+
'@type': 'xsd:dateTime'
777+
},
778+
'skos:notation': {
779+
'@type': 'xsd:string'
780+
},
781+
'oslc:modifiedBy': {
782+
'@type': '@id'
783+
},
784+
'pav:derivedFrom': {
785+
'@type': '@id'
786+
},
787+
'schema:isBasedOn': {
788+
'@type': '@id'
789+
},
790+
'variableMeasured': 'http://schema.org/variableMeasured',
791+
'pav:lastUpdatedOn': {
792+
'@type': 'xsd:dateTime'
793+
},
794+
'schema:description': {
795+
'@type': 'xsd:string'
796+
},
797+
'About this template': 'https://repo.metadatacenter.org/template-fields/bc66544c-e100-439e-9e80-9b35537368e5'
798+
},
799+
'name': {
800+
'@value': 'name'
801+
},
802+
'description': {
803+
'@value': 'description'
804+
},
805+
'variableMeasured': [
806+
{
807+
'@value': 'variable'
808+
}
809+
],
810+
'author': [
811+
{
812+
'@value': None
813+
}
814+
],
815+
'citation': {
816+
'@value': None
817+
},
818+
'license': {
819+
'@value': None
820+
},
821+
'funder': [
822+
{
823+
'@value': '1111111'
824+
}
825+
],
826+
'url': {},
827+
'keywords': {
828+
'@value': None
829+
},
830+
'identifier': {}
831+
}
832+
record = CedarMetadataRecord.objects.create(
833+
guid=unmoderated_collection_submission_public.guid,
834+
template=cedar_template,
835+
metadata=metadata,
836+
is_published=True,
837+
)
838+
result = cedar_record_to_turtle(record.guid.referent, record)
839+
vocab_url = '<https://osf.io/vocab/2022/>'
840+
schema_url = '<http://schema.org/>'
841+
schema_metadata_url = '<https://schema.metadatacenter.org/properties/>'
842+
urls_to_find = {
843+
vocab_url: None,
844+
schema_url: None,
845+
schema_metadata_url: None
846+
}
847+
for url in urls_to_find.keys():
848+
urls_to_find[url] = result[result.index(url) - 3]
849+
850+
# fetch urls from result and assign ns prefixes based on order of appearance in result to make test resilient to changes in order of namespace declaration in turtle output
851+
ns1 = list(filter(lambda url: urls_to_find[url] == '1', urls_to_find.keys()))[0]
852+
ns2 = list(filter(lambda url: urls_to_find[url] == '2', urls_to_find.keys()))[0]
853+
ns3 = list(filter(lambda url: urls_to_find[url] == '3', urls_to_find.keys()))[0]
854+
vocab_n = urls_to_find[vocab_url]
855+
schema_n = urls_to_find[schema_url]
856+
schema_metadata_n = urls_to_find[schema_metadata_url]
857+
# compose expected result dynamically based on ordering of prefixes
858+
# however ns attributes are strictly attached to specific prefix
859+
expected = (
860+
f'@prefix ns1: {ns1} .\n'
861+
f'@prefix ns2: {ns2} .\n'
862+
f'@prefix ns3: {ns3} .\n\n'
863+
f'<http://localhost:5000/{unmoderated_collection_submission_public.guid._id}> ns{vocab_n}:hasCedarRecord [ ns{schema_n}:description "description" ;\n'
864+
f' ns{schema_n}:identifier [ ] ;\n'
865+
f' ns{schema_n}:name "name" ;\n'
866+
f' ns{schema_n}:url [ ] ;\n'
867+
f' ns{schema_n}:variableMeasured "variable" ;\n'
868+
f' ns{schema_metadata_n}:c35f0660-2072-46a3-8e0d-532e40d94919 "1111111" ] .\n\n'
869+
)
870+
871+
assert result == expected
872+
873+
def test_cedar_record_identifier_on_create(self, unmoderated_collection_submission_public, cedar_template):
874+
cedar_template.should_index_for_search = True
875+
cedar_template.save()
876+
877+
with mock.patch('api.share.utils.pls_send_trove_record'):
878+
with mock.patch('api.share.utils.share_update_cedar_metadata_record'):
879+
with mock.patch('api.share.utils.share_delete_cedar_metadata_record'):
880+
to_create_record = CedarMetadataRecord.objects.create(
881+
guid=unmoderated_collection_submission_public.guid,
882+
template=cedar_template,
883+
metadata=cedar_template.template,
884+
is_published=True,
885+
)
886+
887+
with mock.patch('api.share.utils.pls_send_trove_record'):
888+
with mock.patch('api.share.utils.share_delete_cedar_metadata_record'):
889+
with mock.patch('api.share.utils._shtrove_cedar_record_identifier') as mock_identifier:
890+
unmoderated_collection_submission_public.save()
891+
mock_identifier.assert_called_with(
892+
to_create_record._id,
893+
to_create_record.template.cedar_id
894+
)
895+
assert (
896+
_shtrove_cedar_record_identifier(to_create_record._id, to_create_record.template.cedar_id) ==
897+
f'{to_create_record._id}/CedarMetadataRecord:{to_create_record.template.cedar_id}'
898+
)
899+
900+
def test_cedar_record_identifier_on_delete(self, unmoderated_collection_submission_public, cedar_template):
901+
with mock.patch('api.share.utils.pls_send_trove_record'):
902+
with mock.patch('api.share.utils.share_update_cedar_metadata_record'):
903+
with mock.patch('api.share.utils.share_delete_cedar_metadata_record'):
904+
to_delete_record = CedarMetadataRecord.objects.create(
905+
guid=unmoderated_collection_submission_public.guid,
906+
template=cedar_template,
907+
metadata=cedar_template.template,
908+
is_published=False,
909+
)
910+
911+
with mock.patch('api.share.utils.pls_send_trove_record'):
912+
with mock.patch('api.share.utils.share_update_cedar_metadata_record'):
913+
with mock.patch('api.share.utils._shtrove_cedar_record_identifier') as mock_identifier:
914+
unmoderated_collection_submission_public.save()
915+
mock_identifier.assert_called_with(to_delete_record._id, to_delete_record.template.cedar_id)
916+
assert (
917+
_shtrove_cedar_record_identifier(to_delete_record._id, to_delete_record.template.cedar_id) ==
918+
f'{to_delete_record._id}/CedarMetadataRecord:{to_delete_record.template.cedar_id}'
919+
)

0 commit comments

Comments
 (0)