Skip to content

Commit 5d78691

Browse files
committed
Add test for fixed values and for xsd_element on element nodes
1 parent 84b1f48 commit 5d78691

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

tests/test_schema_proxy.py

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ def test_element_substitution(self):
494494
for node in context.root.iter_lazy():
495495
if isinstance(node, ElementNode):
496496
self.assertIsNotNone(node.xsd_type)
497+
self.assertIsNotNone(node.xsd_element)
498+
self.assertEqual(node.name, node.xsd_element.name)
497499

498500
self.assertEqual(context.root.children[0].type_name,
499501
'{http://www.w3.org/2001/XMLSchema}string')
@@ -519,6 +521,8 @@ def test_element_substitution(self):
519521
context.root.apply_schema(schema_proxy)
520522
self.assertEqual(context.root.children[0].type_name,
521523
'{http://www.w3.org/2001/XMLSchema}integer')
524+
self.assertEqual(context.root.children[0].xsd_element.name,
525+
'{http://xpath.test/ns}a')
522526
self.assertIsNone(context.root.children[0].attributes[0].xsd_type) # not found
523527

524528
xml_data = '<values xmlns="http://xpath.test/ns"><ax min="19">80</ax></values>'
@@ -529,6 +533,8 @@ def test_element_substitution(self):
529533
context.root.apply_schema(schema_proxy)
530534
self.assertEqual(context.root.children[0].type_name,
531535
'{http://xpath.test/ns}rangeType')
536+
self.assertEqual(context.root.children[0].xsd_element.name,
537+
'{http://xpath.test/ns}ax')
532538
self.assertEqual(context.root.children[0].attributes[0].type_name,
533539
'{http://www.w3.org/2001/XMLSchema}integer')
534540

@@ -544,8 +550,10 @@ def test_element_substitution(self):
544550
'{http://xpath.test/ns}paramsType')
545551
self.assertEqual(context.root.children[0].children[0].type_name,
546552
'{http://www.w3.org/2001/XMLSchema}float')
553+
self.assertEqual(context.root.children[0].children[0].xsd_element.name, 'p0')
547554
self.assertEqual(context.root.children[0].children[1].type_name,
548555
'{http://www.w3.org/2001/XMLSchema}untyped')
556+
self.assertIsNone(context.root.children[0].children[1].xsd_element)
549557

550558
xml_data = ('<p:values xmlns:p="http://xpath.test/ns">'
551559
'<p:cx><p0>1.0</p0><p1>foo</p1></p:cx></p:values>')
@@ -560,6 +568,7 @@ def test_element_substitution(self):
560568
'{http://www.w3.org/2001/XMLSchema}float')
561569
self.assertEqual(context.root.children[0].children[1].type_name,
562570
'{http://www.w3.org/2001/XMLSchema}string')
571+
self.assertEqual(context.root.children[0].children[1].xsd_element.name, 'p1')
563572

564573
def test_type_substitution(self):
565574
schema = xmlschema.XMLSchema(dedent("""
@@ -654,8 +663,7 @@ def test_type_substitution(self):
654663
self.assertEqual(context.root.children[1].children[3].type_name,
655664
'{http://www.w3.org/2001/XMLSchema}string')
656665

657-
658-
def test_default_and_fixed_values__issue_094(self):
666+
def test_default_values__issue_094(self):
659667
schema = xmlschema.XMLSchema(dedent("""
660668
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
661669
xmlns="http://xpath.test/ns"
@@ -671,6 +679,80 @@ def test_default_and_fixed_values__issue_094(self):
671679

672680
schema_proxy = schema.xpath_proxy
673681

682+
self.assertTrue(schema.is_valid(
683+
'<container xmlns="http://xpath.test/ns"><q1>1000</q1></container>'
684+
))
685+
self.assertTrue(schema.is_valid(
686+
'<container xmlns="http://xpath.test/ns"><q1>1001</q1></container>'
687+
))
688+
689+
# Substitution of simple content
690+
xml_data = dedent("""\
691+
<container xmlns="http://xpath.test/ns">
692+
<q1>1000</q1>
693+
<q1></q1>
694+
<p1/>
695+
</container>""")
696+
697+
self.assertTrue(schema.is_valid(xml_data))
698+
root = self.etree.XML(xml_data)
699+
namespaces = {'': "http://xpath.test/ns",
700+
'xsi': "http://www.w3.org/2001/XMLSchema-instance"}
701+
702+
context = XPathContext(root, namespaces=namespaces)
703+
context.root.apply_schema(schema_proxy)
704+
705+
self.assertEqual(context.root.type_name,
706+
'{http://www.w3.org/2001/XMLSchema}anyType')
707+
self.assertIsNotNone(context.root.xsd_element)
708+
self.assertEqual(context.root.xsd_element.name,
709+
'{http://xpath.test/ns}container')
710+
711+
self.assertEqual(context.root.children[1].type_name,
712+
'{http://www.w3.org/2001/XMLSchema}integer')
713+
self.assertIsNotNone(context.root.children[1].xsd_element)
714+
self.assertEqual(context.root.children[1].xsd_element.name,
715+
'{http://xpath.test/ns}q1')
716+
717+
self.assertEqual(context.root.children[3].type_name,
718+
'{http://www.w3.org/2001/XMLSchema}integer')
719+
self.assertIsNotNone(context.root.children[3].xsd_element)
720+
self.assertEqual(context.root.children[3].xsd_element.name,
721+
'{http://xpath.test/ns}q1')
722+
723+
self.assertEqual(context.root.children[1].typed_value, 1000)
724+
self.assertEqual(context.root.children[3].typed_value, 1000)
725+
726+
self.assertEqual(context.root.children[5].type_name,
727+
'{http://xpath.test/ns}pType')
728+
self.assertEqual(len(context.root.children[5].attributes), 1)
729+
self.assertEqual(context.root.children[5].attributes[0].name, 'p1')
730+
self.assertEqual(context.root.children[5].xsd_element.name,
731+
'{http://xpath.test/ns}p1')
732+
733+
def test_fixed_values__issue_094(self):
734+
schema = xmlschema.XMLSchema(dedent("""
735+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
736+
xmlns="http://xpath.test/ns"
737+
targetNamespace="http://xpath.test/ns">
738+
<xs:element id="container" name="container"/>
739+
<xs:element id="q1" name="q1" type="xs:integer" fixed="1000"/>
740+
<xs:element name="p1" type="pType"/>
741+
<xs:complexType name="pType">
742+
<xs:attribute name="p0" type="xs:integer"/>
743+
<xs:attribute name="p1" type="xs:integer" fixed="1000"/>
744+
</xs:complexType>
745+
</xs:schema>"""))
746+
747+
schema_proxy = schema.xpath_proxy
748+
749+
self.assertTrue(schema.is_valid(
750+
'<container xmlns="http://xpath.test/ns"><q1>1000</q1></container>'
751+
))
752+
self.assertFalse(schema.is_valid(
753+
'<container xmlns="http://xpath.test/ns"><q1>1001</q1></container>'
754+
))
755+
674756
# Substitution of simple content
675757
xml_data = dedent("""\
676758
<container xmlns="http://xpath.test/ns">

0 commit comments

Comments
 (0)