Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-pol committed Dec 1, 2023
1 parent 4ef2501 commit a1c70f5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/datastation/dataverse/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
65 changes: 65 additions & 0 deletions src/tests/test_datasets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from datastation.dataverse.datasets import Datasets
from datastation.dataverse.dataverse_client import DataverseClient

Expand Down Expand Up @@ -31,3 +33,66 @@ def test_update_metadata(self, caplog, capsys):
assert caplog.records[2].message == 'None' # without dryrun: <Response [200]>
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) == "<class 'json.decoder.JSONDecodeError'>"

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\']"}'

0 comments on commit a1c70f5

Please sign in to comment.