Пример #1
0
def test_view_agents_rero(client, agent_rero_record):
    """Test redirect RERO."""

    AgentReroRecord.create(
        data=agent_rero_record,
        delete_pid=False,
        dbcommit=True,
        reindex=True,
    )
    AgentReroRecord.update_indexes()
    pid = agent_rero_record.get('pid')
    url = url_for('api_agents_rero.redirect_list')
    res = client.get(url)
    assert res.status_code == 308
    res = client.get(url, follow_redirects=True)
    assert res.status_code == 200
    assert json.loads(res.get_data(as_text=True))['aggregations'] == {
        'agent_type': {
            'buckets': [{
                'doc_count': 1,
                'key': 'bf:Person'
            }],
            'doc_count_error_upper_bound': 0,
            'sum_other_doc_count': 0
        },
        'deleted': {
            'doc_count': 0
        }
    }

    url = url_for('api_agents_rero.redirect_item', pid=pid)
    res = client.get(url)
    assert res.status_code == 308
    res = client.get(url, follow_redirects=True)
    assert res.status_code == 200
    data = json.loads(res.get_data(as_text=True))
    assert data.get('id') == pid
    assert data.get('metadata', {}).get('pid') == pid

    url = url_for('api_agents_rero.redirect_item', pid='WRONG')
    res = client.get(url, follow_redirects=True)
    assert res.status_code == 404
    assert json.loads(res.get_data(as_text=True)) == {
        "status": 404,
        "message": "PID does not exist."
    }
Пример #2
0
def test_create_agent_record_no_viaf_links(app, agent_gnd_record,
                                           agent_rero_record,
                                           agent_idref_record):
    """Test create agent record without VIAF links."""
    record, action, m_record, m_action, v_record, online = \
        AgentGndRecord.create_or_update_agent_mef_viaf(
            data=agent_gnd_record,
            dbcommit=True,
            reindex=True,
            online=False
        )
    assert action.name == 'CREATE'
    assert record['pid'] == '12391664X'
    assert m_action.name == 'CREATE'
    assert m_record == {
        '$schema': 'https://mef.rero.ch/schemas/mef/mef-v0.0.1.json',
        'gnd': {
            '$ref': 'https://mef.rero.ch/api/agents/gnd/12391664X'
        },
        'pid': '1',
    }

    record, action, m_record, m_action, v_record, online = \
        AgentReroRecord.create_or_update_agent_mef_viaf(
            data=agent_rero_record,
            dbcommit=True,
            reindex=True,
            online=False
        )
    assert action.name == 'CREATE'
    assert record['pid'] == 'A023655346'
    assert m_action.name == 'CREATE'
    assert m_record == {
        '$schema': 'https://mef.rero.ch/schemas/mef/mef-v0.0.1.json',
        'pid': '2',
        'rero': {
            '$ref': 'https://mef.rero.ch/api/agents/rero/A023655346'
        },
    }

    record, action, m_record, m_action, v_record, online = \
        AgentIdrefRecord.create_or_update_agent_mef_viaf(
            data=agent_idref_record,
            dbcommit=True,
            reindex=True,
            online=False
        )
    assert action.name == 'CREATE'
    assert record['pid'] == '069774331'
    assert m_action.name == 'CREATE'
    assert m_record == {
        '$schema': 'https://mef.rero.ch/schemas/mef/mef-v0.0.1.json',
        'idref': {
            '$ref': 'https://mef.rero.ch/api/agents/idref/069774331'
        },
        'pid': '3',
    }
Пример #3
0
def test_ref_resolvers(app, gnd_record, rero_record, viaf_record,
                       idref_record):
    """Test ref resolvers."""

    # VIAF record
    record, action = ViafRecord.create_or_update(data=viaf_record,
                                                 dbcommit=True,
                                                 reindex=True)
    viaf_pid = record['pid']

    # GND record
    record, action, m_record, m_action, v_record, online = \
        AgentGndRecord.create_or_update_agent_mef_viaf(
            data=gnd_record,
            dbcommit=True,
            reindex=True,
            online=False
        )
    gnd_pid = record.get('pid')

    # RERO record
    record, action, m_record, m_action, v_record, online = \
        AgentReroRecord.create_or_update_agent_mef_viaf(
            data=rero_record,
            dbcommit=True,
            reindex=True,
            online=False
        )
    rero_pid = record.get('pid')

    # IDREF record
    record, action, m_record, m_action, v_record, online = \
        AgentIdrefRecord.create_or_update_agent_mef_viaf(
            data=idref_record,
            dbcommit=True,
            reindex=True,
            online=False
        )
    idref_pid = record.get('pid')

    # MEF record
    mef_rec_resolved = MefRecord.get_mef_by_viaf_pid(viaf_pid=viaf_pid)
    mef_rec_resolved = mef_rec_resolved.replace_refs()
    assert mef_rec_resolved.get('gnd').get('pid') == gnd_pid
    assert mef_rec_resolved.get('rero').get('pid') == rero_pid
    assert mef_rec_resolved.get('idref').get('pid') == idref_pid
