def test_pop_with_wrong_index(self): d = { 'a': [1, 2, 3], } b = KeypathDict(d) with self.assertRaises(KeyError): b.pop('a[5]')
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)
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])
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)
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)
def test_pop_with_1_valid_key(self): d = { 'a': 1, } b = KeypathDict(d) val = b.pop('a') self.assertEqual(val, 1)
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, {})
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')
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], ], })
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)
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)