File: test-integer.py

package info (click to toggle)
pyxb 1.2.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 42,668 kB
  • sloc: python: 239,437; sh: 905; xml: 690; makefile: 60
file content (56 lines) | stat: -rw-r--r-- 2,109 bytes parent folder | download | duplicates (3)
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
# -*- 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
    """
    def testParentage (self):
        self.assertTrue(self.PARENT_TYPE == self.THIS_TYPE.XsdSuperType())

    def testRange (self):
        self.assertRaises(SimpleTypeValueError, self.THIS_TYPE, self.MIN_IN_RANGE - 1)
        self.assertEqual(self.MIN_IN_RANGE, self.THIS_TYPE(self.MIN_IN_RANGE))
        self.assertEqual(0, self.THIS_TYPE(0))
        self.assertEqual(self.MAX_IN_RANGE, self.THIS_TYPE(self.MAX_IN_RANGE))
        self.assertRaises(SimpleTypeValueError, self.THIS_TYPE, self.MAX_IN_RANGE+1)

    PARENT_EXCLUDE = []

    def testStringConversion (self):
        numbers = [ self.MIN_IN_RANGE-1, self.MIN_IN_RANGE, 0, 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 <= n) and (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)
                self.assertRaises(SimpleTypeValueError, self.THIS_TYPE, p)

class Test_int (unittest.TestCase, _TestIntegerType):
    THIS_TYPE = xsd.int
    PARENT_TYPE = xsd.long
    MIN_IN_RANGE = -2147483648
    MAX_IN_RANGE = 2147483647

if __name__ == '__main__':
    unittest.main()