Ejemplo n.º 1
0
    def test_untyped_atomic_constructor(self):
        self.check_value('xs:untypedAtomic(())', [])

        root = self.etree.XML('<root>1999</root>')
        context = XPathContext(root)
        self.check_value('xs:untypedAtomic(.)', UntypedAtomic(1999), context=context)

        context.item = UntypedAtomic('true')
        self.check_value('xs:untypedAtomic(.)', UntypedAtomic(True), context=context)
    def test_base64_binary_constructor(self):
        self.check_value('xs:base64Binary(())', [])
        self.check_value('xs:base64Binary("ODQ=")', b'ODQ=')
        self.check_value('xs:base64Binary(xs:base64Binary("ODQ="))', b'ODQ=')
        self.check_value('xs:base64Binary("YWJjZWZnaGk=")', b'YWJjZWZnaGk=')

        self.wrong_value('xs:base64Binary("xyz")')
        self.wrong_value('xs:base64Binary("\u0411")')
        self.wrong_type('xs:base64Binary(1e2)')
        self.wrong_type('xs:base64Binary(1.1)')

        root = self.etree.XML('<root a="YWJjZWZnaGk="/>')
        context = XPathContext(root)
        self.check_value('xs:base64Binary(@a)',
                         b'YWJjZWZnaGk=',
                         context=context)

        context.item = UntypedAtomic('YWJjZWZnaGk=')
        self.check_value('xs:base64Binary(.)',
                         b'YWJjZWZnaGk=',
                         context=context)

        context.item = b'abcefghi'  # Don't change, it can be an encoded value.
        self.check_value('xs:base64Binary(.)', b'abcefghi', context=context)

        context.item = b'YWJjZWZnaGlq'
        self.check_value('xs:base64Binary(.)',
                         b'YWJjZWZnaGlq',
                         context=context)
Ejemplo n.º 3
0
    def test_data_value_function(self):
        token = self.parser.parse('true()')

        if self.parser.version != '1.0':
            xsd_type = DummyXsdType()
            context = XPathContext(ElementTree.XML('<age>19</age>'))
            context.root.xsd_type = xsd_type
            self.assertEqual(token.data_value(context.root), 19)

        context = XPathContext(ElementTree.XML('<dummy/>'))
        obj = AttributeNode('age', '19')
        self.assertEqual(token.data_value(obj), UntypedAtomic('19'))

        obj = NamespaceNode('tns', 'http://xpath.test/ns')
        self.assertEqual(token.data_value(obj), 'http://xpath.test/ns')

        obj = TextNode('19')
        self.assertEqual(token.data_value(obj), UntypedAtomic('19'))

        obj = ElementTree.XML('<root>a<e1>b</e1>c<e2>d</e2>e</root>')
        element_node = ElementNode(obj)
        self.assertEqual(token.data_value(element_node),
                         UntypedAtomic('abcde'))

        obj = ElementTree.parse(
            io.StringIO('<root>a<e1>b</e1>c<e2>d</e2>e</root>'))
        document_node = DocumentNode(obj)
        self.assertEqual(token.data_value(document_node),
                         UntypedAtomic('abcde'))

        obj = ElementTree.Comment("foo bar")
        comment_node = CommentNode(obj)
        self.assertEqual(token.data_value(comment_node), 'foo bar')

        obj = ElementTree.ProcessingInstruction('action', 'nothing to do')
        pi_node = ProcessingInstructionNode(obj)
        self.assertEqual(token.data_value(pi_node), 'action nothing to do')

        self.assertIsNone(token.data_value(None))
        self.assertEqual(token.data_value(19), 19)
        self.assertEqual(token.data_value('19'), '19')
        self.assertFalse(token.data_value(False))

        # Does not check type of non nodes, simply returns the object.
        tagged_object = Tagged()
        self.assertIs(token.data_value(tagged_object), tagged_object)
