-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,29 @@ | ||
import aiohttp | ||
import aioresponses | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def aioresponse(): | ||
"""Fixture to mock aiohttp responses.""" | ||
with aioresponses.aioresponses() as aioresponse: | ||
yield aioresponse | ||
|
||
|
||
@pytest.fixture | ||
@pytest.mark.asyncio | ||
async def http_session(aioresponse) -> aiohttp.ClientSession: | ||
""" | ||
Fixture function for a aiohttp.ClientSession. | ||
Requests fixture aioresponse to ensure that all client sessions do not make actual requests. | ||
""" | ||
resolver = aiohttp.AsyncResolver() | ||
connector = aiohttp.TCPConnector(resolver=resolver) | ||
client_session = aiohttp.ClientSession(connector=connector) | ||
|
||
yield client_session | ||
|
||
await client_session.close() | ||
await connector.close() | ||
await resolver.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import aiohttp | ||
import pytest | ||
|
||
|
||
if TYPE_CHECKING: | ||
import aioresponses | ||
|
||
|
||
class TestSessionFixture: | ||
"""Grouping for aiohttp.ClientSession fixture tests.""" | ||
|
||
@pytest.mark.asyncio | ||
async def test_session_fixture_no_requests(self, http_session: aiohttp.ClientSession): | ||
""" | ||
Test all requests fail. | ||
This means that aioresponses is being requested by the http_session fixture. | ||
""" | ||
url = "https://github.com/" | ||
|
||
with pytest.raises(aiohttp.ClientConnectionError): | ||
await http_session.get(url) | ||
|
||
@pytest.mark.asyncio | ||
async def test_session_fixture_mock_requests( | ||
self, aioresponse: aioresponses.aioresponses, http_session: aiohttp.ClientSession | ||
): | ||
""" | ||
Test all requests fail. | ||
This means that aioresponses is being requested by the http_session fixture. | ||
""" | ||
url = "https://github.com/" | ||
status = 200 | ||
aioresponse.get(url, status=status) | ||
|
||
async with http_session.get(url) as resp: | ||
assert status == resp.status |