def test_core_is_not_written_in_extradata_if_article_is_non_core(app):
    schema = load_schema('hep')
    subschema = schema['properties']['arxiv_eprints']

    categories_config = {
        'ARXIV_CATEGORIES': {
            'core': ['hep-ph'],
            'non-core': ['astro-ph.CO', 'gr-qc']
        }
    }

    with patch.dict(app.config, categories_config):
        data = {}
        extra_data = {}

        obj = MockObj(data, extra_data)
        eng = MockEng()

        obj.data = {
            'arxiv_eprints': [
                {
                    'categories': [
                        'astro-ph.CO',
                    ],
                    'value': '1705.01122',
                },
            ],
        }
        assert validate(obj.data['arxiv_eprints'], subschema) is None
        set_core_in_extra_data(obj, eng)
        assert 'core' not in obj.extra_data
Esempio n. 2
0
def test_new_ticket_context_handles_unicode(unicode_data, extra_data, user):
    obj = MockObj(unicode_data, extra_data)
    ctx = new_ticket_context(user, obj)

    assert isinstance(ctx['object'], MockObj)
    assert ctx['email'] == '*****@*****.**'
    assert ctx[
        'subject'] == u'Your suggestion to INSPIRE: author Diego Martínez'
    assert ctx['user_comment'] == 'Foo bar'
def test_is_experimental_paper():
    obj = MockObj({'arxiv_eprints': [
        {
            'categories': ['hep-ex']
        },
    ]}, {})
    eng = MockEng()

    assert is_experimental_paper(obj, eng)
Esempio n. 4
0
def test_halt_record():
    obj = MockObj({}, {'halt_action': 'foo', 'halt_message': 'bar'})
    eng = MockEng()

    default_halt_record = halt_record()

    assert default_halt_record(obj, eng) is None
    assert eng.action == 'foo'
    assert eng.msg == 'bar'
Esempio n. 5
0
def test_curation_ticket_context(data, extra_data, user):
    obj = MockObj(data, extra_data)
    ctx = curation_ticket_context(user, obj)
    assert isinstance(ctx['object'], MockObj)
    assert ctx['recid'] == 123
    assert ctx['user_comment'] == 'Foo bar'
    assert ctx['subject'] == 'Curation needed for author John Doe [John.Doe.1]'
    assert ctx['email'] == '*****@*****.**'
    assert ctx['record_url'] == 'http://example.com'
Esempio n. 6
0
def test_arxiv_author_list_handles_multiple_author_xml_files():
    schema = load_schema('hep')
    eprints_subschema = schema['properties']['arxiv_eprints']

    filename = pkg_resources.resource_filename(
        __name__, os.path.join('fixtures', '1703.09986.multiple_author_lists.tar.gz'))

    data = {
        '$schema': 'http://localhost:5000/hep.json',
        'arxiv_eprints': [
            {
                'categories': [
                    'hep-ex',
                ],
                'value': '1703.09986',
            },
        ],
    }  # record/1519995
    validate(data['arxiv_eprints'], eprints_subschema)

    extra_data = {}
    files = MockFiles({
        '1703.09986.tar.gz': AttrDict({
            'file': AttrDict({
                'uri': filename,
            })
        })
    })

    obj = MockObj(data, extra_data, files=files)
    eng = MockEng()

    default_arxiv_author_list = arxiv_author_list()
    default_arxiv_author_list(obj, eng)

    authors_subschema = schema['properties']['authors']
    expected_authors = [
        {
            'affiliations': [{'value': 'Yerevan Phys. Inst.'}],
            'ids': [
                {'value': 'INSPIRE-00312131', 'schema': 'INSPIRE ID'},
                {'value': 'CERN-432142', 'schema': 'CERN'},
            ],
            'full_name': 'Sirunyan, Albert M',
        },
        {
            'affiliations': [{'value': 'Yerevan Phys. Inst.'}],
            'ids': [
                {'value': 'INSPIRE-00312132', 'schema': 'INSPIRE ID'},
                {'value': 'CERN-432143', 'schema': 'CERN'},
            ],
            'full_name': 'Weary, Jake',
        }
    ]
    validate(expected_authors, authors_subschema)

    assert obj.data.get('authors') == expected_authors
