def test_match_reference_doesnt_touch_curated(isolated_app):
    cited_record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        '_collections': ['Literature'],
        'control_number': 1,
        'document_type': ['article'],
        'dois': [{
            'value': '10.1371/journal.pone.0188398',
        }],
    }

    TestRecordMetadata.create_from_kwargs(
        json=cited_record_json, index_name='records-hep')

    reference = {
        'curated_relation': True,
        'record': {
            '$ref': 'http://localhost:5000/api/literature/42',
        },
        'reference': {
            'dois': ['10.1371/journal.pone.0188398'],
        }
    }

    schema = load_schema('hep')
    subschema = schema['properties']['references']

    assert validate([reference], subschema) is None
    reference = match_reference(reference)

    assert reference['record']['$ref'] == 'http://localhost:5000/api/literature/42'
def test_match_reference_for_data_config(isolated_app):
    """Test reference matcher for the JCAP and JHEP configuration"""

    cited_record_json = {
        '$schema': 'http://localhost:5000/schemas/records/data.json',
        '_collections': ['Data'],
        'control_number': 1,
        'dois': [
            {
                'value': '10.5281/zenodo.11020'
            }
        ],
    }

    TestRecordMetadata.create_from_kwargs(
        json=cited_record_json, index_name='records-data', pid_type='dat')

    reference = {
        'reference': {
            'dois': [
                '10.5281/zenodo.11020'
            ],
            'publication_info': {
                'year': 2007
            }
        }
    }

    reference = match_reference(reference)

    assert reference['record']['$ref'] == 'http://localhost:5000/api/data/1'
def test_match_reference_ignores_hidden_collections(isolated_app):
    cited_record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        '_collections': ['HAL Hidden'],
        'control_number': 1,
        'document_type': ['article'],
        'dois': [{
            'value': '10.1371/journal.pone.0188398',
        }],
    }

    TestRecordMetadata.create_from_kwargs(
        json=cited_record_json, index_name='records-hep')

    reference = {
        'reference': {
            'dois': ['10.1371/journal.pone.0188398'],
        }
    }

    schema = load_schema('hep')
    subschema = schema['properties']['references']

    assert validate([reference], subschema) is None
    reference = match_reference(reference)

    assert 'record' not in reference
def test_match_reference_for_data_config(isolated_app):
    """Test reference matcher for the JCAP and JHEP configuration"""

    cited_record_json = {
        '$schema': 'http://localhost:5000/schemas/records/data.json',
        '_collections': ['Data'],
        'control_number': 1,
        'dois': [{
            'value': '10.5281/zenodo.11020'
        }],
    }

    TestRecordMetadata.create_from_kwargs(json=cited_record_json,
                                          index_name='records-data',
                                          pid_type='dat')

    reference = {
        'reference': {
            'dois': ['10.5281/zenodo.11020'],
            'publication_info': {
                'year': 2007
            }
        }
    }

    reference = match_reference(reference)

    assert reference['record']['$ref'] == 'http://localhost:5000/api/data/1'
def test_match_reference_on_texkey(isolated_app):
    cited_record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        '_collections': ['Literature'],
        'control_number': 1,
        'document_type': ['article'],
        'texkeys': [
            'Giudice:2007fh',
        ],
        'titles': [
            {
                'title': 'The Strongly-Interacting Light Higgs'
            }
        ],
    }

    TestRecordMetadata.create_from_kwargs(
        json=cited_record_json, index_name='records-hep')

    reference = {
        'reference': {
            'texkey': 'Giudice:2007fh',
        }
    }

    schema = load_schema('hep')
    subschema = schema['properties']['references']

    assert validate([reference], subschema) is None
    reference = match_reference(reference)

    assert reference['record']['$ref'] == 'http://localhost:5000/api/literature/1'
    assert validate([reference], subschema) is None
def test_match_reference_doesnt_touch_curated(isolated_app):
    cited_record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        '_collections': ['Literature'],
        'control_number': 1,
        'document_type': ['article'],
        'dois': [{
            'value': '10.1371/journal.pone.0188398',
        }],
    }

    TestRecordMetadata.create_from_kwargs(json=cited_record_json,
                                          index_name='records-hep')

    reference = {
        'curated_relation': True,
        'record': {
            '$ref': 'http://localhost:5000/api/literature/42',
        },
        'reference': {
            'dois': ['10.1371/journal.pone.0188398'],
        }
    }

    schema = load_schema('hep')
    subschema = schema['properties']['references']

    assert validate([reference], subschema) is None
    reference = match_reference(reference)

    assert reference['record'][
        '$ref'] == 'http://localhost:5000/api/literature/42'
def test_match_reference_ignores_hidden_collections(isolated_app):
    cited_record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        '_collections': ['HAL Hidden'],
        'control_number': 1,
        'document_type': ['article'],
        'dois': [{
            'value': '10.1371/journal.pone.0188398',
        }],
    }

    TestRecordMetadata.create_from_kwargs(json=cited_record_json,
                                          index_name='records-hep')

    reference = {
        'reference': {
            'dois': ['10.1371/journal.pone.0188398'],
        }
    }

    schema = load_schema('hep')
    subschema = schema['properties']['references']

    assert validate([reference], subschema) is None
    reference = match_reference(reference)

    assert 'record' not in reference