Ejemplo n.º 4
0
 def test_conversion(self):
     self.assertEqual(str(UntypedAtomic(25.1)), '25.1')
     self.assertEqual(int(UntypedAtomic(25.1)), 25)
     self.assertEqual(float(UntypedAtomic(25.1)), 25.1)
     self.assertEqual(bool(UntypedAtomic(True)), True)
     if sys.version_info >= (3,):
         self.assertEqual(str(UntypedAtomic(u'Joan Miró')), u'Joan Miró')
     else:
         self.assertEqual(unicode(UntypedAtomic(u'Joan Miró')), u'Joan Miró')
     self.assertEqual(bytes(UntypedAtomic(u'Joan Miró')), b'Joan Mir\xc3\xb3')
Ejemplo n.º 5
0
    def test_data_value_function(self):
        token = self.parser.parse('true()')

        if self.parser.version != '1.0':
            with patch.multiple(DummyXsdType(),
                                is_simple=lambda x: False,
                                has_simple_content=lambda x: True) as xsd_type:
                obj = TypedElement(ElementTree.XML('<age>19</age>'), xsd_type,
                                   19)
                self.assertEqual(token.data_value(obj), 19)

        obj = AttributeNode('age', '19')
        self.assertEqual(token.data_value(obj), UntypedAtomic('19'))

        obj = NamespaceNode('tns', 'http://xpath.test/ns')
        self.assertEqual(token.data_value(obj), 'http://xpath.test/ns')

        obj = TextNode('19')
        self.assertEqual(token.data_value(obj), UntypedAtomic('19'))

        obj = ElementTree.XML('<root>a<e1>b</e1>c<e2>d</e2>e</root>')
        self.assertEqual(token.data_value(obj), UntypedAtomic('abcde'))

        obj = ElementTree.parse(
            io.StringIO('<root>a<e1>b</e1>c<e2>d</e2>e</root>'))
        self.assertEqual(token.data_value(obj), UntypedAtomic('abcde'))

        obj = ElementTree.Comment("foo bar")
        self.assertEqual(token.data_value(obj), 'foo bar')

        obj = ElementTree.ProcessingInstruction('action', 'nothing to do')
        self.assertEqual(token.data_value(obj), 'action nothing to do')

        self.assertIsNone(token.data_value(None))
        self.assertEqual(token.data_value(19), 19)
        self.assertEqual(token.data_value('19'), '19')
        self.assertFalse(token.data_value(False))

        tagged_object = Tagged()
        self.assertIsNone(token.data_value(tagged_object))
Ejemplo n.º 6
0
    def test_extended_name_token(self):
        parser = XPath2Parser(strict=False)
        context = XMLSchemaContext(self.schema1)

        elem_a = self.schema1.elements['a']
        token = parser.parse('{http://xpath.test/ns}a')
        self.assertEqual(token.symbol, '{')
        self.assertIsNone(token.xsd_types)
        self.assertEqual(token[0].symbol, '(string)')
        self.assertEqual(token[1].symbol, '(name)')
        self.assertEqual(token[1].value, 'a')

        result = token.evaluate(context)
        self.assertListEqual(result, [TypedElement(elem_a, elem_a.type, UntypedAtomic('1'))])
        self.assertEqual(token.xsd_types, {"{http://xpath.test/ns}a": elem_a.type})
        self.assertIsNone(token[0].xsd_types)
        self.assertIsNone(token[1].xsd_types)
