i am working in a project with fastAPI
As the title says, i have an endpoint which calls a logging event when a HTTPException occurs, and when the request is called and finished
Something like this:
@router.get(
"/chat/{chat_id}/messages/",
status_code=status.HTTP_200_OK,
)
async def get_messages(chat_message: GetMessageValidator = Depends(), request: Request = None):
logging.info(request.url.path+" request started") ##LOGGING
if chat_message.chat_id_validator(chat_message.chat_id):
logging.error(request.url.path+settings.GET_MESSAGES_CHAT_ID_ERROR) ##LOGGING
raise HTTPException(
status_code=404, detail=settings.GET_MESSAGES_CHAT_ID_ERROR
)
logging.info(request.url.path+" request OK") ##LOGGING
return chat_message
And i have build a test with pytest which call that endpoint, something like this:
@dataclass
class ChatMessage:
from_user: str
to_user: str
chat_id: str
body: str
@pytest.mark.asyncio()
async def test_pagination_get_messages(client: AsyncSession,
user_token_header):
conversation = [
ChatMessage(frank_id, pepe_id, chat_record.chat_id, 'Hello Pepe!')
]
page_1 = await client.get( ##ENDPOINT CALL
f"/api/v1/chat/messages/",
json={
"chat_id": str(chat_record.chat_id),
"quantity": 3
},
headers=user_token_header
)
assert page_1.status_code == 200
The pytest response is okay, but i don't want that the logging event works when i call the endpoint from a pytest, and i don't have an idea to avoid the logging event call when the pytest of that endpoint is running..
Can you give an idea or a solution of how to mock logging events in the endpoint when is called from the pytest?
Thanks!