Beispiel #1
0
    def test_xmlschema_proxy(self):
        context = XPathContext(root=self.etree.XML(
            '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/>'))

        self.wrong_syntax("schema-element(*)")
        self.wrong_name("schema-element(nil)")
        self.wrong_name("schema-element(xs:string)")
        self.check_value("schema-element(xs:complexType)", MissingContextError)
        self.check_value("self::schema-element(xs:complexType)", NameError,
                         context)
        self.check_value("self::schema-element(xs:schema)", [context.item],
                         context)
        self.check_tree("schema-element(xs:group)",
                        '(schema-element (: (xs) (group)))')

        context.item = AttributeNode(XML_LANG, 'en')
        self.wrong_syntax("schema-attribute(*)")
        self.wrong_name("schema-attribute(nil)")
        self.wrong_name("schema-attribute(xs:string)")
        self.check_value("schema-attribute(xml:lang)", MissingContextError)
        self.check_value("schema-attribute(xml:lang)", NameError, context)
        self.check_value("self::schema-attribute(xml:lang)", [context.item],
                         context)
        self.check_tree("schema-attribute(xsi:schemaLocation)",
                        '(schema-attribute (: (xsi) (schemaLocation)))')

        token = self.parser.parse("self::schema-attribute(xml:lang)")
        context.item = AttributeNode(XML_LANG, 'en')
        context.axis = 'attribute'
        self.assertEqual(list(token.select(context)), [context.item])
    def test_datetime_constructor(self):
        tz1 = Timezone(datetime.timedelta(hours=5, minutes=24))
        self.check_value('xs:dateTime(())', [])
        self.check_value('xs:dateTime("1969-07-20T20:18:00")',
                         DateTime10(1969, 7, 20, 20, 18))
        self.check_value(
            'xs:dateTime(xs:untypedAtomic("1969-07-20T20:18:00"))',
            DateTime10(1969, 7, 20, 20, 18))
        self.check_value(
            'xs:dateTime("2000-05-10T21:30:00+05:24")',
            datetime.datetime(2000, 5, 10, hour=21, minute=30, tzinfo=tz1))
        self.check_value('xs:dateTime("1999-12-31T24:00:00")',
                         datetime.datetime(2000, 1, 1, 0, 0))
        self.check_value('xs:dateTime(xs:date("1969-07-20"))',
                         DateTime10(1969, 7, 20))

        self.check_value('xs:dateTime(xs:date("1969-07-20"))', DateTime10)
        with self.assertRaises(AssertionError):
            self.check_value('xs:dateTime(xs:date("1969-07-20"))', DateTime)

        self.parser._xsd_version = '1.1'
        try:
            self.check_value('xs:dateTime(xs:date("1969-07-20"))',
                             DateTime(1969, 7, 20))
            self.check_value('xs:dateTime(xs:date("1969-07-20"))', DateTime)
        finally:
            self.parser._xsd_version = '1.0'

        self.wrong_value('xs:dateTime("2000-05-10t21:30:00+05:24")')
        self.wrong_value('xs:dateTime("2000-5-10T21:30:00+05:24")')
        self.wrong_value('xs:dateTime("2000-05-10T21:3:00+05:24")')
        self.wrong_value('xs:dateTime("2000-05-10T21:13:0+05:24")')
        self.wrong_value('xs:dateTime("2000-05-10T21:13:0")')
        self.check_value('xs:dateTime("-25252734927766554-12-31T12:00:00")',
                         OverflowError)
        self.wrong_type('xs:dateTime(50)', 'FORG0006',
                        '1st argument has an invalid type')
        self.wrong_type('xs:dateTime("2000-05-10T21:30:00", "+05:24")',
                        'XPST0017')

        root = self.etree.XML('<root a="1969-07-20T20:18:00"/>')
        context = XPathContext(root)
        self.check_value('xs:dateTime(@a)',
                         DateTime10(1969, 7, 20, 20, 18),
                         context=context)

        context.item = AttributeNode('a', str(DateTime10(1969, 7, 20, 20, 18)))
        self.check_value('xs:dateTime(.)',
                         DateTime10(1969, 7, 20, 20, 18),
                         context=context)

        context.item = AttributeNode('a', 'true')
        self.check_value('xs:dateTime(.)', ValueError, context=context)

        context.item = DateTime10(1969, 7, 20, 20, 18)
        self.check_value('xs:dateTime(.)',
                         DateTime10(1969, 7, 20, 20, 18),
                         context=context)
