Пример #1
0
def record_with_two_revisions(app):
    record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'control_number': 111,
        'document_type': [
            'article',
        ],
        'titles': [
            {'title': 'record rev0'},
        ],
        'self': {
            '$ref': 'http://localhost:5000/schemas/records/hep.json',
        },
        '_collections': ['Literature']
    }

    with db.session.begin_nested():
        record = InspireRecord.create_or_update(record)
        record.commit()
    db.session.commit()

    record['titles'][0]['title'] = 'record rev1'

    with db.session.begin_nested():
        record = InspireRecord.create_or_update(record)
        record.commit()
    db.session.commit()

    yield

    _delete_record('lit', 111)
def normal_record(app):
    record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'arxiv_eprints': [{
            'categories': ['hep-th'],
            'value': '1607.12345'
        }],
        'control_number': 111,
        'document_type': [
            'article',
        ],
        'dois': [{
            'value': '10.1234/PhysRevD.94.054021'
        }],
        'titles': [
            {
                'title': 'deleted'
            },
        ],
        'self': {
            '$ref': 'http://localhost:5000/schemas/records/hep.json',
        },
        '_collections': ['Literature']
    }

    _create_record(record)

    yield record

    _delete_record('lit', 111)
Пример #3
0
def test_literature_citations_api_without_results(api_client):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature']
    }
    record = InspireRecord.create(record_json)
    record.commit()

    current_search.flush_and_refresh('records-hep')

    response = api_client.get('/literature/111/citations',
                              headers={'Accept': 'application/json'})
    result = json.loads(response.get_data(as_text=True))

    expected_metadata = {
        "citation_count": 0,
        "citations": [],
    }

    assert response.status_code == 200
    assert expected_metadata == result['metadata']

    _delete_record('lit', 111)
Пример #4
0
def record_1502656():
    record = push_to_redis('1502656.xml')

    yield record

    flush_redis()
    _delete_record('lit', 1502656)
Пример #5
0
def record_1502656():
    record = push_to_redis('1502656.xml')

    yield record

    flush_redis()
    _delete_record('lit', 1502656)
Пример #6
0
def test_literature_citations_api_without_results(api_client):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature']
    }
    record = InspireRecord.create(record_json)
    record.commit()

    es.indices.refresh('records-hep')

    response = api_client.get(
        '/literature/111/citations',
        headers={'Accept': 'application/json'}
    )
    result = json.loads(response.get_data(as_text=True))

    expected_metadata = {
        "citation_count": 0,
        "citations": [],
    }

    assert response.status_code == 200
    assert expected_metadata == result['metadata']

    _delete_record('lit', 111)
def normal_record(app):
    record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'arxiv_eprints': [
            {
                'categories': [
                    'hep-th'
                ],
                'value': '1607.12345'
            }
        ],
        'control_number': 111,
        'document_type': [
            'article',
        ],
        'dois': [
            {
                'value': '10.1234/PhysRevD.94.054021'
            }
        ],
        'titles': [
            {'title': 'deleted'},
        ],
        'self': {
            '$ref': 'http://localhost:5000/schemas/records/hep.json',
        },
        '_collections': ['Literature']
    }

    _create_record(record)

    yield record

    _delete_record('lit', 111)
 def test_new_record(self):
     recid = 9999912587
     record_json = {
         '$schema': 'http://localhost:5000/schemas/records/hep.json',
         'document_type': [
             'article',
         ],
         'control_number': recid,
         'titles': [
             {
                 'title': 'Jessica Jones',
             },
         ],
         '_collections': ['Literature'],
         'references': [{'record': {
             '$ref': 'http://localhost:5000/api/literature/1498589'}}]
     }
     inspire_record = InspireRecord.create(record_json)
     with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True,
                          FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*',
                          ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \
             mock.patch('inspirehep.modules.records.receivers.push_access_tokens') as mock_push_access_tokens, \
             mock.patch('inspirehep.modules.orcid.tasks.orcid_push.apply_async') as mock_apply_async:
         mock_push_access_tokens.get_access_tokens.return_value = [('myorcid', 'mytoken')]
         inspire_record.commit()
         mock_apply_async.assert_called_once_with(
             kwargs={'orcid': 'myorcid',
                     'oauth_token': 'mytoken',
                     'kwargs_to_pusher': {'record_db_version': inspire_record.model.version_id},
                     'rec_id': recid},
             queue='orcid_push')
     _delete_record('lit', recid)
Пример #9
0
def not_yet_deleted_record(app):
    record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'control_number': 111,
        'document_type': [
            'article',
        ],
        'titles': [
            {
                'title': 'deleted'
            },
        ],
        'self': {
            '$ref': 'http://localhost:5000/schemas/records/hep.json',
        },
        '_collections': ['Literature']
    }

    with db.session.begin_nested():
        _create_record(record)
    db.session.commit()

    yield

    _delete_record('lit', 111)
def test_record_enhanced_in_es_and_not_enhanced_in_db(app):
    record_json = {
        '$schema':
        'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number':
        111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature'],
        'references': [{
            'record': {
                '$ref': 'http://localhost:5000/api/literature/1498589'
            }
        }]
    }
    record = InspireRecord.create(record_json)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')
    rec1 = get_db_record('lit', 111)
    rec2 = get_es_record('lit', 111)
    assert 'facet_author_name' not in rec1
    assert 'facet_author_name' in rec2
    _delete_record('lit', 111)
Пример #11
0
def record_with_two_revisions(app):
    record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'control_number': 111,
        'document_type': [
            'article',
        ],
        'titles': [
            {'title': 'record rev0'},
        ],
        'self': {
            '$ref': 'http://localhost:5000/schemas/records/hep.json',
        },
        '_collections': ['Literature']
    }

    with db.session.begin_nested():
        record_insert_or_replace(record)
    db.session.commit()

    record['titles'][0]['title'] = 'record rev1'

    with db.session.begin_nested():
        record_insert_or_replace(record)
    db.session.commit()

    yield

    _delete_record('lit', 111)
def raw_record(app):
    record_fixture_path = pkg_resources.resource_filename(
        __name__, os.path.join('fixtures', '1608652.xml'))

    with open(record_fixture_path) as _record_fixture_fd:
        yield _record_fixture_fd.read()

    _delete_record('lit', 1608652)
Пример #13
0
def record_1502656_and_update():
    record1 = push_to_redis('1502656.xml')
    record2 = push_to_redis('1502656_update.xml')

    yield record1, record2

    flush_redis()
    _delete_record('lit', 1502656)
