Esempio n. 1
0
 def test_pop_with_wrong_index(self):
     d = {
         'a': [1, 2, 3],
     }
     b = KeypathDict(d)
     with self.assertRaises(KeyError):
         b.pop('a[5]')
Esempio n. 2
0
    def test_pop_with_flat_list_and_default(self):
        d = {
            'a': [1, 2, 3],
        }
        b = KeypathDict(d)

        with self.assertRaises(KeyError):
            b.pop('a[3]')
        self.assertEqual(b.pop('a[3]', 4), 4)
Esempio n. 3
0
    def test_pop_with_flat_list(self):
        d = {
            'a': [1, 2, 3],
        }
        b = KeypathDict(d)

        self.assertEqual(b.pop('a[-1]'), 3)
        self.assertEqual(b.pop('a[0]'), 1)
        self.assertEqual(b.pop('a'), [2])
Esempio n. 4
0
 def test_pop_with_1_invalid_key(self):
     d = {
         'a': 1,
     }
     b = KeypathDict(d)
     with self.assertRaises(KeyError):
         val = b.pop('b')
     val = b.pop('b', False)
     self.assertFalse(val)
     val = b.pop('b', None)
     self.assertEqual(val, None)
Esempio n. 5
0
 def test_pop_default(self):
     d = {
         'a': 1,
     }
     b = KeypathDict(d)
     val = b.pop('a', 2)
     self.assertEqual(val, 1)
     val = b.pop('b', 2)
     self.assertEqual(val, 2)
     val = b.pop('c', 3)
     self.assertEqual(val, 3)
Esempio n. 6
0
 def test_pop_with_1_valid_key(self):
     d = {
         'a': 1,
     }
     b = KeypathDict(d)
     val = b.pop('a')
     self.assertEqual(val, 1)
Esempio n. 7
0
    def test_pop_with_2_valid_keys(self):
        d = {
            'a': {
                'b': 1,
            }
        }
        b = KeypathDict(d)

        val = b.pop('a.b')
        with self.assertRaises(KeyError):
            val = b.pop('a.b')
        self.assertEqual(val, 1)

        val = b.pop('a')
        with self.assertRaises(KeyError):
            val = b.pop('a')
        self.assertEqual(val, {})
Esempio n. 8
0
    def test_pop_with_2_invalid_keys(self):
        d = {
            'a': {
                'b': 1,
            }
        }
        b = KeypathDict(d)

        val = b.pop('a.c', 2)
        self.assertEqual(val, 2)
        with self.assertRaises(KeyError):
            val = b.pop('a.c')
        self.assertEqual(b.get('a'), {'b': 1})

        val = b.pop('x.y', 1)
        self.assertEqual(val, 1)
        with self.assertRaises(KeyError):
            val = b.pop('x.y')
Esempio n. 9
0
    def test_pop_with_nested_list(self):
        d = {
            'a': {
                'b': [
                    {
                        'c': 1,
                        'd': [
                            1,
                            2,
                            3,
                            [0],
                        ],
                    },
                    {
                        'c': 2,
                        'd': [
                            4,
                            5,
                            6,
                            [0],
                        ],
                    },
                ],
            },
        }
        b = KeypathDict(d)

        self.assertEqual(b.pop('a.b[0].d[-1]'), [0])
        self.assertEqual(b['a.b[0].d'], [1, 2, 3])

        self.assertEqual(b.pop('a.b[0].d'), [1, 2, 3])
        self.assertEqual(b['a.b[0]'], {'c': 1})

        self.assertEqual(b.pop('a.b[0]'), {'c': 1})
        self.assertEqual(b['a.b[0]'], {
            'c': 2,
            'd': [
                4,
                5,
                6,
                [0],
            ],
        })
Esempio n. 10
0
    def test_pop_with_keys_list_and_no_keypath_separator(self):
        d = {
            'a': {
                'b': 1,
            }
        }
        b = KeypathDict(d, keypath_separator=None)

        val = b.pop(['a', 'c'], 2)
        self.assertEqual(val, 2)
        with self.assertRaises(KeyError):
            val = b.pop(['a', 'c'])
        self.assertEqual(b.get('a'), {'b': 1})

        val = b.pop(['x', 'y'], 1)
        self.assertEqual(val, 1)
        with self.assertRaises(KeyError):
            val = b.pop(['x', 'y'])

        val = b.pop(['a', 'b'])
        self.assertEqual(val, 1)
Esempio n. 11
0
 def test_pop_with_wrong_index_and_default(self):
     d = {
         'a': [1, 2, 3],
     }
     b = KeypathDict(d)
     self.assertEqual(b.pop('a[5]', 6), 6)