From fb97a9247ec3aab468b69be52b47e3cc97f80116 Mon Sep 17 00:00:00 2001 From: Iuliana Voinea Date: Fri, 8 Dec 2017 14:37:15 +0100 Subject: [PATCH] Apply review suggestions and refactor Signed-off-by: Iuliana Voinea --- inspirehep/modules/records/receivers.py | 52 ++++--- tests/unit/records/test_records_receivers.py | 150 ++++--------------- 2 files changed, 65 insertions(+), 137 deletions(-) diff --git a/inspirehep/modules/records/receivers.py b/inspirehep/modules/records/receivers.py index 21b30334d0..e6e4b482dc 100644 --- a/inspirehep/modules/records/receivers.py +++ b/inspirehep/modules/records/receivers.py @@ -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', []) @@ -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, @@ -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': { @@ -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', '') diff --git a/tests/unit/records/test_records_receivers.py b/tests/unit/records/test_records_receivers.py index 8f398d6082..7ce0f8e851 100644 --- a/tests/unit/records/test_records_receivers.py +++ b/tests/unit/records/test_records_receivers.py @@ -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' }, @@ -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'] @@ -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'] @@ -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'}