Skip to content

Commit

Permalink
Merge pull request #359 from harunurhan/add-record-methods
Browse files Browse the repository at this point in the history
literature: add record params
  • Loading branch information
harunurhan committed May 10, 2019
2 parents 34bc124 + 29400e0 commit b36f6d4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
42 changes: 36 additions & 6 deletions inspire_schemas/builders/literature.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"""Literature builder class and related code."""

from __future__ import absolute_import, division, print_function
from six import string_types

import warnings

Expand Down Expand Up @@ -228,7 +229,8 @@ def make_author(self, full_name,
source=None,
ids=(),
emails=(),
alternative_names=()):
alternative_names=(),
record=None):
"""Make a subrecord representing an author.
Args:
Expand All @@ -244,12 +246,13 @@ def make_author(self, full_name,
elements are of the form ``(schema, value)``.
emails(List[str]): email addresses of the author.
alternative_names(List[str]): alternative names of the author.
record(dict): reference to the author record
Returns:
dict: a schema-compliant subrecord.
"""
builder = SignatureBuilder()
builder.set_full_name(full_name)
builder.set_record(record)

for affiliation in affiliations:
builder.add_affiliation(affiliation)
Expand Down Expand Up @@ -403,6 +406,8 @@ def add_publication_info(
material=None,
parent_record=None,
parent_isbn=None,
journal_record=None,
conference_record=None,
):
"""Add publication info.
Expand Down Expand Up @@ -441,10 +446,16 @@ def add_publication_info(
:type material: string
:param parent_record: reference for the parent record
:type parent_record: string
:type parent_record: dict/string
:param parent_isbn: isbn for the parent record
:type parent_isbn: string
:param journal_record: reference for the journal record
:type journal_record: dict
:param conference_record: reference for the conference record
:type conference_record: dict
"""

# If only journal title is present, and no other fields, assume the
Expand All @@ -458,12 +469,15 @@ def add_publication_info(
publication_item = {}
for key in ('cnum', 'artid', 'page_end', 'page_start',
'journal_issue', 'journal_title',
'journal_volume', 'year', 'pubinfo_freetext', 'material'):
'journal_volume', 'year', 'pubinfo_freetext', 'material',
'journal_record', 'conference_record'):
if locals()[key] is not None:
publication_item[key] = locals()[key]
if parent_record is not None:
parent_item = {'$ref': parent_record}
publication_item['parent_record'] = parent_item
# TODO: remove `if string` check [BREAKING] while bumping major version
if isinstance(parent_record, string_types):
parent_record = {'$ref': parent_record}
publication_item['parent_record'] = parent_record
if parent_isbn is not None:
publication_item['parent_isbn'] = normalize_isbn(parent_isbn)
if page_start and page_end:
Expand Down Expand Up @@ -546,6 +560,22 @@ def add_accelerator_experiments_legacy_name(self, legacy_name):
'legacy_name': legacy_name
})

@filter_empty_parameters
def add_accelerator_experiment(self, legacy_name, record=None):
"""Add legacy name in accelerator experiment.
:type legacy_name: string
:param record: reference to the experiment record
:type record: dict
"""
experiment = {'legacy_name': legacy_name}

if record is not None:
experiment['record'] = record

self._append_to('accelerator_experiments', experiment)

@filter_empty_parameters
def add_language(self, language):
"""Add language.
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_literature_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,18 @@ def test_add_reference():
assert builder.record['references'] == [reference]


def test_add_accelerator_experiment():
builder = LiteratureBuilder()

legacy_name = 'FNAL-E-0900'
experiment_record = {'$ref': 'http://url/api/experiments/123'}
builder.add_accelerator_experiment('FNAL-E-0900', record=experiment_record)
assert builder.record['accelerator_experiments'] == [{
'legacy_name': legacy_name,
'record': experiment_record
}]


def test_publication_info_public_note():
schema = load_schema('hep')
subschema = schema['properties']['public_notes']
Expand Down Expand Up @@ -585,6 +597,24 @@ def test_make_author_handles_none_in_id_value():
assert expected == result


def test_make_author_sets_record():
schema = load_schema('hep')
subschema = schema['properties']['authors']
builder = LiteratureBuilder()
author_record = {'$ref': 'http://url/api/authors/1234'}
result = builder.make_author(
'Smith, John',
record=author_record,
)
expected = {
'full_name': 'Smith, John',
'record': author_record,
}

assert validate([result], subschema) is None
assert expected == result


def test_make_author_handles_none_in_id_schema():
schema = load_schema('hep')
subschema = schema['properties']['authors']
Expand Down

0 comments on commit b36f6d4

Please sign in to comment.