def test_metadata_get_metadata_from_researchId_filtered(self): """ This unit tests the ability of metadata object to get metadata from a given researchId. This is the handy way with researchId. Notice: Should be very similar to `test_metadata_get_metadata_from_connector` """ md = Metadata(testing=testing_address) userId = 20 researchIndex = 21 researchId = 22 projectId = 5 metadata = { "Creators": [{ "name": "Mustermann, Max", "nameType": "Personal", "familyName": "Mustermann", "givenName": "Max", }], "Identifiers": [{ "identifierType": "DOI", "identifier": "10.5072/example" }], "PublicationYear": "2020", "Publisher": "University of Münster", "ResourceType": "Poster", "SchemaVersion": "http://datacite.org/schema/kernel-4", "Titles": [{ "title": "This is a test title", "lang": "de" }] } wanted_metadata = {"Titles": "", "Publisher": ""} expected_metadata_from_port = { "Titles": metadata["Titles"], "Publisher": metadata["Publisher"] } expected_research = {"userId": userId, "researchIndex": researchIndex} expected_research["researchId"] = researchId expected_research["portIn"] = [{ "port": "port-zenodo", "properties": [{ "portType": "metadata", "value": True }, { "portType": "customProperties", "value": [{ "key": "projectId", "value": str(projectId) }] }] }, { "port": "port-owncloud", "properties": [{ "portType": "fileStorage", "value": True }] }] expected_research["portOut"] = [{ "port": "port-zenodo", "properties": [{ "portType": "metadata", "value": True }] }] pact.given('The research manager.').upon_receiving( f'A call to get the researchIndex {researchIndex} and user {userId}.' ).with_request('GET', f"/research/id/{researchId}").will_respond_with( 200, body=expected_research) expected_metadata = [] # only portOut, because it has a duplicate from portIn and portIn has port-owncloud, # which should be out in results, because it is not of type `metadata`. for ports in expected_research["portOut"]: skip = True for prop in ports["properties"]: if prop["portType"] == "metadata": skip = False if skip: continue pact.given('An access token for userid.').upon_receiving( f'A call to get the access token for user {userId} for metadata usage.' ).with_request( 'GET', f"/user/{userId}/service/port-zenodo").will_respond_with( 200, body=json.dumps(self.token1, cls=Util.get_json_encoder())) port = ports["port"] pact.given('A port with metadata informations.').upon_receiving( f'A call to get the metadata from specific projectId {projectId} and port {port} for {expected_metadata_from_port}.' ).with_request('GET', f"/metadata/project/{projectId}").will_respond_with( 200, body=expected_metadata_from_port) expected_metadata.append({ "port": port, "metadata": expected_metadata_from_port }) with pact: result = md.getMetadataForResearch(researchId=researchId, metadataFields=wanted_metadata) self.assertEqual(result, expected_metadata)
def test_metadata_response(updateMetadata): userId = 0 researchIndex = 0 researchId = 2 projectId = 5 metadata = { "Creators": [], "Identifiers": [], "PublicationYear": "", "Publisher": "", "ResourceType": "", "SchemaVersion": "http://datacite.org/schema/kernel-4", "Titles": [] } metadata = dict( list(metadata.items()) + list(updateMetadata.items())) expected_pid = {"userId": userId, "researchIndex": researchIndex} expected_research = expected_pid.copy() expected_research["status"] = 1 expected_research["researchId"] = researchId expected_research["portIn"] = [{ "port": "port-zenodo", "properties": [{ "portType": "metadata", "value": True }] }, { "port": "port-owncloud", "properties": [{ "portType": "fileStorage", "value": True }] }] expected_research["portOut"] = [{ "port": "port-zenodo", "properties": [{ "portType": "metadata", "value": True }, { "portType": "customProperties", "value": [{ "key": "projectId", "value": str(projectId) }] }] }] pact.given('The research manager.').upon_receiving( 'A call to get the researchIndex and user.').with_request( 'GET', f"/research/id/{researchId}").will_respond_with( 200, body=expected_research) expected_metadata = [] # add patch requests for all given example ports, which are portType `metadata` for port in expected_research["portOut"]: skip = True for prop in port["properties"]: if prop["portType"] == "metadata": skip = False if skip: continue pact.given('An access token for userid.').upon_receiving( f'A call to get the access token for user {userId} for project.' ).with_request( 'GET', f"/user/{userId}/service/port-zenodo").will_respond_with( 200, body=json.dumps(self.token1, cls=Util.get_json_encoder())) portname = port["port"] pact.given( 'A port with metadata informations.' ).upon_receiving( f'A call to update the metadata from specific projectId {projectId} for port {portname} with update {updateMetadata.keys()}.' ).with_request( 'PATCH', f"/metadata/project/{projectId}").will_respond_with( 200, body=updateMetadata) expected_metadata.append({ "port": portname, "metadata": updateMetadata }) with pact: result = md.updateMetadataForResearch(researchId, updateMetadata) self.assertEqual(result, expected_metadata)