示例#1
0
def test_add_document_inspire_next():
    schema = load_schema('hep')
    subschema = schema['properties']['documents']

    builder = LiteratureBuilder('test')

    builder.add_document(
        'key',
        description='description',
        fulltext=True,
        hidden=True,
        material='preprint',
        original_url='http://www.example.com/original_url',
        source='source',
        url='url',
    )

    expected = [
        {
            'description': 'description',
            'fulltext': True,
            'hidden': True,
            'key': 'key',
            'material': 'preprint',
            'original_url': 'http://www.example.com/original_url',
            'source': 'source',
            'url': 'url',
        },
    ]
    result = builder.record

    assert validate(result['documents'], subschema) is None
    assert expected == result['documents']
示例#2
0
def test_add_figure_inspire_next():
    schema = load_schema('hep')
    subschema = schema['properties']['figures']

    builder = LiteratureBuilder('test')

    builder.add_figure(
        'key',
        caption='caption',
        label='label',
        material='publication',
        source='source',
        url='url',
        description='description',
        original_url='http://www.example.com/original_url'
    )

    expected = [
        {
            'caption': 'caption',
            'key': 'key',
            'label': 'label',
            'material': 'publication',
            'source': 'source',
            'url': 'url',
            'original_url': 'http://www.example.com/original_url'
        },
    ]
    result = builder.record

    assert validate(result['figures'], subschema) is None
    assert expected == result['figures']
def test_add_document():
    schema = load_schema('hep')
    subschema = schema['properties']['documents']

    builder = LiteratureBuilder('test')

    builder.add_document(
        'key',
        description='description',
        fulltext=True,
        hidden=True,
        material='material',
        original_url='original_url',
        source='source',
        url='url',
    )

    expected = [
        {
            'description': 'description',
            'fulltext': True,
            'hidden': True,
            'key': 'key',
            'material': 'material',
            'original_url': 'original_url',
            'source': 'source',
            'url': 'url',
        },
    ]
    result = builder.record

    assert expected == result['documents']

    for key in subschema['items']['properties'].keys():
        assert key in result['documents'][0]
def test_add_figure():
    schema = load_schema('hep')
    subschema = schema['properties']['figures']

    builder = LiteratureBuilder('test')

    builder.add_figure(
        'key',
        caption='caption',
        label='label',
        material='publication',
        source='source',
        url='url',
    )

    expected = [
        {
            'caption': 'caption',
            'key': 'key',
            'label': 'label',
            'material': 'publication',
            'source': 'source',
            'url': 'url',
        },
    ]
    result = builder.record

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

    for key in subschema['items']['properties'].keys():
        assert key in result['figures'][0]
def test_sourced_dict_global_source():
    builder = LiteratureBuilder('global')

    expected = {'source': 'global', 'value': 'foo'}

    result = builder._sourced_dict(None, value='foo')

    assert result == expected
def test_sourced_dict_no_source():
    builder = LiteratureBuilder()

    expected = {'value': 'foo'}

    result = builder._sourced_dict(None, value='foo')

    assert result == expected
def test_imprint_date_normalizes_date():
    builder = LiteratureBuilder()
    builder.add_imprint_date('19 September 2005')

    result = builder.record['imprints']

    expected = [{'date': '2005-09-19'}]

    assert expected == result
def test_add_book_normalizes_date():
    builder = LiteratureBuilder()
    builder.add_book(date='9 November 1990')

    result = builder.record['imprints']

    expected = [{'date': '1990-11-09'}]

    assert expected == result
def test_add_isbn_normalizes_isbn():
    builder = LiteratureBuilder()
    builder.add_isbn(isbn='978-3-642-23908-3')

    result = builder.record['isbns']

    expected = [{'value': '9783642239083'}]

    assert expected == result
def test_add_parent_isbn_normalizes_isbn():
    builder = LiteratureBuilder()
    builder.add_publication_info(parent_isbn='978-3-642-23908-3')

    result = builder.record['publication_info']

    expected = [{'parent_isbn': '9783642239083'}]

    assert expected == result
