def test_operation_log_es_mapping(item_lib_sion, operation_log_1_data):
    """Test operation log elasticsearch mapping."""
    search = OperationLogsSearch()
    mapping = get_mapping(search.Meta.index)
    assert mapping
    OperationLog.create(
        operation_log_1_data,
        dbcommit=True,
        reindex=True,
        delete_pid=True)
    assert mapping == get_mapping(search.Meta.index)

    count = search.query(
        'query_string', query=OperationLogOperation.CREATE
    ).count()
    assert count == 3

    count = search.query(
        'query_string', query=OperationLogOperation.UPDATE
    ).count()
    assert count == 4

    count = search.query(
        'match',
        **{'user_name': 'updated_user'}).\
        count()
    assert count == 1
示例#2
0
文件: cli.py 项目: zannkukai/rero-ils
def migrate_virtua_operation_logs(infile, verbose, debug, lazy):
    """Migrate Virtua operation log records in reroils.

    :param infile: Json operation log file.
    :param lazy: lazy reads file
    """
    enabled_logs = current_app.config.get('RERO_ILS_ENABLE_OPERATION_LOG')
    click.secho('Migrate Virtua operation log records:', fg='green')
    if lazy:
        # try to lazy read json file (slower, better memory management)
        data = read_json_record(infile)
    else:
        # load everything in memory (faster, bad memory management)
        data = json.load(infile)
    index_count = 0
    with click.progressbar(data) as bar:
        for oplg in bar:
            try:
                operation = oplg.get('operation')
                resource = extracted_data_from_ref(
                    oplg.get('record').get('$ref'), data='resource')
                pid_type = enabled_logs.get(resource)
                if pid_type and operation == OperationLogOperation.CREATE:
                    # The virtua create operation log overrides the reroils
                    # create operation log, the method to use is UPDATE
                    record_pid = extracted_data_from_ref(
                        oplg.get('record').get('$ref'), data='pid')

                    create_rec = \
                        OperationLog.get_create_operation_log_by_resource_pid(
                            pid_type, record_pid)
                    if create_rec:
                        create_rec.update(oplg, dbcommit=True, reindex=True)
                elif pid_type and operation == OperationLogOperation.UPDATE:
                    # The virtua update operation log is a new entry in the
                    # reroils operation log, the method to use is CREATE
                    OperationLog.create(data=oplg, dbcommit=True, reindex=True)
            except Exception:
                pass
        index_count += len(data)
    click.echo(f'created {index_count} operation logs.')
def test_operation_create(client, es_clear, operation_log_data):
    """Test operation logs creation."""
    oplg = OperationLog.create(operation_log_data, index_refresh='wait_for')
    assert oplg
    assert oplg.id
    # need to compare with dumps as it has resolve $refs
    data = OperationLog.get_record(oplg.id)
    del data['_created']
    del data['_updated']
    assert data == OperationLog(operation_log_data).dumps()
    tmp = deepcopy(operation_log_data)
    tmp['date'] = '2020-01-21T09:51:52.879533+00:00'
    oplg2 = OperationLog.create(tmp, index_refresh='wait_for')
    assert OperationLog.get_indices() == set((
        'operation_logs-2020',
        f'operation_logs-{datetime.now().year}'
    ))
    assert OperationLog.get_record(oplg.id)
    assert OperationLog.get_record(oplg2.id)
    # clean up the index
    assert OperationLog.delete_indices()
def test_update(app, es_clear, operation_log_data, monkeypatch):
    """Test update log."""
    operation_log = OperationLog.create(deepcopy(operation_log_data),
                                        index_refresh='wait_for')

    log_data = OperationLog.get_record(operation_log.id)
    assert log_data['record']['value'] == 'item4'

    # Update OK
    log_data['record']['value'] = '1234'
    OperationLog.update(log_data.id, log_data['date'], log_data)
    log_data = OperationLog.get_record(operation_log.id)
    assert log_data['record']['value'] == '1234'

    # Update KO
    monkeypatch.setattr(
        'elasticsearch_dsl.Document.update', lambda *args, **kwargs: 'error')
    with pytest.raises(Exception) as exception:
        OperationLog.update(log_data.id, log_data['date'], log_data)
        assert str(exception) == 'Operation log cannot be updated.'
def test_operation_logs_es_mapping(db, item_lib_sion, operation_log_1_data):
    """Test operation logs elasticsearch mapping."""
    search = OperationLogsSearch()
    mapping = get_mapping(search.Meta.index)
    assert mapping
    oplg = OperationLog.create(operation_log_1_data, dbcommit=True,
                               reindex=True, delete_pid=True)
    flush_index(OperationLogsSearch.Meta.index)
    assert mapping == get_mapping(search.Meta.index)

    assert oplg == operation_log_1_data
    assert oplg.get('pid') == '7'

    oplg = OperationLog.get_record_by_pid('7')
    assert oplg == operation_log_1_data

    fetched_pid = fetcher(oplg.id, oplg)
    assert fetched_pid.pid_value == '7'
    assert fetched_pid.pid_type == 'oplg'

    assert oplg.get('operation') == OperationLogOperation.UPDATE
示例#6
0
def test_operation_log_es_mapping(item_lib_sion, operation_log_data):
    """Test operation log elasticsearch mapping."""
    mapping = get_mapping(OperationLog.index_name)
    assert mapping
    OperationLog.create(operation_log_data)
    assert mapping == get_mapping(OperationLog.index_name)
示例#7
0
def operation_log(operation_log_data, item_lib_sion):
    """Load operation log record."""
    return OperationLog.create(operation_log_data, index_refresh=True)