Exemple #1
0
    def test_wildcard_empty(self):
        self.assertEqual([], pluckit([], '*'))
        self.assertEqual([], pluckit({}, '*'))

        self.assertEqual([], pluckit([], '*.*'))
        self.assertEqual([], pluckit({}, '*.*'))

        self.assertEqual([], pluckit([], '*.a'))
Exemple #2
0
    def test_set(self):
        data = set([1, 2, 3])

        self.assertEqual(3, pluckit(data, len))

        self.assertEqual(6, pluckit(data, lambda x: sum(x)))

        self.assertEqual(1, pluckit(data, 'pop'))
Exemple #3
0
    def test_none(self):
        self.assertEqual(None, pluckit(None, 'abc'))
        self.assertEqual(None, pluckit(None, 123))
        self.assertEqual(None, pluckit(None, str))

        def explode():
            raise Exception('this should never execute')

        self.assertEqual(None, pluckit(None, explode))
Exemple #4
0
    def test_path_errors(self):
        def bad_path(path):
            self.assertFalse(path_regex.match(path))

        bad_path('')

        bad_path('.')
        bad_path('[]')

        bad_path('[')
        bad_path(']')
        bad_path('.[')
        bad_path('.]')

        bad_path('..')
        bad_path('.[]')
        bad_path('.[a]')
        bad_path('.[0]')

        with self.assertRaises(ValueError):
            pluckit([0], '0.')

        with self.assertRaises(ValueError):
            pluckit([], '*.')

        with self.assertRaises(ValueError):
            pluckit([], '[')

        with self.assertRaises(ValueError):
            pluckit([], '*[')
Exemple #5
0
    def test_obj(self):
        class Foo():
            def foo(self):
                return 'foo'

            @classmethod
            def bar(cls):
                return 'bar'

            @staticmethod
            def baz():
                return 'baz'

            @property
            def prop(self):
                return 'abc'

            CONST = 123

        self.assertEqual('foo', pluckit(Foo(), 'foo'))

        self.assertEqual('bar', pluckit(Foo, 'bar'))
        self.assertEqual('bar', pluckit(Foo(), 'bar'))

        self.assertEqual('baz', pluckit(Foo, 'baz'))
        self.assertEqual('baz', pluckit(Foo(), 'baz'))

        self.assertEqual('abc', pluckit(Foo(), 'prop'))

        self.assertEqual(123, pluckit(Foo, 'CONST'))

        self.assertEqual('Foo', pluckit(Foo, '__name__'))
Exemple #6
0
    def test_dict(self):
        abc = {'a': 1, 'b': 2, 'c': 3}

        self.assertEqual(1, pluckit(abc, 'a'))

        with self.assertRaises(KeyError):
            pluckit(abc, 'values')

        self.assertEqual(3, pluckit(abc, len))

        # dict keys can be ints
        moreData = {0: 'a', 3: 'c'}
        self.assertEqual([int, int], list(map(type, moreData.keys())))
        self.assertEqual('a', pluckit(moreData, 0))
Exemple #7
0
    def test_fns(self):
        class Foo:
            def foo(self):
                return 'foo'

        data = {'a': {'b': 1}, 'c': {'d': Foo()}}

        self.assertEqual(['a', 'c'], list(pluckit(data, '.keys')))

        self.assertEqual(['b'], list(pluckit(data, 'a.keys')))

        self.assertEqual([1], list(pluckit(data, 'a.values')))

        self.assertEqual('foo', pluckit(data, 'c[d].foo'))

        self.assertEqual('Foo', pluckit(data, 'c[d].__class__.__name__'))
Exemple #8
0
    def test_errors(self):
        data = {'a': []}

        with self.assertRaises(IndexError):
            pluckit(data, 'a[0]')

        with self.assertRaises(AttributeError):
            pluckit(data, 'a.0')

        with self.assertRaises(AttributeError):
            pluckit(data, 'a.z')

        with self.assertRaises(KeyError):
            pluckit(data, 'z')

        with self.assertRaises(AttributeError):
            pluckit(data, '.z')
