def test_bind_wrapper_type(self): var = DataType.HEX_BINARY node = StandardNode(var, {}, True, False) objects = [] self.assertTrue(node.bind("a", "13", None, objects)) self.assertEqual(("a", DerivedElement(qname="a", value=b"\x13")), objects[-1])
def test_bind_derived(self): var = DataType.INT node = StandardNode(var, {}, True, False) objects = [] self.assertTrue(node.bind("a", "13", None, objects)) self.assertEqual(("a", DerivedElement("a", 13)), objects[-1])
def bind(self, qname: str, text: NoneStr, tail: NoneStr, objects: List) -> bool: obj: Any = None if self.has_children: obj = AnyElement( qname=qname, text=ParserUtils.normalize_content(text), tail=ParserUtils.normalize_content(tail), attributes=ParserUtils.parse_any_attributes(self.attrs, self.ns_map), children=ParserUtils.fetch_any_children(self.position, objects), ) objects.append((self.var.qname, obj)) else: var = self.var ns_map = self.ns_map datatype = ParserUtils.data_type(self.attrs, self.ns_map) obj = ParserUtils.parse_value(text, [datatype.type], var.default, ns_map) if var.derived: obj = DerivedElement(qname=qname, value=obj) objects.append((qname, obj)) if self.mixed: tail = ParserUtils.normalize_content(tail) if tail: objects.append((None, tail)) return True
def bind(self, qname: str, text: NoneStr, tail: NoneStr, objects: List) -> bool: params: Dict = {} wild_node = False text_node = False ParserUtils.bind_attrs(params, self.meta, self.attrs, self.ns_map) wild_var = self.meta.find_var(mode=FindMode.WILDCARD) if wild_var and wild_var.mixed: ParserUtils.bind_mixed_objects(params, wild_var, self.position, objects) else: ParserUtils.bind_objects(params, self.meta, self.position, objects) text_node = ParserUtils.bind_content(params, self.meta, text, self.ns_map) if not text_node and wild_var: ParserUtils.bind_wild_content(params, wild_var, text, tail, self.attrs, self.ns_map) obj = self.meta.clazz(**params) if self.derived: obj = DerivedElement(qname=qname, value=obj, substituted=self.substituted) objects.append((qname, obj)) if not wild_var and self.mixed and not wild_node: tail = ParserUtils.normalize_content(tail) if tail: objects.append((None, tail)) return True
def test_bind_derived_mode(self): var = XmlVar(text=True, name="foo", qname="foo", types=[int], derived=True) ns_map = {"foo": "bar"} node = PrimitiveNode(var, ns_map) objects = [] self.assertTrue(node.bind("foo", "13", "Impossible", objects)) self.assertEqual(DerivedElement("foo", 13), objects[-1][1])
def prepare_generic_value(cls, qname: str, value: Any) -> Any: """Prepare parsed value before binding to a wildcard field.""" if not is_dataclass(value): value = AnyElement(qname=qname, text=converter.serialize(value)) elif not isinstance(value, (AnyElement, DerivedElement)): value = DerivedElement(qname=qname, value=value) return value
def bind(self, qname: str, text: NoneStr, tail: NoneStr, objects: List) -> bool: var = self.var ns_map = self.ns_map obj = ParserUtils.parse_value(text, var.types, var.default, ns_map, var.tokens) if var.derived: obj = DerivedElement(qname=qname, value=obj) objects.append((qname, obj)) return True
def test_bind_with_simple_type_derived(self): objects = [] self.node.var = XmlVar( element=True, name="a", qname="a", types=[object], derived=True ) self.node.attrs[QNames.XSI_TYPE] = str(DataType.FLOAT) self.assertTrue(self.node.bind("a", "10", None, objects)) self.assertEqual(self.var.qname, objects[-1][0]) self.assertEqual(DerivedElement(qname="a", value=10.0), objects[-1][1])
def test_write_any_type_with_derived_element_primitive(self): var = XmlVar(wildcard=True, qname="a", name="a") value = DerivedElement(qname="a", value=1) expected = [ (XmlWriterEvent.START, "a"), (XmlWriterEvent.ATTR, QNames.XSI_TYPE, QName(str(DataType.SHORT))), (XmlWriterEvent.DATA, 1), (XmlWriterEvent.END, "a"), ] result = self.serializer.write_value(value, var, "xsdata") self.assertIsInstance(result, Generator) self.assertEqual(expected, list(result))
def test_bind_wildcard_with_derived_element(self): var = XmlVar( any_type=True, name="a", qname="a", types=[object], ) actual = DerivedElement(qname="a", value=Books(book=[]), substituted=True) data = {"qname": "a", "value": {"book": []}, "substituted": True} self.assertEqual(actual, self.parser.bind_value(var, data))
def test_write_object_with_derived_element(self): book = BookForm(id="123") obj = DerivedElement(qname="item", value=book) result = self.serializer.write_object(obj) expected = [ (XmlWriterEvent.START, "item"), (XmlWriterEvent.ATTR, "id", "123"), (XmlWriterEvent.ATTR, "lang", "en"), (XmlWriterEvent.ATTR, QNames.XSI_TYPE, "{urn:books}BookForm"), (XmlWriterEvent.END, "item"), ] self.assertIsInstance(result, Generator) self.assertEqual(expected, list(result))
def bind_choice_generic(self, value: Dict, var: XmlVar) -> Any: """Bind data to a either a derived or a user derived model.""" qname = value["qname"] choice = var.find_choice(qname) if not choice: raise ParserError( f"XmlElements undefined choice: `{var.name}` for qname `{qname}`" ) if "value" in value: return DerivedElement(qname, self.bind_value(choice, value["value"])) return self.bind_dataclass(value, AnyElement)
def test_bind_choice_generic_with_derived(self): var = XmlVar( elements=True, qname="compound", name="compound", choices=[ XmlVar(element=True, name="a", qname="a", types=[int]), XmlVar(element=True, name="b", qname="b", types=[float]), ], ) data = {"qname": "a", "value": 1, "substituted": True} self.assertEqual( DerivedElement(qname="a", value=1, substituted=True), self.parser.bind_choice(data, var), )
def test_prepare_generic_value(self): actual = ParserUtils.prepare_generic_value("a", 1) expected = AnyElement(qname="a", text="1") self.assertEqual(expected, actual) actual = ParserUtils.prepare_generic_value("a", "foo") expected = AnyElement(qname="a", text="foo") self.assertEqual(expected, actual) fixture = make_dataclass("Fixture", [("content", str)]) actual = ParserUtils.prepare_generic_value("a", fixture("foo")) expected = DerivedElement(qname="a", value=fixture("foo")) self.assertEqual(expected, actual) actual = ParserUtils.prepare_generic_value("a", expected) self.assertIs(expected, actual)
def test_write_choice_with_derived_dataclass(self): var = XmlVar( elements=True, name="compound", qname="compound", choices=[XmlVar(element=True, qname="a", name="a", types=[A])], ) value = DerivedElement(qname="a", value=A("foo")) expected = [ (XmlWriterEvent.START, "a"), (XmlWriterEvent.ATTR, "a0", "foo"), (XmlWriterEvent.END, "a"), ] result = self.serializer.write_value(value, var, "xsdata") self.assertIsInstance(result, Generator) self.assertEqual(expected, list(result))
def test_bind_with_derived_element(self): a = make_dataclass("A", fields=[]) node = ElementNode( position=0, meta=self.context.build(a), context=self.context, config=ParserConfig(), attrs={}, ns_map={}, derived=True, ) objects = [] self.assertTrue(node.bind("foo", None, None, objects)) self.assertEqual("foo", objects[-1][0]) self.assertEqual(DerivedElement("foo", a()), objects[-1][1])
def test_bind_mixed_objects(self): generic = AnyElement(qname="foo") data_class = make_dataclass("A", fields=[]) derived = DerivedElement(qname="d", value=data_class) objects = [ ("a", 1), ("b", None), ("d", data_class), ("foo", generic), (None, "foo"), ] var = XmlVar(wildcard=True, name="foo", qname="{any}foo") params = {} ParserUtils.bind_mixed_objects(params, var, 1, objects) expected = {var.name: [AnyElement(qname="b", text=""), derived, generic, "foo"]} self.assertEqual(expected, params)
def test_bind_choice_generic_with_derived(self): var = XmlVar( elements=True, qname="compound", name="compound", choices=[ XmlVar(element=True, name="a", qname="a", types=[int]), XmlVar(element=True, name="b", qname="b", types=[float]), ], ) self.assertEqual( DerivedElement(qname="a", value=1), self.parser.bind_choice({ "qname": "a", "value": 1 }, var), )
def test_write_any_type_with_derived_element_dataclass(self): var = XmlVar(wildcard=True, qname="a", name="a") value = DerivedElement(qname="a", value=BookForm(title="def"), substituted=True) expected = [ (XmlWriterEvent.START, "a"), (XmlWriterEvent.ATTR, "lang", "en"), (XmlWriterEvent.ATTR, QNames.XSI_TYPE, QName("{urn:books}BookForm")), (XmlWriterEvent.START, "title"), (XmlWriterEvent.DATA, "def"), (XmlWriterEvent.END, "title"), (XmlWriterEvent.END, "a"), ] result = self.serializer.write_value(value, var, "xsdata") self.assertIsInstance(result, Generator) self.assertEqual(expected, list(result))
def bind(self, qname: str, text: NoneStr, tail: NoneStr, objects: List) -> bool: obj = ParserUtils.parse_value( text, self.var.types, self.var.default, self.ns_map, self.var.tokens, self.var.format, ) if obj is None and not self.var.nillable: obj = "" if self.var.derived: obj = DerivedElement(qname=qname, value=obj) objects.append((qname, obj)) return True
def bind(self, qname: str, text: NoneStr, tail: NoneStr, objects: List) -> bool: obj = ParserUtils.parse_value( text, [self.datatype.type], None, self.ns_map, False, self.datatype.format, ) if obj is None and not self.nillable: obj = "" if self.datatype.wrapper: obj = self.datatype.wrapper(obj) if self.derived: obj = DerivedElement(qname=qname, value=obj) objects.append((qname, obj)) return True
def test_write_mixed_content(self): var = XmlVar(wildcard=True, qname="a", name="a", mixed=True) book = BookForm(id="123") ebook = DerivedElement("ebook", BookForm(id="123")) value = ["text", AnyElement(qname="br"), book, ebook, "tail"] result = self.serializer.write_value(value, var, "xsdata") expected = [ (XmlWriterEvent.DATA, "text"), (XmlWriterEvent.START, "br"), (XmlWriterEvent.DATA, None), (XmlWriterEvent.END, "br"), (XmlWriterEvent.START, "{xsdata}BookForm"), (XmlWriterEvent.ATTR, "id", "123"), (XmlWriterEvent.ATTR, "lang", "en"), (XmlWriterEvent.END, "{xsdata}BookForm"), (XmlWriterEvent.START, "ebook"), (XmlWriterEvent.ATTR, "id", "123"), (XmlWriterEvent.ATTR, "lang", "en"), (XmlWriterEvent.END, "ebook"), (XmlWriterEvent.DATA, "tail"), ] self.assertIsInstance(result, Generator) self.assertEqual(expected, list(result))