Last active
October 3, 2019 11:28
-
-
Save hoefling/3b807f3d495a47d54cc8af4299610c1f to your computer and use it in GitHub Desktop.
test aiomock
This file contains hidden or 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
from unittest.mock import Mock | |
import asyncpg | |
import pytest | |
from async_lru import alru_cache | |
@alru_cache | |
async def run(): | |
conn = await asyncpg.connect(user='user', password='pass', database='db', host='127.0.0.1') | |
await conn.close() | |
class AsyncMock(Mock): | |
def __call__(self, *args, **kwargs): | |
sup = super(AsyncMock, self) | |
async def coro(): | |
return sup.__call__(*args, **kwargs) | |
return coro() | |
def __await__(self): | |
return self().__await__() | |
@pytest.fixture | |
def mock_asyncpg(monkeypatch): | |
""" Patch asyncpg.connect """ | |
monkeypatch.setattr(asyncpg, "connect", AsyncMock()) | |
@pytest.fixture(autouse=True) | |
def clear_alru_cache(): | |
run.cache_clear() | |
@pytest.mark.asyncio | |
async def test_mocked(mock_asyncpg): | |
await run() | |
@pytest.mark.asyncio | |
async def test_not_mocked(): | |
with pytest.raises(asyncpg.exceptions.InvalidAuthorizationSpecificationError): | |
await run() |
Author
hoefling
commented
Oct 2, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment