def search_traversal(**kwargs): """Search Traversal in Database""" db_inst = DB() db_inst.get_database() graph = db_inst.get_graph(kwargs.get('graph_name')) traversal_results = graph.traverse( start_vertex=kwargs.get('start_vertex'), direction=kwargs.get('direction'), item_order=kwargs.get('item_order'), strategy=kwargs.get('strategy'), order=kwargs.get('order'), edge_uniqueness=kwargs.get('edge_uniqueness'), vertex_uniqueness=kwargs.get('vertex_uniqueness'), max_iter=kwargs.get('max_iter'), min_depth=kwargs.get('min_depth'), max_depth=kwargs.get('max_depth'), init_func=kwargs.get('init_func'), sort_func=kwargs.get('sort_func'), filter_func=kwargs.get('filter_func'), visitor_func=kwargs.get('visitor_func'), expander_func=kwargs.get('expander_func') ) traversal_results = util.filter_transversal(traversal_results) traversal_results.update({'graph': kwargs.get('graph_name')}) return traversal_results
class TestDB(unittest.TestCase): def setUp(self): self.app = create_app('tests.config') with self.app.app_context(): self.db_inst = DB() self._cleanup() self.db_inst.get_database() def _cleanup(self): dbs = ['test_database'] for db_name in dbs: try: self.db_inst.delete_database(db_name) except Exception: pass def test_get_database(self): """Test get database""" db_name = 'test_database' self.db_inst.create_database(db_name) col = self.db_inst.get_database(db_name) self.assertEqual(col.name, db_name) def test_create_database(self): """Test create database""" db_name = 'test_database' col = self.db_inst.create_database(db_name) self.assertEqual(col.name, db_name) def test_delete_database(self): """Test delete database""" db_name = 'test_database' self.db_inst.create_database(db_name) self.db_inst.delete_database(db_name) with self.assertRaises(gmap_exceptions.DatabaseNotExist): self.db_inst.get_database(db_name) def test_create_db_duplicated(self): """Test if create db with duplicated name""" db_name = 'test_database' self.db_inst.create_database(db_name) with self.assertRaises(gmap_exceptions.DatabaseAlreadyExist): self.db_inst.create_database(db_name) def test_delete_db_not_exists(self): """Test if delete db that not exists""" db_name = 'test_database_not_exist' with self.assertRaises(gmap_exceptions.DatabaseNotExist): self.db_inst.delete_database(db_name)
def clear_collection(name, data): """Clear document in Collection""" db_inst = DB() db_inst.get_database() db_inst.clear_collection(name, data) return {}
def list_graphs(): """Return all graph from Database""" db_inst = DB() db_inst.get_database() graphs = db_inst.database.graphs() graphs = util.filter_graphs(graphs) return graphs
def list_collections(kind): """Return all collections or edges from Database""" db_inst = DB() db_inst.get_database() collections = db_inst.database.collections() collections = util.filter_collections(collections, kind) return collections
class TestDB(unittest.TestCase): def setUp(self): self.app = create_app('tests.config') with self.app.app_context(): self.db_inst = DB() self._cleanup() self.db_inst.get_database() def _cleanup(self): dbs = ['test_database'] for db_name in dbs: try: self.db_inst.delete_database(db_name) except Exception: pass def test_get_database(self): """Test get database""" db_name = 'test_database' self.db_inst.create_database(db_name) col = self.db_inst.get_database(db_name) self.assertEqual(col.name, db_name) def test_create_database(self): """Test create database""" db_name = 'test_database' col = self.db_inst.create_database(db_name) self.assertEqual(col.name, db_name) def test_delete_database(self): """Test delete database""" db_name = 'test_database' self.db_inst.create_database(db_name) self.db_inst.delete_database(db_name) with self.assertRaises(gmap_exceptions.DatabaseNotExist): self.db_inst.get_database(db_name) def test_create_db_duplicated(self): """Test if create db with duplicated name""" db_name = 'test_database' self.db_inst.create_database(db_name) with self.assertRaises(gmap_exceptions.DatabaseAlreadyExist): self.db_inst.create_database(db_name) def test_delete_db_not_exists(self): """Test if delete db that not exists""" db_name = 'test_database_not_exist' with self.assertRaises(gmap_exceptions.DatabaseNotExist): self.db_inst.delete_database(db_name)
def search_document(name, field, value, offset=0, count=100): """Search in Database""" db_inst = DB() db_inst.get_database() cursor = db_inst.search_in_database( name, field, value, offset, count) docs = [doc for doc in cursor] return docs
def execute_query(key, variable): query = get_query(key) query['params']['variable'] = variable db_inst = DB() db_inst.get_database() cursor = db_inst.execute_aql(query['query'], query['params']) docs = [doc for doc in cursor] return docs
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')
def search_traversal(**kwargs): """Search Traversal in Database""" db_inst = DB() db_inst.get_database() graph = db_inst.get_graph(kwargs.get('graph_name')) try: traversal_results = graph.traverse( start_vertex=kwargs.get('start_vertex'), direction=kwargs.get('direction'), item_order=kwargs.get('item_order'), strategy=kwargs.get('strategy'), order=kwargs.get('order'), edge_uniqueness=kwargs.get('edge_uniqueness'), vertex_uniqueness=kwargs.get('vertex_uniqueness'), max_iter=kwargs.get('max_iter'), min_depth=kwargs.get('min_depth'), max_depth=kwargs.get('max_depth'), init_func=kwargs.get('init_func'), sort_func=kwargs.get('sort_func'), filter_func=kwargs.get('filter_func'), visitor_func=kwargs.get('visitor_func'), expander_func=kwargs.get('expander_func') ) except GraphTraverseError as err: if traverse_err.get(err.error_code): if err.error_code == 1202: msg = traverse_err.get(1202) raise gmap_exceptions.GraphTraverseException(msg) raise gmap_exceptions.GraphTraverseException( traverse_err.get(err.error_code).format(err.message)) else: raise gmap_exceptions.GraphTraverseException( traverse_err.get(0).format( kwargs.get('graph_name'), err.message)) except Exception as err: raise gmap_exceptions.GraphTraverseException( traverse_err.get(0).format(kwargs.get('graph_name'), err.message)) traversal_results = util.filter_transversal(traversal_results) traversal_results.update({'graph': kwargs.get('graph_name')}) return traversal_results
def _is_arango_ok(): try: db = DB() db.get_database() graphs = facade.list_graphs() collections = facade.list_collections('document') edges = facade.list_collections('edge') except: app.logger.error('Failed to healthcheck arango.') deps = {'status': False} else: deps = { 'status': True, 'graphs': graphs, 'collections': collections, 'edges': edges } return deps
def list_deps(): deps = { 'arango': {} } try: db = DB() db.get_database() graphs = facade.list_graphs() collections = facade.list_collections('document') edges = facade.list_collections('edge') except: deps['arango']['status'] = False else: deps['arango']['status'] = True deps['arango']['graphs'] = graphs deps['arango']['collections'] = collections deps['arango']['edges'] = edges return deps
def make_query(data): """Validate query and make document""" query = data key = 'query_{}'.format(data.get('name')) query = { '_key': key, 'name': data.get('name'), 'description': data['description'], 'query': data['query'], 'params': data.get('params'), 'collection': data.get('collection') } db_inst = DB() db_inst.get_database() db_inst.validate_aql(data['query']) return query
def search_collections(collections, data, page, per_page): """Search in Database""" db_inst = DB() db_inst.get_database() cursor = db_inst.search_in_collections(collections, data, page, per_page) total_pages = int(math.ceil(cursor.statistics()[ 'fullCount'] / (per_page * 1.0))) total_documents = cursor.statistics()['fullCount'] docs = [doc for doc in cursor] res = { 'total_pages': total_pages, 'total': len(docs), 'total_documents': total_documents, 'documents': docs } return res
class TestDocument(unittest2.TestCase): def setUp(self): self.app = create_app('tests.config') self.db_name = self.app.config['ARANGO_DB'] with self.app.app_context(): self.db_inst = DB() self._cleanup() self.db_inst.get_database() self.db_inst.create_collection('test_collection_db') def test_search_document(self): """Test search document by property""" col_name = 'test_collection_db' self._import_bulk(col_name) docs = self.db_inst.search_in_database('test_collection_db', 'value', '1') docs = (set(sorted([d['_key'] for d in docs]))) self.assertEqual(docs, {'doc04', 'doc05'}) def test_get_document(self): """Test get document""" 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 get document""" 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""" inst_doc = Document(self.db_inst.collection) with self.assertRaises(gmap_exceptions.DocumentNotExist): inst_doc.get_document('doc04') def test_delete_document(self): """Test get document""" 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 get document""" inst_doc = Document(self.db_inst.collection) with self.assertRaises(gmap_exceptions.DocumentNotExist): inst_doc.delete_document('doc04') def _import_bulk(self, col_name): self.db_inst.database.collection(col_name).import_bulk([ {'_key': 'doc04', 'value': 1}, {'_key': 'doc05', 'value': 1}, {'_key': 'doc06', 'value': 3}, ]) def _cleanup(self): try: self.db_inst.delete_database(self.db_name) except Exception: pass finally: self.db_inst.create_database(self.db_name)
class TestGraph(unittest2.TestCase): def setUp(self): self.app = create_app('tests.config') self.db_name = self.app.config['ARANGO_DB'] with self.app.app_context(): self.db_inst = DB() self._cleanup() self.db_inst.get_database() def _cleanup(self): try: self.db_inst.delete_database(self.db_name) except Exception: pass finally: self.db_inst.create_database(self.db_name) def test_get_graph(self): """Test get graph""" with self.app.app_context(): 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""" with self.app.app_context(): 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 self.app.app_context(): 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 graph""" with self.app.app_context(): 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""" with self.app.app_context(): 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""" with self.app.app_context(): 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""" with self.app.app_context(): 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""" with self.app.app_context(): 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_name = self.app.config['ARANGO_DB'] with self.app.app_context(): self.db_inst = DB() self._cleanup() self.db_inst.get_database() self.db_inst.create_collection('test_collection_db') def test_search_document(self): """Test search document by property""" col_name = 'test_collection_db' self._import_bulk(col_name) docs = self.db_inst.search_in_database('test_collection_db', 'value', '1') docs = (set(sorted([d['_key'] for d in docs]))) self.assertEqual(docs, {'doc04', 'doc05'}) def test_get_document(self): """Test get document""" 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 get document""" 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""" inst_doc = Document(self.db_inst.collection) with self.assertRaises(gmap_exceptions.DocumentNotExist): inst_doc.get_document('doc04') def test_delete_document(self): """Test get document""" 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 get document""" inst_doc = Document(self.db_inst.collection) with self.assertRaises(gmap_exceptions.DocumentNotExist): inst_doc.delete_document('doc04') def _import_bulk(self, col_name): self.db_inst.database.collection(col_name).import_bulk([ { '_key': 'doc04', 'value': 1 }, { '_key': 'doc05', 'value': 1 }, { '_key': 'doc06', 'value': 3 }, ]) def _cleanup(self): try: self.db_inst.delete_database(self.db_name) except Exception: pass finally: self.db_inst.create_database(self.db_name)
class TestCollection(unittest2.TestCase): def setUp(self): self.app = create_app('tests.config') self.db_name = self.app.config['ARANGO_DB'] with self.app.app_context(): self.db_inst = DB() self._cleanup() self.db_inst.get_database() def _cleanup(self): try: self.db_inst.delete_database(self.db_name) except Exception: pass finally: self.db_inst.create_database(self.db_name) ############## # 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)
class TestGraph(unittest2.TestCase): def setUp(self): self.app = create_app('tests.config') self.db_name = self.app.config['ARANGO_DB'] with self.app.app_context(): self.db_inst = DB() self._cleanup() self.db_inst.get_database() def _cleanup(self): try: self.db_inst.delete_database(self.db_name) except Exception: pass finally: self.db_inst.create_database(self.db_name) 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""" 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""" 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 graph""" 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)