Beispiel #3
0
    def test_date_constructor(self):
        tz = Timezone(datetime.timedelta(hours=-14, minutes=0))

        self.check_value('xs:date("2017-01-19")',
                         datetime.datetime(2017, 1, 19))
        self.check_value('xs:date(xs:untypedAtomic("2017-01-19"))',
                         datetime.datetime(2017, 1, 19))
        self.check_value('xs:date("2011-11-11-14:00")',
                         datetime.datetime(2011, 11, 11, tzinfo=tz))
        self.check_value('xs:date(xs:dateTime("1969-07-20T20:18:00"))',
                         Date10(1969, 7, 20))

        self.wrong_value('xs:date("2011-11-11-14:01")')
        self.wrong_value('xs:date("11-11-11")')
        self.check_value('xs:date(())', [])

        root = self.etree.XML('<root a="2017-01-19"/>')
        context = XPathContext(root)
        self.check_value('xs:date(@a)', Date10(2017, 1, 19), context=context)

        class DummyXsdDateType(xpath_test_class.DummyXsdType):
            def is_simple(self):
                return True

            def decode(self, obj, *args, **kwargs):
                return Date10.fromstring(obj)

            def validate(self, obj, *args, **kwargs):
                if not isinstance(obj, Date10):
                    raise TypeError()

        context.item = AttributeNode('a', 'true', xsd_type=DummyXsdDateType())
        self.check_value('xs:date(.)', TypeError, context=context)

        context.item = AttributeNode('a', str(Date10(2017, 1, 19)))
        self.check_value('xs:date(.)', Date10(2017, 1, 19), context=context)

        context.item = AttributeNode('a', 'true')
        self.check_value('xs:date(.)', ValueError, context=context)

        root = self.etree.XML("<root>2017-10-02</root>")
        context = XPathContext(root)
        self.check_value('xs:date(.)', Date10(2017, 10, 2), context=context)

        root = self.etree.XML("<root>2017<a>-10-02</a></root>")
        context = XPathContext(root)
        self.check_value('xs:date(.)', Date10(2017, 10, 2), context=context)

        context = XPathContext(root, item=Date10(2017, 10, 2))
        self.check_value('xs:date(.)', Date10(2017, 10, 2), context=context)