Esempio n. 7
0
def test_preserve_root():
    config = {'FEATURE_FLAG_ENABLE_MERGER': True}

    with patch.dict(current_app.config, config):
        obj = MockObj({'foo': 'bar'}, {})
        eng = MockEng()

        assert preserve_root(obj, eng) is None
        assert obj.extra_data['merger_root'] == {'foo': 'bar'}
Esempio n. 8
0
def test_set_exact_match_as_approved_in_extradata_no_exact_key_raises_exception(
):
    data = {}
    extra_data = {'matches': {'wrongkey': [1]}}

    obj = MockObj(data, extra_data)
    eng = MockEng()
    with pytest.raises(KeyError):
        set_exact_match_as_approved_in_extradata(obj, eng)
Esempio n. 9
0
def test_guess_coreness_does_not_fail_when_request_fails(j_a_r, g_b_u):
    j_a_r.side_effect = requests.exceptions.RequestException()
    g_b_u.return_value = 'https://beard.inspirehep.net/predictor/coreness'

    obj = MockObj({}, {})
    eng = MockEng()

    assert guess_coreness(obj, eng) is None
    assert 'relevance_prediction' not in obj.extra_data
Esempio n. 10
0
def test_set_exact_match_as_approved_in_extradata_with_empty_list_raises_exception(
):
    data = {}
    extra_data = {'matches': {'exact': []}}

    obj = MockObj(data, extra_data)
    eng = MockEng()
    with pytest.raises(IndexError):
        set_exact_match_as_approved_in_extradata(obj, eng)
Esempio n. 11
0
def test_is_fuzzy_match_approved_returns_False_if_there_is_not_a_match_approved(
        mock_match):
    data = {}
    extra_data = {}

    obj = MockObj(data, extra_data)
    eng = MockEng()

    assert not is_fuzzy_match_approved(obj, eng)
Esempio n. 12
0
def test_is_fuzzy_match_approved_returns_true_if_there_is_a_match_approved(
        mock_match):
    data = {}
    extra_data = {'fuzzy_match_approved_id': 4328}

    obj = MockObj(data, extra_data)
    eng = MockEng()

    assert is_fuzzy_match_approved(obj, eng)
Esempio n. 13
0
def test_guess_keywords_does_not_fail_when_request_fails(j_a_r, g_m_u):
    j_a_r.side_effect = requests.exceptions.RequestException()
    g_m_u.return_value = 'https://magpie.inspirehep.net/predict'

    obj = MockObj({}, {})
    eng = MockEng()

    assert guess_keywords(obj, eng) is None
    assert obj.extra_data == {}
def test_send_robotupload_update_authors_when_feature_flag_is_enabled():
    with requests_mock.Mocker() as requests_mocker:
        requests_mocker.register_uri(
            'POST', 'http://inspirehep.net/batchuploader/robotupload/insert',
            text='[INFO] foo bar baz'
        )

        config = {
            'LEGACY_ROBOTUPLOAD_URL': 'http://inspirehep.net',
            'PRODUCTION_MODE': True,
            'FEATURE_FLAG_ENABLE_UPDATE_TO_LEGACY': True
        }

        with patch.dict(current_app.config, config), \
                patch('inspirehep.modules.workflows.tasks.submission.record2marcxml'):
            data = {
                '$schema': 'http://localhost:5000/schemas/records/authors.json',
                'name': {
                    'preferred_name': 'Jessica Jones',
                    'value': 'Jones, Jessica'
                }
            }
            extra_data = {
                'is-update': True
            }

            obj = MockObj(data, extra_data)
            obj.workflow = MockWorkflow('author')
            eng = MockEng(data_type='authors')

            _send_robotupload = send_robotupload(
                mode='insert',
            )

            assert _send_robotupload(obj, eng) is None

            expected = (
                'Robotupload sent!'
                '[INFO] foo bar baz'
                'end of upload'
            )
            result = obj.log._info.getvalue()

            assert expected == result
