Ejemplo n.º 1
0
def test_operation_log_on_ill_request(client, ill_request_martigny,
                                      librarian_martigny):
    """Test operation log on ILL request."""
    # Using the ``ill_request_martigny`` fixtures, an operation log is created
    # for 'create' operation. Check this operation log to check if special
    # additional informations are include into this OpLog.
    login_user_via_session(client, librarian_martigny.user)

    fake_data = {'date': datetime.now().isoformat()}
    oplg_index = OperationLog.get_index(fake_data)
    flush_index(oplg_index)

    q = f'record.type:illr AND record.value:{ill_request_martigny.pid}'
    es_url = url_for('invenio_records_rest.oplg_list', q=q, sort='mostrecent')
    res = client.get(es_url)
    data = get_json(res)
    assert data['hits']['total']['value'] == 1
    metadata = data['hits']['hits'][0]['metadata']
    assert metadata['operation'] == OperationLogOperation.CREATE
    assert 'ill_request' in metadata
    assert 'status' in metadata['ill_request']
Ejemplo n.º 2
0
def test_operation_log_on_item(client, item_lib_martigny_data_tmp,
                               librarian_martigny, json_header,
                               item_lib_martigny):
    """Test operation log on Item."""
    login_user_via_session(client, librarian_martigny.user)

    # Get the operation log index
    fake_data = {'date': datetime.now().isoformat()}
    oplg_index = OperationLog.get_index(fake_data)

    # STEP #1 : Create an item. This will generate an operation log
    item_data = deepcopy(item_lib_martigny_data_tmp)
    del item_data['pid']
    item = Item.create(item_data, dbcommit=True, reindex=True)
    flush_index(oplg_index)

    q = f'record.type:item AND record.value:{item.pid}'
    es_url = url_for('invenio_records_rest.oplg_list', q=q, sort='mostrecent')
    res = client.get(es_url)
    data = get_json(res)
    assert data['hits']['total']['value'] == 1
    metadata = data['hits']['hits'][0]['metadata']
    assert metadata['operation'] == OperationLogOperation.CREATE

    # STEP #2 : Update the item ``price`` attribute.
    #   As any changes on this attribute must be logged, a new operation log
    #   will be generated.
    item['price'] = 10
    item = item.update(item, dbcommit=True, reindex=True)
    flush_index(oplg_index)

    res = client.get(es_url)
    data = get_json(res)
    assert data['hits']['total']['value'] == 2
    metadata = data['hits']['hits'][0]['metadata']
    assert metadata['operation'] == OperationLogOperation.UPDATE

    # STEP #3 : Update the item ``status`` attribute.
    #   This attribute doesn't need to be tracked. So if it's the only change
    #   on this record then no OpLog should be created.
    item['status'] = ItemStatus.EXCLUDED
    item = item.update(item, dbcommit=True, reindex=True)
    flush_index(oplg_index)

    res = client.get(es_url)
    data = get_json(res)
    assert data['hits']['total']['value'] == 2

    # STEP #4 : Update the item ``status`` and ``price`` attributes.
    #   As we update at least one attribute that need to be tracked, this
    #   update will generate a new OpLog (UPDATE)
    item['status'] = ItemStatus.AT_DESK
    item['price'] = 12
    item = item.update(item, dbcommit=True, reindex=True)
    flush_index(oplg_index)

    res = client.get(es_url)
    data = get_json(res)
    assert data['hits']['total']['value'] == 3
    metadata = data['hits']['hits'][0]['metadata']
    assert metadata['operation'] == OperationLogOperation.UPDATE

    # STEP #5 : Delete the item
    #   This will generate the last OpLog about the item.
    item.delete(dbcommit=True, delindex=True)
    flush_index(oplg_index)

    res = client.get(es_url)
    data = get_json(res)
    assert data['hits']['total']['value'] == 4
    metadata = data['hits']['hits'][0]['metadata']
    assert metadata['operation'] == OperationLogOperation.DELETE