Debian Codegraph @

test/unit/utils/codec_test.py

raw
1 import logging
2 from unittest.mock import patch
3 from pytest import (
4 raises, fixture
5 )
6
7 from kiwi.utils.codec import Codec
8
9 from kiwi.exceptions import KiwiDecodingError
10
11
12 class TestCodec:
13 @fixture(autouse=True)
14 def inject_fixtures(self, caplog):
15 self._caplog = caplog
16
17 def setup(self):
18 self.literal = bytes(b'\xc3\xbc')
19
20 def setup_method(self, cls):
21 self.setup()
22
23 @patch('kiwi.utils.codec.Codec._wrapped_decode')
24 def test_decode_ascii_failure(self, mock_decode):
25 msg = 'utf-8 compatible string'
26
27 def mocked_decode(literal, encoding, error_handling_schema):
28 if encoding:
29 return msg
30 else:
31 raise KiwiDecodingError('ascii decoding failure')
32
33 mock_decode.side_effect = mocked_decode
34 with self._caplog.at_level(logging.WARNING):
35 assert msg == Codec.decode(self.literal)
36
37 def test_decode_None_literal(self):
38 assert '' == Codec.decode(None)
39
40 @patch('kiwi.utils.codec.Codec._wrapped_decode')
41 def test_decode(self, mock_decode):
42 msg = 'utf-8 compatible string'
43 mock_decode.return_value = msg
44 assert msg == Codec.decode(self.literal)
45
46 @patch('kiwi.utils.codec.Codec._wrapped_decode')
47 def test_decode_utf8_failure(self, mock_decode):
48 def mocked_decode(literal, encoding, error_handling_schema):
49 if encoding:
50 raise KiwiDecodingError('utf-8 decoding failure')
51 else:
52 raise KiwiDecodingError('ascii decoding failure')
53
54 mock_decode.side_effect = mocked_decode
55 with raises(KiwiDecodingError):
56 with self._caplog.at_level(logging.WARNING):
57 Codec.decode(self.literal)
58
59 def test_wrapped_decode(self):
60 assert self.literal.decode() == Codec._wrapped_decode(
61 self.literal, 'utf-8'
62 )