Esempio n. 15
0
def test_arxiv_plot_extract_is_safe_to_rerun(mock_os, tmpdir):
    schema = load_schema('hep')
    subschema = schema['properties']['arxiv_eprints']

    filename = pkg_resources.resource_filename(
        __name__, os.path.join('fixtures', '0804.1873.tar.gz'))

    data = {
        'arxiv_eprints': [
            {
                'categories': [
                    'nucl-ex',
                ],
                'value': '0804.1873',
            },
        ],
    }  # literature/783246
    extra_data = {}
    files = MockFiles({
        '0804.1873.tar.gz':
        AttrDict({
            'file': AttrDict({
                'uri': filename,
            }),
        }),
    })
    assert validate(data['arxiv_eprints'], subschema) is None

    obj = MockObj(data, extra_data, files=files)
    eng = MockEng()

    temporary_dir = tmpdir.mkdir('plots')
    mock_os.path.abspath.return_value = str(temporary_dir)

    for _ in range(2):
        assert arxiv_plot_extract(obj, eng) is None

        expected_figures = [{
            'url':
            '/api/files/0b9dd5d1-feae-4ba5-809d-3a029b0bc110/figure1.png',
            'source':
            'arxiv',
            'material':
            'preprint',
            'key':
            'figure1.png',
            'caption':
            'Difference (in MeV) between the theoretical and experimental masses for the 2027 selected nuclei as a function of the mass number.'
        }]
        result = obj.data['figures']

        assert expected_figures == result

        expected_files = ['0804.1873.tar.gz', 'figure1.png']

        assert expected_files == obj.files.keys
def test_curation_ticket_context():
    data = {
        'arxiv_eprints': [
            {
                'value': 'math.GT/0309136',
            },
            {
                'value': '0706.0001v1',
            },
        ],
        'report_numbers': [
            {
                'value': 'foo',
            },
        ],
        'dois': [
            {
                'value': 'bar',
            },
        ],
    }
    extra_data = {
        'recid': 'baz',
        'url': 'qux',
        'formdata': {
            'url': 'quux',
            'references': [
                'plugh',
            ],
            'extra_comments': [
                'xyzzy',
            ]
        }
    }

    obj = MockObj(data, extra_data)
    user = MockUser('*****@*****.**')

    expected = {
        'recid': 'baz',
        'record_url': 'qux',
        'link_to_pdf': 'quux',
        'email': '*****@*****.**',
        'references': [
            'plugh',
        ],
        'user_comment': [
            'xyzzy',
        ],
        'server_name': 'localhost:5000',
        'subject': 'math.GT/0309136 arXiv:0706.0001v1 doi:bar foo (#baz)',
        'legacy_url': 'https://old.inspirehep.net',
    }
    result = curation_ticket_context(user, obj)

    assert expected == result
def test_pending_in_holding_pen_returns_true_if_something_matched(mock_match):
    mock_match.return_value = iter([{'_id': 1}])

    data = {}
    extra_data = {}

    obj = MockObj(data, extra_data, id=2)
    eng = MockEng()

    assert _pending_in_holding_pen(obj, eng)
def test_pending_in_holding_pen_returns_false_if_nothing_matched(mock_match):
    mock_match.return_value = iter([])

    data = {}
    extra_data = {}

    obj = MockObj(data, extra_data)
    eng = MockEng()

    assert not _pending_in_holding_pen(obj, eng)
