Ejemplo n.º 1
0
 def test_marshall_edge(self):
     from grapheekdb.backends.data.localmem import LocalMemoryGraph
     from grapheekdb.client.api import ProxyEdge
     graph = LocalMemoryGraph()
     node1 = graph.add_node(foo=1)
     node2 = graph.add_node(foo=2)
     edge = graph.add_edge(node1, node2, foo=3)
     entity = unmarshall(graph, marshall(edge))
     assert (isinstance(entity, ProxyEdge))
     assert (entity.foo == 3)
Ejemplo n.º 2
0
class TModifiedBackendLeadToError(FillMethod):
    def setup(self):
        from grapheekdb.backends.data.localmem import LocalMemoryGraph
        self.graph = LocalMemoryGraph()
        self.fill()

    def test_create_index_node(self):
        # Manually modifying backend
        txn = self.graph._transaction_begin()
        try:
            self.graph._remove(txn, METADATA_VERTEX_COUNTER)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.graph.add_node_index('foo')
        except GrapheekIndexCreationFailedException:
            exception_raised = True
        assert (exception_raised)

    def test_remove_index_node(self):
        from grapheekdb.backends.data.indexes import normalize_value
        self.graph.add_node_index('foo')
        txn = self.graph._transaction_begin()
        args = ['foo']
        kwargs = {}
        index_signature = [args, list(kwargs.items())]
        index_signature_string = normalize_value(index_signature)
        index_key = '/'.join([METADATA_VERTEX_INDEX_PREFIX] +
                             [index_signature_string])
        try:
            self.graph._remove(txn, index_key)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.graph.remove_node_index('foo')
        except GrapheekIndexRemovalFailedException:
            exception_raised = True
        assert (exception_raised)

    def test_create_index_edge(self):
        # Manually modifying backend
        txn = self.graph._transaction_begin()
        try:
            self.graph._remove(txn, METADATA_EDGE_COUNTER)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.graph.add_edge_index('foo')
        except GrapheekIndexCreationFailedException:
            exception_raised = True
        assert (exception_raised)

    def test_remove_index_edge(self):
        from grapheekdb.backends.data.indexes import normalize_value
        self.graph.add_edge_index('foo')
        txn = self.graph._transaction_begin()
        args = ['foo']
        kwargs = {}
        index_signature = [args, list(kwargs.items())]
        index_signature_string = normalize_value(index_signature)
        index_key = '/'.join([METADATA_EDGE_INDEX_PREFIX] +
                             [index_signature_string])
        try:
            # Manually modifying backend
            self.graph._remove(txn, index_key)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.graph.remove_edge_index('foo')
        except GrapheekIndexRemovalFailedException:
            exception_raised = True
        assert (exception_raised)

    def test_add_node(self):
        # Manually modifying backend
        txn = self.graph._transaction_begin()
        try:
            self.graph._remove(txn, METADATA_VERTEX_COUNTER)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.graph.add_node(foo=3)
        except GrapheekDataException:
            exception_raised = True
        assert (exception_raised)

    def test_remove_node(self):
        # Manually modifying backend
        from grapheekdb.backends.data.keys import KIND_VERTEX, OUT_EDGES_SUFFIX
        from grapheekdb.backends.data.keys import build_key
        txn = self.graph._transaction_begin()
        try:
            self.graph._remove(
                txn, build_key(KIND_VERTEX, self.n1.get_id(),
                               OUT_EDGES_SUFFIX))
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.n1.remove()
        except GrapheekDataException:
            exception_raised = True
        assert (exception_raised)

    def test_update_node_data(self):
        # Manually modifying backend
        from grapheekdb.backends.data.keys import KIND_VERTEX, DATA_SUFFIX
        from grapheekdb.backends.data.keys import build_key
        txn = self.graph._transaction_begin()
        try:
            self.graph._remove(
                txn, build_key(KIND_VERTEX, self.n1.get_id(), DATA_SUFFIX))
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.n1.foobar = 3
        except GrapheekDataException:
            exception_raised = True
        assert (exception_raised)

    def test_add_edge(self):
        # Manually modifying backend
        txn = self.graph._transaction_begin()
        try:
            self.graph._remove(txn, METADATA_EDGE_COUNTER)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.graph.add_edge(self.n1, self.n2)
        except GrapheekDataException:
            exception_raised = True
        assert (exception_raised)

    def test_remove_edge(self):
        # Manually modifying backend
        from grapheekdb.backends.data.keys import KIND_EDGE, OUT_VERTICES_SUFFIX
        from grapheekdb.backends.data.keys import build_key
        txn = self.graph._transaction_begin()
        try:
            self.graph._remove(
                txn, build_key(KIND_EDGE, self.e1.get_id(),
                               OUT_VERTICES_SUFFIX))
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.e1.remove()
        except GrapheekDataException:
            exception_raised = True
        assert (exception_raised)

    def test_update_edge_data(self):
        # Manually modifying backend
        from grapheekdb.backends.data.keys import KIND_EDGE, DATA_SUFFIX
        from grapheekdb.backends.data.keys import build_key
        txn = self.graph._transaction_begin()
        try:
            self.graph._remove(
                txn, build_key(KIND_EDGE, self.e1.get_id(), DATA_SUFFIX))
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.e1.foobar = 3
        except GrapheekDataException:
            exception_raised = True
        assert (exception_raised)
