Commit 7da40bab authored by Jelte Jansen's avatar Jelte Jansen

additional tests + one fix


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac405@3675 e5f2f494-b856-4b98-b285-d166d9295462
parent dbfbbb2b
......@@ -90,7 +90,9 @@ def split_identifier_list_indices(identifier):
'a[0]/b[1]/c[2]' will return ('a[0]/b[1]/c, [2])
"""
if type(identifier) != str:
raise DataTypeError("identifier in split_identifier_list_indices() contains '/': " + str(identifier))
raise DataTypeError("identifier in "
"split_identifier_list_indices() "
"not a string: " + str(identifier))
id_parts = split_identifier(identifier)
id_str = id_parts[-1]
i = id_str.find('[')
......@@ -110,7 +112,9 @@ def split_identifier_list_indices(identifier):
raise DataTypeError("List index in " + identifier + " not an integer")
id_str = id_str[e + 1:]
i = id_str.find('[')
if id.find(']') >= 0:
if i > 0:
raise DataTypeError("Bad format in identifier: " + str(identifier))
if id.find(']') >= 0 or len(id_str) > 0:
raise DataTypeError("Bad format in identifier: " + str(identifier))
id_parts = id_parts[:-1]
id_parts.append(id)
......@@ -231,7 +235,4 @@ def parse_value_str(value_str):
except ValueError as ve:
# simply return the string itself
return value_str
except SyntaxError as ve:
# simply return the string itself
return value_str
......@@ -70,6 +70,11 @@ class TestData(unittest.TestCase):
c = { "a": { "b": "c" } }
data.remove_identical(a, b)
self.assertEqual(a, c)
self.assertRaises(data.DataTypeError, data.remove_identical,
a, 1)
self.assertRaises(data.DataTypeError, data.remove_identical,
1, b)
def test_merge(self):
d1 = { 'a': 'a', 'b': 1, 'c': { 'd': 'd', 'e': 2 } }
......@@ -83,7 +88,7 @@ class TestData(unittest.TestCase):
self.assertRaises(data.DataTypeError, data.merge, None, None)
def testsplit_identifier_list_indices(self):
def test_split_identifier_list_indices(self):
id, indices = data.split_identifier_list_indices('a')
self.assertEqual(id, 'a')
self.assertEqual(indices, None)
......@@ -98,6 +103,11 @@ class TestData(unittest.TestCase):
self.assertRaises(data.DataTypeError, data.split_identifier_list_indices, 'a[')
self.assertRaises(data.DataTypeError, data.split_identifier_list_indices, 'a]')
self.assertRaises(data.DataTypeError, data.split_identifier_list_indices, 'a[[0]]')
self.assertRaises(data.DataTypeError, data.split_identifier_list_indices, 'a[0]a')
self.assertRaises(data.DataTypeError, data.split_identifier_list_indices, 'a[0]a[1]')
self.assertRaises(data.DataTypeError, data.split_identifier_list_indices, 1)
def test_find(self):
d1 = { 'a': 'a', 'b': 1, 'c': { 'd': 'd', 'e': 2, 'more': { 'data': 'here' } } }
......@@ -146,7 +156,9 @@ class TestData(unittest.TestCase):
self.assertRaises(data.DataTypeError, data.set, d1, 1, 2)
self.assertRaises(data.DataTypeError, data.set, 1, "", 2)
self.assertRaises(data.DataTypeError, data.set, d1, 'c[1]', 2)
self.assertRaises(data.DataTypeError, data.set, d1, 'c[1][2]', 2)
self.assertRaises(data.DataNotFoundError, data.set, d1, 'c/f[5]', 2)
self.assertRaises(data.DataNotFoundError, data.set, d1, 'c/f[5][2]', 2)
d3 = {}
e3 = data.set(d3, "does/not/exist", 123)
......@@ -202,6 +214,9 @@ class TestData(unittest.TestCase):
self.assertEqual(data.parse_value_str("{ \"a\": \"b\", \"c\": 1 }"), { 'a': 'b', 'c': 1 })
self.assertEqual(data.parse_value_str("[ a c"), "[ a c")
self.assertEqual(data.parse_value_str(1), None)
if __name__ == '__main__':
#if not 'CONFIG_TESTDATA_PATH' in os.environ:
# print("You need to set the environment variable CONFIG_TESTDATA_PATH to point to the directory containing the test data files")
......
......@@ -22,6 +22,7 @@ two through the classes in ccsession)
import isc.cc.data
import isc.config.module_spec
import ast
class ConfigDataError(Exception): pass
......@@ -56,14 +57,14 @@ def check_type(spec_part, value):
raise isc.cc.data.DataTypeError(str(value) + " is not a map")
def convert_type(spec_part, value):
"""Convert the give value(type is string) according specification
"""Convert the given value(type is string) according specification
part relevant for the value. Raises an isc.cc.data.DataTypeError
exception if conversion failed.
"""
if type(spec_part) == dict and 'item_type' in spec_part:
data_type = spec_part['item_type']
else:
raise isc.cc.data.DataTypeError(str("Incorrect specification part for type convering"))
raise isc.cc.data.DataTypeError(str("Incorrect specification part for type conversion"))
try:
if data_type == "integer":
......@@ -81,18 +82,25 @@ def convert_type(spec_part, value):
ret.append(convert_type(spec_part['list_item_spec'], item))
elif type(value) == str:
value = value.split(',')
for item in value:
for item in value:
sub_value = item.split()
for sub_item in sub_value:
ret.append(convert_type(spec_part['list_item_spec'], sub_item))
ret.append(convert_type(spec_part['list_item_spec'],
sub_item))
if ret == []:
raise isc.cc.data.DataTypeError(str(value) + " is not a list")
return ret
elif data_type == "map":
return dict(value)
# todo: check types of map contents too
map = ast.literal_eval(value)
if type(map) == dict:
# todo: check types of map contents too
return map
else:
raise isc.cc.data.DataTypeError(
"Value in convert_type not a string "
"specifying a dict")
else:
return value
except ValueError as err:
......@@ -416,6 +424,7 @@ class MultiConfigData:
if spec:
spec_part = find_spec_part(spec.get_config_spec(), id)
if type(spec_part) == list:
# list of items to show
for item in spec_part:
value, status = self.get_value("/" + identifier\
+ "/" + item['item_name'])
......@@ -424,6 +433,7 @@ class MultiConfigData:
value, status)
result.append(entry)
elif type(spec_part) == dict:
# Sub-specification
item = spec_part
if item['item_type'] == 'list':
li_spec = item['list_item_spec']
......
......@@ -639,6 +639,8 @@ class TestUIModuleCCSession(unittest.TestCase):
self.assertEqual({'Spec2': {'item5': ['foo']}}, uccs._local_changes)
uccs.remove_value("Spec2/item5[0]", None)
self.assertEqual({'Spec2': {'item5': []}}, uccs._local_changes)
uccs.remove_value("", "Spec2/item5[0]")
self.assertEqual({'Spec2': {'item5': []}}, uccs._local_changes)
def test_commit(self):
fake_conn = fakeUIConn()
......
......@@ -107,6 +107,8 @@ class TestConfigData(unittest.TestCase):
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, "a")
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, [ 1, 2 ])
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, { "a": 1 })
self.assertRaises(isc.cc.data.DataTypeError, convert_type, 1, "a")
self.assertRaises(isc.cc.data.DataTypeError, convert_type, { 'somedict': 'somevalue' }, "a")
spec_part = find_spec_part(config_spec, "value2")
self.assertEqual(1.1, convert_type(spec_part, '1.1'))
......@@ -146,6 +148,18 @@ class TestConfigData(unittest.TestCase):
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, [ "1", "b" ])
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, { "a": 1 })
spec_part = find_spec_part(config_spec, "value6")
self.assertEqual({}, convert_type(spec_part, '{}'))
self.assertEqual({ 'v61': 'a' }, convert_type(spec_part, '{ \'v61\': \'a\' }'))
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, 1.1)
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, True)
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, "a")
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, "1")
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, [ "a", "b" ])
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, [ "1", "b" ])
self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, { "a": 1 })
spec_part = find_spec_part(config_spec, "value7")
self.assertEqual(['1', '2'], convert_type(spec_part, '1, 2'))
self.assertEqual(['1', '2', '3'], convert_type(spec_part, '1 2 3'))
......@@ -342,6 +356,12 @@ class TestMultiConfigData(unittest.TestCase):
self.assertEqual(1, value)
value = self.mcd.get_default_value("/Spec2/item1")
self.assertEqual(1, value)
value = self.mcd.get_default_value("Spec2/item5[0]")
self.assertEqual('a', value)
value = self.mcd.get_default_value("Spec2/item5[5]")
self.assertEqual(None, value)
value = self.mcd.get_default_value("Spec2/item5[0][1]")
self.assertEqual(None, value)
value = self.mcd.get_default_value("Spec2/item6/value1")
self.assertEqual('default', value)
value = self.mcd.get_default_value("Spec2/item6/value2")
......@@ -419,6 +439,8 @@ class TestMultiConfigData(unittest.TestCase):
maps = self.mcd.get_value_maps("/Spec2/item5")
self.assertEqual([{'default': False, 'type': 'string', 'name': 'list_element', 'value': 'a', 'modified': False},
{'default': False, 'type': 'string', 'name': 'list_element', 'value': 'b', 'modified': False}], maps)
maps = self.mcd.get_value_maps("/Spec2/item5[0]")
self.assertEqual([{'default': True, 'modified': False, 'name': 'list_element', 'type': 'string', 'value': 'a'}], maps)
maps = self.mcd.get_value_maps("/Spec2/item1")
self.assertEqual([{'default': False, 'type': 'integer', 'name': 'item1', 'value': 2, 'modified': False}], maps)
maps = self.mcd.get_value_maps("/Spec2/item2")
......@@ -472,6 +494,8 @@ class TestMultiConfigData(unittest.TestCase):
self.assertEqual(['Spec2/item1', 'Spec2/item2', 'Spec2/item3', 'Spec2/item4', 'Spec2/item5/', 'Spec2/item6/value1', 'Spec2/item6/value2'], config_items)
config_items = self.mcd.get_config_item_list("Spec2")
self.assertEqual(['Spec2/item1', 'Spec2/item2', 'Spec2/item3', 'Spec2/item4', 'Spec2/item5/', 'Spec2/item6/'], config_items)
config_items = self.mcd.get_config_item_list("/Spec2")
self.assertEqual(['Spec2/item1', 'Spec2/item2', 'Spec2/item3', 'Spec2/item4', 'Spec2/item5/', 'Spec2/item6/'], config_items)
config_items = self.mcd.get_config_item_list("Spec2", True)
self.assertEqual(['Spec2/item1', 'Spec2/item2', 'Spec2/item3', 'Spec2/item4', 'Spec2/item5/', 'Spec2/item6/value1', 'Spec2/item6/value2'], config_items)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment