Example #1
0
    def test_attributes_without_text(self):
        attrs = Attributes(None, {"one": "two"})

        p2x = PyToXml("a", {"b": attrs})
        p2x.add_type_handler(Attributes)

        self.assertEqual(str(p2x.encode()), "<a><b one=\"two\"/></a>")
Example #2
0
    def test_attributes_without_text(self):
        attrs = Attributes(None, { "one": "two" })

        p2x = PyToXml("a", { "b": attrs } )
        p2x.add_type_handler(Attributes)

        self.assertEqual(str(p2x.encode()), "<a><b one=\"two\"/></a>")
Example #3
0
    def test_cdata(self):
        cdata = CData("<xml>is pretty</horrible>")

        p2x = PyToXml("a", {"b": cdata})
        self.assertEqual(
            str(p2x.encode()),
            "<a><b><![CDATA[<xml>is pretty</horrible>]]></b></a>")
Example #4
0
    def test_add_type_handler(self):
        def temp_convertor(structure, element, name, pytoxml):
            element.text = str(structure)

        p2x = PyToXml("a", { "b": Exception("Should now serialise") })
        p2x.add_type_handler(Exception, temp_convertor)

        self.assertEqual(str(p2x.encode()), "<a><b>Should now serialise</b></a>")
Example #5
0
    def test_add_type_handler(self):
        def temp_convertor(structure, element, name, pytoxml):
            element.text = str(structure)

        p2x = PyToXml("a", {"b": Exception("Should now serialise")})
        p2x.add_type_handler(Exception, temp_convertor)

        self.assertEqual(str(p2x.encode()),
                         "<a><b>Should now serialise</b></a>")
Example #6
0
    def test_illegal_unicode(self):
        "Illegal unicode errors unless told to escape"
        p2x = PyToXml("root", {"a": u"\u001a"})
        with pytest.raises(ValueError):
            p2x.encode()

        p2x = PyToXml("root", {"a": u"\u001a"}, escape_illegal_chars=True)
        text = p2x.encode()
        assert str(text) == "<root><a></a></root>"
Example #7
0
    def test_illegal_unicode(self):
        "Illegal unicode errors unless told to escape"
        p2x = PyToXml("root", {"a": u"\u001a"})
        with pytest.raises(ValueError):
            p2x.encode()

        p2x = PyToXml("root", {"a": u"\u001a"}, escape_illegal_chars=True)
        text = p2x.encode()
        assert str(text) == "<root><a></a></root>"
Example #8
0
 def test_simple_root(self):
     p2x = PyToXml("a_root_element", {})
     self.assertEqual(str(p2x), "<a_root_element/>")
Example #9
0
 def test_attributes_on_root(self):
     p2x = PyToXml("a", {}, root_attributes={"one": "two"})
     self.assertEqual(str(p2x.encode()), "<a one=\"two\"/>")
Example #10
0
 def test_string_values(self):
     p2x = PyToXml("root", { "one": "two" })
     p2x.encode()
     self.assertEqual(str(p2x), "<root><one>two</one></root>")
Example #11
0
 def test_attributes_on_root(self):
     p2x = PyToXml("a", { }, root_attributes={"one": "two"} )
     self.assertEqual(str(p2x.encode()), "<a one=\"two\"/>")
Example #12
0
 def test_string_values(self):
     p2x = PyToXml("root", {"one": "two"})
     p2x.encode()
     self.assertEqual(str(p2x), "<root><one>two</one></root>")
Example #13
0
    def test_tuple_values(self):
        p2x = PyToXml("root", {"a": (1, 2)})
        p2x.encode()

        output = "<root><a><item>1</item><item>2</item></a></root>"
        self.assertEqual(str(p2x), output)
Example #14
0
 def test_booleans(self):
     p2x = PyToXml("root", { "bald": True })
     self.assertEqual(str(p2x.encode()), "<root><bald>true</bald></root>")
Example #15
0
 def test_xmldecloration_default_encoding(self):
     p2x = PyToXml("root", "hi", xml_declaration=True)
     self.assertEqual(
         str(p2x.encode()),
         "<?xml version='1.0' encoding='UTF-8'?>\n<root>hi</root>")
Example #16
0
 def test_xmldecloration_default_encoding(self):
     p2x = PyToXml("root", "hi", xml_declaration=True)
     self.assertEqual(str(p2x.encode()),
                      "<?xml version='1.0' encoding='UTF-8'?>\n<root>hi</root>")