Пример #14
0
def record_1502656_and_update():
    record1 = push_to_redis('1502656.xml')
    record2 = push_to_redis('1502656_update.xml')

    yield record1, record2

    flush_redis()
    _delete_record('lit', 1502656)
Пример #15
0
def test_selecting_2_facets_generates_search_with_must_query(api_client):
    record_json = {
        'control_number': 843386527,
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'Article 1'
        }],
        '_collections': ['Literature'],
        'authors': [{
            'full_name': 'John Doe'
        }]
    }

    rec = InspireRecord.create(data=record_json)
    rec.commit()

    record_json2 = {
        'control_number': 843386521,
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'Article 2'
        }],
        '_collections': ['Literature'],
        'authors': [{
            'full_name': 'John Doe'
        }, {
            'full_name': 'John Doe2'
        }]
    }

    rec2 = InspireRecord.create(data=record_json2)
    rec2.commit()

    db.session.commit()
    current_search.flush_and_refresh('records-hep')

    response = api_client.get('/literature?q=&author=BAI_John%20Doe')
    data = json.loads(response.data)
    response_recids = [
        record['metadata']['control_number'] for record in data['hits']['hits']
    ]
    assert rec['control_number'] in response_recids
    assert rec2['control_number'] in response_recids

    response = api_client.get(
        '/literature?q=&author=BAI_John%20Doe&author=BAI_John%20Doe2')
    data = json.loads(response.data)
    response_recids = [
        record['metadata']['control_number'] for record in data['hits']['hits']
    ]
    assert rec['control_number'] not in response_recids
    assert rec2['control_number'] in response_recids

    _delete_record('lit', 843386527)
    _delete_record('lit', 843386521)
    db.session.commit()
def test_index_after_commit_indexes_raises_if_cited_records_are_not_in_db(
    mocked_indexing_task,
    mocked_permission_check,
    app,
):
    # this test doesn't use the isolated_app because it needs to commit to
    # the DB in order to create records versions.

    citing_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{'title': 'Record citing the first one'}],
        '_collections': ['Literature'],
        'control_number': 8888,
        'references': [
            {"reference": {'authors': [{'full_name': 'Smith, J.'}]}}
        ]
    }

    record = InspireRecord.create(data=citing_json, skip_files=True)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = 'lit', record['control_number'], 2
    mocked_indexing_task.assert_called_with(*expected_args)

    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    references = {
        'references': [
            {
                "curated_relation": False,
                "record": {
                    "$ref": "http://localhost:5000/api/literature/9999"
                },
                "reference": {
                    'authors': [{'full_name': 'Smith, J.'}],
                }
            }
        ]
    }

    citing_json.update(references)
    record.clear()
    record.update(citing_json)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', record['control_number'], 3)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    with pytest.raises(MissingCitedRecordError):
        index_modified_citations_from_record(*expected_args)

    _delete_record('lit', 8888)
Пример #17
0
def record_1502655_and_1502656():
    record1 = push_to_redis('1502655.xml')
    record2 = push_to_redis('1502656.xml')

    yield record1, record2

    flush_redis()
    _delete_record('aut', 1502655)
    _delete_record('lit', 1502656)
Пример #18
0
def record_1502655_and_1502656():
    record1 = push_to_redis('1502655.xml')
    record2 = push_to_redis('1502656.xml')

    yield record1, record2

    flush_redis()
    _delete_record('aut', 1502655)
    _delete_record('lit', 1502656)
def raw_record(app):
    record_fixture_path = pkg_resources.resource_filename(
        __name__,
        os.path.join('fixtures', '1608652.xml')
    )

    with open(record_fixture_path) as _record_fixture_fd:
        yield _record_fixture_fd.read()

    _delete_record('lit', 1608652)
Пример #20
0
def test_selecting_2_facets_generates_search_with_must_query(api_client):
    record_json = {
        'control_number': 843386527,
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{'title': 'Article 1'}],
        '_collections': ['Literature'],
        'authors': [{'full_name': 'John Doe'}]
    }

    rec = InspireRecord.create(data=record_json)
    rec.commit()

    record_json2 = {
        'control_number': 843386521,
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{'title': 'Article 2'}],
        '_collections': ['Literature'],
        'authors': [{'full_name': 'John Doe'}, {'full_name': 'John Doe2'}]
    }

    rec2 = InspireRecord.create(data=record_json2)
    rec2.commit()

    db.session.commit()
    es.indices.refresh('records-hep')

    response = api_client.get('/literature?q=&author=BAI_John%20Doe')
    data = json.loads(response.data)
    response_recids = [record['metadata']['control_number'] for record in data['hits']['hits']]
    assert rec['control_number'] in response_recids
    assert rec2['control_number'] in response_recids

    response = api_client.get('/literature?q=&author=BAI_John%20Doe&author=BAI_John%20Doe2')
    data = json.loads(response.data)
    response_recids = [record['metadata']['control_number'] for record in data['hits']['hits']]
    assert rec['control_number'] not in response_recids
    assert rec2['control_number'] in response_recids

    _delete_record('lit', 843386527)
    _delete_record('lit', 843386521)
    db.session.commit()
Пример #21
0
 def test_new_record(self):
     recid = 9999912587
     record_json = {
         '$schema':
         'http://localhost:5000/schemas/records/hep.json',
         'document_type': [
             'article',
         ],
         'control_number':
         recid,
         'titles': [
             {
                 'title': 'Jessica Jones',
             },
         ],
         '_collections': ['Literature'],
         'references': [{
             'record': {
                 '$ref': 'http://localhost:5000/api/literature/1498589'
             }
         }]
     }
     inspire_record = InspireRecord.create(record_json)
     with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True,
                          FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*',
                          ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \
             mock.patch('inspirehep.modules.records.receivers.push_access_tokens') as mock_push_access_tokens, \
             mock.patch('inspirehep.modules.orcid.tasks.orcid_push.apply_async') as mock_apply_async:
         mock_push_access_tokens.get_access_tokens.return_value = [
             ('myorcid', 'mytoken')
         ]
         inspire_record.commit()
         mock_apply_async.assert_called_once_with(kwargs={
             'orcid': 'myorcid',
             'oauth_token': 'mytoken',
             'kwargs_to_pusher': {
                 'record_db_version': inspire_record.model.version_id
             },
             'rec_id': recid
         },
                                                  queue='orcid_push')
     _delete_record('lit', recid)
