Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
 def __init__(self, path):
     """
     Constructor.
     """
     self._path = path
     self.init_queries = list()
     self._db = CsvDB(self._path)
Ejemplo n.º 4
0
 def __init__(self, path):
     '''
     Constructor.
     '''
     self._path = path
     self.init_queries = list()
     self._db = CsvDB(self._path)
Ejemplo n.º 5
0
 def test_list_databases(self):
     '''
     Test list databases.
     :return:
     '''
     csvdb = CsvDB('/foobar')
     assert csvdb.list() == ['test_db']
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
 def test_list_databases(self):
     """
     Test list databases.
     :return:
     """
     with patch("os.listdir", MagicMock(return_value=["test_db"])):
         csvdb = CsvDB("/foobar")
         assert csvdb.list() == ["test_db"]
Ejemplo n.º 8
0
 def test_list_databases(self):
     '''
     Test list databases.
     :return:
     '''
     with patch("os.listdir", MagicMock(return_value=['test_db'])):
         csvdb = CsvDB('/foobar')
         assert csvdb.list() == ['test_db']
Ejemplo n.º 9
0
    def test_obj_validation(self):
        '''
        Test object validation.

        :return:
        '''
        obj = FoobarEntity()
        obj.foo = 123
        obj.bar = 'test entity'
        obj.spam = 0.123

        csvdb = CsvDB('/foobar')
        csvdb._tables = {'some_table': OrderedDict([tuple(elm.split(':'))
                                                    for elm in ["foo:int", "bar:str", "spam:float"]])}
        assert csvdb._validate_object(obj) == [123, 'test entity', 0.123]
Ejemplo n.º 10
0
    def test_obj_validation(self):
        '''
        Test object validation.

        :return:
        '''
        with patch("os.path.exists", MagicMock(return_value=False)), \
                patch("os.listdir", MagicMock(return_value=['some_table'])):
            obj = FoobarEntity()
            obj.foo = 123
            obj.bar = 'test entity'
            obj.spam = 0.123

            csvdb = CsvDB('/foobar')
            csvdb._tables = {'some_table': OrderedDict([tuple(elm.split(':'))
                                                        for elm in ["foo:int", "bar:str", "spam:float"]])}
            assert csvdb._validate_object(obj) == [123, 'test entity', 0.123]
Ejemplo n.º 11
0
    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'
Ejemplo n.º 12
0
    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"
Ejemplo n.º 13
0
    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
Ejemplo n.º 14
0
 def test_close(self):
     '''
     Test closing the database.
     :return:
     '''
     csvdb = CsvDB('/foobar')
     csvdb.open()
     csvdb.close()
     assert csvdb.is_closed() is True
Ejemplo n.º 15
0
    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
Ejemplo n.º 16
0
    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
Ejemplo n.º 17
0
    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
Ejemplo n.º 18
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
Ejemplo n.º 19
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
Ejemplo n.º 20
0
    def test_obj_validation(self):
        """
        Test object validation.

        :return:
        """
        with patch("os.path.exists", MagicMock(return_value=False)), patch(
                "os.listdir", MagicMock(return_value=["some_table"])):
            obj = FoobarEntity()
            obj.foo = 123
            obj.bar = "test entity"
            obj.spam = 0.123

            csvdb = CsvDB("/foobar")
            csvdb._tables = {
                "some_table":
                OrderedDict([
                    tuple(elm.split(":"))
                    for elm in ["foo:int", "bar:str", "spam:float"]
                ])
            }
            assert csvdb._validate_object(obj) == [123, "test entity", 0.123]
Ejemplo n.º 21
0
 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
Ejemplo n.º 22
0
 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
Ejemplo n.º 23
0
    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"
Ejemplo n.º 24
0
    def test_criteria(self):
        '''
        Test criteria selector.

        :return:
        '''
        with patch("os.path.exists", MagicMock(return_value=False)), \
                patch("os.listdir", MagicMock(return_value=['some_table'])):
            obj = FoobarEntity()
            obj.foo = 123
            obj.bar = 'test entity'
            obj.spam = 0.123
            obj.pi = 3.14

            cmp = CsvDB('/foobar')._CsvDB__criteria

            # Single
            assert cmp(obj, eq={'foo': 123}) is True
            assert cmp(obj, lt={'foo': 124}) is True
            assert cmp(obj, mt={'foo': 122}) is True

            assert cmp(obj, eq={'foo': 0}) is False
            assert cmp(obj, lt={'foo': 123}) is False
            assert cmp(obj, mt={'foo': 123}) is False

            assert cmp(obj, matches={'bar': r't\se.*?'}) is True
            assert cmp(obj, matches={'bar': r'\s\sentity'}) is False

            # Combined
            assert cmp(obj, eq={'foo': 123, 'bar': r'test entity', 'spam': 0.123}) is True
            assert cmp(obj, eq={'foo': 123, 'bar': r'test', 'spam': 0.123}) is False

            assert cmp(obj, lt={'foo': 124, 'spam': 0.124}) is True
            assert cmp(obj, lt={'foo': 124, 'spam': 0.123}) is False

            assert cmp(obj, mt={'foo': 122, 'spam': 0.122}) is True
            assert cmp(obj, mt={'foo': 122, 'spam': 0.123}) is False

            assert cmp(obj, matches={'bar': r'test'}, mt={'foo': 122}, lt={'spam': 0.124}, eq={'pi': 3.14}) is True

            assert cmp(obj, matches={'bar': r'^test.*?y$'}, mt={'foo': 122}, lt={'spam': 0.124}, eq={'pi': 3.14}) is True
            assert cmp(obj, matches={'bar': r'^ent'}, mt={'foo': 122}, lt={'spam': 0.124}, eq={'pi': 3.14}) is False
            assert cmp(obj, matches={'bar': r'^test.*?y$'}, mt={'foo': 123}, lt={'spam': 0.124}, eq={'pi': 3.14}) is False
