Skip to content

Commit

Permalink
include empty lists to make preprints happy
Browse files Browse the repository at this point in the history
update sharev2_elastic formatter to include empty lists in the record
sent to elasticsearch -- the preprints frontend breaks silently if those
lists aren't there
  • Loading branch information
aaxelb committed Feb 15, 2021
1 parent 88eade8 commit bf81c2f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
7 changes: 4 additions & 3 deletions share/metadata_formats/sharev2_elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings

from share.util.graph import MutableGraph
from share.util import IDObfuscator

from .base import MetadataFormatter

Expand All @@ -22,7 +23,7 @@ def format_node_type_lineage(node):


# values that, for the purpose of indexing in elasticsearch, are equivalent to absence
EMPTY_VALUES = (None, '', [], (), {}, set())
EMPTY_VALUES = (None, '')


def strip_empty_values(thing):
Expand Down Expand Up @@ -61,13 +62,13 @@ def format(self, normalized_datum):
# TODO handle deletion better -- maybe put a `deleted` field on suids and actually delete the FormattedMetadataRecord
if central_work['is_deleted']:
return json.dumps({
'id': str(suid.id),
'id': IDObfuscator.encode(suid),
'is_deleted': True,
})

source_name = suid.source_config.source.long_title
return json.dumps(strip_empty_values({
'id': str(suid.id),
'id': IDObfuscator.encode(suid),
'sources': [source_name],

'type': format_node_type(central_work),
Expand Down
32 changes: 28 additions & 4 deletions tests/share/metadata_formats/test_sharev2_elastic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import pytest
import dateutil
from unittest.mock import patch

from share.metadata_formats.sharev2_elastic import ShareV2ElasticFormatter, format_type
from share.util import IDObfuscator

from tests.factories import RawDatumFactory
from tests.factories.core import NormalizedDataFactory
Expand Down Expand Up @@ -34,20 +36,28 @@
'date_created': '2017-04-07T21:09:05.023090+00:00',
'date_modified': '2017-04-07T21:09:05.023090+00:00',
'date_updated': '2017-03-31T05:39:48+00:00',
'id': '7',
'id': 'encoded-7',
'identifiers': ['http://dx.doi.org/10.5772/9813'],
'publishers': ['InTech'],
'retracted': False,
'sources': ['SomeSource'],
'title': 'The Role of Mycorrhizas in Forest Soil Stability with Climate Change',
'type': 'creative work',
'types': ['creative work'],
'affiliations': [],
'funders': [],
'hosts': [],
'subject_synonyms': [],
'subjects': [],
'tags': [],
'lists': {
'affiliations': [],
'contributors': [
{
'cited_as': 'Suzanne Simard',
'family_name': 'Simard',
'given_name': 'Suzanne',
'identifiers': [],
'name': 'Suzanne Simard',
'order_cited': 0,
'relation': 'creator',
Expand All @@ -58,16 +68,21 @@
'cited_as': 'Mary Austi',
'family_name': 'Austi',
'given_name': 'Mary',
'identifiers': [],
'name': 'Mary Austi',
'order_cited': 1,
'relation': 'creator',
'type': 'person',
'types': ['person', 'agent'],
},
],
'funders': [],
'hosts': [],
'lineage': [],
'publishers': [
{
'name': 'InTech',
'identifiers': [],
'relation': 'publisher',
'type': 'organization',
'types': ['organization', 'agent'],
Expand All @@ -88,7 +103,7 @@
},
},
'expected_formatted': {
'id': '57',
'id': 'encoded-57',
'is_deleted': True,
},
},
Expand Down Expand Up @@ -353,7 +368,7 @@
'date_created': '2020-02-02T20:20:02.020000+00:00',
'date_modified': '2020-02-02T20:20:02.020000+00:00',
'date_published': '2019-01-23T20:34:21.633684+00:00',
'id': '123',
'id': 'encoded-123',
'identifiers': ['http://staging.osf.io/chair/'],
'registration_type': 'Open-Ended Registration',
'retracted': False,
Expand All @@ -369,10 +384,15 @@
'type': 'registration',
'types': ['registration', 'publication', 'creative work'],
'withdrawn': False,
'funders': [],
'hosts': [],
'publishers': [],
'tags': [],
'lists': {
'affiliations': [
{
'cited_as': 'Wassamatter University',
'identifiers': [],
'name': 'Wassamatter University',
'relation': 'agent work relation',
'type': 'institution',
Expand Down Expand Up @@ -404,6 +424,9 @@
'types': ['registration', 'publication', 'creative work'],
},
],
'funders': [],
'hosts': [],
'publishers': [],
},
},
},
Expand All @@ -420,7 +443,8 @@
for test_case in TEST_CASES
])
@pytest.mark.django_db
def test_format_sharev2_elastic(suid_id, source_name, normalized_datum_kwargs, expected_formatted):
@patch.object(IDObfuscator, 'encode', wraps=lambda suid: f'encoded-{suid.id}')
def test_format_sharev2_elastic(mock_encode, suid_id, source_name, normalized_datum_kwargs, expected_formatted):
normd = NormalizedDataFactory(
raw=RawDatumFactory(
suid__id=suid_id,
Expand Down

0 comments on commit bf81c2f

Please sign in to comment.