Ejemplo n.º 7
0
    def test_colon_token(self):
        parser = XPath2Parser(namespaces={'tst': "http://xpath.test/ns"})
        context = XMLSchemaContext(self.schema1)

        elem_a = self.schema1.elements['a']
        token = parser.parse('tst:a')
        self.assertEqual(token.symbol, ':')
        self.assertIsNone(token.xsd_types)

        result = token.evaluate(context.copy())
        self.assertEqual(token.xsd_types, {"{http://xpath.test/ns}a": elem_a.type})
        self.assertListEqual(result, [TypedElement(elem_a, elem_a.type, UntypedAtomic('1'))])

        elem_b1 = elem_a.type.content[0]
        token = parser.parse('tst:a/b1')
        self.assertEqual(token.symbol, '/')
        self.assertEqual(token[0].symbol, ':')
        self.assertIsNone(token[0].xsd_types)
        self.assertIsNone(token[1].xsd_types)

        result = token.evaluate(context.copy())
        self.assertListEqual(result, [TypedElement(elem_b1, elem_b1.type, '  alpha\t')])
        self.assertEqual(token[0].xsd_types, {"{http://xpath.test/ns}a": elem_a.type})
        self.assertEqual(token[1].xsd_types, {"b1": elem_b1.type})

        token = parser.parse('tst:a/tst:b1')
        result = token.evaluate(context.copy())
        self.assertListEqual(result, [])
        self.assertEqual(token[0].xsd_types, {"{http://xpath.test/ns}a": elem_a.type})
        self.assertIsNone(token[1].xsd_types)

        elem_b3 = elem_a.type.content[2]
        token = parser.parse('tst:a/tst:b3')
        self.assertEqual(token.symbol, '/')
        self.assertEqual(token[0].symbol, ':')
        self.assertIsNone(token[0].xsd_types)
        self.assertIsNone(token[1].xsd_types)

        result = token.evaluate(context.copy())
        self.assertListEqual(result, [TypedElement(elem_b3, elem_b3.type, 1.0)])
        self.assertEqual(token[0].xsd_types, {"{http://xpath.test/ns}a": elem_a.type})
        self.assertEqual(token[1].xsd_types, {"{http://xpath.test/ns}b3": elem_b3.type})
Ejemplo n.º 8
0
    def test_name_token(self):
        parser = XPath2Parser(default_namespace="http://xpath.test/ns")
        context = XMLSchemaContext(self.schema1)

        elem_a = self.schema1.elements['a']
        token = parser.parse('a')
        self.assertIsNone(token.xsd_types)

        result = token.evaluate(context.copy())
        self.assertEqual(token.xsd_types, {"{http://xpath.test/ns}a": elem_a.type})
        self.assertListEqual(result, [TypedElement(elem_a, elem_a.type, UntypedAtomic('1'))])

        elem_b1 = elem_a.type.content[0]
        token = parser.parse('a/b1')
        self.assertIsNone(token[0].xsd_types)
        self.assertIsNone(token[1].xsd_types)

        result = token.evaluate(context.copy())
        self.assertEqual(token[0].xsd_types, {"{http://xpath.test/ns}a": elem_a.type})
        self.assertEqual(token[1].xsd_types, {"b1": elem_b1.type})
        self.assertListEqual(result, [TypedElement(elem_b1, elem_b1.type, '  alpha\t')])
    def test_hex_binary_constructor(self):
        self.check_value('xs:hexBinary(())', [])
        self.check_value('xs:hexBinary("84")', b'84')
        self.check_value('xs:hexBinary(xs:hexBinary("84"))', b'84')
        self.wrong_type('xs:hexBinary(12)')

        root = self.etree.XML('<root a="84"/>')
        context = XPathContext(root)
        self.check_value('xs:hexBinary(@a)', b'84', context=context)

        context.item = UntypedAtomic('84')
        self.check_value('xs:hexBinary(.)', b'84', context=context)

        context.item = '84'
        self.check_value('xs:hexBinary(.)', b'84', context=context)

        context.item = b'84'
        self.check_value('xs:hexBinary(.)', b'84', context=context)

        context.item = b'XY'
        self.check_value('xs:hexBinary(.)', ValueError, context=context)

        context.item = b'F859'
        self.check_value('xs:hexBinary(.)', b'F859', context=context)