def insert_journals_in_db(workflow_app):
    """Temporarily add few journals in the DB"""
    from inspirehep.modules.migrator.tasks import record_insert_or_replace  # imported here because it is a Celery task

    journal_no_pro_and_ref = json.loads(pkg_resources.resource_string(
        __name__, os.path.join('fixtures', 'jou_record_refereed.json')))

    journal_pro_and_ref = json.loads(pkg_resources.resource_string(
        __name__, os.path.join('fixtures', 'jou_record_refereed_and_proceedings.json')))

    with db.session.begin_nested():
        record_insert_or_replace(journal_no_pro_and_ref)
        record_insert_or_replace(journal_pro_and_ref)
    db.session.commit()
    es.indices.refresh('records-journals')

    yield

    _delete_record('jou', 1936475)
    _delete_record('jou', 1936476)
    es.indices.refresh('records-journals')
Пример #23
0
def deleted_record(app):
    snippet = ('<record>'
               '  <controlfield tag="001">111</controlfield>'
               '  <datafield tag="245" ind1=" " ind2=" ">'
               '    <subfield code="a">deleted</subfield>'
               '  </datafield>'
               '  <datafield tag="980" ind1=" " ind2=" ">'
               '    <subfield code="a">HEP</subfield>'
               '    <subfield code="c">DELETED</subfield>'
               '  </datafield>'
               '</record>')

    record = marcxml2record(snippet)
    record['$schema'] = 'http://localhost:5000/schemas/records/hep.json'

    with db.session.begin_nested():
        _create_record(record)
    db.session.commit()

    yield

    _delete_record('lit', 111)
Пример #24
0
def not_yet_deleted_record(app):
    record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'control_number': 111,
        'document_type': [
            'article',
        ],
        'titles': [
            {'title': 'deleted'},
        ],
        'self': {
            '$ref': 'http://localhost:5000/schemas/records/hep.json',
        },
        '_collections': ['Literature']
    }

    with db.session.begin_nested():
        _create_record(record)
    db.session.commit()

    yield

    _delete_record('lit', 111)
Пример #25
0
def deleted_record(app):
    snippet = (
        '<record>'
        '  <controlfield tag="001">111</controlfield>'
        '  <datafield tag="245" ind1=" " ind2=" ">'
        '    <subfield code="a">deleted</subfield>'
        '  </datafield>'
        '  <datafield tag="980" ind1=" " ind2=" ">'
        '    <subfield code="a">HEP</subfield>'
        '    <subfield code="c">DELETED</subfield>'
        '  </datafield>'
        '</record>'
    )

    record = marcxml2record(snippet)
    record['$schema'] = 'http://localhost:5000/schemas/records/hep.json'

    with db.session.begin_nested():
        _create_record(record)
    db.session.commit()

    yield

    _delete_record('lit', 111)
Пример #26
0
def insert_journals_in_db(workflow_app):
    """Temporarily add few journals in the DB"""
    from inspirehep.modules.migrator.tasks import record_insert_or_replace  # imported here because it is a Celery task

    journal_full_1 = json.loads(pkg_resources.resource_string(
        __name__, os.path.join('fixtures', 'jou_record_fully_covered_1.json')))

    journal_partial_1 = json.loads(pkg_resources.resource_string(
        __name__, os.path.join('fixtures', 'jou_record_partially_covered_1.json')))

    journal_partial_2 = json.loads(pkg_resources.resource_string(
        __name__, os.path.join('fixtures', 'jou_record_partially_covered_2.json')))

    journal_no_pro_and_ref = json.loads(pkg_resources.resource_string(
                __name__, os.path.join('fixtures', 'jou_record_refereed.json')))

    journal_pro_and_ref = json.loads(pkg_resources.resource_string(
                __name__, os.path.join('fixtures', 'jou_record_refereed_and_proceedings.json')))

    with db.session.begin_nested():
        record_insert_or_replace(journal_full_1)
        record_insert_or_replace(journal_partial_1)
        record_insert_or_replace(journal_partial_2)
        record_insert_or_replace(journal_no_pro_and_ref)
        record_insert_or_replace(journal_pro_and_ref)
    db.session.commit()
    es.indices.refresh('records-journals')

    yield

    _delete_record('jou', 1936475)
    _delete_record('jou', 1936476)
    _delete_record('jou', 1936480)
    _delete_record('jou', 1936481)
    _delete_record('jou', 1936482)
    es.indices.refresh('records-journals')
def test_record_enhanced_in_es_and_not_enhanced_in_db(app):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature'],
        'references': [{'record': {'$ref': 'http://localhost:5000/api/literature/1498589'}}]
    }
    record = InspireRecord.create(record_json)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')
    rec1 = get_db_record('lit', 111)
    rec2 = get_es_record('lit', 111)
    assert 'facet_author_name' not in rec1
    assert 'facet_author_name' in rec2
    _delete_record('lit', 111)
def insert_journals_in_db(workflow_app):
    """Temporarily add few journals in the DB"""

    journal_no_pro_and_ref = json.loads(pkg_resources.resource_string(
        __name__, os.path.join('fixtures', 'jou_record_refereed.json')))

    journal_pro_and_ref = json.loads(pkg_resources.resource_string(
        __name__, os.path.join('fixtures', 'jou_record_refereed_and_proceedings.json')))

    with db.session.begin_nested():
        journal_no_pro_and_ref = InspireRecord.create_or_update(
            journal_no_pro_and_ref, skip_files=False)
        journal_no_pro_and_ref.commit()
        journal_pro_and_ref = InspireRecord.create_or_update(
            journal_pro_and_ref, skip_files=False)
        journal_pro_and_ref.commit()
    db.session.commit()
    es.indices.refresh('records-journals')

    yield

    _delete_record('jou', 1936475)
    _delete_record('jou', 1936476)
    es.indices.refresh('records-journals')
