def test_create_table(self): ''' Test creating table. :return: ''' with patch("os.path.exists", MagicMock(return_value=False)), \ patch("os.listdir", MagicMock(return_value=['some_table'])): writable = Writable() with patch("gzip.open", MagicMock(return_value=writable)): csvdb = CsvDB('/foobar') csvdb.open() csvdb.create_table_from_object(FoobarEntity()) if six.PY2: assert writable.data[0].strip() == "foo:int,bar:str,spam:float" else: # Order in PY3 is not the same for every run writable_data = writable.data[0].strip() assert_order_options = [ 'bar:str,foo:int,spam:float', 'bar:str,spam:float,foo:int', 'foo:int,spam:float,bar:str', 'foo:int,bar:str,spam:float', 'spam:float,foo:int,bar:str', 'spam:float,bar:str,foo:int' ] while assert_order_options: assert_option = assert_order_options.pop() try: assert writable_data == assert_option break except AssertionError: if not assert_order_options: raise continue
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_create_table(self): ''' Test creating table. :return: ''' writable = Writable() with patch("gzip.open", MagicMock(return_value=writable)): csvdb = CsvDB('/foobar') csvdb.open() csvdb.create_table_from_object(FoobarEntity()) assert writable.data[0].strip() == "foo:int,bar:str,spam:float"
def test_create_table(self): """ Test creating table. :return: """ with patch("os.path.exists", MagicMock(return_value=False)), patch( "os.listdir", MagicMock(return_value=["some_table"])): writable = Writable() with patch("gzip.open", MagicMock(return_value=writable)): csvdb = CsvDB("/foobar") csvdb.open() csvdb.create_table_from_object(FoobarEntity()) sorted_writable_data = sorted(writable.data[0].strip().split(",")) sorted_expected_data = sorted( "foo:int,bar:str,spam:float".split(",")) self.assertEqual(sorted_writable_data, sorted_expected_data)
def test_create_table(self): """ Test creating table. :return: """ with patch("os.path.exists", MagicMock(return_value=False)), patch( "os.listdir", MagicMock(return_value=["some_table"])): writable = Writable() with patch("gzip.open", MagicMock(return_value=writable)) as gzip_mock_open: csvdb = CsvDB("/foobar") csvdb.open() csvdb.create_table_from_object(FoobarEntity()) # test the second call to gzip.open, the first is in the list_tables function assert gzip_mock_open.call_args_list[1][0][1] == "wt" sorted_writable_data = sorted(writable.data[0].strip().split(",")) sorted_expected_data = sorted( "foo:int,bar:str,spam:float".split(",")) self.assertEqual(sorted_writable_data, sorted_expected_data)
def test_create_table(self): ''' Test creating table. :return: ''' with patch("os.path.exists", MagicMock(return_value=False)), \ patch("os.listdir", MagicMock(return_value=['some_table'])): writable = Writable() with patch("gzip.open", MagicMock(return_value=writable)): csvdb = CsvDB('/foobar') csvdb.open() csvdb.create_table_from_object(FoobarEntity()) sorted_writable_data = sorted(writable.data[0].strip().split(',')) if six.PY2: sorted_expected_data = sorted( "foo:int,bar:unicode,spam:float".split(',')) else: sorted_expected_data = sorted( "foo:int,bar:str,spam:float".split(',')) self.assertEqual(sorted_writable_data, sorted_expected_data)