def test_element_schema_to_python_int_list_pass(): es = ElementSchema('numbers', validator=PositiveInteger()) element = Element('numbers', value=['1', 2, 3, '4'], path='/top-0,name') expected = Element('numbers', value=[1, 2, 3, 4], path='/top-0,name') expected.isValidated = True actual = es.to_python(element) nose.tools.eq_(actual, expected)
def test_choice_build_random_pass(): random.seed(0) choice = Choice(options=[name, [drive0, timing]]) actual = choice.build(random_choice=True) expected = [Element(tag='drive', value=None), Element(tag='timing', value=None)] nose.tools.eq_(actual, expected)
def test_sequence_schema_initial_pass(): class RegisterField(SequenceSchema): sequence = [ElementSchema('spirit:name', validator=KeyName(key_names='fieldKey', level=2))] class Register(SequenceSchema): initial = InitKeyStore('fieldKey') sequence = [ElementSchema('spirit:field', validator=RegisterField())] utils.reset_message_counters() register_path = '/register-0,' field0_path = register_path + '/field-0,field0' field1_path = register_path + '/field-1,field1' field0_name_path = field0_path + '/name-0,field0' field1_name_path = field1_path + '/name-1,field1' field0 = Element('spirit:field', path=field0_path, value=[Element('spirit:name', path=field0_name_path, value='field0')]) field1 = Element('spirit:field', path=field1_path, value=[Element('spirit:name', path=field1_name_path, value='field1')]) register = Element('register', path=register_path, value=[field0, field1]) stores = Stores() Register().to_python(register.value, path=register.path, stores=stores) nose.tools.eq_(utils.error_count, 0)
def test_id_count_1_pass(): stores = constraints.Stores() ks = constraints.ID() el = Element('test', path='/el42-0,', attributes=dict(id='ID42')) id_element = Element('id', path=el.path + '@id', value=el.attributes['id']) ks.to_python(id_element, stores=stores) actual = stores.idStore.id_count() nose.tools.eq_(actual, 1)
def test_build_choice_pass(): class Test(SequenceSchema): sequence = [name, Choice(options=[count, wire])] actual = Test().build(random_choice=False) expected = [Element(tag='name', value='NCName'), Element(tag='count', value=None)] nose.tools.eq_(actual, expected)
def test_element_to_dict_three_levels_mix_ns_prefix_pass(): level2 = Element('post:level2', value='level2Name') level1 = Element('pre:level1', value=[level2]) root = Element('parent', value=[level1]) actual = root.to_dict nose.tools.eq_( actual, OrderedDict([('pre:level1', OrderedDict([('post:level2', 'level2Name') ]))]))
def test_check_keys_path_mismatch_fail(): stores = constraints.Stores() ks = constraints.InitKeyStore('FieldKey') path = "/register-0,reg6" el = Element('reg', path=path) ks.to_python(el, path=path, stores=stores) ck = constraints.CheckKeys(key_names='FieldKey', level=1) field = Element('field', value='field22', path='/field-2,field22') ck.to_python(field, stores=stores)
def test_build_pass(): class Test(SequenceSchema): sequence = [name, count, wire] actual = Test().build() expected = [Element(tag='name', value='NCName'), Element(tag='count', value=None), Element(tag='wire', value=None)] nose.tools.eq_(actual, expected)
def test_check_ids_add_id_pass(): stores = constraints.Stores() ks = constraints.ID() el = Element('test', path='/el42-0,', attributes=dict(id='ID42')) id_element = Element('id', path=el.path + '@id', value=el.attributes['id']) ks.to_python(id_element, stores=stores) stores.idStore.add_id('key_value', '/ads/cgfh') expected = {'ID:/': {'key_value': '/ads/cgfh', 'ID42': '/el42-0,@id'}} nose.tools.eq_(stores.idStore.keys, expected)
def test_check_keys_duplicate_value_fail(): stores = constraints.Stores() ks = constraints.InitKeyStore('FieldKey') path = "/register-0,reg6" ks.to_python(None, path=path, stores=stores) ck = constraints.CheckKeys(key_names='FieldKey', level=1) field0 = Element('field', value='field22', path=path + '/field-2,field22') field1 = Element('field', value='field22', path=path + '/field-12,field22') nose.tools.eq_(ck.to_python(field0, stores=stores), 'field22') ck.to_python(field1, stores=stores)
def test_element_to_dict_two_levels_three_children_pass(): child0 = Element('pre:child', value='childName1') child1 = Element('pre:child', value='childName2') child2 = Element('pre:child', value='childName3') parent = Element('pre:parent', value=[child0, child1, child2]) actual = parent.to_dict nose.tools.eq_( actual, OrderedDict([('pre:child', ['childName1', 'childName2', 'childName3'])]))
def test_check_uniques_empty_value_pass(): stores = constraints.Stores() ks = constraints.InitUniqueStore('FieldKey') path = "/register-0,reg6" el = Element('reg', path=path) ks.to_python(el, stores=stores) ck = constraints.CheckUniques(key_names='FieldKey', level=1) field = Element('field', value=None, path=path + '/field-2,field22') nose.tools.eq_(ck.to_python(field.value, path=field.path, stores=stores), None)
def test_document_to_dict_two_levels_round_trip_pass(): p = '/xsi:parent-0,/xsi:child-' child = Element('xsi:child', value='childName1', path=p + '0,') root = Element('xsi:parent', value=[child], path='/xsi:parent-0,') stats = defaultdict(int) stats['xsi:parent'] = 1 stats['xsi:child'] = 1 doc = Document('test', [nameSpaces[0]], root, stats=stats) xml_dict = doc.to_dict doc_new = create_document('test', xml_dict) nose.tools.eq_(doc.__dict__.items(), doc_new.__dict__.items())
def test_match_refs_value_not_found_fail(): stores = constraints.Stores() root_element = Element('root', value=None, path='/component-0,test') ref_element = Element( 'memoryMapRef', value='myMemoryMap', path='/component-0,test/busInterface-0,/memoryMapRef-0,@memoryMapRef') constraints.InitKeyStore('memoryMapKey').to_python(root_element, path=root_element.path, stores=stores) kr = constraints.SetupKeyRefsStore('memoryMapKey') kr.to_python(ref_element, stores=stores) constraints.match_refs(stores)
def test_document_to_dict_two_levels_empty_ns_prefix_pass(): child = Element('child', value='childName1') root = Element('prefix:parent', value=[child]) doc = Document('test', [nameSpaces[2]], root) actual = doc.to_dict nose.tools.eq_( actual, OrderedDict([( 'prefix:parent', OrderedDict([ ('@xmlns', 'http://www.spiritconsortium.org/XMLSchema/SPIRIT/1685-2009'), ('child', 'childName1') ]))]))
def test_create_element_two_levels(): register_copy = copy(register) field10 = OrderedDict([('@id', 'ID10'), ('name', 'field10'), ('bitOffset', '0'), ('bitWidth', '2'), ('volatile', 'false')]) field11 = OrderedDict([('@id', 'ID11'), ('name', 'field11'), ('bitOffset', '2'), ('bitWidth', '4'), ('volatile', 'true')]) register_copy['field'] = [field10, field11] el = create_element('register', register_copy, nameSpaces) expected = [ Element( tag='field', attributes=OrderedDict([('id', 'ID10')]), path="/register-0,reg6/field-0,field10", value=[ Element(tag='name', path="/register-0,reg6/field-0,field10/name-0,", value='field10'), Element(tag='bitOffset', path="/register-0,reg6/field-0,field10/bitOffset-0,", value='0'), Element(tag='bitWidth', path="/register-0,reg6/field-0,field10/bitWidth-0,", value='2'), Element(tag='volatile', path="/register-0,reg6/field-0,field10/volatile-0,", value='false') ]), Element( tag='field', attributes=OrderedDict([('id', 'ID11')]), path="/register-0,reg6/field-1,field11", value=[ Element(tag='name', path="/register-0,reg6/field-1,field11/name-0,", value='field11'), Element(tag='bitOffset', path="/register-0,reg6/field-1,field11/bitOffset-0,", value='2'), Element(tag='bitWidth', path="/register-0,reg6/field-1,field11/bitWidth-0,", value='4'), Element(tag='volatile', path="/register-0,reg6/field-1,field11/volatile-0,", value='true') ]) ] nose.tools.eq_(el.value[5:], expected)
def test_create_element_attribute_text_pass(): value = OrderedDict([('text', OrderedDict([('@attr', '42'), ('#text', 'root value')]))]) el = create_element('root', value, nameSpaces) nose.tools.eq_( el, Element(tag='root', path="/root-0,", value=[ Element(tag='text', attributes=OrderedDict([('attr', '42')]), path="/root-0,/text-0,", value='root value') ]))
def test_element_str(): actual = str( Element('tagName', value=[1, 2, 3], attributes=dict(id='ID42'), path='/root-0,name')) nose.tools.eq_(actual, '<tagName>')
def test_element_schema_build_no_value_unbounded_maxoccurs_pass(): es = ElementSchema('dim', validator=PositiveInteger(), unbounded=True) path = '/dim-0,' stores = Stores() actual = es.build(path=path, stores=stores, maxOccurs=2) expected = Element('dim', value=[1, 1], path=path) nose.tools.eq_(actual, expected)
def test_element_schema_validate_attributes_validator_none_pass(): es = ElementSchema('testable', attributes=ElementSchema('index', validator=None)) element = Element('testable', attributes=dict(index='42')) # noinspection PyProtectedMember validated_attributes = es._validate_attributes(element) nose.tools.eq_(validated_attributes, dict(index='42'))
def test_document_to_dict_two_levels_three_children_pass(): child0 = Element('xsi:child', value='childName1') child1 = Element('xsi:child', value='childName2') child2 = Element('xsi:child', value='childName3') root = Element('test:parent', value=[child0, child1, child2]) doc = Document('test', [nameSpaces[0]], root) actual = doc.to_dict nose.tools.eq_( actual, OrderedDict([ ('test:parent', OrderedDict([ ('@xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'), ('xsi:child', ['childName1', 'childName2', 'childName3']) ])) ]))
def test_check_keys_single_key_build_no_value_pass(): stores = constraints.Stores() ks = constraints.InitKeyStore('FieldKey') path = "/register-0,reg6" ks.build(path=path, stores=stores) ck = constraints.CheckKeys(key_names='FieldKey', level=1) field = Element('field', value='field22', path=path + '/field-2,field22') nose.tools.eq_(ck.build(path=field.path, stores=stores), 'FieldKey0')
def test_check_keys_empty_value_fail(): stores = constraints.Stores() ks = constraints.InitKeyStore('FieldKey') path = "/register-0,reg6" ks.to_python(None, path=path, stores=stores) ck = constraints.CheckKeys(key_names='FieldKey', level=1) field = Element('field', path=path + '/field-2,field22') ck.to_python(field, stores=stores)
def test_element_schema_validate_attributes_min_pass(): es = ElementSchema('testable', attributes=ElementSchema('index', validator=PositiveInteger())) element = Element('testable', attributes=dict(index='42')) # noinspection PyProtectedMember validated_attributes = es._validate_attributes(element) nose.tools.eq_(validated_attributes, dict(index=42))
def test_match_id_pass(): stores = constraints.Stores() path = '/root-0,' ck = constraints.ID() field = Element('field', value='field22', path=path + '/field-2,field22') ck.to_python(field, stores=stores) instance_path = stores.idStore.match_id('field22') nose.tools.eq_(instance_path, path + '/field-2,field22')
def test_check_uniques_single_key_pass(): stores = constraints.Stores() ks = constraints.InitUniqueStore('FieldKey') path = "/register-0,reg6" ks.to_python(None, path=path, stores=stores) ck = constraints.CheckUniques(key_names='FieldKey', level=1) field = Element('field', value='field22', path=path + '/field-2,field22') actual = ck.to_python(field.value, path=field.path, stores=stores) nose.tools.eq_(actual, 'field22')
def test_check_uniques_single_key_build_no_value_pass(): stores = constraints.Stores() ks = constraints.InitUniqueStore('UniqueKey') path = "/register-0,reg6" ks.build(path=path, stores=stores) ck = constraints.CheckUniques(key_names='UniqueKey', level=1) field = Element('field', value='field22', path=path + '/field-2,field22') actual = ck.build(path=field.path, stores=stores) nose.tools.eq_(actual, 'UniqueKey0')
def test_element_schema_validate_attributes_extra_fail(): utils.reset_message_counters() es = ElementSchema('testable', attributes=ElementSchema('index', validator=PositiveInteger())) element = Element('testable', attributes=dict(index='42', refer='extra')) # noinspection PyProtectedMember es._validate_attributes(element) nose.tools.eq_(utils.error_count, 1)
def test_match_ref_pass(): stores = constraints.Stores() ks = constraints.InitKeyStore('key_name') path = '/root-0,' ks.to_python(None, path=path, stores=stores) ck = constraints.CheckKeys(key_names=['OtherKey', 'key_name'], level=1) field = Element('field', value='field22', path=path + '/field-2,field22') ck.to_python(field.value, path=field.path, stores=stores) instance_path = stores.keyStore.match_ref('key_name', 'field22') nose.tools.eq_(instance_path, path + '/field-2,field22')
def test_element_repr(): id_string = str('ID42') actual = repr( Element('tagName', value=[1, 2, 3], attributes=dict(id=id_string), path='/root-0,name')) expected = 'Element(tag=\'tagName\', attributes={\'id\': \'ID42\'}, ' \ 'path="/root-0,name", value=[1, 2, 3])' nose.tools.eq_(actual, str(expected))