Esempio n. 1
0
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_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)
Esempio n. 3
0
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)