Beispiel #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>")
Beispiel #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>")
Beispiel #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>")
Beispiel #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>")
Beispiel #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>")
Beispiel #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>"
Beispiel #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>"
Beispiel #8
0
 def test_simple_root(self):
     p2x = PyToXml("a_root_element", {})
     self.assertEqual(str(p2x), "<a_root_element/>")
Beispiel #9
0
 def test_attributes_on_root(self):
     p2x = PyToXml("a", {}, root_attributes={"one": "two"})
     self.assertEqual(str(p2x.encode()), "<a one=\"two\"/>")
Beispiel #10
0
 def test_string_values(self):
     p2x = PyToXml("root", { "one": "two" })
     p2x.encode()
     self.assertEqual(str(p2x), "<root><one>two</one></root>")
Beispiel #11
0
 def test_attributes_on_root(self):
     p2x = PyToXml("a", { }, root_attributes={"one": "two"} )
     self.assertEqual(str(p2x.encode()), "<a one=\"two\"/>")
Beispiel #12
0
 def test_string_values(self):
     p2x = PyToXml("root", {"one": "two"})
     p2x.encode()
     self.assertEqual(str(p2x), "<root><one>two</one></root>")
Beispiel #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)
Beispiel #14
0
 def test_booleans(self):
     p2x = PyToXml("root", { "bald": True })
     self.assertEqual(str(p2x.encode()), "<root><bald>true</bald></root>")
Beispiel #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>")
Beispiel #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>")
Beispiel #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>")
Beispiel #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)
Beispiel #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)
Beispiel #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>")
Beispiel #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>")
Beispiel #22
0
 def test_booleans(self):
     p2x = PyToXml("root", {"bald": True})
     self.assertEqual(str(p2x.encode()), "<root><bald>true</bald></root>")
Beispiel #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>")
Beispiel #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>")
Beispiel #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>")
Beispiel #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>")
Beispiel #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)
Beispiel #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>")
Beispiel #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>")
Beispiel #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>")
Beispiel #31
0
 def test_type_unknown(self):
     p2x = PyToXml("root", {"unknown": Exception("Shouldn't serialise")})
     self.assertRaises(TypeError, p2x.encode)
Beispiel #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>")
Beispiel #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>")
Beispiel #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>")