-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtest_formats.py
65 lines (44 loc) · 1.61 KB
/
test_formats.py
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
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
from unittest import mock
from berserk import formats as fmts
def test_base_headers():
fmt = fmts.FormatHandler('foo')
assert fmt.headers == {'Accept': 'foo'}
def test_base_handle():
fmt = fmts.FormatHandler('foo')
fmt.parse = mock.Mock(return_value='bar')
fmt.parse_stream = mock.Mock()
result = fmt.handle('baz', is_stream=False)
assert result == 'bar'
assert fmt.parse_stream.call_count == 0
def test_base_handle_stream():
fmt = fmts.FormatHandler('foo')
fmt.parse = mock.Mock()
fmt.parse_stream = mock.Mock(return_value='bar')
result = fmt.handle('baz', is_stream=True)
assert list(result) == list('bar')
assert fmt.parse.call_count == 0
def test_json_handler_parse():
fmt = fmts.JsonHandler('foo')
m_response = mock.Mock()
m_response.json.return_value = 'bar'
result = fmt.parse(m_response)
assert result == 'bar'
def test_json_handler_parse_stream():
fmt = fmts.JsonHandler('foo')
m_response = mock.Mock()
m_response.iter_lines.return_value = [b'{"x": 5}', b'', b'{"y": 3}']
result = fmt.parse_stream(m_response)
assert list(result) == [{'x': 5}, {'y': 3}]
def test_pgn_handler_parse():
fmt = fmts.PgnHandler()
m_response = mock.Mock()
m_response.text = 'bar'
result = fmt.parse(m_response)
assert result == 'bar'
def test_pgn_handler_parse_stream():
fmt = fmts.PgnHandler()
m_response = mock.Mock()
m_response.iter_lines.return_value = [b'one', b'two', b'', b'', b'three']
result = fmt.parse_stream(m_response)
assert list(result) == ['one\ntwo', 'three']