Add tests for helpers and schema proxy

This commit is contained in:
Davide Brunato 2019-08-09 08:56:39 +02:00
parent 852d50c087
commit 116e43fc4a
4 changed files with 24 additions and 28 deletions

View File

@ -1114,7 +1114,7 @@ class ArithmeticTypeProxy(object):
@staticmethod
def instance_check(other):
return isinstance(other, (int, float, decimal.Decimal, AbstractDateTime, Duration)) \
and not isinstance(other, bool)
and not isinstance(other, bool)
@staticmethod
def subclass_check(subclass):

View File

@ -235,30 +235,3 @@ def node_string_value(obj):
elif is_processing_instruction_node(obj):
return obj.text
###
# XPath base functions
def boolean_value(obj, token=None):
"""
The effective boolean value, as computed by fn:boolean().
Moved to token class but kept for backward compatibility.
"""
if isinstance(obj, list):
if not obj:
return False
elif isinstance(obj[0], tuple) or is_element_node(obj[0]):
return True
elif len(obj) == 1:
return bool(obj[0])
else:
raise xpath_error(
code='FORG0006', token=token, prefix=getattr(token, 'error_prefix', 'err'),
message="Effective boolean value is not defined for a sequence of two or "
"more items not starting with an XPath node.",
)
elif isinstance(obj, tuple) or is_element_node(obj):
raise xpath_error(
code='FORG0006', token=token, prefix=getattr(token, 'error_prefix', 'err'),
message="Effective boolean value is not defined for {}.".format(obj)
)
return bool(obj)

View File

@ -22,6 +22,7 @@ from elementpath.xpath_nodes import AttributeNode, NamespaceNode, is_etree_eleme
is_namespace_node, is_processing_instruction_node, is_text_node, node_attributes, \
node_base_uri, node_document_uri, node_children, node_is_id, node_is_idrefs, \
node_nilled, node_kind, node_name, node_string_value
from elementpath.xpath_helpers import boolean_value
from elementpath.xpath1_parser import XPath1Parser
@ -245,5 +246,23 @@ class NodeHelpersTest(unittest.TestCase):
self.assertIsNone(node_string_value(10))
class CompatibilityHelpersTest(unittest.TestCase):
def test_boolean_value_function(self):
elem = ElementTree.Element('A')
self.assertFalse(boolean_value([]))
self.assertTrue(boolean_value([elem]))
self.assertFalse(boolean_value([0]))
self.assertTrue(boolean_value([1]))
with self.assertRaises(TypeError):
boolean_value([1, 1])
with self.assertRaises(TypeError):
boolean_value(elem)
self.assertFalse(boolean_value(0))
self.assertTrue(boolean_value(1))
if __name__ == '__main__':
unittest.main()

View File

@ -114,6 +114,10 @@ class XPath2ParserXMLSchemaTest(test_xpath2_parser.XPath2ParserTest):
facet_type = schema_proxy.get_type('{%s}facet' % XSD_NAMESPACE)
any_type = schema_proxy.get_type('{%s}anyType' % XSD_NAMESPACE)
self.assertEqual(schema_proxy.get_primitive_type(facet_type), any_type)
self.assertEqual(schema_proxy.get_primitive_type(any_type), any_type)
any_simple_type = schema_proxy.get_type('{%s}anySimpleType' % XSD_NAMESPACE)
self.assertEqual(schema_proxy.get_primitive_type(any_simple_type), any_simple_type)
def test_is_instance_api(self):
self.assertFalse(self.schema_proxy.is_instance(True, '{%s}integer' % XSD_NAMESPACE))