def test_preprint_date_normalizes_date():
    builder = LiteratureBuilder()
    builder.add_preprint_date('12 April 2010')

    result = builder.record['preprint_date']

    expected = '2010-04-12'

    assert expected == result
示例#12
0
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_add_license_doesnt_overwrite_name_if_no_url():
    schema = load_schema('hep')
    subschema = schema['properties']['license']
    builder = LiteratureBuilder()
    builder.add_license(license='foo')

    result = builder.record['license']
    expected = [{
        'license': 'foo',
    }]

    assert validate(result, subschema) is None
    assert expected == result
def test_add_doi_normalizes_doi():
    schema = load_schema('hep')
    subschema = schema['properties']['dois']
    builder = LiteratureBuilder()
    builder.add_doi('doi.org/10.1234/foo')

    result = builder.record['dois']
    expected = [{
        'value': '10.1234/foo',
    }]

    assert validate(result, subschema) is None
    assert expected == result
def test_publication_info_public_note():
    schema = load_schema('hep')
    subschema = schema['properties']['public_notes']
    builder = LiteratureBuilder(source="APS")
    builder.add_publication_info(journal_title="Phys. Rev. B")

    expected = [{
        'source': 'APS',
        'value': 'Submitted to Phys. Rev. B',
    }]
    result = builder.record['public_notes']

    assert validate(result, subschema) is None
    assert expected == result
    assert 'publication_info' not in builder.record
def test_add_reference():
    builder = LiteratureBuilder()
    reference = {
        "reference": {
            "authors": [{
                "full_name": "Smith, J."
            }],
            "label": "1",
            "publication_info": {
                "year": 1996
            }
        }
    }
    builder.add_reference(reference)
    assert builder.record['references'] == [reference]
def test_make_author_handles_none_in_id_schema():
    schema = load_schema('hep')
    subschema = schema['properties']['authors']
    builder = LiteratureBuilder()

    result = builder.make_author(
        'Smith, John',
        ids=[(None, 'J.Smith.1')],
    )
    expected = {
        'full_name': 'Smith, John',
    }

    assert validate([result], subschema) is None
    assert expected == result
def test_add_keyword():
    schema = load_schema('hep')
    subschema = schema['properties']['keywords']
    builder = LiteratureBuilder(source='Publisher')
    builder.add_keyword('29.27.Fh', schema='PACS')

    result = builder.record['keywords']
    expected = [{
        'value': '29.27.Fh',
        'schema': 'PACS',
        'source': 'Publisher',
    }]

    assert validate(result, subschema) is None
    assert expected == result
示例#19
0
def test_add_external_system_identifier_kwargs():
    schema = load_schema('hep')
    subschema = schema['properties']['external_system_identifiers']
    builder = LiteratureBuilder()
    builder.add_external_system_identifier(schema='osti', extid='12345')

    result = builder.record['external_system_identifiers']
    expected = [
        {
            'value': '12345',
            'schema': 'osti',
        }
    ]

    assert validate(result, subschema) is None
    assert expected == result
示例#20
0
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
示例#21
0
def test_add_figure_fails_on_non_file_api_relative_url():
    schema = load_schema('hep')
    subschema = schema['properties']['figures']

    builder = LiteratureBuilder('test')

    with pytest.raises(ValidationError):
        builder.add_figure(
            'key',
            caption='caption',
            label='label',
            material='publication',
            source='source',
            url='/not/api/url/for/files',
            description='description',
            original_url='http://www.example.com/original_url'
        )
        result = builder.record
        validate(result['figures'], subschema)
