Skip to content

Commit 697db9e

Browse files
author
Antti Nykänen
committed
Refactor event tests to use AioHTTPTestCase
1 parent e6ac445 commit 697db9e

File tree

1 file changed

+75
-36
lines changed

1 file changed

+75
-36
lines changed

tests/test_client.py

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
from urllib.parse import urlparse
33
from yarl import URL
44

5-
from aiohttp import ClientResponse
6-
from aiohttp.client_exceptions import ServerDisconnectedError
5+
from aiohttp import ClientResponse, web
76
from aiohttp.helpers import TimerNoop
8-
from aiohttp.test_utils import make_mocked_coro
7+
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop, make_mocked_coro
98
import jsonschema
109
import pytest
1110
from requests import Response
@@ -1946,39 +1945,6 @@ async def test_history_async_post(loop, session):
19461945
assert latest.status_code == 500
19471946

19481947

1949-
@pytest.mark.asyncio
1950-
async def test_on_request_chunk_sent_async_hook():
1951-
data_sent = make_mocked_coro()
1952-
s = Session(
1953-
'http://localhost/api',
1954-
schema=api_schema_all,
1955-
enable_async=True,
1956-
enable_history=True,
1957-
event_hooks={'on_request_chunk_sent': data_sent}
1958-
)
1959-
1960-
s.create('leases')
1961-
a = s.create('leases')
1962-
assert a.is_dirty
1963-
a.lease_id = '1'
1964-
a.active_status = 'pending'
1965-
a.reference_number = 'test'
1966-
with pytest.raises(ConnectionRefusedError):
1967-
await a.commit()
1968-
assert data_sent.called
1969-
assert json.loads(data_sent.call_args[0][2].chunk) == {
1970-
'data': {
1971-
'attributes': {
1972-
'active-status': 'pending',
1973-
'lease-id': '1',
1974-
'reference-number': 'test'
1975-
},
1976-
'relationships': {},
1977-
'type': 'leases'
1978-
}
1979-
}
1980-
1981-
19821948
def test_set_event_hooks_for_requests():
19831949
"""Event hooks for requests library is a keyword argument
19841950
so this test only tests that the request_kwargs are updated correctly.
@@ -2018,3 +1984,76 @@ def test_set_event_hooks_for_requests():
20181984
)
20191985
assert 'hooks' in s._request_kwargs
20201986
assert s._request_kwargs.get('hooks') == {'test': None}
1987+
1988+
1989+
class TestEventHooks(AioHTTPTestCase):
1990+
async def get_application(self):
1991+
async def leases_create(request):
1992+
headers = {'Content-Type': 'application/vnd.api+json'}
1993+
data = {'errors': [{'title': 'Internal server error'}]}
1994+
data = json.dumps(data)
1995+
return web.Response(body=data, status=500, headers=headers)
1996+
1997+
app = web.Application()
1998+
app.router.add_post('/api/leases', leases_create)
1999+
return app
2000+
2001+
@unittest_run_loop
2002+
async def test_on_request_chunk_sent_async_hook(self):
2003+
data_sent = make_mocked_coro()
2004+
2005+
url = f'http://{self.server.host}:{self.server.port}/api'
2006+
s = Session(
2007+
url,
2008+
schema=api_schema_all,
2009+
enable_async=True,
2010+
enable_history=True,
2011+
event_hooks={'on_request_chunk_sent': data_sent}
2012+
)
2013+
2014+
s.create('leases')
2015+
a = s.create('leases')
2016+
assert a.is_dirty
2017+
a.lease_id = '1'
2018+
a.active_status = 'pending'
2019+
a.reference_number = 'test'
2020+
with pytest.raises(DocumentError):
2021+
await a.commit()
2022+
assert data_sent.called
2023+
assert json.loads(data_sent.call_args[0][2].chunk) == {
2024+
'data': {
2025+
'attributes': {
2026+
'active-status': 'pending',
2027+
'lease-id': '1',
2028+
'reference-number': 'test'
2029+
},
2030+
'relationships': {},
2031+
'type': 'leases'
2032+
}
2033+
}
2034+
2035+
@unittest_run_loop
2036+
async def test_on_response_chunk_received_async_hook(self):
2037+
data_received = make_mocked_coro()
2038+
2039+
url = f'http://{self.server.host}:{self.server.port}/api'
2040+
s = Session(
2041+
url,
2042+
schema=api_schema_all,
2043+
enable_async=True,
2044+
enable_history=True,
2045+
event_hooks={'on_response_chunk_received': data_received}
2046+
)
2047+
2048+
s.create('leases')
2049+
a = s.create('leases')
2050+
assert a.is_dirty
2051+
a.lease_id = '1'
2052+
a.active_status = 'pending'
2053+
a.reference_number = 'test'
2054+
with pytest.raises(DocumentError):
2055+
await a.commit()
2056+
assert data_received.called
2057+
assert json.loads(data_received.call_args[0][2].chunk) == {
2058+
'errors': [{'title': 'Internal server error'}]
2059+
}

0 commit comments

Comments
 (0)