コード例 #1
0
def test_delete_graph():
    # Test delete graph from the last test
    result = db.delete_graph(graph_name)
    assert result is True
    assert graph_name not in db.graphs()

    # Test delete missing graph
    with pytest.raises(GraphDeleteError):
        db.delete_graph(graph_name)

    # Test delete missing graph (ignore_missing)
    result = db.delete_graph(graph_name, ignore_missing=True)
    assert result is False

    major, minor = arango_version(arango_client)

    if major == 3 and minor >= 1:
        # Create a graph with vertex and edge collections and delete them all
        new_graph_name = generate_graph_name()
        graph = db.create_graph(new_graph_name)
        vcol_name_1 = generate_col_name()
        graph.create_vertex_collection(vcol_name_1)
        vcol_name_2 = generate_col_name()
        graph.create_vertex_collection(vcol_name_2)
        ecol_name = generate_col_name()
        graph.create_edge_definition(name=ecol_name,
                                     from_collections=[vcol_name_1],
                                     to_collections=[vcol_name_2])
        collections = set(col['name'] for col in db.collections())
        assert vcol_name_1 in collections
        assert vcol_name_2 in collections
        assert ecol_name in collections

        db.delete_graph(new_graph_name)
        collections = set(col['name'] for col in db.collections())
        assert vcol_name_1 in collections
        assert vcol_name_2 in collections
        assert ecol_name in collections

        graph = db.create_graph(new_graph_name)
        graph.create_edge_definition(name=ecol_name,
                                     from_collections=[vcol_name_1],
                                     to_collections=[vcol_name_2])
        db.delete_graph(new_graph_name, drop_collections=True)
        collections = set(col['name'] for col in db.collections())
        assert vcol_name_1 not in collections
        assert vcol_name_2 not in collections
        assert ecol_name not in collections
コード例 #2
0
def test_get_user_access_collection_level():
    # Create a new test user
    username = generate_user_name()
    db.create_user(username=username, password='******')
    arango_client.database(name=db_name,
                           username=username,
                           password='******')
    col_name = generate_col_name()
    col = db.create_collection(col_name)

    # The new user should have no access to the collection
    assert col.user_access(username) is None

    # After granting access to the collection it should work
    col.grant_user_access(username)
    assert col.user_access(username) == 'rw'

    # Revoke the access again to see that 401 is back
    col.revoke_user_access(username)
    assert col.user_access(username) is None

    # Grant access from a bad database (missing user)
    with pytest.raises(ArangoError) as err:
        bad_db.collection(col_name).user_access(generate_user_name())
    assert err.value.http_code in HTTP_AUTH_ERR
    assert isinstance(err.value, UserAccessError) \
        or isinstance(err.value, AsyncExecuteError) \
        or isinstance(err.value, BatchExecuteError)
コード例 #3
0
def test_revoke_user_access_collection_level():
    # Create a new test user
    username = generate_user_name()
    db.create_user(username=username, password='******')
    user_db = arango_client.database(name=db_name,
                                     username=username,
                                     password='******')
    col_name = generate_col_name()
    col = db.create_collection(col_name)

    # The new user should have no access to the collection
    with pytest.raises(ArangoError) as err:
        user_db.collection(col_name).count()
    assert err.value.http_code in HTTP_AUTH_ERR

    # After granting access to the collection it should work
    col.grant_user_access(username)
    # TODO enable again once the bug is fixed on ArangoDB side
    # assert user_db.collection(col_name).count() == 0

    # Revoke the access again to see that 401 is back
    col.revoke_user_access(username)
    with pytest.raises(ArangoError) as err:
        user_db.collection(col_name).count()
    assert err.value.http_code in HTTP_AUTH_ERR

    # Grant access from a bad database (missing user)
    with pytest.raises(ArangoError) as err:
        bad_db.collection(col_name).revoke_user_access(generate_user_name())
    assert err.value.http_code in HTTP_AUTH_ERR
    assert isinstance(err.value, UserRevokeAccessError) \
        or isinstance(err.value, AsyncExecuteError) \
        or isinstance(err.value, BatchExecuteError)