Ejemplo n.º 3
0
from grapheekdb.backends.data.localmem import LocalMemoryGraph
g = LocalMemoryGraph()

raf = g.add_node(name='Raphael')
grapheekdb = g.add_node(name='Grapheekdb')
python = g.add_node(name='Python')
localmem = g.add_node(name='Local Memory')
kyoto = g.add_node(name='Kyoto Cabinet')
lmdb = g.add_node(name='Symas LMDB')
gpl = g.add_node(name='GPL V3')
fr = g.add_node(name='french')
persistent = g.add_node(name='persistent')
kvs = g.add_node(name='Key Value Store')

g.add_edge(raf, grapheekdb, action='created')
g.add_edge(raf, python, action='loves')
g.add_edge(raf, fr, action='is')

g.add_edge(grapheekdb, python, action='is implemented in')
g.add_edge(grapheekdb, gpl, action='is')
g.add_edge(grapheekdb, localmem, action='can use')
g.add_edge(grapheekdb, lmdb, action='can use')
g.add_edge(grapheekdb, kyoto, action='can use')

g.add_edge(localmem, kvs, action='is a')
g.add_edge(kyoto, kvs, action='is a')
g.add_edge(lmdb, kvs, action='is a')

g.add_edge(kyoto, persistent, action='is')
g.add_edge(lmdb, persistent, action='is')
Ejemplo n.º 4
0
gabriel = g.add_node(name='gabriel', gender='m')

nathalie_c = g.add_node(name='nathalie_c', gender='f')
nicolas = g.add_node(name='nicolas', gender='m')
hanae = g.add_node(name='hanae', gender='f')
come = g.add_node(name='come', gender='m')

clarisse = g.add_node(name='clarisse', gender='f')
thomas = g.add_node(name='thomas', gender='m')
noam = g.add_node(name='noam', gender='m')

# Creating 'is_parent' relationship

# grandparents -> parents

g.add_edge(martine, raf, rel='is_parent')
g.add_edge(gerard, raf, rel='is_parent')

g.add_edge(martine, pierre_yves, rel='is_parent')
g.add_edge(gerard, pierre_yves, rel='is_parent')

g.add_edge(laou, flo, rel='is_parent')
g.add_edge(daniel, flo, rel='is_parent')

g.add_edge(laou, nicolas, rel='is_parent')
g.add_edge(daniel, nicolas, rel='is_parent')

g.add_edge(laou, clarisse, rel='is_parent')
g.add_edge(daniel, clarisse, rel='is_parent')