Пример #4
0
def test_update_agent_record_with_viaf_links(
        app, viaf_record, gnd_record, rero_record, idref_record):
    """Test create agent record with viaf links."""
    returned_record, action = AgentGndRecord.create_or_update(
        gnd_record, dbcommit=True, reindex=True
    )
    assert action.name == 'UPDATE'
    assert returned_record['pid'] == '12391664X'

    returned_record, action = AgentReroRecord.create_or_update(
        rero_record, dbcommit=True, reindex=True
    )
    assert action.name == 'UPDATE'
    assert returned_record['pid'] == 'A023655346'

    returned_record, action = AgentIdrefRecord.create_or_update(
        idref_record, dbcommit=True, reindex=True
    )
    assert action.name == 'UPDATE'
    assert returned_record['pid'] == '069774331'
Пример #5
0
def test_uptodate_agent_record_with_viaf_links_md5(app, agent_viaf_record,
                                                   agent_gnd_record,
                                                   agent_rero_record,
                                                   agent_idref_record):
    """Test create agent record with VIAF links."""
    returned_record, action = AgentGndRecord.create_or_update(agent_gnd_record,
                                                              dbcommit=True,
                                                              reindex=True,
                                                              test_md5=True)
    assert action.name == 'UPTODATE'
    assert returned_record['pid'] == '12391664X'

    returned_record, action = AgentReroRecord.create_or_update(
        agent_rero_record, dbcommit=True, reindex=True, test_md5=True)
    assert action.name == 'UPTODATE'
    assert returned_record['pid'] == 'A023655346'

    returned_record, action = AgentIdrefRecord.create_or_update(
        agent_idref_record, dbcommit=True, reindex=True, test_md5=True)
    assert action.name == 'UPTODATE'
    assert returned_record['pid'] == '069774331'