Beispiel #4
0
    def test_iter_schema_nodes_function(self):
        vh_elements = set(e for e in self.xs1.maps.iter_components(XsdElement)
                          if e.target_namespace == self.xs1.target_namespace)

        self.assertEqual(set(iter_schema_nodes(self.xs1)), vh_elements | {self.xs1})
        self.assertEqual(set(iter_schema_nodes(self.xs1, with_root=False)), vh_elements)

        vh_nodes = set()
        for node in self.xs1.maps.iter_components((XsdElement, XsdAttribute)):
            if node.target_namespace != self.xs1.target_namespace:
                continue
            elif isinstance(node, XsdAttribute):
                vh_nodes.add(AttributeNode(node.local_name, node))
            else:
                vh_nodes.add(node)

        self.assertEqual(set(iter_schema_nodes(self.xs1, with_attributes=True)),
                         vh_nodes | {self.xs1})
        self.assertEqual(set(iter_schema_nodes(self.xs1, with_root=False, with_attributes=True)),
                         vh_nodes)

        cars = self.xs1.elements['cars']
        car = self.xs1.find('//vh:car')
        typed_cars = TypedElement(cars, cars.type, None)
        self.assertListEqual(list(iter_schema_nodes(cars)), [cars, car])
        self.assertListEqual(list(iter_schema_nodes(typed_cars)), [cars, car])
        self.assertListEqual(list(iter_schema_nodes(cars, with_root=False)), [car])
        self.assertListEqual(list(iter_schema_nodes(typed_cars, with_root=False)), [car])
        self.assertEqual(len(list(iter_schema_nodes(cars, with_attributes=True))), 4)
        self.assertEqual(len(list(iter_schema_nodes(cars, False, with_attributes=True))), 3)
    def test_date_constructor(self):
        tz = Timezone(datetime.timedelta(hours=-14, minutes=0))

        self.check_value('xs:date("2017-01-19")',
                         datetime.datetime(2017, 1, 19))
        self.check_value('xs:date(xs:untypedAtomic("2017-01-19"))',
                         datetime.datetime(2017, 1, 19))
        self.check_value('xs:date("2011-11-11-14:00")',
                         datetime.datetime(2011, 11, 11, tzinfo=tz))
        self.check_value('xs:date(xs:dateTime("1969-07-20T20:18:00"))',
                         Date10(1969, 7, 20))

        self.wrong_value('xs:date("2011-11-11-14:01")')
        self.wrong_value('xs:date("11-11-11")')
        self.check_value('xs:date(())', [])

        root = self.etree.XML('<root a="2017-01-19"/>')
        context = XPathContext(root)
        self.check_value('xs:date(@a)', Date10(2017, 1, 19), context=context)

        context.item = AttributeNode('a', str(Date10(2017, 1, 19)))
        self.check_value('xs:date(.)', Date10(2017, 1, 19), context=context)

        context.item = AttributeNode('a', 'true')
        self.check_value('xs:date(.)', ValueError, context=context)
        context.item = AttributeNode('a', True)
        self.check_value('xs:date(.)', ValueError, context=context)

        with patch.multiple(self.dummy_type, is_simple=lambda x: True):
            context.item = TypedAttribute(AttributeNode('a', 'true'),
                                          self.dummy_type, True)
            self.check_value('xs:date(.)', TypeError, context=context)

        root = self.etree.XML("<root>2017-10-02</root>")
        context = XPathContext(root)
        self.check_value('xs:date(.)', Date10(2017, 10, 2), context=context)

        root = self.etree.XML("<root>2017<a>-10-02</a></root>")
        context = XPathContext(root)
        self.check_value('xs:date(.)', Date10(2017, 10, 2), context=context)

        context = XPathContext(root, item=Date10(2017, 10, 2))
        self.check_value('xs:date(.)', Date10(2017, 10, 2), context=context)
    def test_double_constructor(self):
        self.wrong_value('xs:double("world")')
        self.check_value('xs:double("39.09")', 39.09)
        self.check_value('xs:double(xs:untypedAtomic("39.09"))', 39.09)
        self.check_value('xs:double(-5)', -5.0)
        self.check_value('xs:double(-5)', float)

        root = self.etree.XML('<root a="10.3"/>')
        context = XPathContext(root, item=AttributeNode('a', 10.3))
        self.check_value('xs:double(.)', float, context=context)
        self.check_value('xs:double(.)', 10.3, context=context)
    def test_xmlschema_proxy(self):
        context = XPathContext(root=self.etree.XML(
            '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/>'))

        self.wrong_name("schema-element(nil)")
        self.wrong_name("schema-element(xs:string)")
        self.check_value("schema-element(xs:complexType)", None)
        self.check_value("schema-element(xs:schema)", context.item, context)
        self.check_tree("schema-element(xs:group)",
                        '(schema-element (: (xs) (group)))')

        context.item = AttributeNode(XML_LANG, 'en')
        self.wrong_name("schema-attribute(nil)")
        self.wrong_name("schema-attribute(xs:string)")
        self.check_value("schema-attribute(xml:lang)", None)
        self.check_value("schema-attribute(xml:lang)", context.item, context)
        self.check_tree("schema-attribute(xsi:schemaLocation)",
                        '(schema-attribute (: (xsi) (schemaLocation)))')
    def test_float_constructor(self):
        self.wrong_value('xs:float("..")')
        self.wrong_value('xs:float("ab")', 'FORG0001')
        self.wrong_value('xs:float("inf")')

        self.check_value('xs:float(25.05)', 25.05)
        self.check_value('xs:float(xs:untypedAtomic(25.05))', 25.05)
        self.check_value('xs:float(-0.00001)', -0.00001)
        self.check_value('xs:float(0.00001)', float)
        self.check_value('xs:float("INF")', float('inf'))
        self.check_value('xs:float("-INF")', float('-inf'))

        root = self.etree.XML('<root a="10.3"/>')
        context = XPathContext(root, item=AttributeNode('a', 10.3))
        self.check_value('xs:float(.)', float, context=context)
        self.check_value('xs:float(.)', 10.3, context=context)

        self.parser._xsd_version = '1.1'
        try:
            self.check_value('xs:float(9.001)', 9.001)
        finally:
            self.parser._xsd_version = '1.1'
Beispiel #9
0
 def attribute_node(x):
     return AttributeNode(*x)