# parents -> children
class TestModifiedBackendLeadToError(FillMethod):

    def setup(self):
        from grapheekdb.backends.data.localmem import LocalMemoryGraph
        self.graph = LocalMemoryGraph()
        self.fill()

    def test_create_index_node(self):
        txn = self.graph._transaction_begin()
        try:
            # Manually modifying backend
            self.graph._remove(txn, METADATA_VERTEX_INDEX_COUNTER)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.graph.add_node_index('foo')
        except GrapheekIndexCreationFailedException:
            exception_raised = True
        assert(exception_raised)

    def test_remove_index_node(self):
        from grapheekdb.backends.data.indexes import normalize_value
        self.graph.add_node_index('foo')
        txn = self.graph._transaction_begin()
        args = ['foo']
        kwargs = {}
        index_signature = [args, list(kwargs.items())]
        index_signature_string = normalize_value(index_signature)
        index_key = '/'.join([METADATA_VERTEX_INDEX_PREFIX] + [index_signature_string])
        try:
            # Manually modifying backend
            self.graph._remove(txn, index_key)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.graph.remove_node_index('foo')
        except GrapheekIndexRemovalFailedException:
            exception_raised = True
        assert(exception_raised)

    def test_create_index_edge(self):
        txn = self.graph._transaction_begin()
        try:
            # Manually modifying backend
            self.graph._remove(txn, METADATA_EDGE_INDEX_COUNTER)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.graph.add_edge_index('foo')
        except GrapheekIndexCreationFailedException:
            exception_raised = True
        assert(exception_raised)

    def test_remove_index_edge(self):
        from grapheekdb.backends.data.indexes import normalize_value
        self.graph.add_edge_index('foo')
        txn = self.graph._transaction_begin()
        args = ['foo']
        kwargs = {}
        index_signature = [args, list(kwargs.items())]
        index_signature_string = normalize_value(index_signature)
        index_key = '/'.join([METADATA_EDGE_INDEX_PREFIX] + [index_signature_string])
        try:
            # Manually modifying backend
            self.graph._remove(txn, index_key)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        # Manually modifying backend
        exception_raised = False
        try:
            self.graph.remove_edge_index('foo')
        except GrapheekIndexRemovalFailedException:
            exception_raised = True
        assert(exception_raised)

    def test_add_node(self):
        txn = self.graph._transaction_begin()
        try:
            # Manually modifying backend
            self.graph._remove(txn, METADATA_VERTEX_COUNTER)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        # Manually modifying backend
        exception_raised = False
        try:
            self.graph.add_node(foo=3)
        except GrapheekDataException:
            exception_raised = True
        assert(exception_raised)

    def test_remove_node(self):
        # Manually modifying backend
        from grapheekdb.backends.data.keys import KIND_VERTEX, OUT_EDGES_SUFFIX
        from grapheekdb.backends.data.keys import build_key
        txn = self.graph._transaction_begin()
        try:
            # Manually modifying backend
            self.graph._remove(txn, build_key(KIND_VERTEX, self.n1.get_id(), OUT_EDGES_SUFFIX))
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.n1.remove()
        except GrapheekDataException:
            exception_raised = True
        assert(exception_raised)

    def test_update_node_data(self):
        # Manually modifying backend
        from grapheekdb.backends.data.keys import KIND_VERTEX, DATA_SUFFIX
        from grapheekdb.backends.data.keys import build_key
        txn = self.graph._transaction_begin()
        try:
            # Manually modifying backend
            self.graph._remove(txn, build_key(KIND_VERTEX, self.n1.get_id(), DATA_SUFFIX))
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.n1.foobar = 3
        except GrapheekDataException:
            exception_raised = True
        assert(exception_raised)

    def test_add_edge(self):
        # Manually modifying backend
        txn = self.graph._transaction_begin()
        try:
            # Manually modifying backend
            self.graph._remove(txn, METADATA_EDGE_COUNTER)
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.graph.add_edge(self.n1, self.n2)
        except GrapheekDataException:
            exception_raised = True
        assert(exception_raised)

    def test_remove_edge(self):
        # Manually modifying backend
        from grapheekdb.backends.data.keys import KIND_EDGE, OUT_VERTICES_SUFFIX
        from grapheekdb.backends.data.keys import build_key
        txn = self.graph._transaction_begin()
        try:
            # Manually modifying backend
            self.graph._remove(txn, build_key(KIND_EDGE, self.e1.get_id(), OUT_VERTICES_SUFFIX))
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.e1.remove()
        except GrapheekDataException:
            exception_raised = True
        assert(exception_raised)

    def test_update_edge_data(self):
        # Manually modifying backend
        from grapheekdb.backends.data.keys import KIND_EDGE, DATA_SUFFIX
        from grapheekdb.backends.data.keys import build_key
        txn = self.graph._transaction_begin()
        try:
            # Manually modifying backend
            self.graph._remove(txn, build_key(KIND_EDGE, self.e1.get_id(), DATA_SUFFIX))
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.e1.foobar = 3
        except GrapheekDataException:
            exception_raised = True
        assert(exception_raised)

    # Test invalid data for bulk add node

    def test_invalid_bulk_add_node_1(self):
        count = 2
        data = []
        for i in range(count):
            data.append(('document_id', i))  # this should be a dictionnary not a 2-uple
        exception_raised = False
        try:
            self.graph.bulk_add_node(data)
        except GrapheekDataException:
            exception_raised = True
        assert(exception_raised)

    def test_invalid_bulk_add_node_2(self):
        count = 2
        data = []
        for i in range(count):
            data.append(dict(document_id=i))
        txn = self.graph._transaction_begin()
        try:
            # Manually modifying backend
            self.graph._remove(txn, METADATA_VERTEX_COUNTER)  # This is a hack
            self.graph._transaction_commit(txn)
        except:
            self.graph._transaction_rollback(txn)
        exception_raised = False
        try:
            self.graph.bulk_add_node(data)
        except GrapheekDataException:
            exception_raised = True
        assert(exception_raised)

    # Test rollbacked update

    def test_node_iterator_invalid_update(self):
        exception_raised = False
        try:
            # hack : direct modification of backend data :
            self.graph._dic = {}
            self.graph.V().update(updated=True)
        except GrapheekDataException:
            exception_raised = True
        assert(exception_raised)

    """
class TestEntityRemoval(object):

    def setup(self):
        from grapheekdb.backends.data.localmem import LocalMemoryGraph
        self.graph = LocalMemoryGraph()

    def test_add_remove_one_free_node_will_reinitialize_raw_data(self):
        from grapheekdb.backends.data.keys import METADATA_VERTEX_ID_LIST_PREFIX
        from grapheekdb.backends.data.keys import METADATA_VERTEX_REMOVED_COUNTER
        raw_data_1 = deepcopy(self.graph._dic)
        node = self.graph.add_node(foo=1, bar=2)
        raw_data_2 = deepcopy(self.graph._dic)
        assert(len(raw_data_1) < len(raw_data_2))
        node.remove()
        raw_data_3 = deepcopy(self.graph._dic)
        vertex_counter_key = 'm/v/c'
        vertex_counter_1 = raw_data_1[vertex_counter_key]
        vertex_counter_3 = raw_data_3[vertex_counter_key]
        # The counter should have increased but other keys shouldn't have changed :
        assert(vertex_counter_3 == vertex_counter_1 + 1)
        del raw_data_1[vertex_counter_key]
        del raw_data_3[vertex_counter_key]
        # entity ids list should be created
        keyv = METADATA_VERTEX_ID_LIST_PREFIX + '/0'
        assert(not(keyv in raw_data_1))
        assert(keyv in raw_data_3)
        del raw_data_3[keyv]
        # entity removed counter should have have been incremented :
        assert(raw_data_3[METADATA_VERTEX_REMOVED_COUNTER] == raw_data_1[METADATA_VERTEX_REMOVED_COUNTER] + 1)
        del raw_data_1[METADATA_VERTEX_REMOVED_COUNTER]
        del raw_data_3[METADATA_VERTEX_REMOVED_COUNTER]
        # but other keys shouldn't have changed :
        assert(raw_data_1 == raw_data_3)

    def test_add_remove_multiple_free_node_will_reinitialize_raw_data(self):
        from grapheekdb.backends.data.keys import METADATA_VERTEX_ID_LIST_PREFIX
        from grapheekdb.backends.data.keys import METADATA_VERTEX_REMOVED_COUNTER
        raw_data_1 = deepcopy(self.graph._dic)
        count = 10
        nodes = []
        for i in range(count):
            node = self.graph.add_node(foo=i, bar=i + 1)
            nodes.append(node)
        raw_data_2 = deepcopy(self.graph._dic)
        assert(len(raw_data_1) < len(raw_data_2))
        for node in nodes:
            node.remove()
        raw_data_3 = deepcopy(self.graph._dic)
        vertex_counter_1 = raw_data_1[METADATA_VERTEX_COUNTER]
        vertex_counter_3 = raw_data_3[METADATA_VERTEX_COUNTER]
        # The counter should have increased but other keys shouldn't have changed :
        assert(vertex_counter_3 == vertex_counter_1 + count)
        del raw_data_1[METADATA_VERTEX_COUNTER]
        del raw_data_3[METADATA_VERTEX_COUNTER]
        # entity ids list should be created
        keyv = METADATA_VERTEX_ID_LIST_PREFIX + '/0'
        assert(not(keyv in raw_data_1))
        assert(keyv in raw_data_3)
        del raw_data_3[keyv]
        # entity removed counter should have have been incremented :
        assert(raw_data_3[METADATA_VERTEX_REMOVED_COUNTER] == raw_data_1[METADATA_VERTEX_REMOVED_COUNTER] + count)
        del raw_data_1[METADATA_VERTEX_REMOVED_COUNTER]
        del raw_data_3[METADATA_VERTEX_REMOVED_COUNTER]
        # but other keys shouldn't have changed :
        assert(raw_data_1 == raw_data_3)

    def test_add_remove_edge(self):
        from grapheekdb.backends.data.keys import METADATA_EDGE_ID_LIST_PREFIX, METADATA_VERTEX_ID_LIST_PREFIX, METADATA_EDGE_REMOVED_COUNTER
        from copy import deepcopy
        node1 = self.graph.add_node(foo=1, bar=2)
        node2 = self.graph.add_node(foo=2, bar=3)
        raw_data_1 = deepcopy(self.graph._dic)
        edge1 = self.graph.add_edge(node1, node2, baz=4)
        raw_data_2 = deepcopy(self.graph._dic)
        assert(len(raw_data_1) < len(raw_data_2))
        edge1.remove()
        raw_data_3 = deepcopy(self.graph._dic)
        edge_counter_1 = raw_data_1[METADATA_EDGE_COUNTER]
        edge_counter_3 = raw_data_3[METADATA_EDGE_COUNTER]
        # The counter should have increased but other keys shouldn't have changed :
        assert(edge_counter_3 == edge_counter_1 + 1)
        del raw_data_1[METADATA_EDGE_COUNTER]
        del raw_data_3[METADATA_EDGE_COUNTER]
        # entity ids list should be created
        keyv = METADATA_VERTEX_ID_LIST_PREFIX + '/0'
        keye = METADATA_EDGE_ID_LIST_PREFIX + '/0'
        assert(keyv in raw_data_1)
        assert(keyv in raw_data_3)
        assert(not(keye in raw_data_1))
        assert(keye in raw_data_3)
        del raw_data_3[keye]
        # entity removed counter should have have been incremented :
        assert(raw_data_3[METADATA_EDGE_REMOVED_COUNTER] == raw_data_1[METADATA_EDGE_REMOVED_COUNTER] + 1)
        del raw_data_1[METADATA_EDGE_REMOVED_COUNTER]
        del raw_data_3[METADATA_EDGE_REMOVED_COUNTER]
        # but other keys shouldn't have changed :
        assert(raw_data_1 == raw_data_3)

    def test_add_remove_multiple_edges(self):
        from grapheekdb.backends.data.keys import METADATA_EDGE_COUNTER, METADATA_EDGE_REMOVED_COUNTER
        from grapheekdb.backends.data.keys import METADATA_EDGE_ID_LIST_PREFIX, METADATA_VERTEX_ID_LIST_PREFIX
        from copy import deepcopy
        node1 = self.graph.add_node(foo=1, bar=2)
        node2 = self.graph.add_node(foo=2, bar=3)
        node3 = self.graph.add_node(foo=3, bar=4)
        raw_data_1 = deepcopy(self.graph._dic)
        edge1 = self.graph.add_edge(node1, node2, baz=4)
        edge2 = self.graph.add_edge(node2, node3, baz=5)
        raw_data_2 = deepcopy(self.graph._dic)
        assert(len(raw_data_1) < len(raw_data_2))
        edge1.remove()
        edge2.remove()
        raw_data_3 = deepcopy(self.graph._dic)
        edge_counter_1 = raw_data_1[METADATA_EDGE_COUNTER]
        edge_counter_3 = raw_data_3[METADATA_EDGE_COUNTER]
        # The counter should have increased but other keys shouldn't have changed :
        assert(edge_counter_3 == edge_counter_1 + 2)
        del raw_data_1[METADATA_EDGE_COUNTER]
        del raw_data_3[METADATA_EDGE_COUNTER]
        # entity ids list should be created
        keyv = METADATA_VERTEX_ID_LIST_PREFIX + '/0'
        keye = METADATA_EDGE_ID_LIST_PREFIX + '/0'
        assert(keyv in raw_data_1)
        assert(keyv in raw_data_3)
        assert(not(keye in raw_data_1))
        assert(keye in raw_data_3)
        del raw_data_3[keye]
        # entity removed counter should have have been incremented :
        assert(raw_data_3[METADATA_EDGE_REMOVED_COUNTER] == raw_data_1[METADATA_EDGE_REMOVED_COUNTER] + 2)
        del raw_data_1[METADATA_EDGE_REMOVED_COUNTER]
        del raw_data_3[METADATA_EDGE_REMOVED_COUNTER]
        # but other keys shouldn't have changed :
        assert(raw_data_1 == raw_data_3)

    def test_removing_node_removes_its_edges_too(self):
        from grapheekdb.backends.data.keys import METADATA_EDGE_COUNTER, METADATA_VERTEX_COUNTER
        from grapheekdb.backends.data.keys import METADATA_EDGE_REMOVED_COUNTER, METADATA_VERTEX_REMOVED_COUNTER

        from grapheekdb.backends.data.keys import METADATA_EDGE_ID_LIST_PREFIX, METADATA_VERTEX_ID_LIST_PREFIX
        from copy import deepcopy
        # I will create a "star" : one central node linked to 3 other nodes
        # Then remove the central and check that all edges have been removed properly
        node1 = self.graph.add_node(foo=1, bar=2)
        node2 = self.graph.add_node(foo=2, bar=3)
        node3 = self.graph.add_node(foo=3, bar=4)
        raw_data_1 = deepcopy(self.graph._dic)
        vertex_counter_1 = raw_data_1[METADATA_VERTEX_COUNTER]
        edge_counter_1 = raw_data_1[METADATA_EDGE_COUNTER]
        center = self.graph.add_node(foo=0, bar=1)
        self.graph.add_edge(center, node1, idx=1)
        self.graph.add_edge(center, node2, idx=2)
        self.graph.add_edge(node3, center, idx=3)  # this edge has a different direction
        # Now removing center
        center.remove()
        raw_data_2 = deepcopy(self.graph._dic)
        vertex_counter_2 = raw_data_2[METADATA_VERTEX_COUNTER]
        edge_counter_2 = raw_data_2[METADATA_EDGE_COUNTER]
        # node and edge counters should have increased
        assert(edge_counter_2 > edge_counter_1)
        assert(vertex_counter_2 > vertex_counter_1)
        del raw_data_1[METADATA_VERTEX_COUNTER]
        del raw_data_2[METADATA_VERTEX_COUNTER]
        del raw_data_1[METADATA_EDGE_COUNTER]
        del raw_data_2[METADATA_EDGE_COUNTER]
        # entity ids list should be created
        keyv = METADATA_VERTEX_ID_LIST_PREFIX + '/0'
        keye = METADATA_EDGE_ID_LIST_PREFIX + '/0'
        assert(keyv in raw_data_1)
        assert(keyv in raw_data_2)
        assert(not(keye in raw_data_1))
        assert(keye in raw_data_2)
        del raw_data_2[keye]
        # entity removed counter should have have been incremented :
        assert(raw_data_2[METADATA_VERTEX_REMOVED_COUNTER] == raw_data_1[METADATA_VERTEX_REMOVED_COUNTER] + 1)
        del raw_data_1[METADATA_VERTEX_REMOVED_COUNTER]
        del raw_data_2[METADATA_VERTEX_REMOVED_COUNTER]
        assert(raw_data_2[METADATA_EDGE_REMOVED_COUNTER] == raw_data_1[METADATA_EDGE_REMOVED_COUNTER] + 3)
        del raw_data_1[METADATA_EDGE_REMOVED_COUNTER]
        del raw_data_2[METADATA_EDGE_REMOVED_COUNTER]
        # but other keys shouldn't have changed :
        assert(raw_data_1 == raw_data_2)
class TestServerSideScript(object):

    def setup(self):
        from grapheekdb.backends.data.localmem import LocalMemoryGraph
        self.graph = LocalMemoryGraph()
        # setup server scripts
        self.graph.setup_server_scripts()
        self.n1 = self.graph.add_node(nid=1)
        self.n2 = self.graph.add_node(nid=2)
        self.n3 = self.graph.add_node(nid=3)
        self.graph.add_edge(self.n1, self.n2)
        self.graph.add_edge(self.n2, self.n3)

    def test_server_script_add_edge(self):
        edge_count_before = self.graph.E().count()
        self.graph.V(nid=1).aka('x').outV().outV().aka('y').call('add_edge', 'x', 'y', foo=1)
        edge_count_after = self.graph.E().count()
        assert(edge_count_after == edge_count_before + 1)

    def test_server_check_call_is_silent(self):
        res = self.graph.V(nid=1).aka('x').outV().outV().aka('y').call('add_edge', 'x', 'y', foo=1)
        assert(res is None)

    def test_server_check_request_is_verbose(self):
        self.graph.setup_server_scripts('grapheekdb.server.dummy_scripts')
        res = self.graph.V(nid=1).aka('x').outV().outV().aka('y').request('echo', msg="hello")
        assert(isinstance(res, list))

    def test_server_script_unknown_function(self):
        exception_raised = False
        try:
            self.graph.V(nid=1).aka('x').outV().outV().aka('y').call('this_function_doesnt_exist', 'x', 'y', foo=1)
        except GrapheekUnknownScriptException:
            exception_raised = True
        assert(exception_raised)

    def test_user_custom_script(self):
        # "Install" custom script
        self.graph.setup_server_scripts('grapheekdb.server.dummy_scripts')
        tmp = dict(foo=1, bar=2, baz=3)
        result = self.graph.V(nid=1).aka('x').outV().outV().aka('y').request('echo', 'x', 'y', **tmp)
        assert(len(result))
        res0 = result[0]
        assert(res0['args'] == ('x', 'y'))
        assert(res0['kwargs'] == tmp)

    # Test dot generation - representation

    def test_dot_generation_basic(self):
        str(self.graph.V().dot())

    def test_dot_generation_with_filter(self):
        str(self.graph.V(foo=1).dot())

    def test_dot_generation_boolean_label_1(self):
        # Adding a node with a boolean property
        self.graph.add_node(visible=True)
        # Get the representation
        str(self.graph.V().dot('visible'))

    def test_dot_generation_boolean_label_2(self):
        # Adding a node with a boolean property
        self.graph.add_node(visible=True)
        # Get the representation, check that using node_label works
        str(self.graph.V().dot(node_label='visible'))

    def test_dot_generation_edge_boolean_label(self):
        # Adding a node with a boolean property
        self.graph.add_edge(self.n2, self.n1, baz=True)
        # Get the representation, check that using node_label works
        str(self.graph.V().dot(edge_label='baz'))

    def test_dot_with_limit(self):
        str(self.graph.V(foo=1).dot(limit=1))

    # test name conflicts

    def test_frequenty_used_variable_name_dont_mess_filter(self):
        self.graph.add_node(kind='foo')
        self.graph.V(kind='foo').count()

        self.graph.add_node(entity_id='foo')
        self.graph.V(entity_id='foo').count()

        self.graph.add_node(txn='foo')
        self.graph.V(txn='foo').count()

        self.graph.add_node(fname='foo')
        self.graph.V(fname='foo').count()