def test_match_reference_on_texkey(isolated_app):
    cited_record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        '_collections': ['Literature'],
        'control_number': 1,
        'document_type': ['article'],
        'texkeys': [
            'Giudice:2007fh',
        ],
        'titles': [{
            'title': 'The Strongly-Interacting Light Higgs'
        }],
    }

    TestRecordMetadata.create_from_kwargs(json=cited_record_json,
                                          index_name='records-hep')

    reference = {
        'reference': {
            'texkey': 'Giudice:2007fh',
        }
    }

    schema = load_schema('hep')
    subschema = schema['properties']['references']

    assert validate([reference], subschema) is None
    reference = match_reference(reference)

    assert reference['record'][
        '$ref'] == 'http://localhost:5000/api/literature/1'
    assert validate([reference], subschema) is None
def test_match_reference_for_jcap_and_jhep_config(isolated_app):
    """Test reference matcher for the JCAP and JHEP configuration"""

    cited_record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        '_collections': ['Literature'],
        'control_number': 1,
        'document_type': ['article'],
        'publication_info': [
            {
                'artid': '045',
                'journal_title': 'JHEP',
                'journal_volume': '06',
                'page_start': '045',
                'year': 2007
            }
        ],
        'titles': [
            {
                'title': 'The Strongly-Interacting Light Higgs'
            }
        ],
    }

    TestRecordMetadata.create_from_kwargs(
        json=cited_record_json, index_name='records-hep')

    reference = {
        'reference': {
            'publication_info': {
                'artid': '045',
                'journal_title': 'JHEP',
                'journal_volume': '06',
                'page_start': '045',
                'year': 2007
            }
        }
    }

    schema = load_schema('hep')
    subschema = schema['properties']['references']

    assert validate([reference], subschema) is None
    reference = match_reference(reference)

    assert reference['record']['$ref'] == 'http://localhost:5000/api/literature/1'
    assert validate([reference], subschema) is None
def test_match_reference_for_jcap_and_jhep_config(isolated_app):
    """Test reference matcher for the JCAP and JHEP configuration"""

    cited_record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        '_collections': ['Literature'],
        'control_number': 1,
        'document_type': ['article'],
        'publication_info': [
            {
                'artid': '045',
                'journal_title': 'JHEP',
                'journal_volume': '06',
                'page_start': '045',
                'year': 2007
            }
        ],
        'titles': [
            {
                'title': 'The Strongly-Interacting Light Higgs'
            }
        ],
    }

    TestRecordMetadata.create_from_kwargs(
        json=cited_record_json, index_name='records-hep')

    reference = {
        'reference': {
            'publication_info': {
                'artid': '045',
                'journal_title': 'JHEP',
                'journal_volume': '06',
                'page_start': '045',
                'year': 2007
            }
        }
    }

    schema = load_schema('hep')
    subschema = schema['properties']['references']

    assert validate([reference], subschema) is None
    reference = match_reference(reference)

    assert reference['record']['$ref'] == 'http://localhost:5000/api/literature/1'
    assert validate([reference], subschema) is None
def test_match_reference_on_texkey_has_lower_priority_than_pub_info(isolated_app):
    cited_record_with_texkey_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        '_collections': ['Literature'],
        'control_number': 1,
        'document_type': ['article'],
        'texkeys': [
            'MyTexKey:2008fh',
        ],
        'titles': [
            {
                'title': 'The Strongly-Interacting Light Higgs'
            }
        ],
    }

    TestRecordMetadata.create_from_kwargs(
        json=cited_record_with_texkey_json, index_name='records-hep')

    cited_record_with_pub_info_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        '_collections': ['Literature'],
        'control_number': 2,
        'document_type': ['article'],
        'publication_info': [
            {
                'artid': '100',
                'journal_title': 'JHEP',
                'journal_volume': '100',
                'page_start': '100',
                'year': 2020
            }
        ],
        'titles': [
            {
                'title': 'The Strongly-Interacting Light Higgs'
            }
        ],
    }

    TestRecordMetadata.create_from_kwargs(
        json=cited_record_with_pub_info_json, index_name='records-hep')

    reference = {
        'reference': {
            'texkey': 'MyTexKey:2008fh',
            'publication_info': {
                'artid': '100',
                'journal_title': 'JHEP',
                'journal_volume': '100',
                'page_start': '100',
                'year': 2020
            }
        }
    }

    schema = load_schema('hep')
    subschema = schema['properties']['references']

    assert validate([reference], subschema) is None
    reference = match_reference(reference)

    assert reference['record']['$ref'] == 'http://localhost:5000/api/literature/2'
    assert validate([reference], subschema) is None