コード例 #4
0
def test_grant_user_access_db_level():
    # Create a test user and login as that user
    username = generate_user_name()
    db.create_user(username=username, password='******')
    user_db = arango_client.database(name=db_name,
                                     username=username,
                                     password='******')

    # Create a collection with the user (should have no access)
    col_name = generate_col_name()
    with pytest.raises(CollectionCreateError) as err:
        user_db.create_collection(col_name)
    assert err.value.http_code in HTTP_AUTH_ERR
    assert col_name not in set(col['name'] for col in db.collections())

    # Grant the user access and try again
    db.grant_user_access(username)
    db.create_collection(col_name)
    assert col_name in set(col['name'] for col in db.collections())

    # Grant access to a missing user
    bad_username = generate_user_name()
    with pytest.raises(UserGrantAccessError) as err:
        db.grant_user_access(bad_username)
    assert err.value.http_code == 404
コード例 #5
0
def test_revoke_user_access_db_level():
    # Create a test user with access and login as that user
    username = generate_user_name()
    db.create_user(username=username, password='******')
    db.grant_user_access(username, db_name)
    user_db = arango_client.database(name=db_name,
                                     username=username,
                                     password='******')

    # Test user access by creating a collection
    col_name = generate_col_name()
    user_db.create_collection(col_name)
    assert col_name in set(col['name'] for col in db.collections())

    # Revoke access from the user
    db.revoke_user_access(username)
    with pytest.raises(CollectionDeleteError) as err:
        user_db.delete_collection(col_name)
    assert err.value.http_code in HTTP_AUTH_ERR

    # Test revoke access to missing user
    bad_username = generate_user_name()
    with pytest.raises(UserRevokeAccessError) as err:
        db.revoke_user_access(bad_username)
    assert err.value.http_code == 404
コード例 #6
0
def test_rename():
    assert col.name == col_name
    new_name = generate_col_name()
    while new_name == bad_col_name:
        new_name = generate_col_name()

    # Test rename collection
    result = col.rename(new_name)
    assert result['name'] == new_name
    assert col.name == new_name
    assert repr(col) == '<ArangoDB collection "{}">'.format(new_name)

    # Try again (the operation should be idempotent)
    result = col.rename(new_name)
    assert result['name'] == new_name
    assert col.name == new_name
    assert repr(col) == '<ArangoDB collection "{}">'.format(new_name)

    with pytest.raises(CollectionRenameError):
        bad_col.rename(new_name)
コード例 #7
0
def test_execute_with_errors():
    txn = db.transaction(write=col_name)
    bad_col_name = generate_col_name()
    with pytest.raises(TransactionError):
        txn.execute(
            command='''
                function (params) {{
                var db = require('internal').db;
                db.{col}.save({{ '_key': '1', 'val': params.one }});
                db.{col}.save({{ '_key': '2', 'val': params.two }});
                return 'this transaction should fail!';
            }}'''.format(col=bad_col_name),
            params={'one': 3, 'two': 4}
        )
コード例 #8
0
def test_replace_match():
    # Set up test edges
    ecol.insert_many(test_edges)

    # Test replace single matching document with invalid body
    with pytest.raises(DocumentReplaceError):
        ecol.replace_match({'_key': '3'}, {'_to': edge2['_to']})

    # Test replace single matching document with valid body
    assert ecol.replace_match(
        {'_key': '3'},
        {'_to': edge2['_to'], '_from': edge3['_from']}
    ) == 1
    assert ecol['3']['_to'] == edge2['_to']
    assert ecol['3']['_from'] == edge3['_from']

    # Test replace multiple matching documents
    assert ecol.replace_match(
        {'_from': col_name + '/1'},
        {'_to': edge1['_to'], '_from': edge1['_from'], 'foo': 'bar'}
    ) == 2
    assert ecol['1']['foo'] == 'bar'
    assert ecol['1']['_to'] == edge1['_to']
    assert ecol['1']['_from'] == edge1['_from']

    assert ecol['4']['foo'] == 'bar'

    # Test replace multiple matching documents with arguments
    assert ecol.replace_match(
        {'_from': col_name + '/1'},
        {'_to': edge3['_to'], '_from': edge3['_from'], 'foo': 'baz'},
        limit=1,
        sync=True,
    ) == 1
    assert ecol['1']['foo'] == 'baz'
    assert ecol['4']['foo'] == 'bar'

    # Test unaffected document
    assert ecol['2']['_to'] == edge2['_to']
    assert 'foo' not in ecol['2']

    # Test replace matching documents in missing collection
    bad_ecol_name = generate_col_name()
    with pytest.raises(DocumentReplaceError):
        bad_ecol = db.collection(bad_ecol_name)
        bad_ecol.replace_match({'_key': '1'}, {'foo': 100})
