def test_cascade_default_value(self): target = ClassFactory.create( default="4", fixed=True, nillable=True, attrs=[ AttrFactory.native(DataType.STRING, tag=Tag.SIMPLE_TYPE), AttrFactory.native(DataType.STRING, tag=Tag.SIMPLE_TYPE, default="1"), AttrFactory.native(DataType.STRING, tag=Tag.ELEMENT), ], ) self.processor.process(target) for attr in target.attrs: self.assertNotEqual("4", attr.default) self.assertFalse(attr.fixed) target.nillable = False self.processor.process(target) # Xml text field with no default value self.assertEqual("4", target.attrs[0].default) self.assertEqual(True, target.attrs[0].fixed) # The rest are untouched. self.assertEqual("1", target.attrs[1].default) self.assertFalse(target.attrs[1].fixed) self.assertIsNone(target.attrs[2].default) self.assertFalse(target.attrs[2].fixed)
def test_build_class_mixed_content(self): element = AnyElement( qname="{xsdata}root", children=[ AnyElement(qname="{xsdata}child", text="primitive"), AnyElement(qname="something", text="foo", tail="bar"), ], ) actual = ElementMapper.build_class(element, None) expected = ClassFactory.create( tag=Tag.ELEMENT, qname="root", namespace="xsdata", module="", mixed=True, ns_map={}, attrs=[ AttrFactory.native(DataType.STRING, name="child", namespace="xsdata", index=0), AttrFactory.native(DataType.STRING, name="something", index=1), ], ) self.assertEqual(expected, actual)
def test_build_class_attribute_from_dict(self): target = ClassFactory.create() data = {"sub1": 1, "sub2": "value"} DictMapper.build_class_attribute(target, "a", data) expected = AttrFactory.create( name="a", tag=Tag.ELEMENT, types=[AttrTypeFactory.create(qname="a", forward=True)], ) expected_inner = ClassFactory.create( qname="a", tag=Tag.ELEMENT, module="", ns_map={}, attrs=[ AttrFactory.native(DataType.SHORT, name="sub1"), AttrFactory.native(DataType.STRING, name="sub2"), ], ) self.assertEqual(expected, target.attrs[0]) self.assertEqual(expected_inner, target.inner[0]) self.assertEqual(1, len(target.inner))
def test_reset_unsupported_types_with_successful_token_deserialize(self): attr = AttrFactory.native(DataType.FLOAT, default="aaa", fixed=True) attr.restrictions.format = "something" target = ClassFactory.create() target.attrs.append(attr) self.processor.process(target) self.assertEqual(DataType.STRING, attr.types[0].datatype) self.assertIsNone(attr.restrictions.format)
def test_reset_unsupported_types_with_different_float_lexical_repr(self): attr = AttrFactory.native(DataType.FLOAT, default=" 1e4 ", fixed=True) attr.restrictions.format = "base16" target = ClassFactory.create() target.attrs.append(attr) self.processor.process(target) self.assertEqual(DataType.STRING, attr.types[0].datatype) self.assertIsNone(attr.restrictions.format)
def test_build_class_simple_type(self): element = AnyElement( qname="{xsdata}root", attributes={ "{foo}bar": "1", "{bar}foo": "2.0" }, text="true", ) actual = ElementMapper.build_class(element, "target") expected = ClassFactory.create( tag=Tag.ELEMENT, qname="{target}root", namespace="xsdata", module="", ns_map={}, attrs=[ AttrFactory.native( DataType.INT, tag=Tag.ATTRIBUTE, name="bar", namespace="foo", index=0, ), AttrFactory.native( DataType.FLOAT, tag=Tag.ATTRIBUTE, name="foo", namespace="bar", index=1, ), AttrFactory.native( DataType.BOOLEAN, tag=Tag.SIMPLE_TYPE, name="value", namespace=None, index=2, ), ], ) self.assertEqual(expected, actual)
def test_process_native_type(self): attr = AttrFactory.native(DataType.INT) nm_tokens_type = AttrTypeFactory.native(DataType.NMTOKENS) self.processor.process_native_type(attr, attr.types[0]) self.assertEqual(str(DataType.INT), attr.types[0].qname) attr.restrictions.pattern = "[a-z]" self.processor.process_native_type(attr, attr.types[0]) self.assertEqual(str(DataType.STRING), attr.types[0].qname) self.processor.process_native_type(attr, nm_tokens_type) self.assertTrue(attr.restrictions.tokens)
def test_reset_unsupported_types_with_different_bytes_lexical_repr(self): attr = AttrFactory.native(DataType.HEX_BINARY, default="abcd EFGH", fixed=True) attr.restrictions.format = "base16" attr.restrictions.tokens = True target = ClassFactory.create() target.attrs.append(attr) self.processor.process(target) self.assertEqual(DataType.STRING, attr.types[0].datatype) self.assertIsNone(attr.restrictions.format) self.assertTrue(attr.restrictions.tokens)
def test_build_class(self): data = {"a": 1, "b": True} actual = DictMapper.build_class(data, "root") expected = ClassFactory.create( tag=Tag.ELEMENT, qname="root", module="", ns_map={}, attrs=[ AttrFactory.native( DataType.SHORT, tag=Tag.ELEMENT, name="a", index=0, ), AttrFactory.native( DataType.BOOLEAN, tag=Tag.ELEMENT, name="b", index=1, ), ], ) self.assertEqual(expected, actual)
def test_build_class_complex_type(self): element = AnyElement( qname="{xsdata}root", children=[ AnyElement(qname="{xsdata}child", text="primitive"), AnyElement(qname="{inner}child", attributes={ "{foo}bar": "1", "{bar}foo": "2" }), ], ) actual = ElementMapper.build_class(element, "target") expected = ClassFactory.create( tag=Tag.ELEMENT, qname="{target}root", namespace="xsdata", module="", ns_map={}, attrs=[ AttrFactory.native( DataType.STRING, tag=Tag.ELEMENT, name="child", namespace="xsdata", index=0, ), AttrFactory.element( name="child", namespace="inner", types=[ AttrTypeFactory.create(qname="{target}child", forward=True) ], index=1, ), ], ) self.assertEqual(1, len(actual.inner)) actual.inner.clear() self.assertEqual(expected, actual)
def test_reset_unsupported_types_with_successful_deserialize(self): attr = AttrFactory.native(DataType.FLOAT, default=" 0 2.0 3.0", fixed=True) attr.restrictions.format = "base16" attr.restrictions.tokens = True target = ClassFactory.create() target.attrs.append(attr) self.processor.process(target) self.assertEqual(DataType.STRING, attr.types[0].datatype) self.assertIsNone(None, attr.restrictions.format) self.assertTrue(attr.restrictions.tokens) attr.default = "0" attr.tag = Tag.ENUMERATION self.processor.process(target) self.assertFalse(attr.restrictions.tokens)