Example #17
0
 def test_xmldecloration_custom_encoding(self):
     p2x = PyToXml("root", "hi", xml_declaration=True, encoding="latin1")
     self.assertEqual(str(p2x.encode()),
                      "<?xml version='1.0' encoding='latin1'?>\n<root>hi</root>")
Example #18
0
    def test_unicode(self):
        p2x = PyToXml("root", { "a": u"\u2603" })
        p2x.encode()

        output = u"<root><a>☃</a></root>"
        self.assertEqual(six.text_type(p2x), output)
Example #19
0
    def test_tuple_values(self):
        p2x = PyToXml("root", { "a": (1, 2) })
        p2x.encode()

        output = "<root><a><item>1</item><item>2</item></a></root>"
        self.assertEqual(str(p2x), output)
Example #20
0
 def test_embedded_dict_values(self):
     p2x = PyToXml("root", { "a": { "b": "c" } })
     p2x.encode()
     self.assertEqual(str(p2x), "<root><a><b>c</b></a></root>")
Example #21
0
 def test_six_integer_types(self):
     for six_type in six.integer_types:
         p2x = PyToXml("root", {"one": six_type(2)})
         p2x.encode()
         self.assertEqual(str(p2x), "<root><one>2</one></root>")
Example #22
0
 def test_booleans(self):
     p2x = PyToXml("root", {"bald": True})
     self.assertEqual(str(p2x.encode()), "<root><bald>true</bald></root>")
Example #23
0
 def test_float_values(self):
     p2x = PyToXml("root", {"one": 2.3})
     p2x.encode()
     self.assertEqual(str(p2x), "<root><one>2.3</one></root>")
Example #24
0
 def test_six_integer_types(self):
     for six_type in six.integer_types:
         p2x = PyToXml("root", { "one": six_type(2) })
         p2x.encode()
         self.assertEqual(str(p2x), "<root><one>2</one></root>")
Example #25
0
 def test_embedded_dict_values(self):
     p2x = PyToXml("root", {"a": {"b": "c"}})
     p2x.encode()
     self.assertEqual(str(p2x), "<root><a><b>c</b></a></root>")
Example #26
0
    def test_attributes_with_dict(self):
        attrs = Attributes({'test': 'name'}, { "one": "two" })

        p2x = PyToXml("a", { "b": attrs } )
        self.assertEqual(str(p2x.encode()),
                         "<a><b one=\"two\"><test>name</test></b></a>")
Example #27
0
    def test_unicode(self):
        p2x = PyToXml("root", {"a": u"\u2603"})
        p2x.encode()

        output = u"<root><a>☃</a></root>"
        self.assertEqual(six.text_type(p2x), output)
Example #28
0
 def test_float_values(self):
     p2x = PyToXml("root", { "one": 2.3 })
     p2x.encode()
     self.assertEqual(str(p2x), "<root><one>2.3</one></root>")
Example #29
0
 def test_xmldecloration_custom_encoding(self):
     p2x = PyToXml("root", "hi", xml_declaration=True, encoding="latin1")
     self.assertEqual(
         str(p2x.encode()),
         "<?xml version='1.0' encoding='latin1'?>\n<root>hi</root>")
Example #30
0
    def test_cdata(self):
        cdata = CData("<xml>is pretty</horrible>")

        p2x = PyToXml("a", { "b": cdata } )
        self.assertEqual(str(p2x.encode()),
                         "<a><b><![CDATA[<xml>is pretty</horrible>]]></b></a>")
Example #31
0
 def test_type_unknown(self):
     p2x = PyToXml("root", {"unknown": Exception("Shouldn't serialise")})
     self.assertRaises(TypeError, p2x.encode)
Example #32
0
    def test_attributes_with_text(self):
        attrs = Attributes("c", {"one": "two"})

        p2x = PyToXml("a", {"b": attrs})
        self.assertEqual(str(p2x.encode()), "<a><b one=\"two\">c</b></a>")
Example #33
0
    def test_attributes_with_dict(self):
        attrs = Attributes({'test': 'name'}, {"one": "two"})

        p2x = PyToXml("a", {"b": attrs})
        self.assertEqual(str(p2x.encode()),
                         "<a><b one=\"two\"><test>name</test></b></a>")
Example #34
0
    def test_attributes_with_text(self):
        attrs = Attributes("c", { "one": "two" })

        p2x = PyToXml("a", { "b": attrs } )
        self.assertEqual(str(p2x.encode()),
                         "<a><b one=\"two\">c</b></a>")