def test_dump_with_missing_attribute(self): """ L{NodeSchema.dump} ignores missing attributes if C{min_occurs} is zero. """ schema = NodeSchema("foo") schema.add(LeafSchema("bar"), min_occurs=0) foo = NodeItem(schema) self.assertEqual("<foo/>", etree.tostring(schema.dump(foo)))
def test_get_with_non_required_tag(self): """ No error is raised if a tag is missing and its min count is zero. """ schema = NodeSchema("foo") schema.add(LeafSchema("bar"), min_occurs=0) root = etree.fromstring("<foo></foo>") foo = schema.create(root) self.assertIdentical(None, foo.bar)
def test_set_with_optional_node_tag(self): """ It is possible to set an optional node tag to C{None}, in that case it will be removed from the tree. """ schema = NodeSchema("foo") schema.add(NodeSchema("bar", [LeafSchema("egg")]), min_occurs=0) root = etree.fromstring("<foo><bar><egg>spam</egg></bar></foo>") foo = schema.create(root) foo.bar = None self.assertEqual("<foo/>", etree.tostring(schema.dump(foo)))
def test_set_with_non_required_tag(self): """ It is possible to set a non-required tag value to C{None}, in that case the element will be removed if present. """ schema = NodeSchema("foo") schema.add(LeafSchema("bar"), min_occurs=0) root = etree.fromstring("<foo><bar>spam</bar></foo>") foo = schema.create(root) foo.bar = None self.assertEqual("<foo/>", etree.tostring(schema.dump(foo)))
def test_get_with_non_required_nested(self): """ It is possible to access a non-required nested node that has no associated element in the XML yet, in that case a new element is created for it. """ schema = NodeSchema("foo") schema.add(NodeSchema("bar", [LeafSchema("egg")]), min_occurs=0) root = etree.fromstring("<foo/>") foo = schema.create(root) foo.bar.egg = "spam" self.assertEqual("<foo><bar><egg>spam</egg></bar></foo>", etree.tostring(schema.dump(foo)))
def test_set_with_sequence_tag(self): """ It is possible to set a sequence tag to C{None}, in that case all its children will be removed """ schema = NodeSchema("foo") schema.add( SequenceSchema("bar", NodeSchema("item", [LeafSchema("egg")]))) root = etree.fromstring("<foo>" "<bar><item><egg>spam</egg></item></bar><" "/foo>") foo = schema.create(root) foo.bar = None self.assertEqual("<foo><bar/></foo>", etree.tostring(schema.dump(foo)))
def test_set_with_sequence_tag(self): """ It is possible to set a sequence tag to C{None}, in that case all its children will be removed """ schema = NodeSchema("foo") schema.add(SequenceSchema("bar", NodeSchema("item", [LeafSchema("egg")]))) root = etree.fromstring("<foo>" "<bar><item><egg>spam</egg></item></bar><" "/foo>") foo = schema.create(root) foo.bar = None self.assertEqual("<foo><bar/></foo>", etree.tostring(schema.dump(foo)))