Example #1
0
class TestDB(unittest.TestCase):

    def setUp(self):
        self.app = create_app('tests.config')
        self.db_inst = DB(self.app.config)

        self.conn_db()
        self.cleanup()
        self.db_inst.database.create_database('test')

        self.db_inst.conn_database('test')

    def tearDown(self):
        self.conn_db()
        self.cleanup()

    def conn_db(self):
        db_name = self.app.config['ARANGO_DB']
        self.db_inst.conn_database(db_name)

    def cleanup(self):
        try:
            self.db_inst.database.delete_database('test')
        except:
            pass

    def test_get_database(self):
        """Test get database"""

        col = self.db_inst.get_database('test')
        self.assertEqual(col.name, 'test')
Example #2
0
def cleanup():
    app = create_app('tests.config')
    db_inst = DB(app.config)
    db_name = app.config['ARANGO_DB']
    db_inst.conn_database(db_name)
    for col in db_inst.database.collections():
        if 'meta' not in col['name'] and col['system'] is False:
            db_inst.database.delete_collection(col['name'])
    db_inst.database.collection('meta_collection').truncate()
class TestCollection(unittest2.TestCase):

    def setUp(self):
        self.app = create_app('tests.config')
        self.db_inst = DB(self.app.config)

        self.conn_db()
        self.cleanup()
        self.db_inst.database.create_database('test')

        self.db_inst.conn_database('test')

    def tearDown(self):
        self.conn_db()
        self.cleanup()

    def conn_db(self):
        db_name = self.app.config['ARANGO_DB']
        self.db_inst.conn_database(db_name)

    def cleanup(self):
        try:
            self.db_inst.database.delete_database('test')
        except:
            pass

    ##############
    # COLLECTION #
    ##############
    def test_get_collection(self):
        """Test get collection"""

        with self.app.app_context():
            col_name = 'get_collection'
            self.db_inst.create_collection(col_name)

            col = self.db_inst.get_collection(col_name)
            self.assertEqual(col.name, col_name)

    def test_create_collection(self):
        """Test create collection"""

        with self.app.app_context():
            col_name = 'create_collection'
            self.db_inst.create_collection(col_name)
            col = self.db_inst.get_collection(col_name)
            self.assertEqual(col.name, col_name)

    def test_delete_collection(self):
        """Test delete collection"""

        with self.app.app_context():
            col_name = 'delete_collection'
            self.db_inst.create_collection(col_name)
            self.db_inst.delete_collection(col_name)
            with self.assertRaises(gmap_exceptions.CollectionNotExist):
                self.db_inst.get_collection(col_name)

    def test_get_collection_not_exists(self):
        """Test if get collection not exists"""

        with self.app.app_context():
            col_name = 'collection_not_exist'
            with self.assertRaises(gmap_exceptions.CollectionNotExist):
                self.db_inst.get_collection(col_name)

    def test_create_collection_duplicated(self):
        """Test if create collection with duplicated name"""

        with self.app.app_context():
            col_name = 'collection_duplicated'
            self.db_inst.create_collection(col_name)
            with self.assertRaises(gmap_exceptions.CollectionAlreadyExist):
                self.db_inst.create_collection(col_name)

    def test_delete_collection_not_exists(self):
        """Test if delete collection not exists"""

        with self.app.app_context():
            col_name = 'collection_not_exist'
            with self.assertRaises(gmap_exceptions.CollectionNotExist):
                self.db_inst.delete_collection(col_name)

    #########
    # EDGES #
    #########
    def test_get_edge(self):
        """Test get edge"""

        with self.app.app_context():
            col_name = 'get_edge'
            self.db_inst.create_edge(col_name)

            col = self.db_inst.get_edge(col_name)
            self.assertEqual(col.name, col_name)

    def test_create_edge(self):
        """Test create edge"""

        with self.app.app_context():
            col_name = 'create_edge'
            self.db_inst.create_edge(col_name)
            col = self.db_inst.get_edge(col_name)
            self.assertEqual(col.name, col_name)

    def test_delete_edge(self):
        """Test delete edge"""

        with self.app.app_context():
            col_name = 'delete_edge'
            self.db_inst.create_edge(col_name)
            self.db_inst.delete_edge(col_name)
            with self.assertRaises(gmap_exceptions.EdgeNotExist):
                self.db_inst.get_edge(col_name)

    def test_get_edge_not_exists(self):
        """Test if get edge not exists"""

        with self.app.app_context():
            col_name = 'edge_not_exist'
            with self.assertRaises(gmap_exceptions.EdgeNotExist):
                self.db_inst.get_edge(col_name)

    def test_create_edge_duplicated(self):
        """Test if create edge with duplicated name"""

        with self.app.app_context():
            col_name = 'edge_duplicated'
            self.db_inst.create_edge(col_name)
            with self.assertRaises(gmap_exceptions.EdgeAlreadyExist):
                self.db_inst.create_edge(col_name)

    def test_delete_edge_not_exists(self):
        """Test if delete edge not exists"""

        with self.app.app_context():
            col_name = 'edge_not_exist'
            with self.assertRaises(gmap_exceptions.EdgeNotExist):
                self.db_inst.delete_edge(col_name)