Пример #29
0
def test_index_after_commit_indexes_also_cites_record_when_citer_is_deleted(
    mocked_indexing_task,
    mocked_permission_check,
    app,
):
    # this test doesn't use the isolated_app because it needs to commit to
    # the DB in order to create records versions.

    json_data = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'This is the record being cited'
        }],
        'control_number': 9999,
        '_collections': ['Literature']
    }

    cited = InspireRecord.create(data=json_data, skip_files=True)
    cited.commit()
    db.session.commit()
    current_search.flush_and_refresh('records-hep')

    expected_args = ('lit', 9999, 2)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec = get_es_record('lit', 9999)
    assert es_rec['citation_count'] == 0
    assert LiteratureSearch.citations(es_rec).total == 0

    citing_json = {
        '$schema':
        'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'Record citing the first one'
        }],
        '_collections': ['Literature'],
        'control_number':
        8888,
        'references': [{
            'record': {
                '$ref': 'http://localhost:5000/api/literature/9999'
            },
            'reference': {
                'authors': [{
                    'full_name': 'Smith, J.'
                }],
            }
        }]
    }

    record = InspireRecord.create(data=citing_json, skip_files=True)
    record.commit()
    db.session.commit()
    current_search.flush_and_refresh('records-hep')

    expected_args = ('lit', record['control_number'], 2)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec = get_es_record('lit', 9999)
    assert es_rec['citation_count'] == 1
    assert LiteratureSearch.citations(es_rec).total == 1

    record.delete()
    record.commit()
    db.session.commit()
    current_search.flush_and_refresh('records-hep')

    expected_args = ('lit', record['control_number'], 3)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec = get_es_record('lit', 9999)
    assert es_rec['citation_count'] == 0
    assert LiteratureSearch.citations(es_rec).total == 0

    _delete_record('lit', record['control_number'])
    _delete_record('lit', cited['control_number'])
Пример #30
0
def test_literature_citations_api_with_full_citing_record(api_client):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature']
    }
    record = InspireRecord.create(record_json)
    record.commit()

    record_json_ref_1 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 222,
        'titles': [
            {
                'title': 'Frank Castle',
            },
        ],
        'authors': [
            {
                "full_name": "Urhan, Ahmet",
            }
        ],
        'publication_info': [
            {
                "artid": "HAL Id : hal-01735421, https://hal.archives-ouvertes.fr/hal-01735421",
                "page_start": "1",
            }
        ],
        'references': [
            {
                'record': {
                    '$ref': record._get_ref()
                }
            }
        ],
        '_collections': ['Literature']
    }
    record_ref_1 = InspireRecord.create(record_json_ref_1)
    record_ref_1.commit()

    record_json_ref_2 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 333,
        'titles': [
            {
                'title': 'Luke Cage',
            },
        ],
        'references': [
            {
                'record': {
                    '$ref': record._get_ref()
                }
            }
        ],
        '_collections': ['Literature']
    }
    record_ref_2 = InspireRecord.create(record_json_ref_2)
    record_ref_2.commit()

    db.session.commit()
    es.indices.refresh('records-hep')

    response = api_client.get(
        '/literature/111/citations',
        headers={'Accept': 'application/json'}
    )
    result = json.loads(response.get_data(as_text=True))

    result['metadata']['citations'].sort()

    expected_metadata = {
        "citation_count": 2,
        "citations": [
            {
                'authors': [
                    {
                        "full_name": "Urhan, Ahmet",
                        "first_name": "Ahmet",
                        "last_name": "Urhan",
                        "signature_block": "URANa",
                        "uuid": result['metadata']['citations'][1]['authors'][0]['uuid']
                    }
                ],
                'control_number': 222,
                'titles': [
                    {
                        'title': 'Frank Castle',
                    },
                ]
            },
            {
                "control_number": 333,
                "titles": [
                    {
                        "title": "Luke Cage"
                    }
                ]
            }
        ]
    }

    assert response.status_code == 200

    expected_metadata['citations'].sort()

    assert expected_metadata == result['metadata']

    _delete_record('lit', 111)
    _delete_record('lit', 222)
    _delete_record('lit', 333)
Пример #31
0
def test_literature_citations_api_with_superseded_records(app, api_client):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature']
    }
    record = InspireRecord.create(record_json)
    record.commit()

    citing_superseded_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'related_records': [
            {
                'record': {'$ref': 'https://link-to-successor'},
                'relation': 'successor'
            }
        ],
        'document_type': [
            'article',
        ],
        'control_number': 222,
        'titles': [
            {
                'title': 'Frank Castle',
            },
        ],
        'references': [
            {
                'record': {
                    '$ref': record._get_ref()
                }
            }
        ],
        '_collections': ['Literature']
    }
    citing_superseded_record = InspireRecord.create(citing_superseded_json)
    citing_superseded_record.commit()
    db.session.commit()

    es.indices.refresh('records-hep')

    response = api_client.get(
        '/literature/111/citations',
        headers={'Accept': 'application/json'}
    )
    result = json.loads(response.get_data(as_text=True))

    expected_metadata = {
        "citation_count": 0,
        "citations": []
    }

    expected_metadata['citations'].sort()
    result['metadata']['citations'].sort()

    assert response.status_code == 200
    assert expected_metadata == result['metadata']

    _delete_record('lit', 111)
    _delete_record('lit', 222)
def test_index_after_commit_indexes_also_cites_record_when_new_citation_is_added(
    mocked_indexing_task,
    mocked_permission_check,
    app,
):
    # this test doesn't use the isolated_app because it needs to commit to
    # the DB in order to create records versions.
    json_data = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'This is the record being cited'
        }],
        'control_number': 9999,
        '_collections': ['Literature']
    }
    cited = InspireRecord.create(data=json_data, skip_files=True)
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = 'lit', cited['control_number'], 1
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec = get_es_record('lit', 9999)
    assert es_rec['citation_count'] == 0
    assert get_citations_from_es(es_rec).total == 0

    citing_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'Record citing the first one'
        }],
        '_collections': ['Literature'],
        'control_number': 8888,
        'references': [{
            "reference": {
                'authors': [{
                    'full_name': 'Smith, J.'
                }]
            }
        }]
    }

    record = InspireRecord.create(data=citing_json, skip_files=True)
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = 'lit', record['control_number'], 1
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec = get_es_record('lit', 9999)
    assert es_rec['citation_count'] == 0
    assert get_citations_from_es(es_rec).total == 0

    references = {
        'references': [{
            "curated_relation": False,
            "record": {
                "$ref": "http://localhost:5000/api/literature/9999"
            },
            "reference": {
                'authors': [{
                    'full_name': 'Smith, J.'
                }],
            }
        }]
    }

    citing_json.update(references)
    record.clear()
    record.update(citing_json)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = 'lit', record['control_number'], 2
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec = get_es_record('lit', 9999)
    assert es_rec['citation_count'] == 1
    assert get_citations_from_es(es_rec).total == 1

    _delete_record('lit', 8888)
    _delete_record('lit', 9999)