コード例 #9
0
def test_update_match():
    # Set up test edges
    ecol.insert_many(test_edges)

    # Test update single matching document
    assert ecol.update_match(
        {'_key': '1'},
        {'_to': 'foo'}
    ) == 1
    assert ecol['1']['_to'] == 'foo'
    assert ecol['1']['_from'] == edge1['_from']

    # Test update multiple matching documents
    assert ecol.update_match(
        {'_from': col_name + '/1'},
        {'foo': 'bar'}
    ) == 2
    assert ecol['1']['foo'] == 'bar'
    assert ecol['4']['foo'] == 'bar'

    # Test update multiple matching documents with arguments
    assert ecol.update_match(
        {'_from': col_name + '/1'},
        {'foo': None, 'bar': 'baz'},
        limit=1,
        sync=True,
        keep_none=False
    ) == 1
    assert ecol['1']['foo'] == 'bar'
    assert 'foo' not in ecol['4']
    assert ecol['4']['bar'] == 'baz'

    # Test unaffected document
    assert ecol['2']['_to'] == edge2['_to']
    assert 'foo' not in ecol['2']

    # Test update matching documents in missing collection
    bad_ecol_name = generate_col_name()
    with pytest.raises(DocumentUpdateError):
        bad_ecol = db.collection(bad_ecol_name)
        bad_ecol.update_match({'_key': '1'}, {'foo': 100})
コード例 #10
0
def test_create_collection():
    global col_name_2

    # Test create duplicate collection
    with pytest.raises(CollectionCreateError):
        db.create_collection(col_name_1)

    # Test create collection with parameters
    col_name_2 = generate_col_name()
    col = db.create_collection(name=col_name_2,
                               sync=True,
                               compact=False,
                               journal_size=7774208,
                               system=False,
                               volatile=False,
                               key_generator='autoincrement',
                               user_keys=False,
                               key_increment=9,
                               key_offset=100,
                               edge=True,
                               shard_count=2,
                               shard_fields=['test_attr'],
                               index_bucket_count=10,
                               replication_factor=1)
    properties = col.properties()
    assert 'id' in properties
    assert properties['name'] == col_name_2
    assert properties['sync'] is True
    assert properties['compact'] is False
    assert properties['journal_size'] == 7774208
    assert properties['system'] is False
    assert properties['volatile'] is False
    assert properties['edge'] is True
    assert properties['keygen'] == 'autoincrement'
    assert properties['user_keys'] is False
    assert properties['key_increment'] == 9
    assert properties['key_offset'] == 100
コード例 #11
0
def test_delete():
    # Set up test edges
    ecol.import_bulk(test_edges)

    # Test delete (edge) with default options
    result = ecol.delete(edge1)
    assert result['_id'] == '{}/{}'.format(ecol.name, edge1['_key'])
    assert result['_key'] == edge1['_key']
    assert isinstance(result['_rev'], string_types)
    assert result['sync'] is False
    assert 'old' not in result
    assert edge1['_key'] not in ecol
    assert len(ecol) == 4

    # Test delete (edge key) with default options
    result = ecol.delete(edge2['_key'])
    assert result['_id'] == '{}/{}'.format(ecol.name, edge2['_key'])
    assert result['_key'] == edge2['_key']
    assert isinstance(result['_rev'], string_types)
    assert result['sync'] is False
    assert 'old' not in result
    assert edge2['_key'] not in ecol
    assert len(ecol) == 3

    # Test delete (edge) with return_old
    result = ecol.delete(edge3, return_old=True)
    assert result['_id'] == '{}/{}'.format(ecol.name, edge3['_key'])
    assert result['_key'] == edge3['_key']
    assert isinstance(result['_rev'], string_types)
    assert result['sync'] is False
    assert result['old']['_key'] == edge3['_key']
    assert result['old']['_to'] == edge3['_to']
    assert result['old']['_from'] == edge3['_from']
    assert edge3['_key'] not in ecol
    assert len(ecol) == 2

    # Test delete (edge key) with sync
    result = ecol.delete(edge4, sync=True)
    assert result['_id'] == '{}/{}'.format(ecol.name, edge4['_key'])
    assert result['_key'] == edge4['_key']
    assert isinstance(result['_rev'], string_types)
    assert result['sync'] is True
    assert edge4['_key'] not in ecol
    assert len(ecol) == 1

    # Test delete (edge) with check_rev
    rev = ecol[edge5['_key']]['_rev'] + '000'
    bad_edge = edge5.copy()
    bad_edge.update({'_rev': rev})
    with pytest.raises(ArangoError):
        ecol.delete(bad_edge, check_rev=True)
    assert bad_edge['_key'] in ecol
    assert len(ecol) == 1

    # Test delete (edge) with check_rev
    assert ecol.delete(edge4, ignore_missing=True) is False
    with pytest.raises(DocumentDeleteError):
        ecol.delete(edge4, ignore_missing=False)
    assert len(ecol) == 1

    # Test delete with missing edge collection
    bad_col = generate_col_name()
    with pytest.raises(DocumentDeleteError):
        db.collection(bad_col).delete(edge5)

    bad_col = generate_col_name()
    with pytest.raises(DocumentDeleteError):
        db.collection(bad_col).delete(edge5['_key'])
