1313from osf .utils .workflows import CollectionSubmissionStates
1414from framework .exceptions import PermissionsError
1515from api_tests .utils import UserRoles
16+ from api .share .utils import cedar_record_to_turtle , _shtrove_cedar_record_identifier
1617from django .utils import timezone
1718from 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