Пример #1
0
 def test_set_attribute_with_qname_key(self, mock_to_xml):
     key = QName("foo", "bar")
     SerializeUtils.set_attribute(self.element, key, "value",
                                  self.namespaces)
     self.assertEqual("val", self.element.attrib[key])
     self.assertEqual({"ns0": "foo"}, self.namespaces.ns_map)
     mock_to_xml.assert_called_once_with("value", self.namespaces)
Пример #2
0
    def set_xsi_type(self, parent: Element, value: Any, var: XmlVar,
                     namespaces: Namespaces):
        if not var.clazz or value.__class__ is var.clazz:
            return

        if self.context.is_derived(value, var.clazz):
            meta = self.context.fetch(value.__class__,
                                      QName(parent.tag).namespace)
            SerializeUtils.set_attribute(parent, QNames.XSI_TYPE,
                                         meta.source_qname, namespaces)
        else:
            raise SerializerError(
                f"{value.__class__.__name__} is not derived from {var.clazz.__name__}"
            )
Пример #3
0
    def render_complex_node(self, parent: Element, obj: Any,
                            namespaces: Namespaces):
        meta = self.context.build(obj.__class__, QName(parent).namespace)
        for var, value in self.next_value(meta, obj):
            if value is None:
                continue
            elif var.is_attribute:
                SerializeUtils.set_attribute(parent, var.qname, value,
                                             namespaces)
            elif var.is_attributes:
                SerializeUtils.set_attributes(parent, value, namespaces)
            elif var.is_text:
                namespaces.add(var.qname.namespace)
                SerializeUtils.set_text(parent, value, namespaces)
            elif isinstance(value, list):
                self.render_sub_nodes(parent, value, var, namespaces)
            else:
                self.render_sub_node(parent, value, var, namespaces)

        SerializeUtils.set_nil_attribute(parent, meta.nillable, namespaces)
Пример #4
0
    def test_set_attribute_with_qname_xsi_nil_and_element_has_children(self):
        SubElement(self.element, "bar")

        SerializeUtils.set_attribute(self.element, QNames.XSI_NIL, True,
                                     self.namespaces)
        self.assertNotIn(QNames.XSI_NIL, self.element.attrib)
Пример #5
0
    def test_set_attribute_with_qname_xsi_nil_and_element_has_text(self):
        self.element.text = "foo"

        SerializeUtils.set_attribute(self.element, QNames.XSI_NIL, True,
                                     self.namespaces)
        self.assertNotIn(QNames.XSI_NIL, self.element.attrib)
Пример #6
0
 def test_set_attribute_with_qname_xsi_nil(self):
     SerializeUtils.set_attribute(self.element, QNames.XSI_NIL, True,
                                  self.namespaces)
     self.assertEqual("true", self.element.attrib[QNames.XSI_NIL])
Пример #7
0
 def test_set_attribute_when_value_empty(self, mock_to_xml):
     SerializeUtils.set_attribute(self.element, "key", "value",
                                  self.namespaces)
     self.assertNotIn("key", self.element.attrib)
     self.assertEqual(0, len(self.namespaces.ns_map))
     mock_to_xml.assert_called_once_with("value", self.namespaces)