示例#22
0
def test_make_author():
    schema = load_schema('hep')
    subschema = schema['properties']['authors']
    builder = LiteratureBuilder()

    result = builder.make_author(
        'Smith, John',
        affiliations=['CERN', 'SLAC'],
        source='submitter',
        raw_affiliations=['CERN, 1211 Geneva', 'SLAC, Stanford'],
        emails=['*****@*****.**'],
        ids=[('INSPIRE BAI', 'J.Smith.1')],
        alternative_names=['Johnny Smith']
    )
    expected = {
        'full_name': 'Smith, John',
        'affiliations': [
            {'value': 'CERN'},
            {'value': 'SLAC'},
        ],
        'raw_affiliations': [
            {
                'value': 'CERN, 1211 Geneva',
                'source': 'submitter'
            },
            {
                'value': 'SLAC, Stanford',
                'source': 'submitter',
            }
        ],
        'emails': ['*****@*****.**'],
        'ids': [
            {
                'schema': 'INSPIRE BAI',
                'value': 'J.Smith.1',
            }
        ],
        'alternative_names': ['Johnny Smith'],
    }

    assert validate([result], subschema) is None
    assert expected == result
示例#23
0
def test_add_document_fails_on_non_file_api_relative_url():
    schema = load_schema('hep')
    subschema = schema['properties']['documents']

    builder = LiteratureBuilder('test')

    with pytest.raises(ValidationError):
        builder.add_document(
            'key',
            description='description',
            fulltext=True,
            hidden=True,
            material='preprint',
            original_url='http://www.example.com/original_url',
            source='source',
            url='/not/api/url/for/files',
            filename='filename'
        )
        result = builder.record
        validate(result['documents'], subschema)
示例#24
0
def test_add_figure_fails_on_duplicated_key():
    builder = LiteratureBuilder('test')

    builder.add_figure(
        'key',
        caption='caption',
        label='label',
        material='publication',
        source='source',
        url='url',
        description='description',
        filename='filename',
        original_ur='original_url'
    )

    with pytest.raises(ValueError):
        builder.add_figure(
            'key',
            caption='caption',
            label='label',
            material='publication',
            source='source',
            url='url',
            description='description',
            filename='filename',
            original_ur='original_url'
        )
示例#25
0
def test_add_document_fails_on_existing_key():
    builder = LiteratureBuilder('test')
    builder.add_document(
        'key',
        description='description',
        fulltext=True,
        hidden=True,
        material='preprint',
        original_url='http://www.example.com/original_url',
        source='source',
        url='url',
        filename='filename'
    )

    with pytest.raises(ValueError):
        builder.add_document(
            'key',
            description='description',
            fulltext=True,
            hidden=True,
            material='preprint',
            original_url='http://www.example.com/original_url',
            source='source',
            url='url',
            filename='filename'
        )
def test_append_to():
    formdata = ''
    builder = LiteratureBuilder("test")
    expected_result = None
    builder._append_to('test_field', formdata)
    assert builder.record.get('test_field') is expected_result
    formdata = 'value'
    expected_result = ['value']
    builder._append_to('test_field_2', formdata)
    assert builder.record.get('test_field_2') == expected_result
示例#27
0
def test_add_many_external_system_identifier():
    schema = load_schema('hep')
    subschema = schema['properties']['external_system_identifiers']
    builder = LiteratureBuilder()
    builder.add_external_system_identifier('5758037', 'osti')
    builder.add_external_system_identifier('1992PhRvD..45..124K', 'ADS')

    result = builder.record['external_system_identifiers']
    expected = [
        {
            'value': '5758037',
            'schema': 'osti',
        },
        {
            'value': '1992PhRvD..45..124K',
            'schema': 'ADS',
        },
    ]

    assert validate(result, subschema) is None
    assert expected == result
def test_add_figure_fails_on_duplicated_key():
    builder = LiteratureBuilder('test')

    builder.add_figure(
        'key',
        caption='caption',
        label='label',
        material='publication',
        source='source',
        url='url',
    )

    with pytest.raises(ValueError):
        builder.add_figure(
            'key',
            caption='caption',
            label='label',
            material='publication',
            source='source',
            url='url',
        )
def test_repr_handles_source_none():
    builder = LiteratureBuilder()
    assert repr(builder).startswith('LiteratureBuilder(source=None, record={')
def test_repr_handles_source_present():
    builder = LiteratureBuilder('publisher')
    assert repr(builder).startswith(
        "LiteratureBuilder(source='publisher', record={")