Пример #6
0
def test_create_agent_updates(mock_get, app, gnd_record, rero_record,
                              idref_record):
    """Test create agent record with viaf links."""
    # we have to mock the access to viaf
    mock_get.return_value = {
        'pid': '37268949',
        'idref_pid': '069774331',
        'gnd_pid': '100769527xxx'
    }
    # create first record no viaf and mef records exist
    assert MefRecord.count() == 0
    assert ViafRecord.count() == 0
    record, action, m_record, m_action, v_record, online = \
        AgentIdrefRecord.create_or_update_agent_mef_viaf(
            data=idref_record,
            dbcommit=True,
            reindex=True,
            online=True
        )
    assert action.name == 'CREATE'
    assert record['pid'] == '069774331'
    assert MefRecord.count() == 1
    assert ViafRecord.count() == 1
    assert online

    # we should have a mef and viaf record now
    mef_pid = list(MefRecord.get_all_pids())[-1]
    mef_record = MefRecord.get_record_by_pid(mef_pid)
    assert mef_record.get('pid') == '1'
    assert mef_record.get('viaf_pid') == '37268949'
    assert 'idref' in mef_record
    viaf_pid = list(ViafRecord.get_all_pids())[-1]
    viaf_record = ViafRecord.get_record_by_pid(viaf_pid)
    assert viaf_record.get('pid') == '37268949'
    assert viaf_record.get('idref_pid') == '069774331'

    # we have to mock the access to viaf
    mock_get.return_value = {
        'pid': '66739143',
        'gnd_pid': '12391664X',
        'idref_pid': '068979401',
        'rero_pid': 'A023655346'
    }
    # create second record
    record, action, m_record, m_action, v_record, online = \
        AgentGndRecord.create_or_update_agent_mef_viaf(
            data=gnd_record,
            dbcommit=True,
            reindex=True,
            online=True
        )
    mef_pid = list(MefRecord.get_all_pids())[-1]
    mef_record = MefRecord.get_record_by_pid(mef_pid)
    assert mef_record.get('pid') == '2'
    assert mef_record.get('viaf_pid') == '66739143'
    assert 'gnd' in mef_record
    viaf_pid = list(ViafRecord.get_all_pids())[-1]
    viaf_record = ViafRecord.get_record_by_pid(viaf_pid)
    assert viaf_record.get('pid') == '66739143'
    assert viaf_record.get('gnd_pid') == '12391664X'

    record, action, m_record, m_action, v_record, online = \
        AgentReroRecord.create_or_update_agent_mef_viaf(
            data=rero_record,
            dbcommit=True,
            reindex=True,
            online=True
        )

    mef_pid = list(MefRecord.get_all_pids())[-1]
    mef_record = MefRecord.get_record_by_pid(mef_pid)
    assert mef_record.get('pid') == '2'
    assert mef_record.get('viaf_pid') == '66739143'
    assert 'rero' in mef_record
    viaf_pid = list(ViafRecord.get_all_pids())[-1]
    viaf_record = ViafRecord.get_record_by_pid(viaf_pid)
    assert viaf_record.get('pid') == '66739143'
    assert viaf_record.get('rero_pid') == 'A023655346'

    mef_record = MefRecord.get_mef_by_viaf_pid(viaf_pid)
    assert mef_record == {
        'pid': '2',
        'viaf_pid': '66739143',
        '$schema': 'https://mef.rero.ch/schemas/mef/mef-v0.0.1.json',
        'gnd': {
            '$ref': 'https://mef.rero.ch/api/gnd/12391664X'
        },
        'rero': {
            '$ref': 'https://mef.rero.ch/api/rero/A023655346'
        }
    }
    new_viaf_pid = 'xxxxxxxx'
    new_viaf_record = {
        'pid': new_viaf_pid,
        'gnd_pid': '12391664X',
        'idref_pid': '069774331',
        'rero_pid': 'A023655346'
    }
    mock_get.return_value = new_viaf_record
    rec, msg = viaf_record.delete(dbcommit=True, delindex=True, online=True)
    assert ViafRecord.get_record_by_pid(new_viaf_pid) == new_viaf_record
    mef_record = MefRecord.get_mef_by_viaf_pid(new_viaf_pid)
    assert mef_record == {
        'viaf_pid': 'xxxxxxxx',
        'gnd': {
            '$ref': 'https://mef.rero.ch/api/gnd/12391664X'
        },
        'rero': {
            '$ref': 'https://mef.rero.ch/api/rero/A023655346'
        },
        '$schema': 'https://mef.rero.ch/schemas/mef/mef-v0.0.1.json',
        'pid': '3'
    }
Пример #7
0
def test_create_agent_record_with_viaf_links(
        app, viaf_record, gnd_record, rero_record, idref_record):
    """Test create agent record with viaf links."""
    returned_record, action = ViafRecord.create_or_update(
        viaf_record, dbcommit=True, reindex=True
    )
    ViafRecord.update_indexes()
    assert action.name == 'CREATE'
    assert returned_record['pid'] == '66739143'
    assert returned_record['gnd_pid'] == '12391664X'
    assert returned_record['rero_pid'] == 'A023655346'
    assert returned_record['idref_pid'] == '069774331'

    record, action, m_record, m_action, v_record, online = \
        AgentGndRecord.create_or_update_agent_mef_viaf(
            data=gnd_record,
            dbcommit=True,
            reindex=True,
            online=False
        )

    assert action.name == 'CREATE'
    assert record['pid'] == '12391664X'
    assert m_action.name == 'CREATE'
    assert m_record == {
        '$schema':
            'https://mef.rero.ch/schemas/mef/mef-v0.0.1.json',
        'gnd': {'$ref': 'https://mef.rero.ch/api/gnd/12391664X'},
        'pid': '1',
        'viaf_pid': '66739143'
    }

    record, action, m_record, m_action, v_record, online = \
        AgentReroRecord.create_or_update_agent_mef_viaf(
            data=rero_record,
            dbcommit=True,
            reindex=True,
            online=False
        )
    assert action.name == 'CREATE'
    assert record['pid'] == 'A023655346'
    assert m_action.name == 'UPDATE'
    assert m_record == {
        '$schema':
            'https://mef.rero.ch/schemas/mef/mef-v0.0.1.json',
        'gnd': {'$ref': 'https://mef.rero.ch/api/gnd/12391664X'},
        'pid': '1',
        'rero': {'$ref': 'https://mef.rero.ch/api/rero/A023655346'},
        'viaf_pid': '66739143'
    }

    record, action, m_record, m_action, v_record, online = \
        AgentIdrefRecord.create_or_update_agent_mef_viaf(
            data=idref_record,
            dbcommit=True,
            reindex=True,
            online=False
        )
    assert action.name == 'CREATE'
    assert record['pid'] == '069774331'
    assert m_action.name == 'UPDATE'
    assert m_record == {
        '$schema':
            'https://mef.rero.ch/schemas/mef/mef-v0.0.1.json',
        'gnd': {'$ref': 'https://mef.rero.ch/api/gnd/12391664X'},
        'idref': {'$ref': 'https://mef.rero.ch/api/idref/069774331'},
        'pid': '1',
        'rero': {'$ref': 'https://mef.rero.ch/api/rero/A023655346'},
        'viaf_pid': '66739143'
    }