Ejemplo n.º 10
0
    def test_eq(self):
        self.assertTrue(UntypedAtomic(-10) == UntypedAtomic(-10))
        self.assertTrue(UntypedAtomic(5.2) == UntypedAtomic(5.2))
        self.assertTrue(UntypedAtomic('-6.09') == UntypedAtomic('-6.09'))
        self.assertTrue(UntypedAtomic(Decimal('8.91')) == UntypedAtomic(Decimal('8.91')))
        self.assertTrue(UntypedAtomic(False) == UntypedAtomic(False))

        self.assertTrue(UntypedAtomic(-10) == -10)
        self.assertTrue(-10 == UntypedAtomic(-10))
        self.assertTrue('-10' == UntypedAtomic(-10))
        self.assertTrue(bool(False) == UntypedAtomic(False))
        self.assertTrue(Decimal('8.91') == UntypedAtomic(Decimal('8.91')))
        self.assertTrue(UntypedAtomic(Decimal('8.91')) == Decimal('8.91'))

        self.assertFalse(bool(False) == UntypedAtomic(10))
        self.assertFalse(-10.9 == UntypedAtomic(-10))
        self.assertFalse(UntypedAtomic(-10) == -11)

        self.assertFalse(UntypedAtomic(-10.5) == UntypedAtomic(-10))
        self.assertFalse(-10.5 == UntypedAtomic(-10))
        self.assertFalse(-17 == UntypedAtomic(-17.3))
Ejemplo n.º 11
0
 def test_numerical_operators(self):
     self.assertEqual(0.25 * UntypedAtomic(1000), 250)
     self.assertEqual(1200 - UntypedAtomic(1000.0), 200.0)
     self.assertEqual(UntypedAtomic(1000.0) - 250, 750.0)
     self.assertEqual(UntypedAtomic('1000.0') - 250, 750.0)
     self.assertEqual(UntypedAtomic('1000.0') - UntypedAtomic(250), 750.0)
     self.assertEqual(UntypedAtomic(0.75) * UntypedAtomic(100), 75)
     self.assertEqual(UntypedAtomic('0.75') * UntypedAtomic('100'), 75)
     self.assertEqual(UntypedAtomic('9.0') / UntypedAtomic('3'), 3.0)
     self.assertEqual(9.0 / UntypedAtomic('3'), 3.0)
     self.assertEqual(UntypedAtomic('15') * UntypedAtomic('4'), 60)
Ejemplo n.º 12
0
 def test_ge(self):
     self.assertTrue(UntypedAtomic(25) >= UntypedAtomic(25))
     self.assertFalse(25 >= UntypedAtomic(25.1))
Ejemplo n.º 13
0
 def test_gt(self):
     self.assertTrue(UntypedAtomic(25) > UntypedAtomic(15))
     self.assertTrue(25 > UntypedAtomic(15))
     self.assertTrue(UntypedAtomic(25) > 15)
     self.assertTrue(UntypedAtomic(25) > '15')
Ejemplo n.º 14
0
 def test_le(self):
     self.assertTrue(UntypedAtomic(9.0) <= UntypedAtomic(15))
     self.assertTrue(False <= UntypedAtomic(False))
     self.assertTrue(UntypedAtomic('78') <= 100.0)
     self.assertFalse(UntypedAtomic('100.001') <= 100.0)
Ejemplo n.º 15
0
 def test_lt(self):
     self.assertTrue(UntypedAtomic(9.0) < UntypedAtomic(15))
     self.assertTrue(False < UntypedAtomic(True))
     self.assertTrue(UntypedAtomic('78') < 100.0)
     self.assertFalse(UntypedAtomic('100.1') < 100.0)
Ejemplo n.º 16
0
 def test_ne(self):
     self.assertTrue(UntypedAtomic(True) != UntypedAtomic(False))
     self.assertTrue(UntypedAtomic(5.12) != UntypedAtomic(5.2))
     self.assertTrue('29' != UntypedAtomic(5.2))
     self.assertFalse('2.0' != UntypedAtomic('2.0'))
Ejemplo n.º 17
0
 def test_hashing(self):
     self.assertEqual(hash(UntypedAtomic(12345)), 12345)
     self.assertIsInstance(hash(UntypedAtomic('alpha')), int)
Ejemplo n.º 18
0
 def test_mod(self):
     self.assertEqual(UntypedAtomic(1) % 2, 1)
     self.assertEqual(UntypedAtomic('1') % 2, 1.0)
Ejemplo n.º 19
0
 def test_abs(self):
     self.assertEqual(abs(UntypedAtomic(-10)), 10)