Exemple #9
0
    def test_wildcard_advanced(self):
        data = {
            'a': {
                'k': 1,
                'v': [1, 2]
            },
            'b': {
                'k': 2,
                'v': [3, 4]
            },
        }

        self.assertEqual([1, 2], pluckit(data, '*[k]'))

        self.assertEqual([[1, 2], [3, 4]], pluckit(data, '*[v]'))

        self.assertEqual([[1, 2], [3, 4]], pluckit(data, '*[v]*'))

        self.assertEqual([1, 3], pluckit(data, '*[v][0]'))
Exemple #10
0
    def test_list(self):
        data = [0, 1, 2, 3]

        self.assertEqual(
            3,
            pluckit(data, 3),
        )

        self.assertEqual(
            list,
            pluckit(data, '__class__'),
        )

        self.assertEqual(4, len(data))
        self.assertEqual(
            3,
            pluckit(data, 'pop'),
        )
        self.assertEqual(3, len(data))

        self.assertEqual(
            3,
            pluckit(data, len),
        )

        self.assertEqual(
            3,  # 0 + 1 + 2
            pluckit(data, lambda x: sum(x)))

        self.assertEqual(
            data[1:3],  # [ 1, 2 ]
            pluckit(data, slice(1, 3)))
Exemple #11
0
 def test_empty_list(self):
     with self.assertRaises(IndexError):
         pluckit([], '0')
     with self.assertRaises(IndexError):
         pluckit([], '-1')
     with self.assertRaises(AttributeError):
         pluckit([], '.0')
Exemple #12
0
    def test_wildcard_errors(self):
        data = {'a': [], 'b': 2}

        with self.assertRaises(KeyError):
            pluckit(data, '["*"]')

        with self.assertRaises(TypeError):
            pluckit(data, 'a["*"]')

        with self.assertRaises(TypeError):
            pluckit(data, 'b.*')

        with self.assertRaises(ValueError):
            pluckit(data, '*.')
Exemple #13
0
    def test_basic(self):
        data = [[1, 2], [3], 4]

        self.assertEqual(1, pluckit(data, '0[0]'))
        self.assertEqual(2, pluckit(data, '0[1]'))
        self.assertEqual(3, pluckit(data, '1[0]'))

        self.assertEqual(4, pluckit(data, '-1'))
        self.assertEqual(2, pluckit(data, '0[-1]'))

        data = {'a': {'b': 1}, 'c': {'d': 1}}
        self.assertEqual(1, pluckit(data, 'a["b"]'))
        self.assertEqual(1, pluckit(data, "a['b']"))
Exemple #14
0
    def test_slice(self):
        data = [0, 1, 2, 3]

        self.assertEqual(
            data[1:3],  # [ 1, 2 ]
            pluckit(data, '[1:3]'))

        self.assertEqual(data[2:], pluckit(data, '[2:]'))

        self.assertEqual(data[:2], pluckit(data, '[:2]'))

        self.assertEqual(data[::2], pluckit(data, '[::2]'))

        self.assertEqual(data[:], pluckit(data, '[:]'))

        self.assertEqual(data[::], pluckit(data, '[::]'))
Exemple #15
0
    def test_empty_dict(self):
        with self.assertRaises(KeyError):
            pluckit({}, 'a')

        with self.assertRaises(KeyError):
            pluckit({}, '0')
Exemple #16
0
    def test_wildcard(self):
        data = [[1], [2]]
        self.assertEqual(data, pluckit(data, '*'))
        self.assertEqual(data, pluckit(data, '.*'))

        self.assertEqual(data, pluckit(data, '*.*'))
        self.assertEqual(data, pluckit(data, '*[*]'))
        self.assertEqual(data, pluckit(data, '.*.*'))

        self.assertEqual([1, 2], pluckit(data, '*[0]'))

        data = {'a': {'b': 1}}
        self.assertEqual([{'b': 1}], pluckit(data, '*'))
        self.assertEqual([{'b': 1}], pluckit(data, '.*'))

        self.assertEqual([1], pluckit(data, '*[b]'))
        self.assertEqual([1], pluckit(data, 'a[*]'))

        self.assertEqual([[1]], pluckit(data, '*[*]'))
        self.assertEqual([[1]], pluckit(data, '*.*'))
Exemple #17
0
    def test_tuple(self):
        data = (1, 2, 3)

        self.assertEqual(1, pluckit(data, 0))
        self.assertEqual(3, pluckit(data, len))