1 import unittest
2
3 from gchecky import gxml
4
14 self.B = B
15 self.b = B()
17 assert (self.A.fields().keys() == ['a','b'])
19 fnames = self.B.fields().keys()
20 fnames.sort()
21 assert (fnames == ['a','b','c'])
23 assert self.a.a == 'aa', 'Default value is not set to a field'
24 assert self.a.b == None, 'None is not set to a non-required field'
26 assert self.b.a == self.a.a and self.b.b == self.a.b, ''
27 assert self.b.c == 'cc1', 'None is not set to a non-required field'
28
36
39 """Test Field.deconstruct_path method"""
40 assert gxml.Field.deconstruct_path('/') == ([], None)
41 assert gxml.Field.deconstruct_path('//') == ([], None)
42 assert gxml.Field.deconstruct_path('aa') == (['aa'], None)
43 assert gxml.Field.deconstruct_path('/aa') == (['aa'], None)
44 assert gxml.Field.deconstruct_path('aa/') == (['aa'], None)
45 assert gxml.Field.deconstruct_path('/aa/') == (['aa'], None)
46 assert gxml.Field.deconstruct_path('aa/bb/cc') == (['aa', 'bb', 'cc'], None)
47 assert gxml.Field.deconstruct_path('a/b/c') == (['a', 'b', 'c'], None)
48 assert gxml.Field.deconstruct_path('@bb') == ([], 'bb')
49 assert gxml.Field.deconstruct_path('@b') == ([], 'b')
50 assert gxml.Field.deconstruct_path('/@bb') == ([], 'bb')
51 assert gxml.Field.deconstruct_path('//@bb') == ([], 'bb')
52 assert gxml.Field.deconstruct_path('aa/@bb') == (['aa'], 'bb')
53 assert gxml.Field.deconstruct_path('/aa/@bb') == (['aa'], 'bb')
54 assert gxml.Field.deconstruct_path('aa/bb/@cc') == (['aa', 'bb'], 'cc')
55
56 import gxml
66
67 from xml.dom.Document import Node as XmlNode
68 src = any_field_test_t(
69 any1=None,
70 any2='string',
71 any3=u'unicode_str',
72 any4=('tupe', 'with', 'values', 11, 1.5),
73 any5=('tuple', ('of', ((u'nested',), 'tuples'))),
74 any6={'some':'dictionary', u'with':u'values', 'aa':17})
75 result = gxml.Document.fromxml(src.toxml())
76 self.ensure_result_equals_source(result, src)
77 assert isinstance(result.any5_dom, XmlNode)
78 assert unicode(result.any5_xml) == result.any5_xml
79 assert isinstance(result.any6_dom, XmlNode)
80 assert unicode(result.any6_xml) == result.any6_xml
81 return True
83 class zip_field_test_t(gxml.Document):
84 tag_name = 'zip-field-test-document'
85 any1 = gxml.Zip('zip1', complete=False)
86 any2 = gxml.Zip('zip2', complete=False)
87 any3 = gxml.Zip('zip3', complete=False)
88 any4 = gxml.Zip('zip4', complete=False)
89 any5 = gxml.Zip('zip5', complete=True)
90 any6 = gxml.Zip('zip6', complete=True)
91
92 from xml.dom.Document import Node as XmlNode
93 src = zip_field_test_t(
94 any1='10014-3433',
95 any2='10014-34*',
96 any3='10014-*',
97 any4='1001*',
98 any5='10014-34',
99 any6='75001'
100 )
101 result = gxml.Document.fromxml(src.toxml())
102 self.ensure_result_equals_source(result, src)
103 return True
105 for fname in vars(source):
106 equals = hasattr(result, fname) and getattr(source, fname) == getattr(result, fname)
107 assert equals, 'Field %s content does not match: "%s" vs "%s"' % (
108 fname,
109 getattr(source, fname),
110 getattr(source, fname, None)
111 )
118
119 if __name__ == '__main__':
120 unittest.main()
121