Ejemplo n.º 25
0
    def test_criteria(self):
        '''
        Test criteria selector.

        :return:
        '''
        obj = FoobarEntity()
        obj.foo = 123
        obj.bar = 'test entity'
        obj.spam = 0.123
        obj.pi = 3.14

        cmp = CsvDB('/foobar')._CsvDB__criteria

        # Single
        assert cmp(obj, eq={'foo': 123}) is True
        assert cmp(obj, lt={'foo': 124}) is True
        assert cmp(obj, mt={'foo': 122}) is True

        assert cmp(obj, eq={'foo': 0}) is False
        assert cmp(obj, lt={'foo': 123}) is False
        assert cmp(obj, mt={'foo': 123}) is False

        assert cmp(obj, matches={'bar': r't\se.*?'}) is True
        assert cmp(obj, matches={'bar': r'\s\sentity'}) is False

        # Combined
        assert cmp(obj, eq={'foo': 123, 'bar': r'test entity', 'spam': 0.123}) is True
        assert cmp(obj, eq={'foo': 123, 'bar': r'test', 'spam': 0.123}) is False

        assert cmp(obj, lt={'foo': 124, 'spam': 0.124}) is True
        assert cmp(obj, lt={'foo': 124, 'spam': 0.123}) is False

        assert cmp(obj, mt={'foo': 122, 'spam': 0.122}) is True
        assert cmp(obj, mt={'foo': 122, 'spam': 0.123}) is False

        assert cmp(obj, matches={'bar': r'test'}, mt={'foo': 122}, lt={'spam': 0.124}, eq={'pi': 3.14}) is True

        assert cmp(obj, matches={'bar': r'^test.*?y$'}, mt={'foo': 122}, lt={'spam': 0.124}, eq={'pi': 3.14}) is True
        assert cmp(obj, matches={'bar': r'^ent'}, mt={'foo': 122}, lt={'spam': 0.124}, eq={'pi': 3.14}) is False
        assert cmp(obj, matches={'bar': r'^test.*?y$'}, mt={'foo': 123}, lt={'spam': 0.124}, eq={'pi': 3.14}) is False
Ejemplo n.º 26
0
    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)
Ejemplo n.º 27
0
    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'
Ejemplo n.º 28
0
    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)
Ejemplo n.º 29
0
    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)
Ejemplo n.º 30
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)
Ejemplo n.º 31
0
    def test_criteria(self):
        """
        Test criteria selector.

        :return:
        """
        with patch("os.path.exists", MagicMock(return_value=False)), patch(
                "os.listdir", MagicMock(return_value=["some_table"])):
            obj = FoobarEntity()
            obj.foo = 123
            obj.bar = "test entity"
            obj.spam = 0.123
            obj.pi = 3.14

            cmp = CsvDB("/foobar")._CsvDB__criteria

            # Single
            assert cmp(obj, eq={"foo": 123}) is True
            assert cmp(obj, lt={"foo": 124}) is True
            assert cmp(obj, mt={"foo": 122}) is True

            assert cmp(obj, eq={"foo": 0}) is False
            assert cmp(obj, lt={"foo": 123}) is False
            assert cmp(obj, mt={"foo": 123}) is False

            assert cmp(obj, matches={"bar": r"t\se.*?"}) is True
            assert cmp(obj, matches={"bar": r"\s\sentity"}) is False

            # Combined
            assert (cmp(obj,
                        eq={
                            "foo": 123,
                            "bar": r"test entity",
                            "spam": 0.123
                        }) is True)
            assert cmp(obj, eq={
                "foo": 123,
                "bar": r"test",
                "spam": 0.123
            }) is False

            assert cmp(obj, lt={"foo": 124, "spam": 0.124}) is True
            assert cmp(obj, lt={"foo": 124, "spam": 0.123}) is False

            assert cmp(obj, mt={"foo": 122, "spam": 0.122}) is True
            assert cmp(obj, mt={"foo": 122, "spam": 0.123}) is False

            assert (cmp(
                obj,
                matches={"bar": r"test"},
                mt={"foo": 122},
                lt={"spam": 0.124},
                eq={"pi": 3.14},
            ) is True)

            assert (cmp(
                obj,
                matches={"bar": r"^test.*?y$"},
                mt={"foo": 122},
                lt={"spam": 0.124},
                eq={"pi": 3.14},
            ) is True)
            assert (cmp(
                obj,
                matches={"bar": r"^ent"},
                mt={"foo": 122},
                lt={"spam": 0.124},
                eq={"pi": 3.14},
            ) is False)
            assert (cmp(
                obj,
                matches={"bar": r"^test.*?y$"},
                mt={"foo": 123},
                lt={"spam": 0.124},
                eq={"pi": 3.14},
            ) is False)
Ejemplo n.º 32
0
 def __init__(self, path):
     CsvDB.__init__(self, path)
     self._remained = list()
Ejemplo n.º 33
0
 def __init__(self, path):
     CsvDB.__init__(self, path)
     self._remained = list()