From a1c70f594446e37d685d1c74107b99738ff70388 Mon Sep 17 00:00:00 2001 From: jo-pol Date: Fri, 1 Dec 2023 12:51:13 +0100 Subject: [PATCH] more tests --- src/datastation/dataverse/datasets.py | 2 + src/tests/test_datasets.py | 65 +++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/datastation/dataverse/datasets.py b/src/datastation/dataverse/datasets.py index e40817d..1189f48 100644 --- a/src/datastation/dataverse/datasets.py +++ b/src/datastation/dataverse/datasets.py @@ -19,6 +19,8 @@ def update_metadata(self, data: dict): for key in [key for key in data.keys() if key != 'PID' and data[key] is not None]: if data[key].startswith('['): all_fields.append({'typeName': key, 'value': (json.loads(data[key]))}) + elif '@' in key: + raise Exception("Subfields not yet supported.") else: all_fields.append({'typeName': key, 'value': data[key]}) logging.debug(all_fields) diff --git a/src/tests/test_datasets.py b/src/tests/test_datasets.py index d5827a0..4a1247c 100644 --- a/src/tests/test_datasets.py +++ b/src/tests/test_datasets.py @@ -1,3 +1,5 @@ +import pytest + from datastation.dataverse.datasets import Datasets from datastation.dataverse.dataverse_client import DataverseClient @@ -31,3 +33,66 @@ def test_update_metadata(self, caplog, capsys): assert caplog.records[2].message == 'None' # without dryrun: assert caplog.records[2].funcName == 'update_metadata' assert caplog.records[2].levelname == 'INFO' + + def test_update_metadata_with_repetitive_field(self, caplog, capsys): + caplog.set_level('DEBUG') + client = DataverseClient(config=self.cfg) + datasets = Datasets(client, dry_run=True) + data = {'PID': 'doi:10.5072/FK2/8KQW3Y', 'author': '["me","O\'Neill"]'} + + datasets.update_metadata(data) + assert capsys.readouterr().out == ('DRY-RUN: only printing command, not sending it...\n' + f'PUT {self.url}/api/datasets/:persistentId/editMetadata\n' + "headers: {'X-Dataverse-key': 'xxx'}\n" + "params: {'persistentId': 'doi:10.5072/FK2/8KQW3Y', 'replace': 'true'}\n" + 'data: {"fields": [{"typeName": "author", "value": ["me", "O\'Neill"]}]}\n' + '\n') + assert len(caplog.records) == 3 + assert caplog.records[0].message == "{'PID': 'doi:10.5072/FK2/8KQW3Y', 'author': '[\"me\",\"O\\'Neill\"]'}" + assert caplog.records[1].message == '[{\'typeName\': \'author\', \'value\': [\'me\', "O\'Neill"]}]' + + def test_update_metadata_with_subfield(self, caplog, capsys): + caplog.set_level('DEBUG') + client = DataverseClient(config=self.cfg) + datasets = Datasets(client, dry_run=True) + data = {'PID': 'doi:10.5072/FK2/8KQW3Y', + 'author@authorName': 'me', + 'author@authorAffiliation': 'my organization'} + + with pytest.raises(Exception) as e: + datasets.update_metadata(data) + assert str(e.value) == 'Subfields not yet supported.' + + # test driven expectations below + + # datasets.update_metadata(data) + # assert capsys.readouterr().out == ('DRY-RUN: only printing command, not sending it...\n' + # f'PUT {self.url}/api/datasets/:persistentId/editMetadata\n' + # "headers: {'X-Dataverse-key': 'xxx'}\n" + # "params: {'persistentId': 'doi:10.5072/FK2/8KQW3Y', 'replace': 'true'}\n" + # 'data: {"fields": [{"typeName": "author", "value": [{"authorName": "me", ' + # '"authorAffiliation": "my organization"}]}]}\n' + # '\n') + + assert len(caplog.records) == 1 + assert caplog.records[0].message == "{'PID': 'doi:10.5072/FK2/8KQW3Y', 'author@authorName': 'me', " \ + "'author@authorAffiliation': 'my organization'}" + # assert caplog.records[1].message == "[{'typeName': 'author', 'value': [{'authorName': 'me', " \ + # "'authorAffiliation': 'my organization'}]}]" + + def test_update_metadata_with_too_many_values(self, caplog, capsys): + caplog.set_level('DEBUG') + client = DataverseClient(config=self.cfg) + datasets = Datasets(client, dry_run=True) + data = {'PID': 'doi:10.5072/FK2/8KQW3Y', 'title': 'New title', 'author': "['me','you']"} + + with pytest.raises(Exception) as e: + datasets.update_metadata(data) + assert str(e.value) == 'Expecting value: line 1 column 2 (char 1)' # wants double quotes gets single quotes + assert str(e.type) == "" + + assert capsys.readouterr().out == '' + assert len(caplog.records) == 1 + assert caplog.records[0].levelname == 'DEBUG' + assert caplog.records[0].message == "{'PID': 'doi:10.5072/FK2/8KQW3Y', 'title': 'New title', 'author': " \ + '"[\'me\',\'you\']"}'