My main.py looks like this:
from retriever import get_filtered_data
@app.post(
"/getApi",
response_class=JSONResponse,
tags=["Some tags"],
description="Some description"
)
async def get_json_api(request: Basemodel,
params: Params = Depends()) -> Page:
try:
response = get_filtered_data('param_1',
request=request)
return paginate(response['items'],
additional_data={
"ABC": 123
},
params=params
)
except Exception as exception:
return HttpErrorResponse(data=f'ERROR details = {exception}',
timestamp=datetime.now())
I want to write test case for the above, the thing is get_filtered_data has all the logic which does the following:
- Connect to AzureSQL
- Retrieve the data
The final response is passed to paginate.
My current test case looks like this:
from fastapi.testclient import TestClient
from unittest.mock import patch, MagicMock, ANY
def test_get_json_property(self):
mocked_response = requests.Response()
with patch("requests.post", return_value=mocked_response) as mocked_request:
response = self.client.post("/getApi",
json={"in_filter": {"test_key": "test_value"}},
headers={"content-type": "application/json"})
mocked_request.return_value = {"items": {"test_key": "test_value"}}
assert response.status_code == 200
self.assertEqual(response.json(), mocked_response)
The result returns 422 error code with the below output:
{'type': 'missing', 'loc': ('response', 'items'), 'msg': 'Field required', 'input': <HttpErrorResponse.HttpErrorResponse object at 0x000001FD7E0C17B0>}
{'type': 'missing', 'loc': ('response', 'total'), 'msg': 'Field required', 'input': <HttpErrorResponse.HttpErrorResponse object at 0x000001FD7E0C17B0>}
{'type': 'missing', 'loc': ('response', 'page'), 'msg': 'Field required', 'input': <HttpErrorResponse.HttpErrorResponse object at 0x000001FD7E0C17B0>}
{'type': 'missing', 'loc': ('response', 'size'), 'msg': 'Field required', 'input': <HttpErrorResponse.HttpErrorResponse object at 0x000001FD7E0C17B0>}
{'type': 'missing', 'loc': ('response', 'links'), 'msg': 'Field required', 'input': <HttpErrorResponse.HttpErrorResponse object at 0x000001FD7E0C17B0>}
Can someone guide in how to tackle this scenario