Esempio n. 1
0
    def test_find(self):
        """Test DefaultAdapters `find` method."""
        tmp_storage = tempfile.mkdtemp(prefix='tests_', suffix='_adapters')

        adapter = DefaultAdapter(storage=tmp_storage)

        adapter.connect()
        adapter.process(data=[DOCUMENT] * 10)

        # invalid key
        # with self.assertRaises(ValueError):
        #     _ = list(adapter.find({'wrong-key': 'non-existing'}))

        # not finding anything
        collection = list(adapter.find({'cve.id_': 'non-existing'}))
        self.assertEqual(len(collection), 0)

        # find specific id
        collection = list(adapter.find({'cve.id_': 'CVE-2015-0001'}))
        self.assertEqual(len(collection), 10)

        # multiple selectors
        collection = list(
            adapter.find({
                'cve.id_': 'CVE-2015-0001',
                'impact.impact_score': 2.9
            }))
        self.assertEqual(len(collection), 10)

        # special selector
        collection = list(
            adapter.find({'cve.id_': selectors.match('CVE-2015-0001')}))
        self.assertEqual(len(collection), 10)

        # ---
        # array access
        collection = list(
            adapter.find({'cve.affects.data.vendor_name': 'microsoft'}))
        self.assertEqual(len(collection), 10)

        # array access
        collection = list(
            adapter.find(
                {'cve.affects.data.product_name': selectors.match('windows')}))
        self.assertEqual(len(collection), 0)

        collection = list(
            adapter.find(
                {'cve.affects.data.product_name':
                 selectors.search('windows')}))
        self.assertEqual(len(collection), 10)
Esempio n. 2
0
    def test_match(self):
        """Test `match` selector."""
        obj = utils.AttrDict(**{'foo': {'bar': 2}, 'buzz': 1})

        # exact match
        select = selectors.match(pattern=1)
        # simple, exact match
        self.assertTrue(select(obj, 'buzz'))

        # exact match
        select = selectors.match(pattern=2)
        self.assertFalse(select(obj, 'buzz'))

        # ---
        # pattern
        select = selectors.match(pattern=r"(\d)+")

        self.assertTrue(select(obj, 'buzz'))
        # nested
        self.assertTrue(select(obj, 'foo.bar'))
Esempio n. 3
0
    def __find(self, selectors: dict, shard: io.BytesIO):

        shard.seek(0)
        data = pickle.load(shard)

        entry: Document
        for entry in data:
            discard = False
            for attr, pattern in selectors.items():
                if not isinstance(pattern, typing.Callable):
                    select: typing.Callable = query_selectors.match(pattern)
                else:
                    select: typing.Callable = pattern

                if not select(entry, attr):
                    discard = True
                    break

            if not discard:
                yield entry