I have a simple asynchronous functions that fetches JSON from the web:
async def fetch (session : aiohttp.ClientSession, url : str, pageSize : int = 100, pageNumber : int = 0):
async with session.get(url, params={'pageSize' : f"{pageSize}", 'pageNumber' : f"{pageNumber}"}) as response:
return await response.json()
How do I write unit tests for it? I am learning how to do that in Python currently and I have settled down on using unittest and aiounittest modules. The scaffolding that I use for testing is as follows:
class AsyncTest(aiounittest.AsyncTestCase):
async def test_fetch(self):
mocked_session = ... #no idea how to create
json = await fetch(mocked_session, "url_for_mock", 10, 0)
I think I would have to somehow mock the session argument using unittest.Mock() functionality to return some harcoded json data but I am a little lost on how to do this so it works inside async with statement. I have never used an unit testing library in Python and an example would be very helpful here. I am using Python 3.8 and aiohttp for networking.
I was able to mock the asynchronous resource manager by implementing my own version of __aenter__ magic method and using AsyncMock to mock relevant async methods. The proof of concept test that mocks network calls looks like follows:
class AsyncTest(aiounittest.AsyncTestCase):
async def test_fetch(self):
request_mock = AsyncMock()
request_mock.__aenter__.return_value = request_mock
request_mock.json.return_value = { 'hello' : 'world'}
session = Mock()
session.get.return_value = request_mock
json = await fetch(session, "url_for_mock", 10, 0)
Related
I am using https://pypi.org/project/fastapi-microsoft-identity/ package for AD Azure authorization using requires_auth decorator. From my tests would like to avoid the authorization process but I cannot disable this decorator always returns 401.
this is the following code I am using from my test
def mock_decorator(*args, **kwargs) :
def decorator(f)
wraps(f)
def requires_auth(*args, **kwargs):
return f(*args, **kwargs)
return requires_auth
patch('fastapi_microsoft_identity.requires_auth', mock_decorator).start()
#pytest.fixture
def client():
with TestClient(api) as client:
yield client
def test_api_endpoint(client):
response = client.get("/api/weather/london")
assert response.status_code == 200
Since I am patching auth decorator should return 200 status code, instead, I am getting 401 UnAuthorized
The following code from api
#router.get('/api/weather/{city}')
#requires_auth
async def weather(request: Request, loc: Location = Depends(), units: Optional[str] = 'metric'):
try:
validate_scope(expected_scope, request)
return await openweather_service.get_report_async(loc.city, loc.state, loc.country, units)
except AuthError as ae:
return fastapi.Response(content=ae.error_msg, status_code=ae.status_code)
except ValidationError as ve:
return fastapi.Response(content=ve.error_msg, status_code=ve.status_code)
except Exception as x:
return fastapi.Response(content=str(x), status_code=500)
please help what I am doing wrong.
You can't really bypass a decorator but can circumnavigate it if in its implementation it uses #wrap() in the code.
Now # require_auth does use a #wrap in its implementation so we can circumnavigate the decorator by calling the original function in this case ‘weather’ function as:-
weather.__wrapped__()
instead of the usual way of calling the function.
The __wrapped__ method basically has reference to the original function instead of the decorator.
Here I created a small API which basically returns two strings now here I have called the weather function without the .__wrapped__
# main.py
from fastapi_microsoft_identity import requires_auth, validate_scope, AuthError
import fastapi
from fastapi import FastAPI
app = FastAPI()
#requires_auth
async def weather() :
return False
#app.get("/")
async def root():
if await weather.() :
return {"Decorator is working and not allowing the function f() to work "}
else :
return {"Decorator is disabled"}
Here the weather () doesn’t get executed that is why the string “Decorator is working and not allowing the function f() to work” is returned but in your case it is giving unauthorize error
But now when I call it using wrapped
# main.py
from fastapi_microsoft_identity import requires_auth, validate_scope, AuthError
import fastapi
from fastapi import FastAPI
app = FastAPI()
#requires_auth
async def weather() :
return False
#app.get("/")
async def root():
if await weather.__wrapped__() :
return {"Decorator is working and not allowing the function f() to work "}
else :
return {"Decorator is disabled"}
Now that I have used the,__wrapped__ while calling the weather function it is being executed directly circumnavigating the decorator
So if you don’t want the require#authto work call the weather function like this weather.__wrapped__(<arguments>)
Refer this python docs on wrapper method and this gihub repo on require_auth.
I'm trying to test Flask REST-API end points with pytest using test_client(). But I'm getting an Error saying
> RuntimeError: You cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly.
Can Anyone explain me why this happen and what is the solution to avoid this Error.
Test Functiob:
import pytest
from unittest import mock
from flask import request
from app import create_app
from app.base.views import views
app = create_app()
async def save_token_(sugar, payload, instance, bool, domain_id):
return {'valid':True}
payload = {
"password": 'password',
"username": 'crm_admin',
"grant_type": "password"
}
#pytest.mark.asyncio
async def test_post_sugar_token(monkeypatch, aiohttp_client, loop):
mock_save_token = mock.AsyncMock(name = "mock_save_token")
mock_save_token.return_value = await save_token_(None, payload, 'domain.org', True, 89)
monkeypatch.setattr(views, 'save_token', mock_save_token)
await views.save_token(None, payload, 'domain.org', True, 9)
assert mock_save_token.call_args_list == [mock.call(None, payload, 'domain.org', True, 89)]
headers = {'Autherization': 'ehrdmek2492.fkeompvmw.04294002'}
data = {
'password': '12345',
'key':'Hi',
'instance': 'my.domain',
'domain_id': 1
}
##-# using test_client()
client = app.test_client()
res = client.post('/token/sugar/', data = data, headers = headers)
assert res.status_code == 200
assert res.content_type == 'application/json'
assert res.json == {'valid':True}
# # ----------------------------------------------------------------------
Error Message
I ran into the same issue and opened a ticket on flask's github repo:
https://github.com/pallets/flask/issues/4375.
They kindly explained the issue and provided a workaround.
In short, flask can handle async views, and flask's test_client can be used in an async context, but you cannot use both at the same time.
Quoting from the github issue:
The problem in this case is that the Flask codebase is not compatible with asyncio, so you cannot run the test client inside an asyncio loop. There is really nothing to gain from writing your unit test as an async test, since Flask itself isn't async.
Here's the workaround suggested, slightly re-adjusted for your example:
#pytest.mark.asyncio
async def test_post_sugar_token():
# ... same code as before
##-# using test_client()
def sync_test():
with app.test_client() as client:
res = client.post('/token/sugar/', data = data, headers = headers)
assert res.status_code == 200
assert res.content_type == 'application/json'
assert res.json == {'valid':True}
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, sync_test)
I have an async function that calls a streaming api. What is the best way to write unit test for this function? The api response has to be mocked.
I tried with aiounittest and used mock from unittest. But this calls the actual api instead of getting the mocked response. Also tried with pytest.mark.asyncio annotation, but this kept giving me the error - coroutine was never awaited. I have verified that pytest-asyncio has been installed.
I am using VS Code and Python 3.6.6
Here is the relevant code snippet:
async def method1():
response = requests.get(url=url, params=params, stream=True)
for data in response.iter_lines():
# processing logic here
yield data
Pasting some of the tests I tried.
def mocked_get(*args, **kwargs):
#implementation of mock
class TestClass (unittest.TestCase):
#patch("requests.get", side_effect=mocked_get)
async def test_method (self, mock_requests):
resp = []
async for data in method1:
resp.append (data)
#Also tried await method1
assert resp
Also tried with class TestClass (aiounittest.AsyncTestCase):
Use asynctest instead of aiounittest.
Replace unittest.TestCase with asynctest.TestCase.
Replace from unittest.mock import patch with from asynctest.mock import patch.
async for data in method1: should be async for data in method1():.
import asynctest
from asynctest.mock import patch
class TestClass(asynctest.TestCase):
#patch("requests.get", side_effect=mocked_get)
async def test_method(self, mock_requests):
resp = []
async for data in method1():
resp.append(data)
assert resp
I have the following async function:
async def my_func(request):
# preparation code here
with aiohttp.ClientSession() as s:
with s.post(url, headers) as response:
status_code = response.status
if status_code == 200:
json_resp = await response.json()
elif:
# more checks
Trying to test it but still haven't found a way.
from asynctest.mock import CoroutineMock, MagicMock as AsyncMagicMock
#mock.patch("path_to_function.aiohttp.ClientSession", new_callable=AsyncMagicMock) # this is unittest.mock
def test_async_function(self, mocked_session):
s = AsyncMagicMock()
mocked_client_session().__aenter__ = CoroutineMock(side_effect=s)
session_post = s.post()
response_mock = AsyncMagicMock()
session_post.__aenter__ = CoroutineMock(side_effect=response_mock)
response_mock.status = 200
but not working as I want. Any help on how to test context managers would be highly appreciated.
Silly me, Found the solution. I was using side_effect instead of return_value.Works like a charm. Thank you very much
Using the below route definition, I am trying to extract the book_id out of the URL in aiohttp.
from aiohttp import web
routes = web.RouteTableDef()
#routes.get('/books/{book_id}')
async def get_book_pages(request: web.Request) -> web.Response:
book_id = request.match_info.get('book_id', None)
return web.json_response({'book_id': book_id})
Below is the test (using pytest) I have written
import asynctest
import pytest
import json
async def test_get_book() -> None:
request = make_mocked_request('GET', '/books/1')
response = await get_book(request)
assert 200 == response.status
body = json.loads(response.body)
assert 1 == body['book_id']
Test Result:
None != 1
Expected :1
Actual :None
Outside of the tests, when I run a request to /books/1 the response is {'book_id': 1}
What is the correct way to retrieve dynamic values from the path in aiohttp when mocking the request?
make_mocked_request() knows nothing about an application and its routes.
To pass dynamic info you need to provide a custom match_info object:
async def test_get_book() -> None:
request = make_mocked_request('GET', '/books/1',
match_info={'book_id': '1'})
response = await get_book(request)
assert 200 == response.status
body = json.loads(response.body)
assert 1 == body['book_id']
P.S.
In general, I want to warn about mocks over-usage. Usually, functional testing with aiohttp_client is easier to read and maintain.
I prefer mocking for really hard-to-rest things like network errors emulation.
Otherwise your tests do test your own mocks, not a real code.