コード例 #12
0
    DocumentDeleteError,
    DocumentInsertError,
    DocumentReplaceError,
    DocumentRevisionError,
    DocumentUpdateError,
)

from tests.utils import (
    generate_db_name,
    generate_col_name,
)

arango_client = ArangoClient()
db_name = generate_db_name()
db = arango_client.create_database(db_name)
ecol_name = generate_col_name()
ecol = db.create_collection(ecol_name, edge=True)
ecol.add_geo_index(['coordinates'])

# Set up test collection and edges
col_name = generate_col_name()
db.create_collection(col_name).import_bulk([
    {'_key': '1'}, {'_key': '2'}, {'_key': '3'}, {'_key': '4'}, {'_key': '5'}
])
edge1 = {'_key': '1', '_from': col_name + '/1', '_to': col_name + '/2'}
edge2 = {'_key': '2', '_from': col_name + '/2', '_to': col_name + '/3'}
edge3 = {'_key': '3', '_from': col_name + '/3', '_to': col_name + '/4'}
edge4 = {'_key': '4', '_from': col_name + '/1', '_to': col_name + '/1'}
edge5 = {'_key': '5', '_from': col_name + '/5', '_to': col_name + '/3'}
test_edges = [edge1, edge2, edge3, edge4, edge5]
test_edge_keys = [e['_key'] for e in test_edges]
コード例 #13
0
    ServerRoleError,
    ServerSleepError,
    ServerStatisticsError,
    ServerTimeError,
    ServerVersionError,
)

from tests.utils import (generate_db_name, generate_col_name,
                         generate_graph_name, arango_version)

arango_client = ArangoClient()
db_name = generate_db_name()
db = arango_client.create_database(db_name)
bad_db_name = generate_db_name()
bad_db = arango_client.db(bad_db_name)
col_name_1 = generate_col_name()
col_name_2 = ''
db.create_collection(col_name_1)
graph_name = generate_graph_name()
db.create_graph(graph_name)


def teardown_module(*_):
    arango_client.delete_database(db_name, ignore_missing=True)


@pytest.mark.order1
def test_properties():
    assert db.name == db_name
    assert repr(db) == '<ArangoDB database "{}">'.format(db_name)
コード例 #14
0
from arango import ArangoClient
from arango.aql import AQL
from arango.collections.standard import Collection
from arango.exceptions import ArangoError
from arango.graph import Graph

from tests.utils import (
    generate_db_name,
    generate_col_name,
    generate_graph_name
)

arango_client = ArangoClient()
db_name = generate_db_name()
db = arango_client.create_database(db_name)
col_name = generate_col_name()
col = db.create_collection(col_name)
graph_name = generate_graph_name()
graph = db.create_graph(graph_name)
vcol_name = generate_col_name()
graph.create_vertex_collection(vcol_name)


def teardown_module(*_):
    arango_client.delete_database(db_name, ignore_missing=True)


