forked from spec-first/connexion
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_aiohttp_app.py
More file actions
160 lines (120 loc) · 5.42 KB
/
test_aiohttp_app.py
File metadata and controls
160 lines (120 loc) · 5.42 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import logging
import os
import pathlib
from unittest import mock
import pytest
from connexion import AioHttpApp
from connexion.exceptions import ConnexionException
from conftest import TEST_FOLDER
@pytest.fixture
def web_run_app_mock(monkeypatch):
mock_ = mock.MagicMock()
monkeypatch.setattr('connexion.apps.aiohttp_app.web.run_app', mock_)
return mock_
@pytest.fixture
def sys_modules_mock(monkeypatch):
monkeypatch.setattr('connexion.apps.aiohttp_app.sys.modules', {})
def test_app_run(web_run_app_mock, aiohttp_api_spec_dir):
app = AioHttpApp(__name__, port=5001,
specification_dir=aiohttp_api_spec_dir,
debug=True)
app.run(use_default_access_log=True)
logger = logging.getLogger('connexion.aiohttp_app')
assert web_run_app_mock.call_args_list == [
mock.call(app.app, port=5001, host='0.0.0.0', access_log=logger)
]
def test_app_run_new_port(web_run_app_mock, aiohttp_api_spec_dir):
app = AioHttpApp(__name__, port=5001,
specification_dir=aiohttp_api_spec_dir,
debug=True)
app.run(port=5002)
assert web_run_app_mock.call_args_list == [
mock.call(app.app, port=5002, host='0.0.0.0', access_log=None)
]
def test_app_run_default_port(web_run_app_mock, aiohttp_api_spec_dir):
app = AioHttpApp(__name__,
specification_dir=aiohttp_api_spec_dir,
debug=True)
app.run()
assert web_run_app_mock.call_args_list == [
mock.call(app.app, port=5000, host='0.0.0.0', access_log=None)
]
def test_app_run_debug(web_run_app_mock, aiohttp_api_spec_dir):
app = AioHttpApp(__name__, port=5001,
specification_dir=aiohttp_api_spec_dir)
app.add_api('swagger_simple.yaml')
app.run(debug=True)
assert web_run_app_mock.call_args_list == [
mock.call(app.app, port=5001, host='0.0.0.0', access_log=None)
]
def test_app_run_access_log(web_run_app_mock, aiohttp_api_spec_dir):
app = AioHttpApp(__name__, port=5001,
specification_dir=aiohttp_api_spec_dir,
debug=True)
logger = logging.getLogger('connexion.aiohttp_app')
app.run(access_log=logger)
assert web_run_app_mock.call_args_list == [
mock.call(app.app, port=5001, host='0.0.0.0', access_log=logger)
]
def test_app_run_server_error(web_run_app_mock, aiohttp_api_spec_dir):
app = AioHttpApp(__name__, port=5001,
specification_dir=aiohttp_api_spec_dir)
with pytest.raises(Exception) as exc_info:
app.run(server='other')
assert exc_info.value.args == ('Server other not recognized',)
def test_app_get_root_path_return_Path(aiohttp_api_spec_dir):
app = AioHttpApp(__name__, port=5001,
specification_dir=aiohttp_api_spec_dir)
assert isinstance(app.get_root_path(), pathlib.Path) == True
def test_app_get_root_path_exists(aiohttp_api_spec_dir):
app = AioHttpApp(__name__, port=5001,
specification_dir=aiohttp_api_spec_dir)
assert app.get_root_path().exists() == True
def test_app_get_root_path(aiohttp_api_spec_dir):
app = AioHttpApp(__name__, port=5001,
specification_dir=aiohttp_api_spec_dir)
root_path = app.get_root_path()
assert str(root_path).endswith(os.path.join('tests', 'aiohttp')) == True
def test_app_get_root_path_not_in_sys_modules(sys_modules_mock, aiohttp_api_spec_dir):
app = AioHttpApp('connexion', port=5001,
specification_dir=aiohttp_api_spec_dir)
root_path = app.get_root_path()
assert str(root_path).endswith(os.sep + 'connexion') == True
def test_app_get_root_path_invalid(sys_modules_mock, aiohttp_api_spec_dir):
with pytest.raises(RuntimeError) as exc_info:
AioHttpApp('error__', port=5001,
specification_dir=aiohttp_api_spec_dir)
assert exc_info.value.args == ("Invalid import name 'error__'",)
def test_app_with_empty_base_path_error(aiohttp_api_spec_dir):
spec_dir = '..' / aiohttp_api_spec_dir.relative_to(TEST_FOLDER)
app = AioHttpApp(__name__, port=5001,
specification_dir=spec_dir,
debug=True)
with pytest.raises(ConnexionException) as exc_info:
app.add_api('swagger_empty_base_path.yaml')
assert exc_info.value.args == (
"aiohttp doesn't allow to set empty base_path ('/'), "
"use non-empty instead, e.g /api",
)
def test_app_with_empty_base_path_and_only_one_api(aiohttp_api_spec_dir):
spec_dir = '..' / aiohttp_api_spec_dir.relative_to(TEST_FOLDER)
app = AioHttpApp(__name__, port=5001,
specification_dir=spec_dir,
debug=True,
only_one_api=True)
api = app.add_api('swagger_empty_base_path.yaml')
assert api is app.app
def test_app_add_two_apis_error_with_only_one_api(aiohttp_api_spec_dir):
spec_dir = '..' / aiohttp_api_spec_dir.relative_to(TEST_FOLDER)
app = AioHttpApp(__name__, port=5001,
specification_dir=spec_dir,
debug=True,
only_one_api=True)
app.add_api('swagger_empty_base_path.yaml')
with pytest.raises(ConnexionException) as exc_info:
app.add_api('swagger_empty_base_path.yaml')
assert exc_info.value.args == (
"an api was already added, "
"create a new app with 'only_one_api=False' "
"to add more than one api",
)