From 283a3696493e42c2c45131c7386ba55de6e94f41 Mon Sep 17 00:00:00 2001 From: Iuliana Voinea Date: Thu, 30 Nov 2017 13:50:59 +0100 Subject: [PATCH] Add `$ref` to payloads of journals suggester Signed-off-by: Iuliana Voinea --- inspirehep/modules/records/receivers.py | 13 +- tests/unit/records/test_records_receivers.py | 120 ++++++++++++++++++- 2 files changed, 127 insertions(+), 6 deletions(-) diff --git a/inspirehep/modules/records/receivers.py b/inspirehep/modules/records/receivers.py index f8d7d2049f..49b0ce8eb0 100644 --- a/inspirehep/modules/records/receivers.py +++ b/inspirehep/modules/records/receivers.py @@ -174,14 +174,14 @@ def populate_bookautocomplete(sender, json, *args, **kwargs): input_values.extend(titles) input_values = [el for el in input_values if el] - ref = get_value(json, 'self.$ref') + record = get_value(json, 'self.$ref') json.update({ 'bookautocomplete': { 'input': input_values, 'payload': { 'authors': authors, - 'id': ref, + 'id': record if record else '', 'title': titles, }, }, @@ -241,7 +241,7 @@ def populate_conference_suggest(sender, json, *args, **kwargs): 'input': cnum, 'output': cnum, 'payload': { - '$ref': record, + '$ref': record if record else '', } }, }) @@ -268,7 +268,7 @@ def populate_experiment_suggest(sender, json, *args, **kwargs): 'input': input_values, 'output': legacy_name, 'payload': { - '$ref': record, + '$ref': record if record else '', } }, }) @@ -396,14 +396,17 @@ def populate_title_suggest(sender, json, *args, **kwargs): input_values.extend(title_variants) input_values = [el for el in input_values if el] + record = get_value(json, 'self.$ref') + json.update({ 'title_suggest': { 'input': input_values, 'output': short_title if short_title else '', 'payload': { 'full_title': journal_title if journal_title else '', + '$ref': record if record else '', }, - } + }, }) diff --git a/tests/unit/records/test_records_receivers.py b/tests/unit/records/test_records_receivers.py index a5ea87c1db..f796ae852a 100644 --- a/tests/unit/records/test_records_receivers.py +++ b/tests/unit/records/test_records_receivers.py @@ -1049,6 +1049,48 @@ def test_populate_title_suggest_with_all_inputs(): 'journal_title': {'title': 'The Journal of High Energy Physics (JHEP)'}, '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': [ + 'The Journal of High Energy Physics (JHEP)', + 'JHEP', + 'JOURNAL OF HIGH ENERGY PHYSICS', + ], + '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_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 @@ -1059,12 +1101,88 @@ def test_populate_title_suggest_with_all_inputs(): 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': 'The Journal of High Energy Physics (JHEP)' + 'full_title': '', + '$ref': 'https://localhost:5000/api/journals/bar', } }