Blame test/test-exception-py2.py

Packit 130fc8
#!/usr/bin/python
Packit 130fc8
# -*- coding: utf-8 -*-
Packit 130fc8
Packit 130fc8
import sys
Packit 130fc8
import unittest
Packit 130fc8
Packit 130fc8
import dbus
Packit 130fc8
Packit 130fc8
# from test-service.py
Packit 130fc8
class ServiceError(dbus.DBusException):
Packit 130fc8
    """Exception representing a normal "environmental" error"""
Packit 130fc8
    include_traceback = False
Packit 130fc8
    _dbus_error_name = 'com.example.Networking.ServerError'
Packit 130fc8
Packit 130fc8
Packit 130fc8
class DBusExceptionTestCase(unittest.TestCase):
Packit 130fc8
    """Test the DBusException str/unicode behavior with py2"""
Packit 130fc8
Packit 130fc8
    def test_dbus_exception_normal(self):
Packit 130fc8
        """Test the normal Exception behavior"""
Packit 130fc8
        e = dbus.exceptions.DBusException("baaa")
Packit 130fc8
        msg = e.get_dbus_message()
Packit 130fc8
        self.assertEqual(msg, u"baaa")
Packit 130fc8
Packit 130fc8
    def test_dbus_exception_unicode(self):
Packit 130fc8
        """Test that DBusExceptions that take a py2 unicode work"""
Packit 130fc8
        e = dbus.exceptions.DBusException(u"bäää")
Packit 130fc8
        msg = e.get_dbus_message()
Packit 130fc8
        self.assertEqual(msg, u"bäää")
Packit 130fc8
Packit 130fc8
    def test_dbus_exception_convert_str(self):
Packit 130fc8
        """Test that converting a DBusException to str() works as expected"""
Packit 130fc8
        e = dbus.exceptions.DBusException(u"bxxx")
Packit 130fc8
        self.assertEqual(str(e), "bxxx")
Packit 130fc8
Packit 130fc8
    def test_dbus_exception_convert_str_fail(self):
Packit 130fc8
        """Test that a non-ascii Exception fails to convert to str"""
Packit 130fc8
        if sys.getdefaultencoding() == 'ascii':
Packit 130fc8
            self.assertRaises(UnicodeEncodeError,
Packit 130fc8
                    lambda: str(dbus.exceptions.DBusException(u"bä")))
Packit 130fc8
        else:
Packit 130fc8
            self.skipTest("you're using a weird non-ascii "
Packit 130fc8
                    "sys.getdefaultencoding()")
Packit 130fc8
Packit 130fc8
    def test_dbus_exception_convert_unicode(self):
Packit 130fc8
        """Test that converting a DBusEception to unicode works"""
Packit 130fc8
        e = dbus.exceptions.DBusException(u"bäää")
Packit 130fc8
        self.assertEqual(e.get_dbus_message(), u"bäää")
Packit 130fc8
        self.assertEqual(e.__unicode__(), u"bäää")
Packit 130fc8
        self.assertEqual(unicode(e), u"bäää")
Packit 130fc8
Packit 130fc8
    def test_subclass_exception_unicode(self):
Packit 130fc8
        """Test that DBusExceptions that take a py2 unicode work"""
Packit 130fc8
        e = ServiceError(u"bäää")
Packit 130fc8
        msg = e.get_dbus_message()
Packit 130fc8
        self.assertEqual(msg, u"bäää")
Packit 130fc8
        self.assertEqual(
Packit 130fc8
            unicode(e), u"com.example.Networking.ServerError: bäää")
Packit 130fc8
Packit 130fc8
Packit 130fc8
if __name__ == "__main__":
Packit 130fc8
    unittest.main()