def test_set_exact_match_as_approved_in_extradata():
    data = {}
    extra_data = {'matches': {'exact': [1, 2]}}

    obj = MockObj(data, extra_data)
    eng = MockEng()

    set_exact_match_as_approved_in_extradata(obj, eng)

    assert get_value(obj.extra_data, 'matches.approved') is 1
Esempio n. 20
0
def test_curation_ticket_context_handles_unicode(unicode_data, extra_data,
                                                 user):
    obj = MockObj(unicode_data, extra_data)
    ctx = curation_ticket_context(user, obj)
    assert isinstance(ctx['object'], MockObj)
    assert ctx['user_comment'] == 'Foo bar'
    assert ctx[
        'subject'] == u'Curation needed for author Diego Martínez [Diego.Martinez.Santos.1]'
    assert ctx['email'] == '*****@*****.**'
    assert ctx['record_url'] == 'http://example.com'
Esempio n. 21
0
def test_fuzzy_match_returns_true_if_something_matched_with_publication_info(mock_match, enable_fuzzy_matcher):
    schema = load_schema('hep')
    publication_info_schema = schema['properties']['publication_info']
    titles_schema = schema['properties']['titles']

    matched_record = {
        'control_number': 1472986,
        'titles': [
            {
                'title': 'title',
            },
        ],
        'publication_info': [
            {
                'artid': '054021',
                'journal_issue': '5',
                'journal_title': 'Phys.Rev.D',
                'journal_volume': '94',
                'pubinfo_freetext': 'Phys. Rev. D94 (2016) 054021',
                'year': 2016
            },
        ],
    }

    assert validate(matched_record['titles'], titles_schema) is None
    assert validate(matched_record['publication_info'], publication_info_schema) is None

    mock_match.return_value = iter([{'_source': matched_record}])

    data = {}
    extra_data = {}

    obj = MockObj(data, extra_data)
    eng = MockEng()

    assert fuzzy_match(obj, eng)
    assert 'matches' in obj.extra_data

    expected = [{
        'control_number': 1472986,
        'title': 'title',
        'publication_info': [
            {
                'artid': '054021',
                'journal_issue': '5',
                'journal_title': 'Phys.Rev.D',
                'journal_volume': '94',
                'pubinfo_freetext': 'Phys. Rev. D94 (2016) 054021',
                'year': 2016
            },
        ],
    }]
    result = get_value(obj.extra_data, 'matches.fuzzy')

    assert expected == result
def test_set_refereed_and_fix_document_type_does_nothing_if_no_journals_were_found(mock_replace_refs):
    mock_replace_refs.return_value = []

    data = {'document_type': ['article']}
    extra_data = {}

    obj = MockObj(data, extra_data)
    eng = MockEng()

    assert set_refereed_and_fix_document_type(obj, eng) is None
    assert 'refereed' not in obj.data
Esempio n. 23
0
def test_wait_webcoll_does_nothing_otherwise():
    config = {'PRODUCTION_MODE': False}

    with patch.dict(current_app.config, config):
        data = {}
        extra_data = {}

        obj = MockObj(data, extra_data)
        eng = MockEng()

        assert wait_webcoll(obj, eng) is None
def test_fuzzy_matcher_run_on_feat_flag_enabled(enable_fuzzy_matcher):
    data = {}
    extra_data = {}

    obj = MockObj(data, extra_data)
    eng = MockEng()

    with patch('inspirehep.modules.workflows.tasks.matching.match') as match:
        fuzzy_match(obj, eng)

        match.assert_called()
Esempio n. 25
0
def test_match_approval_gets_none():
    data = {}
    extra_data = {}
    obj = MockObj(data, extra_data)

    ui_data = {'request_data': {'match_recid': None}}
    result = MatchApproval.resolve(obj, ui_data)

    assert not result
    assert 'fuzzy_match_approved_id' in obj.extra_data
    assert obj.extra_data['fuzzy_match_approved_id'] is None