@pytest.mark.order1
def test_async_object():
    cluster = db.cluster(
        shard_id=1,
コード例 #15
0
def test_delete_many():
    # Set up test edges
    current_revs = {}
    edges = [edge.copy() for edge in test_edges]

    # Test delete_many (edges) with default options
    ecol.import_bulk(edges)
    results = ecol.delete_many(edges)
    for result, key in zip(results, test_edge_keys):
        assert result['_id'] == '{}/{}'.format(ecol.name, key)
        assert result['_key'] == key
        assert isinstance(result['_rev'], string_types)
        assert result['sync'] is False
        assert 'old' not in result
        assert key not in ecol
        current_revs[key] = result['_rev']
    assert len(ecol) == 0

    # Test delete_many (edge keys) with default options
    ecol.import_bulk(edges)
    results = ecol.delete_many(edges)
    for result, key in zip(results, test_edge_keys):
        assert result['_id'] == '{}/{}'.format(ecol.name, key)
        assert result['_key'] == key
        assert isinstance(result['_rev'], string_types)
        assert result['sync'] is False
        assert 'old' not in result
        assert key not in ecol
        current_revs[key] = result['_rev']
    assert len(ecol) == 0

    # Test delete_many (edges) with return_old
    ecol.import_bulk(edges)
    results = ecol.delete_many(edges, return_old=True)
    for result, edge in zip(results, edges):
        key = edge['_key']
        assert result['_id'] == '{}/{}'.format(ecol.name, key)
        assert result['_key'] == key
        assert isinstance(result['_rev'], string_types)
        assert result['sync'] is False
        assert result['old']['_key'] == key
        assert result['old']['_to'] == edge['_to']
        assert result['old']['_from'] == edge['_from']
        assert key not in ecol
        current_revs[key] = result['_rev']
    assert len(ecol) == 0

    # Test delete_many (edge keys) with sync
    ecol.import_bulk(edges)
    results = ecol.delete_many(edges, sync=True)
    for result, edge in zip(results, edges):
        key = edge['_key']
        assert result['_id'] == '{}/{}'.format(ecol.name, key)
        assert result['_key'] == key
        assert isinstance(result['_rev'], string_types)
        assert result['sync'] is True
        assert 'old' not in result
        assert key not in ecol
        current_revs[key] = result['_rev']
    assert len(ecol) == 0

    # Test delete_many (edges) with check_rev
    ecol.import_bulk(edges)
    for edge in edges:
        edge['_rev'] = current_revs[edge['_key']] + '000'
    results = ecol.delete_many(edges, check_rev=True)
    for result, edge in zip(results, edges):
        assert isinstance(result, DocumentRevisionError)
    assert len(ecol) == 5

    # Test delete_many (edges) with missing edges
    ecol.truncate()
    results = ecol.delete_many([{'_key': '6'}, {'_key': '7'}])
    for result, edge in zip(results, edges):
        assert isinstance(result, DocumentDeleteError)
    assert len(ecol) == 0

    # Test delete_many with missing edge collection
    bad_ecol = generate_col_name()
    with pytest.raises(DocumentDeleteError):
        db.collection(bad_ecol).delete_many(edges)

    bad_ecol = generate_col_name()
    with pytest.raises(DocumentDeleteError):
        db.collection(bad_ecol).delete_many(test_edge_keys)
コード例 #16
0
from __future__ import absolute_import, unicode_literals

import pytest

from arango import ArangoClient
from arango.exceptions import (IndexListError, IndexCreateError,
                               IndexDeleteError)

from tests.utils import (generate_db_name, generate_col_name)

arango_client = ArangoClient()
db_name = generate_db_name()
db = arango_client.create_database(db_name)
col_name = generate_col_name()
col = db.create_collection(col_name)
bad_col_name = generate_col_name()
bad_col = db.collection(bad_col_name)
col.add_geo_index(['coordinates'])


def teardown_module(*_):
    arango_client.delete_database(db_name, ignore_missing=True)


def setup_function(*_):
    col.truncate()


def test_list_indexes():
    expected_index = {
        'id': '0',
コード例 #17
0
from arango import ArangoClient
from arango.exceptions import (PregelJobCreateError, PregelJobGetError,
                               PregelJobDeleteError)

from tests.utils import (
    generate_db_name,
    generate_col_name,
    generate_graph_name,
)

arango_client = ArangoClient()
db_name = generate_db_name()
db = arango_client.create_database(db_name)
graph_name = generate_graph_name()
graph = db.create_graph(graph_name)
from_col_name = generate_col_name()
to_col_name = generate_col_name()
edge_col_name = generate_col_name()
graph.create_vertex_collection(from_col_name)
graph.create_vertex_collection(to_col_name)
graph.create_edge_definition(edge_col_name, [from_col_name], [to_col_name])


def teardown_module(*_):
    arango_client.delete_database(db_name, ignore_missing=True)


@pytest.mark.order1
def test_start_pregel_job():
    # Test start_pregel_job with page rank algorithm (happy path)
    job_id = db.create_pregel_job('pagerank', graph_name)