Пример #33
0
def test_literature_citations_api_sorted_by_earliest_date(api_client):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature']
    }
    record = InspireRecord.create(record_json)
    record.commit()

    record_json_ref_1 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 222,
        'titles': [
            {
                'title': 'Frank Castle',
            },
        ],
        'preprint_date': '2013-10-08',
        'references': [
            {
                'record': {
                    '$ref': record._get_ref()
                }
            }
        ],
        '_collections': ['Literature']
    }
    record_ref_1 = InspireRecord.create(record_json_ref_1)
    record_ref_1.commit()

    record_json_ref_2 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'preprint_date': '2015-10-08',
        'control_number': 333,
        'titles': [
            {
                'title': 'Luke Cage',
            },
        ],
        'references': [
            {
                'record': {
                    '$ref': record._get_ref()
                }
            }
        ],
        '_collections': ['Literature']
    }
    record_ref_2 = InspireRecord.create(record_json_ref_2)
    record_ref_2.commit()

    record_json_ref_3 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'preprint_date': '2015-11-08',
        'control_number': 444,
        'titles': [
            {
                'title': 'John Doe',
            },
        ],
        'references': [
            {
                'record': {
                    '$ref': record._get_ref()
                }
            }
        ],
        '_collections': ['Literature']
    }
    record_ref_3 = InspireRecord.create(record_json_ref_3)
    record_ref_3.commit()

    db.session.commit()
    es.indices.refresh('records-hep')

    response = api_client.get(
        '/literature/111/citations',
        headers={'Accept': 'application/json'}
    )
    result = json.loads(response.get_data(as_text=True))

    expected_metadata = {
        "citation_count": 3,
        "citations": [
            {
                "control_number": 444,
                "titles": [
                    {
                        "title": "John Doe"
                    }
                ],
                "earliest_date": "2015-11-08"
            },
            {
                "control_number": 333,
                "titles": [
                    {
                        "title": "Luke Cage"
                    }
                ],
                "earliest_date": "2015-10-08"
            },
            {
                "control_number": 222,
                "titles": [
                    {
                        "title": "Frank Castle"
                    }
                ],
                "earliest_date": "2013-10-08"
            }
        ]
    }

    assert response.status_code == 200
    assert expected_metadata == result['metadata']

    _delete_record('lit', 111)
    _delete_record('lit', 222)
    _delete_record('lit', 333)
    _delete_record('lit', 444)
Пример #34
0
def records_to_be_merged(app):
    merged_record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'control_number': 111,
        'document_type': [
            'article',
        ],
        'titles': [
            {'title': 'merged'},
        ],
        'self': {
            '$ref': 'http://localhost:5000/api/literature/111',
        },
        '_collections': ['Literature'],
    }

    deleted_record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'control_number': 222,
        'document_type': [
            'article',
        ],
        'titles': [
            {'title': 'deleted'},
        ],
        'self': {
            '$ref': 'http://localhost:5000/api/literature/222',
        },
        '_collections': ['Literature'],
    }

    pointing_record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'accelerator_experiments': [
            {
                'record': {
                    '$ref': 'http://localhost:5000/api/literature/222',
                },
            },
        ],
        'control_number': 333,
        'document_type': [
            'article',
        ],
        'titles': [
            {'title': 'pointing'},
        ],
        'self': {
            '$ref': 'http://localhost:5000/api/literature/333',
        },
        '_collections': ['Literature'],
    }

    with db.session.begin_nested():
        merged_uuid = _create_record(merged_record).id
        deleted_uuid = _create_record(deleted_record).id
        _create_record(pointing_record)
    db.session.commit()

    es.indices.refresh('records-hep')

    yield

    _delete_merged_records('lit', 111, 222, merged_uuid, deleted_uuid)
    _delete_record('lit', 333)
def test_regression_index_after_commit_retries_for_new_record_not_yet_in_db(
    mocked_indexing_task,
    mocked_permission_check,
    app,
):
    # this test doesn't use the isolated_app because it needs to commit to
    # the DB in order to create records versions.

    json_data = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{'title': 'This is the record being cited'}],
        'control_number': 9999,
        '_collections': ['Literature']
    }

    cited = InspireRecord.create(data=json_data, skip_files=True)
    cited.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', 9999, 2)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec = get_es_record('lit', 9999)
    assert es_rec['citation_count'] == 0
    assert LiteratureSearch.citations(es_rec).total == 0

    citing_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{'title': 'Record citing the first one'}],
        '_collections': ['Literature'],
        'control_number': 8888,
        'references': [
            {
                'record': {
                    '$ref': 'http://localhost:5000/api/literature/9999'
                },
                'reference': {
                    'authors': [{'full_name': 'Smith, J.'}],
                }
            }
        ]
    }

    record = InspireRecord.create(data=citing_json, skip_files=True)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', record['control_number'], 2)
    mocked_indexing_task.assert_called_with(*expected_args)

    # execute mocked task pretending record is not committed yet to DB
    _delete_record('lit', record['control_number'])
    with pytest.raises(RecordGetterError):
        # XXX: celery in eager mode does not retry, so it raises the first time
        index_modified_citations_from_record(*expected_args)

    _delete_record('lit', cited['control_number'])
