summaryrefslogtreecommitdiff
path: root/t/unit/test_utils.py
blob: 60f5079c352eeae2bde8e4964a51ad600074fed0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from unittest.mock import Mock, patch

from amqp.utils import bytes_to_str, coro, get_logger, str_to_bytes


class test_coro:

    def test_advances(self):
        @coro
        def x():
            yield 1
            yield 2
        it = x()
        assert next(it) == 2


class test_str_to_bytes:

    def test_from_unicode(self):
        assert isinstance(str_to_bytes(u'foo'), bytes)

    def test_from_bytes(self):
        assert isinstance(str_to_bytes(b'foo'), bytes)

    def test_supports_surrogates(self):
        bytes_with_surrogates = '\ud83d\ude4f'.encode('utf-8', 'surrogatepass')
        assert str_to_bytes(u'\ud83d\ude4f') == bytes_with_surrogates


class test_bytes_to_str:

    def test_from_unicode(self):
        assert isinstance(bytes_to_str(u'foo'), str)

    def test_from_bytes(self):
        assert bytes_to_str(b'foo')

    def test_support_surrogates(self):
        assert bytes_to_str(u'\ud83d\ude4f') == u'\ud83d\ude4f'


class test_get_logger:

    def test_as_str(self):
        with patch('logging.getLogger') as getLogger:
            x = get_logger('foo.bar')
            getLogger.assert_called_with('foo.bar')
            assert x is getLogger()

    def test_as_logger(self):
        with patch('amqp.utils.NullHandler') as _NullHandler:
            m = Mock(name='logger')
            m.handlers = None
            x = get_logger(m)
            assert x is m
            x.addHandler.assert_called_with(_NullHandler())