Debian Codegraph @

neutronclient/tests/unit/test_exceptions.py

raw
1 # All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import sys
16 from unittest import mock
17
18 from oslo_utils import encodeutils
19 import testtools
20
21 from neutronclient._i18n import _
22 from neutronclient.common import exceptions
23
24
25 class TestExceptions(testtools.TestCase):
26
27 def test_exception_print_with_unicode(self):
28 class TestException(exceptions.NeutronException):
29 message = _('Exception with %(reason)s')
30
31 multibyte_unicode_string = u'\uff21\uff22\uff23'
32 e = TestException(reason=multibyte_unicode_string)
33
34 with mock.patch.object(sys, 'stdout') as mock_stdout:
35 print(e)
36
37 exc_str = 'Exception with %s' % multibyte_unicode_string
38 mock_stdout.assert_has_calls([mock.call.write(exc_str)])
39
40 def test_exception_message_with_encoded_unicode(self):
41 class TestException(exceptions.NeutronException):
42 message = _('Exception with %(reason)s')
43
44 multibyte_string = u'\uff21\uff22\uff23'
45 multibyte_binary = encodeutils.safe_encode(multibyte_string)
46 e = TestException(reason=multibyte_binary)
47 self.assertEqual('Exception with %s' % multibyte_string,
48 str(e))