def test_find(self): d1 = { 'a': 'a', 'b': 1, 'c': { 'd': 'd', 'e': 2, 'more': { 'data': 'here' } } } self.assertEqual(data.find(d1, ''), d1) self.assertEqual(data.find(d1, 'a'), 'a') self.assertEqual(data.find(d1, 'c/e'), 2) self.assertEqual(data.find(d1, 'c/more/'), {'data': 'here'}) self.assertEqual(data.find(d1, 'c/more/data'), 'here') self.assertRaises(data.DataNotFoundError, data.find, d1, 'c/f') self.assertRaises(data.DataNotFoundError, data.find, d1, 'f') self.assertRaises(data.DataTypeError, data.find, d1, 1) self.assertRaises(data.DataTypeError, data.find, None, 1) self.assertRaises(data.DataTypeError, data.find, None, "foo") self.assertRaises(data.DataTypeError, data.find, "123", "123") self.assertEqual(data.find("123", ""), "123") d2 = {'a': [1, 2, 3]} self.assertEqual(data.find(d2, 'a[0]'), 1) self.assertEqual(data.find(d2, 'a[1]'), 2) self.assertEqual(data.find(d2, 'a[2]'), 3) self.assertRaises(data.DataNotFoundError, data.find, d2, 'a[3]') self.assertRaises(data.DataTypeError, data.find, d2, 'a[a]') d3 = {'a': [{'b': [{}, {'c': 'd'}]}]} self.assertEqual(data.find(d3, 'a[0]/b[1]/c'), 'd') self.assertRaises(data.DataNotFoundError, data.find, d3, 'a[1]/b[1]/c')
def test_find(self): d1 = { 'a': 'a', 'b': 1, 'c': { 'd': 'd', 'e': 2, 'more': { 'data': 'here' } } } self.assertEqual(data.find(d1, ''), d1) self.assertEqual(data.find(d1, 'a'), 'a') self.assertEqual(data.find(d1, 'c/e'), 2) self.assertEqual(data.find(d1, 'c/more/'), { 'data': 'here' }) self.assertEqual(data.find(d1, 'c/more/data'), 'here') self.assertRaises(data.DataNotFoundError, data.find, d1, 'c/f') self.assertRaises(data.DataNotFoundError, data.find, d1, 'f') self.assertRaises(data.DataTypeError, data.find, d1, 1) self.assertRaises(data.DataTypeError, data.find, None, 1) self.assertRaises(data.DataTypeError, data.find, None, "foo") self.assertRaises(data.DataTypeError, data.find, "123", "123") self.assertEqual(data.find("123", ""), "123") d2 = { 'a': [ 1, 2, 3 ] } self.assertEqual(data.find(d2, 'a[0]'), 1) self.assertEqual(data.find(d2, 'a[1]'), 2) self.assertEqual(data.find(d2, 'a[2]'), 3) self.assertRaises(data.DataNotFoundError, data.find, d2, 'a[3]') self.assertRaises(data.DataTypeError, data.find, d2, 'a[a]') d3 = { 'a': [ { 'b': [ {}, { 'c': 'd' } ] } ] } self.assertEqual(data.find(d3, 'a[0]/b[1]/c'), 'd') self.assertRaises(data.DataNotFoundError, data.find, d3, 'a[1]/b[1]/c')
def __handle_get_config_dict(self, cmd): """Private function that handles the 'get_config' command where the command has been checked to be a dict""" if 'module_name' in cmd and cmd['module_name'] != '': module_name = cmd['module_name'] try: return ccsession.create_answer(0, data.find(self.config.data, module_name)) except data.DataNotFoundError as dnfe: # no data is ok, that means we have nothing that # deviates from default values return ccsession.create_answer(0, { 'version': config_data.BIND10_CONFIG_DATA_VERSION }) else: return ccsession.create_answer(1, "Bad module_name in get_config command")