def test_operation_bulk_index(client, es_clear, operation_log_data):
    """Test operation logs bulk creation."""
    data = []
    for date in [
        '2020-01-21T09:51:52.879533+00:00',
        '2020-02-21T09:51:52.879533+00:00',
        '2020-03-21T09:51:52.879533+00:00',
        '2020-04-21T09:51:52.879533+00:00',
        '2021-01-21T09:51:52.879533+00:00',
        '2021-02-21T09:51:52.879533+00:00'
    ]:
        tmp = deepcopy(operation_log_data)
        tmp['date'] = date
        data.append(tmp)
    OperationLog.bulk_index(data)
    # flush the index for the test
    current_search.flush_and_refresh(OperationLog.index_name)
    assert OperationLog.get_indices() == set((
        'operation_logs-2020',
        'operation_logs-2021'
    ))
    with pytest.raises(Exception) as excinfo:
        data[0]['operation'] = dict(name='foo')
        OperationLog.bulk_index(data)
        assert "BulkIndexError" in str(excinfo.value)
    # clean up the index
    assert OperationLog.delete_indices()
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()
Example #3
0
def destroy_operation_logs():
    """Removes all the operation logs data."""
    OperationLog.delete_indices()
    click.secho('All operations logs have been removed', fg='green')