Example #1
0
class DBHandleBase(object):
    '''
    Handle for the *volatile* database, which serves the purpose of caching
    the inspected data. This database can be destroyed or corrupted, so it should
    be simply re-created from scratch.
    '''

    def __init__(self, path):
        '''
        Constructor.
        '''
        self._path = path
        self.init_queries = list()
        self._db = CsvDB(self._path)

    def open(self, new=False):
        '''
        Init the database, if required.
        '''
        self._db.new() if new else self._db.open()  # pylint: disable=W0106
        self._run_init_queries()

    def _run_init_queries(self):
        '''
        Initialization queries
        '''
        for obj in (Package, PackageCfgFile, PayloadFile, IgnoredDir, AllowedDir):
            self._db.create_table_from_object(obj())

    def purge(self):
        '''
        Purge whole database.
        '''
        for table_name in self._db.list_tables():
            self._db.flush(table_name)

        self._run_init_queries()

    def flush(self, table):
        '''
        Flush the table.
        '''
        self._db.flush(table)

    def close(self):
        '''
        Close the database connection.
        '''
        self._db.close()

    def __getattr__(self, item):
        '''
        Proxy methods from the Database instance.

        :param item:
        :return:
        '''
        return getattr(self._db, item)
 def test_close(self):
     '''
     Test closing the database.
     :return:
     '''
     csvdb = CsvDB('/foobar')
     csvdb.open()
     csvdb.close()
     assert csvdb.is_closed() is True
Example #3
0
 def test_close(self):
     """
     Test closing the database.
     :return:
     """
     with patch("os.listdir", MagicMock(return_value=["test_db"])), patch(
             "gzip.open", mock_open("foo:int,bar:str")):
         csvdb = CsvDB("/foobar")
         csvdb.open()
         csvdb.close()
         assert csvdb.is_closed() is True
Example #4
0
 def test_close(self):
     '''
     Test closing the database.
     :return:
     '''
     with patch("os.listdir", MagicMock(return_value=['test_db'])), \
             patch("gzip.open", mock_open("foo:int,bar:str")):
         csvdb = CsvDB('/foobar')
         csvdb.open()
         csvdb.close()
         assert csvdb.is_closed() is True