コード例 #1
0
ファイル: tests_xml_writer.py プロジェクト: rigoudyg/dr2xml
    def test_equality(self):
        """

        :return:
        """
        # Test __eq__
        a_comment = Comment("some comment")
        a_comment.update_level(6)
        self.assertFalse(a_comment == self.my_comment)
        an_other_comment = Comment("some comment")
        self.assertFalse(a_comment == an_other_comment)
        an_other_comment.update_level(6)
        self.assertTrue(a_comment == an_other_comment)
コード例 #2
0
ファイル: tests_xml_writer.py プロジェクト: rigoudyg/dr2xml
class TestNonSpecificMethods(unittest.TestCase):
    """

    """
    def setUp(self):
        """

        :return:
        """
        self.my_beacon = Beacon()
        self.my_comment = Comment("A new comment !")
        self.my_header = Header(
            "A header", OrderedDict(an_attrib="a_value", an_other_attrib=6))
        self.my_element = Element("An element",
                                  text="A text",
                                  attrib=OrderedDict(an_attrib="a_value",
                                                     an_other_attrib=6))
        self.my_element.append(self.my_comment)

    def test_is_xml_element(self):
        """

        :return:
        """
        # Test is_xml_element
        self.assertTrue(is_xml_element(self.my_beacon))
        self.assertTrue(is_xml_element(self.my_comment))
        self.assertTrue(is_xml_element(self.my_header))
        self.assertTrue(is_xml_element(self.my_element))
        self.assertFalse(is_xml_element("an_element"))

    def test_update_level(self):
        """

        :return:
        """
        # Test update_level
        self.assertEqual(self.my_beacon.level, 0)
        self.my_beacon.update_level(0)
        self.assertEqual(self.my_beacon.level, 0)
        self.my_comment.update_level(2)
        self.assertEqual(self.my_comment.level, 2)

    def test_correct_attrib(self):
        """

        :return:
        """
        # Test correct_attrib
        attrib = None
        with self.assertRaises(TypeError):
            self.my_beacon.correct_attrib(attrib)
        attrib = ["a_value", "an_other_value"]
        with self.assertRaises(TypeError):
            self.my_beacon.correct_attrib(attrib)
        attrib = dict(a_key="a_value", an_other_key=0)
        self.assertDictEqual(self.my_beacon.correct_attrib(attrib),
                             dict(a_key="a_value", an_other_key="0"))
        attrib = OrderedDict(a_key="a_value", an_other_key=0)
        self.assertDictEqual(self.my_beacon.correct_attrib(attrib),
                             OrderedDict(a_key="a_value", an_other_key="0"))

    def test_dict_equality(self):
        """

        :return:
        """
        a = dict(a_key="a_value", an_other_key="an_other_value")
        b = OrderedDict(a_key="a_value",
                        an_other_key="an_other_different_value")
        self.assertFalse(self.my_beacon._test_dict_equality(a, b))
        b["an_other_key"] = "an_other_value"
        self.assertTrue(self.my_beacon._test_dict_equality(a, b))
        del b["a_key"]
        self.assertFalse(self.my_beacon._test_dict_equality(a, b))
        self.assertFalse(self.my_beacon._test_dict_equality(a, "a_string"))

    def test_attrib_equality(self):
        """

        :return:
        """
        an_other_beacon = Beacon()
        an_other_beacon.update_level(5)
        an_other_beacon.an_attrib = "my_attrib"
        self.assertFalse(
            an_other_beacon._test_attribute_equality("level", self.my_beacon))
        self.assertFalse(
            an_other_beacon._test_attribute_equality("an_attrib",
                                                     self.my_beacon))
        an_other_beacon.update_level(self.my_beacon.level)
        self.assertTrue(
            an_other_beacon._test_attribute_equality("level", self.my_beacon))

    def test_dump_dict(self):
        """

        :return:
        """
        # Test _dump_dict
        attrib = OrderedDict()
        self.assertIsNone(self.my_beacon.dump_dict(attrib))
        attrib["some_key"] = "é_value"
        attrib["other_key_à"] = "0"
        self.assertEqual(self.my_beacon.dump_dict(attrib),
                         'some_key="\xe9_value" other_key_\xe0="0"')
        self.assertEqual(self.my_beacon.dump_dict(attrib, sort=True),
                         'other_key_\xe0="0" some_key="\xe9_value"')