gchecky.test.gxml'

Package gchecky :: Package test :: Module gxml' |
[hide private]
[frames] | no frames]

# Source Code for Module gchecky.test.gxml'

      1  import unittest
      2
      3  from gchecky import gxml
      4
      5 -class XmlNodeTestCase(unittest.TestCase):
      6 -    def setUp(self):
      7          class A(gxml.Node):
      8              a = gxml.String('a', default='aa')
      9              b = gxml.String('b', required=False)
     10          self.A = A
     11          self.a = A()
     12          class B(A):
     13              c = gxml.String('c', required=True, values=('cc1', 'cc2'))
     14          self.B = B
     15          self.b = B()
     16 -    def testClassFields(self):
     17          assert (self.A.fields().keys() == ['a','b'])
     18 -    def testSubclassFields(self):
     19          fnames = self.B.fields().keys()
     20          fnames.sort()
     21          assert (fnames == ['a','b','c'])
     22 -    def testObjectValues(self):
     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'
     25 -    def testSubObjectValues(self):
     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
     29 -def xmlNodeSuite():
     30      suite = unittest.TestSuite()
     31      suite.addTest(XmlNodeTestCase('testClassFields'))
     32      suite.addTest(XmlNodeTestCase('testSubclassFields'))
     33      suite.addTest(XmlNodeTestCase('testObjectValues'))
     34      suite.addTest(XmlNodeTestCase('testSubObjectValues'))
     35      return suite
     36
     37 -class XmlFieldTestCase(unittest.TestCase):
     38 -    def testDeconstructPath(self):
     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
     57 -    def testAnyFieldXml(self):
     58          class any_field_test_t(gxml.Document):
     59              tag_name = 'any-field-test-document'
     60              any1 = gxml.Any('any1', required=False)
     61              any2 = gxml.Any('any2', required=True)
     62              any3 = gxml.Any('any3')
     63              any4 = gxml.Any('any4')
     64              any5 = gxml.Any('any5', save_node_and_xml=True)
     65              any6 = gxml.Any('any6', save_node_and_xml=True)
     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
     82 -    def testZipFieldXml(self):
     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
    104 -    def ensure_result_equals_source(self, result, source):
    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                                )
    112 -def xmlFieldSuite():
    113      suite = unittest.TestSuite()
    114      suite.addTest(XmlFieldTestCase('testDeconstructPath'))
    115      suite.addTest(XmlFieldTestCase('testAnyFieldXml'))
    116      suite.addTest(XmlFieldTestCase('testZipFieldXml'))
    117      return suite
    118
    119  if __name__ == '__main__':
    120      unittest.main()
    121