-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
78 lines (58 loc) · 2.14 KB
/
tests.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
import muffin
import pytest
from apiclient.backends import BACKENDS
# Support Python < 3.8
try:
import mock # type: ignore
except ImportError:
import unittest.mock as mock # type: ignore
@pytest.fixture
def aiolib():
return ('asyncio', {'use_uvloop': False})
@pytest.fixture
def app():
return muffin.Application(DEBUG=True)
@pytest.mark.parametrize('backend_type', BACKENDS)
def test_base(app, backend_type):
from muffin_apiclient import Plugin
github = Plugin(
app, name='github', root_url='https://api.github.com', backend_type=backend_type)
assert github
assert github.client
assert github.api
assert github.name == 'github'
assert isinstance(github.client.backend, BACKENDS[backend_type])
@mock.patch('apiclient.backends._httpx.BackendHTTPX.request', new_callable=mock.AsyncMock)
async def test_client(req, app):
from muffin_apiclient import Plugin
class Github(Plugin):
name = 'github'
root_url = 'https://api.github.com'
timeout = 20
def setup(self, app, **options):
super().setup(app, **options)
@self.client_middleware
async def md(method, url, options):
options.setdefault('headers', {})
options['headers']['x-token'] = 'TESTS'
return method, url, options
github = Github(app, backend_type='httpx')
assert github.client
assert github.api
assert github.cfg.root_url == 'https://api.github.com'
assert github.cfg.timeout == 20
req.return_value = True
res = await github.api.repos.klen.muffin()
assert res
req.assert_awaited()
req.assert_called_with(
'GET', 'https://api.github.com/repos/klen/muffin', headers={'x-token': 'TESTS'},
raise_for_status=True, read_response_body=True, parse_response_body=True)
req.reset_mock()
req.return_value = True
res = await github.request('GET', '/test')
assert res
req.assert_awaited()
req.assert_called_with(
'GET', 'https://api.github.com/test', headers={'x-token': 'TESTS'},
raise_for_status=True, read_response_body=True, parse_response_body=True)