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)
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)
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)