def test_add_object(self): ''' Test storing object into the database. :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)): obj = FoobarEntity() obj.foo = 123 obj.bar = 'test entity' obj.spam = 0.123 csvdb = CsvDB('/foobar') csvdb.open() csvdb._tables = { 'some_table': OrderedDict([ tuple(elm.split(':')) for elm in ["foo:int", "bar:str", "spam:float"] ]) } csvdb.store(obj) assert writable.data[0].strip() == '123,test entity,0.123'
def test_add_object(self): """ Test storing object into the database. :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: obj = FoobarEntity() obj.foo = 123 obj.bar = "test entity" obj.spam = 0.123 csvdb = CsvDB("/foobar") csvdb.open() csvdb._tables = { "some_table": OrderedDict([ tuple(elm.split(":")) for elm in ["foo:int", "bar:str", "spam:float"] ]) } csvdb.store(obj) # 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] == "at" assert writable.data[0].strip() == "123,test entity,0.123"
def test_get_object(self): """ Getting an object from the store. :return: """ with patch("os.listdir", MagicMock(return_value=["test_db"])), patch( "gzip.open", MagicMock()) as gzip_mock_open, patch( "csv.reader", MagicMock(return_value=iter([ [], ["foo:int", "bar:str", "spam:float"], ["123", "test", "0.123"], ["234", "another", "0.456"], ])), ): csvdb = CsvDB("/foobar") csvdb.open() entities = csvdb.get(FoobarEntity) # test the second call to gzip.open, the first is in the open function assert gzip_mock_open.call_args_list[1][0][1] == "rt" assert list == type(entities) assert len(entities) == 2 assert entities[0].foo == 123 assert entities[0].bar == "test" assert entities[0].spam == 0.123 assert entities[1].foo == 234 assert entities[1].bar == "another" assert entities[1].spam == 0.456
def test_get_object(self): ''' Getting an object from the store. :return: ''' with patch("gzip.open", MagicMock()): with patch( "csv.reader", MagicMock(return_value=iter([ [], ['foo:int', 'bar:str', 'spam:float'], ['123', 'test', '0.123'], ['234', 'another', '0.456'] ]))): csvdb = CsvDB('/foobar') csvdb.open() entities = csvdb.get(FoobarEntity) assert list == type(entities) assert len(entities) == 2 assert entities[0].foo == 123 assert entities[0].bar == 'test' assert entities[0].spam == 0.123 assert entities[1].foo == 234 assert entities[1].bar == 'another' assert entities[1].spam == 0.456
def test_get_obj_matching(self): """ Getting an object from the store with conditions :return: """ with patch("gzip.open", MagicMock()), patch( "os.listdir", MagicMock(return_value=["test_db"])), patch( "csv.reader", MagicMock(return_value=iter([ [], ["foo:int", "bar:str", "spam:float"], ["123", "this is test of something", "0.123"], ["234", "another test of stuff", "0.456"], ])), ): csvdb = CsvDB("/foobar") csvdb.open() entities = csvdb.get(FoobarEntity, matches={"bar": r"is\stest"}) assert list == type(entities) assert len(entities) == 1 assert entities[0].foo == 123 assert entities[0].bar == "this is test of something" assert entities[0].spam == 0.123
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
def test_open(self): ''' Test opening the database. :return: ''' csvdb = CsvDB('/foobar') csvdb.open() assert csvdb.list_tables() == ['test_db'] assert csvdb.is_closed() is False
def test_close(self): ''' Test closing the database. :return: ''' csvdb = CsvDB('/foobar') csvdb.open() csvdb.close() assert csvdb.is_closed() is True
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
def test_open(self): """ Test opening 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() assert list(csvdb.list_tables()) == ["test_db"] assert csvdb.is_closed() is False
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
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)
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_add_object(self): ''' Test storing object into the database. :return: ''' writable = Writable() with patch("gzip.open", MagicMock(return_value=writable)): obj = FoobarEntity() obj.foo = 123 obj.bar = 'test entity' obj.spam = 0.123 csvdb = CsvDB('/foobar') csvdb.open() csvdb._tables = {'some_table': OrderedDict([tuple(elm.split(':')) for elm in ["foo:int", "bar:str", "spam:float"]])} csvdb.store(obj) assert writable.data[0].strip() == '123,test entity,0.123'
def test_get_obj_matching(self): ''' Getting an object from the store with conditions :return: ''' with patch("gzip.open", MagicMock()): with patch("csv.reader", MagicMock(return_value=iter([[], ['foo:int', 'bar:str', 'spam:float'], ['123', 'this is test of something', '0.123'], ['234', 'another test of stuff', '0.456']]))): csvdb = CsvDB('/foobar') csvdb.open() entities = csvdb.get(FoobarEntity, matches={'bar': r'is\stest'}) assert list == type(entities) assert len(entities) == 1 assert entities[0].foo == 123 assert entities[0].bar == 'this is test of something' assert entities[0].spam == 0.123
def test_get_obj_less_than(self): ''' Getting an object from the store with conditions :return: ''' with patch("gzip.open", MagicMock()), \ patch("os.listdir", MagicMock(return_value=['test_db'])), \ patch("csv.reader", MagicMock(return_value=iter([[], ['foo:int', 'bar:str', 'spam:float'], ['123', 'test', '0.123'], ['234', 'another', '0.456']]))): csvdb = CsvDB('/foobar') csvdb.open() entities = csvdb.get(FoobarEntity, lt={'foo': 234}) assert list == type(entities) assert len(entities) == 1 assert entities[0].foo == 123 assert entities[0].bar == 'test' assert entities[0].spam == 0.123
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)
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_get_object(self): ''' Getting an object from the store. :return: ''' with patch("gzip.open", MagicMock()): with patch("csv.reader", MagicMock(return_value=iter([[], ['foo:int', 'bar:str', 'spam:float'], ['123', 'test', '0.123'], ['234', 'another', '0.456']]))): csvdb = CsvDB('/foobar') csvdb.open() entities = csvdb.get(FoobarEntity) assert list == type(entities) assert len(entities) == 2 assert entities[0].foo == 123 assert entities[0].bar == 'test' assert entities[0].spam == 0.123 assert entities[1].foo == 234 assert entities[1].bar == 'another' assert entities[1].spam == 0.456