1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
# -*- coding: utf-8 -*-
import logging
if __name__ == '__main__':
logging.basicConfig()
_log = logging.getLogger(__name__)
from pyxb.exceptions_ import *
import unittest
import pyxb.binding.datatypes as xsd
class _TestIntegerType (object):
"""Base class for testing any datatype that descends from integer.
Subclasses should define class variables:
THIS_TYPE = the xsd datatype class
PARENT_TYPE = the next dominating type in the hierarchy
MIN_IN_RANGE = the minimum expressible value
MAX_IN_RANGE = the maximum expressible value
Optional values to set:
ZERO_IN_RANGE = False if zero not valid for subclass; default is True
"""
MIN_IN_RANGE = None
ZERO_IN_RANGE = True
MAX_IN_RANGE = None
def testParentage (self):
self.assertTrue(self.PARENT_TYPE == self.THIS_TYPE.XsdSuperType())
def testRange (self):
if self.MIN_IN_RANGE is not None:
if not ((self.MIN_IN_RANGE-1) in self.PARENT_EXCLUDE):
self.assertRaises(SimpleTypeValueError, self.THIS_TYPE, self.MIN_IN_RANGE - 1)
self.assertEqual(self.MIN_IN_RANGE, self.THIS_TYPE(self.MIN_IN_RANGE))
if self.ZERO_IN_RANGE:
self.assertEqual(0, self.THIS_TYPE(0))
if self.MAX_IN_RANGE is not None:
self.assertEqual(self.MAX_IN_RANGE, self.THIS_TYPE(self.MAX_IN_RANGE))
if not ((self.MAX_IN_RANGE+1) in self.PARENT_EXCLUDE):
self.assertRaises(SimpleTypeValueError, self.THIS_TYPE, self.MAX_IN_RANGE+1)
PARENT_EXCLUDE = []
def testStringConversion (self):
numbers = [ ]
if self.MIN_IN_RANGE is not None:
numbers.extend([self.MIN_IN_RANGE-1, self.MIN_IN_RANGE])
if self.ZERO_IN_RANGE:
numbers.append(0)
if self.MAX_IN_RANGE is not None:
numbers.extend([self.MAX_IN_RANGE, self.MAX_IN_RANGE+1])
for n in numbers:
s = '%d' % (n,)
p = None
if not (n in self.PARENT_EXCLUDE):
p = self.PARENT_TYPE(n)
self.assertEqual(n, p)
if ((self.MIN_IN_RANGE is None) or (self.MIN_IN_RANGE <= n)) \
and ((self.MAX_IN_RANGE is None) or (n <= self.MAX_IN_RANGE)):
bs = self.THIS_TYPE(s)
self.assertEqual(n, bs)
self.assertEqual(s, bs.xsdLiteral())
bp = self.THIS_TYPE(p)
self.assertEqual(n, bp)
else:
self.assertRaises(SimpleTypeValueError, self.THIS_TYPE, s)
if p is not None:
self.assertRaises(SimpleTypeValueError, self.THIS_TYPE, p)
class Test_byte (unittest.TestCase, _TestIntegerType):
THIS_TYPE = xsd.byte
PARENT_TYPE = xsd.short
MIN_IN_RANGE = -128
MAX_IN_RANGE = 127
class Test_unsignedByte (unittest.TestCase, _TestIntegerType):
THIS_TYPE = xsd.unsignedByte
PARENT_TYPE = xsd.unsignedShort
PARENT_EXCLUDE = [ -1 ]
MIN_IN_RANGE = 0
MAX_IN_RANGE = 255
class Test_short (unittest.TestCase, _TestIntegerType):
THIS_TYPE = xsd.short
PARENT_TYPE = xsd.int
MIN_IN_RANGE = -32768
MAX_IN_RANGE = 32767
class Test_unsignedShort (unittest.TestCase, _TestIntegerType):
THIS_TYPE = xsd.unsignedShort
PARENT_TYPE = xsd.unsignedInt
PARENT_EXCLUDE = [ -1 ]
MIN_IN_RANGE = 0
MAX_IN_RANGE = 65535
class Test_int (unittest.TestCase, _TestIntegerType):
THIS_TYPE = xsd.int
PARENT_TYPE = xsd.long
MIN_IN_RANGE = -2147483648
MAX_IN_RANGE = 2147483647
class Test_unsignedInt (unittest.TestCase, _TestIntegerType):
THIS_TYPE = xsd.unsignedInt
PARENT_TYPE = xsd.unsignedLong
PARENT_EXCLUDE = [ -1 ]
MIN_IN_RANGE = 0
MAX_IN_RANGE = 4294967295
class Test_long (unittest.TestCase, _TestIntegerType):
THIS_TYPE = xsd.long
PARENT_TYPE = xsd.integer
MIN_IN_RANGE = -9223372036854775808
MAX_IN_RANGE = 9223372036854775807
class Test_unsignedLong (unittest.TestCase, _TestIntegerType):
THIS_TYPE = xsd.unsignedLong
PARENT_TYPE = xsd.nonNegativeInteger
PARENT_EXCLUDE = [ -1 ]
MIN_IN_RANGE = 0
MAX_IN_RANGE = 18446744073709551615
class Test_negativeInteger (unittest.TestCase, _TestIntegerType):
ZERO_IN_RANGE = False
THIS_TYPE = xsd.negativeInteger
PARENT_TYPE = xsd.nonPositiveInteger
MAX_IN_RANGE = -1
class Test_nonPositiveInteger (unittest.TestCase, _TestIntegerType):
THIS_TYPE = xsd.nonPositiveInteger
PARENT_TYPE = xsd.integer
MAX_IN_RANGE = 0
class Test_nonNegativeInteger (unittest.TestCase, _TestIntegerType):
THIS_TYPE = xsd.nonNegativeInteger
PARENT_TYPE = xsd.integer
MIN_IN_RANGE = 0
class Test_positiveInteger (unittest.TestCase, _TestIntegerType):
THIS_TYPE = xsd.positiveInteger
PARENT_TYPE = xsd.nonNegativeInteger
MIN_IN_RANGE = 1
ZERO_IN_RANGE = False
if __name__ == '__main__':
unittest.main()
|