def test_index_after_commit_indexes_also_cites_two_records(
    mocked_indexing_task,
    mocked_permission_check,
    app,
):
    # this test doesn't use the isolated_app because it needs to commit to
    # the DB in order to create records versions.
    json1 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{'title': 'This is the record being cited'}],
        'control_number': 9999,
        '_collections': ['Literature']
    }

    cited1 = InspireRecord.create(data=json1, skip_files=True)
    cited1.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', cited1['control_number'], 2)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    json2 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{'title': 'This also is the record being cited'}],
        'control_number': 9998,
        '_collections': ['Literature']
    }

    cited2 = InspireRecord.create(data=json2, skip_files=True)
    cited2.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', cited2['control_number'], 2)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec1 = get_es_record('lit', 9999)
    es_rec2 = get_es_record('lit', 9998)
    assert es_rec1['citation_count'] == 0
    assert es_rec2['citation_count'] == 0
    assert LiteratureSearch.citations(es_rec1).total == 0
    assert LiteratureSearch.citations(es_rec2).total == 0

    citing_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{'title': 'Record citing the first one'}],
        '_collections': ['Literature'],
        'control_number': 8888,
        'references': [
            {
                'reference': {
                    'authors': [{'full_name': 'Smith, J.'}],
                }
            }
        ]
    }

    record = InspireRecord.create(data=citing_json, skip_files=True)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', record['control_number'], 2)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec1 = get_es_record('lit', 9999)
    es_rec2 = get_es_record('lit', 9998)
    assert es_rec1['citation_count'] == 0
    assert es_rec2['citation_count'] == 0
    assert LiteratureSearch.citations(es_rec1).total == 0
    assert LiteratureSearch.citations(es_rec2).total == 0

    references = {
        'references': [
            {
                'record': {
                    '$ref': 'http://localhost:5000/api/literature/9998'
                },
            },
            {
                'record': {
                    '$ref': 'http://localhost:5000/api/literature/9999'
                },
            }
        ]
    }

    citing_json.update(references)
    record.clear()
    record.update(citing_json)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', record['control_number'], 3)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec1 = get_es_record('lit', 9999)
    es_rec2 = get_es_record('lit', 9998)
    assert es_rec1['citation_count'] == 1
    assert es_rec2['citation_count'] == 1
    assert LiteratureSearch.citations(es_rec1).total == 1
    assert LiteratureSearch.citations(es_rec2).total == 1

    _delete_record('lit', record['control_number'])
    _delete_record('lit', cited1['control_number'])
    _delete_record('lit', cited2['control_number'])
Пример #37
0
def test_literature_citations_api_with_not_existing_pid_value(api_client):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature']
    }
    record = InspireRecord.create(record_json)
    record.commit()

    record_json_ref_1 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 222,
        'titles': [
            {
                'title': 'Frank Castle',
            },
        ],
        'references': [
            {
                'record': {
                    '$ref': record._get_ref()
                }
            }
        ],
        '_collections': ['Literature']
    }
    record_ref_1 = InspireRecord.create(record_json_ref_1)
    record_ref_1.commit()

    record_json_ref_2 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 333,
        'titles': [
            {
                'title': 'Luke Cage',
            },
        ],
        'references': [
            {
                'record': {
                    '$ref': record._get_ref()
                }
            }
        ],
        '_collections': ['Literature']
    }
    record_ref_2 = InspireRecord.create(record_json_ref_2)
    record_ref_2.commit()

    db.session.commit()
    es.indices.refresh('records-hep')

    response = api_client.get(
        '/literature/444/citations',
        headers={'Accept': 'application/json'}
    )

    assert response.status_code == 404

    _delete_record('lit', 111)
    _delete_record('lit', 222)
    _delete_record('lit', 333)
Пример #38
0
def test_literature_citations_api_with_parameter_page_2(api_client):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature']
    }
    record = InspireRecord.create(record_json)
    record.commit()

    record_json_ref_1 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 222,
        'titles': [
            {
                'title': 'Frank Castle',
            },
        ],
        'references': [
            {
                'record': {
                    '$ref': record._get_ref()
                }
            }
        ],
        '_collections': ['Literature']
    }
    record_ref_1 = InspireRecord.create(record_json_ref_1)
    record_ref_1.commit()

    record_json_ref_2 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 333,
        'titles': [
            {
                'title': 'Luke Cage',
            },
        ],
        'references': [
            {
                'record': {
                    '$ref': record._get_ref()
                }
            }
        ],
        '_collections': ['Literature']
    }
    record_ref_2 = InspireRecord.create(record_json_ref_2)
    record_ref_2.commit()

    db.session.commit()
    es.indices.refresh('records-hep')

    response = api_client.get(
        '/literature/111/citations?size=1&page=2',
        headers={'Accept': 'application/json'}
    )
    result = json.loads(response.get_data(as_text=True))

    expected_metadata = [
        {
            "citation_count": 2,
            "citations": [
                {
                    "control_number": 222,
                    "titles": [
                        {
                            "title": "Frank Castle"
                        }
                    ]
                },
            ]
        },
        {
            "citation_count": 2,
            "citations": [
                {
                    "control_number": 333,
                    "titles": [
                        {
                            "title": "Luke Cage"
                        }
                    ]
                },
            ]
        }
    ]

    assert response.status_code == 200
    assert result['metadata'] in expected_metadata

    _delete_record('lit', 111)
    _delete_record('lit', 222)
    _delete_record('lit', 333)
Пример #39
0
def test_literature_citations_api_with_parameter_page_2(api_client):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature']
    }
    record = InspireRecord.create(record_json)
    record.commit()

    record_json_ref_1 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 222,
        'titles': [
            {
                'title': 'Frank Castle',
            },
        ],
        'references': [{
            'record': {
                '$ref': record._get_ref()
            }
        }],
        '_collections': ['Literature']
    }
    record_ref_1 = InspireRecord.create(record_json_ref_1)
    record_ref_1.commit()

    record_json_ref_2 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 333,
        'titles': [
            {
                'title': 'Luke Cage',
            },
        ],
        'references': [{
            'record': {
                '$ref': record._get_ref()
            }
        }],
        '_collections': ['Literature']
    }
    record_ref_2 = InspireRecord.create(record_json_ref_2)
    record_ref_2.commit()

    db.session.commit()
    current_search.flush_and_refresh('records-hep')

    response = api_client.get('/literature/111/citations?size=1&page=2',
                              headers={'Accept': 'application/json'})
    result = json.loads(response.get_data(as_text=True))

    expected_metadata = [{
        "citation_count":
        2,
        "citations": [
            {
                "control_number": 222,
                "titles": [{
                    "title": "Frank Castle"
                }]
            },
        ]
    }, {
        "citation_count":
        2,
        "citations": [
            {
                "control_number": 333,
                "titles": [{
                    "title": "Luke Cage"
                }]
            },
        ]
    }]

    assert response.status_code == 200
    assert result['metadata'] in expected_metadata

    _delete_record('lit', 111)
    _delete_record('lit', 222)
    _delete_record('lit', 333)