Пример #8
0
def test_create_agent_record_with_viaf_links(app, agent_viaf_record,
                                             agent_gnd_record,
                                             agent_rero_record,
                                             agent_idref_record):
    """Test create agent record with VIAF links."""
    viaf_record, action = AgentViafRecord.create_or_update(agent_viaf_record,
                                                           dbcommit=True,
                                                           reindex=True)
    AgentViafRecord.update_indexes()
    assert action.name == 'CREATE'
    assert viaf_record['pid'] == '66739143'
    assert viaf_record['gnd_pid'] == '12391664X'
    assert viaf_record['rero_pid'] == 'A023655346'
    assert viaf_record['idref_pid'] == '069774331'

    record, action, m_record, m_action, v_record, online = \
        AgentGndRecord.create_or_update_agent_mef_viaf(
            data=agent_gnd_record,
            dbcommit=True,
            reindex=True,
            online=False
        )

    assert action.name == 'CREATE'
    assert record['pid'] == '12391664X'
    assert m_action.name == 'CREATE'
    assert m_record == {
        '$schema': 'https://mef.rero.ch/schemas/mef/mef-v0.0.1.json',
        'gnd': {
            '$ref': 'https://mef.rero.ch/api/agents/gnd/12391664X'
        },
        'pid': '1',
        'viaf_pid': '66739143'
    }

    record, action, m_record, m_action, v_record, online = \
        AgentReroRecord.create_or_update_agent_mef_viaf(
            data=agent_rero_record,
            dbcommit=True,
            reindex=True,
            online=False
        )
    assert action.name == 'CREATE'
    assert record['pid'] == 'A023655346'
    assert m_action.name == 'UPDATE'
    assert m_record == {
        '$schema': 'https://mef.rero.ch/schemas/mef/mef-v0.0.1.json',
        'gnd': {
            '$ref': 'https://mef.rero.ch/api/agents/gnd/12391664X'
        },
        'pid': '1',
        'rero': {
            '$ref': 'https://mef.rero.ch/api/agents/rero/A023655346'
        },
        'viaf_pid': '66739143'
    }

    record, action, m_record, m_action, v_record, online = \
        AgentIdrefRecord.create_or_update_agent_mef_viaf(
            data=agent_idref_record,
            dbcommit=True,
            reindex=True,
            online=False
        )
    assert action.name == 'CREATE'
    assert record['pid'] == '069774331'
    assert m_action.name == 'UPDATE'
    assert m_record == {
        '$schema': 'https://mef.rero.ch/schemas/mef/mef-v0.0.1.json',
        'gnd': {
            '$ref': 'https://mef.rero.ch/api/agents/gnd/12391664X'
        },
        'idref': {
            '$ref': 'https://mef.rero.ch/api/agents/idref/069774331'
        },
        'pid': '1',
        'rero': {
            '$ref': 'https://mef.rero.ch/api/agents/rero/A023655346'
        },
        'viaf_pid': '66739143'
    }

    assert m_record == AgentMefRecord.get_mef_by_entity_pid(
        record.pid, 'idref')
    assert m_record.pid == AgentMefRecord.get_mef_by_entity_pid(record.pid,
                                                                'idref',
                                                                pid_only=True)
    assert AgentMefRecord.get_mef_by_viaf_pid(viaf_record.pid) == m_record