Skip to content

Commit

Permalink
Apply review suggestions and refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Iuliana Voinea <[email protected]>
  • Loading branch information
Iuliana Voinea committed Dec 8, 2017
1 parent e9fdb00 commit fb97a92
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 137 deletions.
52 changes: 35 additions & 17 deletions inspirehep/modules/records/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def populate_book_series_suggest(sender, json, *args, **kwargs):
return

doc_types = json.get('document_type', [])
if all(doc_type not in doc_types for doc_type in ['book', 'thesis', 'proceedings']):
if not set(doc_types) & {'book', 'thesis', 'proceedings'}:
return

book_series = json.get('book_series', [])
Expand Down Expand Up @@ -234,12 +234,26 @@ def populate_conference_suggest(sender, json, *args, **kwargs):
if 'conferences.json' not in json.get('$schema'):
return

conference_paths = [
'cnum',
'acronyms',
'series.name',
'titles.source',
'titles.subtitle',
'titles.title',
'opening_date',
]

input_values = [el for el in chain.from_iterable(
[force_list(get_value(json, path)) for path in conference_paths]) if el]

cnum = json.get('cnum', '')

record = get_value(json, 'self.$ref', '')

json.update({
'conference_suggest': {
'input': cnum,
'input': input_values,
'output': cnum,
'payload': {
'$ref': record,
Expand All @@ -253,16 +267,18 @@ def populate_experiment_suggest(sender, json, *args, **kwargs):
if 'experiments.json' not in json.get('$schema'):
return

experiment_paths = [
'legacy_name',
'long_name',
'name_variants',
]

input_values = [el for el in chain.from_iterable(
[force_list(get_value(json, path)) for path in experiment_paths]) if el]

legacy_name = json.get('legacy_name', '')
long_name = json.get('long_name', '')
name_variants = json.get('name_variants', '')
record = get_value(json, 'self.$ref', '')

input_values = []
input_values.append(legacy_name)
input_values.append(long_name)
input_values.extend(name_variants)
input_values = [el for el in input_values if el]
record = get_value(json, 'self.$ref', '')

json.update({
'experiment_suggest': {
Expand Down Expand Up @@ -407,15 +423,17 @@ def populate_title_suggest(sender, json, *args, **kwargs):
if 'journals.json' not in json.get('$schema'):
return

conference_paths = [
'journal_title.title',
'short_title',
'title_variants',
]

input_values = [el for el in chain.from_iterable(
[force_list(get_value(json, path)) for path in conference_paths]) if el]

journal_title = get_value(json, 'journal_title.title', '')
short_title = json.get('short_title', '')
title_variants = json.get('title_variants', [])

input_values = []
input_values.append(journal_title)
input_values.append(short_title)
input_values.extend(title_variants)
input_values = [el for el in input_values if el]

record = get_value(json, 'self.$ref', '')

Expand Down
150 changes: 30 additions & 120 deletions tests/unit/records/test_records_receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,22 @@ def test_populate_conference_suggest():
record = {
'$schema': 'http://localhost:5000/schemas/records/conferences.json',
'cnum': 'C87-12-25',
'acronyms': [
'SUSY 2018',
],
'series': [
{
'name': 'Conf Series',
},
],
'titles': [
{
'source': 'A source',
'subtitle': 'A subtitle',
'title': 'A title',
},
],
'opening_date': '2009-03-12',
'self': {
'$ref': 'http://localhost:5000/api/conferences/bar'
},
Expand All @@ -443,11 +459,19 @@ def test_populate_conference_suggest():
populate_conference_suggest(None, record)

expected = {
'input': 'C87-12-25',
'output': 'C87-12-25',
'payload': {
'$ref': 'http://localhost:5000/api/conferences/bar'
},
'input': [
'C87-12-25',
'SUSY 2018',
'Conf Series',
'A source',
'A subtitle',
'A title',
'2009-03-12',
],
'output': 'C87-12-25',
'payload': {
'$ref': 'http://localhost:5000/api/conferences/bar'
},
}

result = record['conference_suggest']
Expand Down Expand Up @@ -1104,7 +1128,7 @@ def test_populate_abstract_source_suggest_does_nothing_if_record_is_not_literatu
assert expected == result


def test_populate_title_suggest_with_all_inputs():
def test_populate_title_suggest():
schema = load_schema('journals')
journal_title_schema = schema['properties']['journal_title']
short_title_schema = schema['properties']['short_title']
Expand Down Expand Up @@ -1143,120 +1167,6 @@ def test_populate_title_suggest_with_all_inputs():
assert expected == result


def test_populate_title_suggest_without_short_title():
schema = load_schema('journals')
journal_title_schema = schema['properties']['journal_title']
short_title_schema = schema['properties']['short_title']
title_variants_schema = schema['properties']['title_variants']

record = {
'$schema': 'http://localhost:5000/schemas/records/journals.json',
'journal_title': {'title': 'The Journal of High Energy Physics (JHEP)'},
'short_title': '',
'title_variants': ['JOURNAL OF HIGH ENERGY PHYSICS'],
'self': {
'$ref': 'https://localhost:5000/api/journals/bar',
},
}
assert validate(record['journal_title'], journal_title_schema) is None
assert validate(record['short_title'], short_title_schema) is None
assert validate(record['title_variants'], title_variants_schema) is None

populate_title_suggest(None, record)

expected = {
'input': [
'The Journal of High Energy Physics (JHEP)',
'JOURNAL OF HIGH ENERGY PHYSICS'
],
'output': '',
'payload': {
'full_title': 'The Journal of High Energy Physics (JHEP)',
'$ref': 'https://localhost:5000/api/journals/bar',
}
}

result = record['title_suggest']

assert expected == result


def test_populate_title_suggest_without_title_variants():
schema = load_schema('journals')
journal_title_schema = schema['properties']['journal_title']
short_title_schema = schema['properties']['short_title']
title_variants_schema = schema['properties']['title_variants']

record = {
'$schema': 'http://localhost:5000/schemas/records/journals.json',
'journal_title': {'title': 'The Journal of High Energy Physics (JHEP)'},
'short_title': 'JHEP',
'title_variants': [],
'self': {
'$ref': 'https://localhost:5000/api/journals/bar',
},
}
assert validate(record['journal_title'], journal_title_schema) is None
assert validate(record['short_title'], short_title_schema) is None
assert validate(record['title_variants'], title_variants_schema) is None

populate_title_suggest(None, record)

expected = {
'input': [
'The Journal of High Energy Physics (JHEP)',
'JHEP',
],
'output': 'JHEP',
'payload': {
'full_title': 'The Journal of High Energy Physics (JHEP)',
'$ref': 'https://localhost:5000/api/journals/bar',
}
}

result = record['title_suggest']

assert expected == result


def test_populate_title_suggest_without_full_title():
schema = load_schema('journals')
journal_title_schema = schema['properties']['journal_title']
short_title_schema = schema['properties']['short_title']
title_variants_schema = schema['properties']['title_variants']

record = {
'$schema': 'http://localhost:5000/schemas/records/journals.json',
'journal_title': {},
'short_title': 'JHEP',
'title_variants': ['JOURNAL OF HIGH ENERGY PHYSICS'],
'self': {
'$ref': 'https://localhost:5000/api/journals/bar',
},
}
assert validate(record['journal_title'], journal_title_schema) is None
assert validate(record['short_title'], short_title_schema) is None
assert validate(record['title_variants'], title_variants_schema) is None

populate_title_suggest(None, record)

expected = {
'input': [
'JHEP',
'JOURNAL OF HIGH ENERGY PHYSICS'
],
'output': 'JHEP',
'payload': {
'full_title': '',
'$ref': 'https://localhost:5000/api/journals/bar',
}
}

result = record['title_suggest']

assert expected == result


def test_populate_title_suggest_does_nothing_if_record_is_not_journal():
record = {'$schema': 'http://localhost:5000/schemas/records/other.json'}

Expand Down

0 comments on commit fb97a92

Please sign in to comment.