Пример #40
0
def records_to_be_merged(app):
    merged_record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'control_number': 111,
        'document_type': [
            'article',
        ],
        'titles': [
            {'title': 'merged'},
        ],
        'self': {
            '$ref': 'http://localhost:5000/api/literature/111',
        },
        '_collections': ['Literature'],
    }

    deleted_record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'control_number': 222,
        'document_type': [
            'article',
        ],
        'titles': [
            {'title': 'deleted'},
        ],
        'self': {
            '$ref': 'http://localhost:5000/api/literature/222',
        },
        '_collections': ['Literature'],
    }

    pointing_record = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'accelerator_experiments': [
            {
                'record': {
                    '$ref': 'http://localhost:5000/api/literature/222',
                },
            },
        ],
        'control_number': 333,
        'document_type': [
            'article',
        ],
        'titles': [
            {'title': 'pointing'},
        ],
        'self': {
            '$ref': 'http://localhost:5000/api/literature/333',
        },
        '_collections': ['Literature'],
    }

    with db.session.begin_nested():
        merged_uuid = record_insert_or_replace(merged_record).id
        deleted_uuid = record_insert_or_replace(deleted_record).id
        record_insert_or_replace(pointing_record)
    db.session.commit()

    es.indices.refresh('records-hep')

    yield

    _delete_merged_records('lit', 111, 222, merged_uuid, deleted_uuid)
    _delete_record('lit', 333)
Пример #41
0
def test_literature_citations_api_with_not_existing_pid_value(api_client):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature']
    }
    record = InspireRecord.create(record_json)
    record.commit()

    record_json_ref_1 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 222,
        'titles': [
            {
                'title': 'Frank Castle',
            },
        ],
        'references': [{
            'record': {
                '$ref': record._get_ref()
            }
        }],
        '_collections': ['Literature']
    }
    record_ref_1 = InspireRecord.create(record_json_ref_1)
    record_ref_1.commit()

    record_json_ref_2 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 333,
        'titles': [
            {
                'title': 'Luke Cage',
            },
        ],
        'references': [{
            'record': {
                '$ref': record._get_ref()
            }
        }],
        '_collections': ['Literature']
    }
    record_ref_2 = InspireRecord.create(record_json_ref_2)
    record_ref_2.commit()

    db.session.commit()
    current_search.flush_and_refresh('records-hep')

    response = api_client.get('/literature/444/citations',
                              headers={'Accept': 'application/json'})

    assert response.status_code == 404

    _delete_record('lit', 111)
    _delete_record('lit', 222)
    _delete_record('lit', 333)
Пример #42
0
def test_literature_citations_api_with_full_citing_record(api_client):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature']
    }
    record = InspireRecord.create(record_json)
    record.commit()

    record_json_ref_1 = {
        '$schema':
        'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number':
        222,
        'titles': [
            {
                'title': 'Frank Castle',
            },
        ],
        'authors': [{
            "full_name": "Urhan, Ahmet",
        }],
        'publication_info': [{
            "artid":
            "HAL Id : hal-01735421, https://hal.archives-ouvertes.fr/hal-01735421",
            "page_start": "1",
        }],
        'references': [{
            'record': {
                '$ref': record._get_ref()
            }
        }],
        '_collections': ['Literature']
    }
    record_ref_1 = InspireRecord.create(record_json_ref_1)
    record_ref_1.commit()

    record_json_ref_2 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 333,
        'titles': [
            {
                'title': 'Luke Cage',
            },
        ],
        'references': [{
            'record': {
                '$ref': record._get_ref()
            }
        }],
        '_collections': ['Literature']
    }
    record_ref_2 = InspireRecord.create(record_json_ref_2)
    record_ref_2.commit()

    db.session.commit()
    current_search.flush_and_refresh('records-hep')

    response = api_client.get('/literature/111/citations',
                              headers={'Accept': 'application/json'})
    result = json.loads(response.get_data(as_text=True))

    result['metadata']['citations'].sort()

    expected_metadata = {
        "citation_count":
        2,
        "citations": [{
            'authors': [{
                "full_name":
                "Urhan, Ahmet",
                "first_name":
                "Ahmet",
                "last_name":
                "Urhan",
                "signature_block":
                "URANa",
                "uuid":
                result['metadata']['citations'][1]['authors'][0]['uuid']
            }],
            'control_number':
            222,
            'titles': [
                {
                    'title': 'Frank Castle',
                },
            ]
        }, {
            "control_number": 333,
            "titles": [{
                "title": "Luke Cage"
            }]
        }]
    }

    assert response.status_code == 200

    expected_metadata['citations'].sort()

    assert expected_metadata == result['metadata']

    _delete_record('lit', 111)
    _delete_record('lit', 222)
    _delete_record('lit', 333)
Пример #43
0
def test_literature_citations_api_with_superseded_records(app, api_client):
    record_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'control_number': 111,
        'titles': [
            {
                'title': 'Jessica Jones',
            },
        ],
        '_collections': ['Literature']
    }
    record = InspireRecord.create(record_json)
    record.commit()

    citing_superseded_json = {
        '$schema':
        'http://localhost:5000/schemas/records/hep.json',
        'related_records': [{
            'record': {
                '$ref': 'https://link-to-successor'
            },
            'relation': 'successor'
        }],
        'document_type': [
            'article',
        ],
        'control_number':
        222,
        'titles': [
            {
                'title': 'Frank Castle',
            },
        ],
        'references': [{
            'record': {
                '$ref': record._get_ref()
            }
        }],
        '_collections': ['Literature']
    }
    citing_superseded_record = InspireRecord.create(citing_superseded_json)
    citing_superseded_record.commit()
    db.session.commit()

    current_search.flush_and_refresh('records-hep')

    response = api_client.get('/literature/111/citations',
                              headers={'Accept': 'application/json'})
    result = json.loads(response.get_data(as_text=True))

    expected_metadata = {"citation_count": 0, "citations": []}

    expected_metadata['citations'].sort()
    result['metadata']['citations'].sort()

    assert response.status_code == 200
    assert expected_metadata == result['metadata']

    _delete_record('lit', 111)
    _delete_record('lit', 222)