Esempio n. 26
0
def test_update_ticket_context(data, extra_data, user):
    obj = MockObj(data, extra_data)
    expected = {
        'url': 'http://inspirehep.net/record/123',
        'bibedit_url': 'http://inspirehep.net/record/123/edit',
        'email': '*****@*****.**',
        'user_comment': 'Foo bar',
        'subject': 'Your update to author John Doe on INSPIRE',
    }
    ctx = update_ticket_context(user, obj)
    assert ctx == expected
Esempio n. 27
0
def test_update_ticket_context_handles_unicode(unicode_data, extra_data, user):
    obj = MockObj(unicode_data, extra_data)
    expected = {
        'url': 'http://inspirehep.net/record/123',
        'bibedit_url': 'http://inspirehep.net/record/123/edit',
        'email': '*****@*****.**',
        'user_comment': 'Foo bar',
        'subject': u'Your update to author Diego Martínez on INSPIRE',
    }
    ctx = update_ticket_context(user, obj)
    assert ctx == expected
Esempio n. 28
0
def test_merge_approval():
    data = {}
    extra_data = {}
    obj = MockObj(data, extra_data)
    obj.workflow.name = 'manual_merge'

    result = MergeApproval.resolve(obj)

    assert result
    assert obj.extra_data['approved']
    assert not obj.extra_data['auto-approved']
Esempio n. 29
0
def test_match_approval_gets_match_recid():
    data = {}
    extra_data = {}
    obj = MockObj(data, extra_data)

    ui_data = {'match_recid': 1234}
    result = MatchApproval.resolve(obj, request_data=ui_data)

    assert result
    assert 'fuzzy_match_approved_id' in obj.extra_data
    assert obj.extra_data['fuzzy_match_approved_id'] == 1234
Esempio n. 30
0
def test_match_approval_nothing_sent_via_request():
    data = {}
    extra_data = {}
    obj = MockObj(data, extra_data)

    result = MatchApproval.resolve(obj, None)

    assert not result
    assert 'fuzzy_match_approved_id' in obj.extra_data
    assert obj.extra_data['fuzzy_match_approved_id'] is None
    assert obj.status == ObjectStatus.RUNNING
def test_populate_journal_coverage_does_nothing_if_no_journal_is_found(mock_replace_refs):
    mock_replace_refs.return_value = []

    data = {}
    extra_data = {}

    obj = MockObj(data, extra_data)
    eng = MockEng()

    assert populate_journal_coverage(obj, eng) is None
    assert 'journal_coverage' not in obj.extra_data
def test_send_robotupload_logs_on_error_response():
    httpretty.HTTPretty.allow_net_connect = False
    httpretty.register_uri(
        httpretty.POST,
        'http://inspirehep.net/batchuploader/robotupload/insert',
        body='[ERROR] cannot use the service')

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

    config = {
        'LEGACY_ROBOTUPLOAD_URL': 'http://inspirehep.net',
        'PRODUCTION_MODE': True,
    }

    with patch.dict(current_app.config, config):
        data = {
            'arxiv_eprints': [
                {
                    'categories': [
                        'hep-th',
                    ],
                    'value': 'hep-th/9711200',
                },
            ],
        }
        extra_data = {}
        assert validate(data['arxiv_eprints'], subschema) is None

        obj = MockObj(data, extra_data)
        eng = MockEng()

        _send_robotupload = send_robotupload(
            marcxml_processor=hep2marc,
            mode='insert',
        )

        with pytest.raises(Exception) as excinfo:
            _send_robotupload(obj, eng)

        expected = ('Error while submitting robotupload: '
                    '[ERROR] cannot use the service')
        result = str(excinfo.value)

        assert expected == result

        expected = (
            'Your IP is not in app.config_BATCHUPLOADER_WEB_ROBOT_RIGHTS on host: '
            '[ERROR] cannot use the service')
        result = obj.log._error.getvalue()

        assert expected == result

    httpretty.HTTPretty.allow_net_connect = True