forked from robinhood/faust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_base.py
125 lines (94 loc) · 3.4 KB
/
test_base.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import pytest
from faust.web import Blueprint
from faust.web.base import (
BlueprintManager,
DEBUG_BLUEPRINTS,
DEFAULT_BLUEPRINTS,
PRODUCTION_BLUEPRINTS,
Web,
)
from mode.utils.mocks import Mock, patch
from yarl import URL
class test_BlueprintManager:
@pytest.fixture()
def manager(self):
return BlueprintManager()
def test_add(self, *, manager):
bp1 = Blueprint('test1')
manager.add('/v1/', bp1)
assert manager._enabled
assert manager._enabled[0] == ('/v1/', bp1)
def test_add__already_applied(self, *, manager):
bp1 = Blueprint('test1')
manager.applied = True
with pytest.raises(RuntimeError):
manager.add('/v1', bp1)
def test_apply(self, *, manager):
web = Mock(name='web')
bp1 = Mock(name='blueprint1')
bp1.name = 'blueprint1'
bp2 = Mock(name='blueprint2')
bp2.name = 'blueprint2'
manager.add('/v1/', bp1)
manager.add('/v2/', bp2)
manager.apply(web)
assert manager._active['blueprint1'] is bp1
assert manager._active['blueprint2'] is bp2
bp1.register.assert_called_once_with(web.app, url_prefix='/v1/')
bp2.register.assert_called_once_with(web.app, url_prefix='/v2/')
bp1.init_webserver.assert_called_once_with(web)
bp2.init_webserver.assert_called_once_with(web)
assert manager.applied
manager.apply(web)
class MyWeb(Web):
def text(self, *args, **kwargs):
...
def html(self, *args, **kwargs):
...
def json(self, *args, **kwargs):
...
def bytes(self, *args, **kwargs):
...
def bytes_to_response(self, *args, **kwargs):
...
def response_to_bytes(self, *args, **kwargs):
...
def route(self, *args, **kwargs):
...
def add_static(self, *args, **kwargs):
...
async def read_request_content(self, *args, **kwargs):
...
async def wsgi(self, *args, **kwargs):
...
class test_Web:
@pytest.fixture()
def web(self, *, app):
return MyWeb(app)
@pytest.mark.conf(debug=True)
def test_debug_blueprints(self, *, web):
assert web.app.conf.debug
assert web.blueprints._enabled == (
DEFAULT_BLUEPRINTS + DEBUG_BLUEPRINTS)
def test_production_blueprints(self, *, web):
assert not web.app.conf.debug
assert web.blueprints._enabled == (
DEFAULT_BLUEPRINTS + PRODUCTION_BLUEPRINTS)
def test_url_for(self, *, web):
web.reverse_names['test'] = '/foo/{bar}/'
assert web.url_for(
'test', bar='the quick/fox') == '/foo/the%20quick%2Ffox/'
def test_url_for__not_found(self, *, web):
with pytest.raises(KeyError):
web.url_for('foo')
def test_url__on_localhost(self, *, web, app):
with patch('socket.gethostname') as gethostname:
gethostname.return_value = 'foobar.example.com'
app.conf.web_port = 3030
app.conf.canonical_url = URL('http://foobar.example.com')
assert web.url == URL('http://localhost:3030')
def test_url__not_on_localhost(self, *, web, app):
with patch('socket.gethostname') as gethostname:
gethostname.return_value = 'foobar.example.com'
app.conf.canonical_url = URL('http://xuzzy.example.com')
assert web.url == URL('http://xuzzy.example.com')