def test_index_after_commit_indexes_also_cites_two_records(
    mocked_indexing_task,
    mocked_permission_check,
    app,
):
    # this test doesn't use the isolated_app because it needs to commit to
    # the DB in order to create records versions.
    json1 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'This is the record being cited'
        }],
        'control_number': 9999,
        '_collections': ['Literature']
    }

    cited1 = InspireRecord.create(data=json1, skip_files=True)
    cited1.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', cited1['control_number'], 2)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    json2 = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'This also is the record being cited'
        }],
        'control_number': 9998,
        '_collections': ['Literature']
    }

    cited2 = InspireRecord.create(data=json2, skip_files=True)
    cited2.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', cited2['control_number'], 2)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec1 = get_es_record('lit', 9999)
    es_rec2 = get_es_record('lit', 9998)
    assert es_rec1['citation_count'] == 0
    assert es_rec2['citation_count'] == 0
    assert get_citations_from_es(es_rec1).total == 0
    assert get_citations_from_es(es_rec2).total == 0

    citing_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'Record citing the first one'
        }],
        '_collections': ['Literature'],
        'control_number': 8888,
        'references': [{
            'reference': {
                'authors': [{
                    'full_name': 'Smith, J.'
                }],
            }
        }]
    }

    record = InspireRecord.create(data=citing_json, skip_files=True)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', record['control_number'], 2)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec1 = get_es_record('lit', 9999)
    es_rec2 = get_es_record('lit', 9998)
    assert es_rec1['citation_count'] == 0
    assert es_rec2['citation_count'] == 0
    assert get_citations_from_es(es_rec1).total == 0
    assert get_citations_from_es(es_rec2).total == 0

    references = {
        'references': [{
            'record': {
                '$ref': 'http://localhost:5000/api/literature/9998'
            },
        }, {
            'record': {
                '$ref': 'http://localhost:5000/api/literature/9999'
            },
        }]
    }

    citing_json.update(references)
    record.clear()
    record.update(citing_json)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', record['control_number'], 3)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec1 = get_es_record('lit', 9999)
    es_rec2 = get_es_record('lit', 9998)
    assert es_rec1['citation_count'] == 1
    assert es_rec2['citation_count'] == 1
    assert get_citations_from_es(es_rec1).total == 1
    assert get_citations_from_es(es_rec2).total == 1

    _delete_record('lit', record['control_number'])
    _delete_record('lit', cited1['control_number'])
    _delete_record('lit', cited2['control_number'])
def test_regression_index_after_commit_retries_for_new_record_not_yet_in_db(
    mocked_indexing_task,
    mocked_permission_check,
    app,
):
    # this test doesn't use the isolated_app because it needs to commit to
    # the DB in order to create records versions.

    json_data = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'This is the record being cited'
        }],
        'control_number': 9999,
        '_collections': ['Literature']
    }

    cited = InspireRecord.create(data=json_data, skip_files=True)
    cited.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', 9999, 2)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec = get_es_record('lit', 9999)
    assert es_rec['citation_count'] == 0
    assert get_citations_from_es(es_rec).total == 0

    citing_json = {
        '$schema':
        'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'Record citing the first one'
        }],
        '_collections': ['Literature'],
        'control_number':
        8888,
        'references': [{
            'record': {
                '$ref': 'http://localhost:5000/api/literature/9999'
            },
            'reference': {
                'authors': [{
                    'full_name': 'Smith, J.'
                }],
            }
        }]
    }

    record = InspireRecord.create(data=citing_json, skip_files=True)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', record['control_number'], 2)
    mocked_indexing_task.assert_called_with(*expected_args)

    # execute mocked task pretending record is not committed yet to DB
    _delete_record('lit', record['control_number'])
    with pytest.raises(RecordGetterError):
        # XXX: celery in eager mode does not retry, so it raises the first time
        index_modified_citations_from_record(*expected_args)

    _delete_record('lit', cited['control_number'])
def test_index_after_commit_indexes_raises_if_cited_records_are_not_in_db(
    mocked_indexing_task,
    mocked_permission_check,
    app,
):
    # this test doesn't use the isolated_app because it needs to commit to
    # the DB in order to create records versions.

    citing_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{
            'title': 'Record citing the first one'
        }],
        '_collections': ['Literature'],
        'control_number': 8888,
        'references': [{
            "reference": {
                'authors': [{
                    'full_name': 'Smith, J.'
                }]
            }
        }]
    }

    record = InspireRecord.create(data=citing_json, skip_files=True)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = 'lit', record['control_number'], 2
    mocked_indexing_task.assert_called_with(*expected_args)

    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    references = {
        'references': [{
            "curated_relation": False,
            "record": {
                "$ref": "http://localhost:5000/api/literature/9999"
            },
            "reference": {
                'authors': [{
                    'full_name': 'Smith, J.'
                }],
            }
        }]
    }

    citing_json.update(references)
    record.clear()
    record.update(citing_json)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = ('lit', record['control_number'], 3)
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    with pytest.raises(MissingCitedRecordError):
        index_modified_citations_from_record(*expected_args)

    _delete_record('lit', 8888)
def test_index_after_commit_indexes_also_cites_record_when_new_citation_is_added(
    mocked_indexing_task,
    mocked_permission_check,
    app,
):
    # this test doesn't use the isolated_app because it needs to commit to
    # the DB in order to create records versions.
    json_data = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{'title': 'This is the record being cited'}],
        'control_number': 9999,
        '_collections': ['Literature']
    }
    cited = InspireRecord.create(data=json_data, skip_files=True)
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = 'lit', cited['control_number'], 1
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec = get_es_record('lit', 9999)
    assert es_rec['citation_count'] == 0
    assert LiteratureSearch.citations(es_rec).total == 0

    citing_json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': ['article'],
        'titles': [{'title': 'Record citing the first one'}],
        '_collections': ['Literature'],
        'control_number': 8888,
        'references': [
            {"reference": {'authors': [{'full_name': 'Smith, J.'}]}}
        ]
    }

    record = InspireRecord.create(data=citing_json, skip_files=True)
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = 'lit', record['control_number'], 1
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec = get_es_record('lit', 9999)
    assert es_rec['citation_count'] == 0
    assert LiteratureSearch.citations(es_rec).total == 0

    references = {
        'references': [
            {
                "curated_relation": False,
                "record": {
                    "$ref": "http://localhost:5000/api/literature/9999"
                },
                "reference": {
                    'authors': [{'full_name': 'Smith, J.'}],
                }
            }
        ]
    }

    citing_json.update(references)
    record.clear()
    record.update(citing_json)
    record.commit()
    db.session.commit()
    es.indices.refresh('records-hep')

    expected_args = 'lit', record['control_number'], 2
    mocked_indexing_task.assert_called_with(*expected_args)
    # execute mocked task
    index_modified_citations_from_record(*expected_args)

    es_rec = get_es_record('lit', 9999)
    assert es_rec['citation_count'] == 1
    assert LiteratureSearch.citations(es_rec).total == 1

    _delete_record('lit', 8888)
    _delete_record('lit', 9999)