Example #4
0
class TestGraph(unittest2.TestCase):
    def setUp(self):
        self.app = create_app('tests.config')
        self.db_inst = DB(self.app.config)

        self.conn_db()
        self.cleanup()
        self.db_inst.database.create_database('test')

        self.db_inst.conn_database('test')

    def tearDown(self):
        self.conn_db()
        self.cleanup()

    def conn_db(self):
        db_name = self.app.config['ARANGO_DB']
        self.db_inst.conn_database(db_name)

    def cleanup(self):
        try:
            self.db_inst.database.delete_database('test')
        except:
            pass

    def test_get_graph(self):
        """Test get graph"""

        graph_name = 'test_graph_db'
        self.db_inst.create_graph(graph_name)
        col = self.db_inst.get_graph(graph_name)
        self.assertEqual(col.name, graph_name)

    def test_create_graph_without_def(self):
        """Test create graph without def"""

        graph_name = 'test_graph_db'
        self.db_inst.create_graph(graph_name)
        col = self.db_inst.get_graph(graph_name)
        self.assertEqual(col.name, graph_name)
        self.assertEqual(col.name, graph_name)

    def test_create_graph_one_def(self):
        """Test create graph with one def """

        graph_name = 'test_graph_db'
        definitions = [{
            'edge': 'edge_test',
            'from_collections': ['coll_test'],
            'to_collections': ['coll_test']
        }]
        self.db_inst.create_graph(graph_name, definitions)
        col = self.db_inst.get_graph(graph_name)
        self.assertEqual(col.name, graph_name)
        self.assertEqual(col.name, graph_name)

    def test_create_graph_two_def(self):
        """Test create 2 graphs with def"""

        graph_name = 'test_graph_db'
        definitions = [{
            'edge': 'edge_test',
            'from_collections': ['coll_test'],
            'to_collections': ['coll_test']
        }, {
            'edge': 'edge_test2',
            'from_collections': ['coll_test2'],
            'to_collections': ['coll_test2']
        }]
        self.db_inst.create_graph(graph_name, definitions)
        col = self.db_inst.get_graph(graph_name)
        self.assertEqual(col.name, graph_name)

    def test_delete_graph(self):
        """Test delete graph"""

        graph_name = 'test_graph_db'
        self.db_inst.create_graph(graph_name)
        self.db_inst.delete_graph(graph_name)
        with self.assertRaises(gmap_exceptions.GraphNotExist):
            self.db_inst.get_graph(graph_name)

    def test_get_graph_not_exists(self):
        """Test if get graph that not exists"""

        graph_name = 'test_graph_db_2'
        with self.assertRaises(gmap_exceptions.GraphNotExist):
            self.db_inst.get_graph(graph_name)

    def test_create_graph_duplicated(self):
        """Test if create graph with duplicated name"""

        graph_name = 'test_graph_db'
        self.db_inst.create_graph(graph_name)
        with self.assertRaises(gmap_exceptions.GraphAlreadyExist):
            self.db_inst.create_graph(graph_name)

    def test_delete_graph_not_exists(self):
        """Test if delete graph that not exists"""

        graph_name = 'test_graph_db_2'
        with self.assertRaises(gmap_exceptions.GraphNotExist):
            self.db_inst.delete_graph(graph_name)
class TestDocument(unittest2.TestCase):

    def setUp(self):
        self.app = create_app('tests.config')
        self.db_inst = DB(self.app.config)

        self.conn_db()
        self.cleanup()
        self.db_inst.database.create_database('test')

        self.db_inst.conn_database('test')
        self.db_inst.database.create_collection('test_collection_db')
        self.db_inst.get_collection('test_collection_db')

    def tearDown(self):
        self.conn_db()
        self.cleanup()

    def conn_db(self):
        db_name = self.app.config['ARANGO_DB']
        self.db_inst.conn_database(db_name)

    def cleanup(self):
        try:
            self.db_inst.database.delete_database('test')
        except:
            pass

    def test_search_document(self):
        """Test search document by property"""

        with self.app.app_context():
            col_name = 'test_collection_db'
            self._import_bulk(col_name)
            search = [[{'field': 'value', 'operator': '==', 'value': 1}]]
            docs = self.db_inst.search_in_collection(
                'test_collection_db', search)
            docs = (set(sorted([d['_key'] for d in docs])))

            self.assertEqual(docs, {'doc04', 'doc05'})

    def test_get_document(self):
        """Test get document"""

        with self.app.app_context():
            self._import_bulk('test_collection_db')
            inst_doc = Document(self.db_inst.collection)
            doc = inst_doc.get_document('doc04')
            doc = {'_key': doc['_key'], 'value': doc['value'], }

            self.assertDictEqual(doc, {'_key': 'doc04', 'value': 1})

    def test_create_document(self):
        """Test create document"""

        with self.app.app_context():
            inst_doc = Document(self.db_inst.collection)
            doc = inst_doc.create_document({'_key': 'doc04', 'value': 1})
            doc = {'_key': doc['_key'], '_id': doc['_id'], }

            self.assertDictEqual(
                doc, {'_key': 'doc04', '_id': 'test_collection_db/doc04', })

    def test_get_document_not_exist(self):
        """Test get document not existing"""

        with self.app.app_context():
            inst_doc = Document(self.db_inst.collection)

            with self.assertRaises(gmap_exceptions.DocumentNotExist):
                inst_doc.get_document('doc04')

    def test_delete_document(self):
        """Test delete document"""

        with self.app.app_context():
            col_name = 'test_collection_db'
            self._import_bulk(col_name)

            inst_doc = Document(self.db_inst.collection)
            inst_doc.delete_document('doc04')

            with self.assertRaises(gmap_exceptions.DocumentNotExist):
                inst_doc.get_document('doc04')

    def test_delete_document_not_exist(self):
        """Test delee document not existing"""

        with self.app.app_context():
            inst_doc = Document(self.db_inst.collection)

            with self.assertRaises(gmap_exceptions.DocumentNotExist):
                inst_doc.delete_document('doc04')

    def _import_bulk(self, col_name):
        collection = self.db_inst.database.collection(col_name)
        collection.import_bulk([
            {'_key': 'doc04', 'value': 1},
            {'_key': 'doc05', 'value': 1},
            {'_key': 'doc06', 'value': 3},
        ])