Ejemplo n.º 1
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)
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)

    """