Mock testing of API endpoint

Hi,

I am trying to mock a function : test_func but when i run the test, it don’t take mock values.
please let me know if you need more info. any suggestions are appreciated.

Below is my project structure :

|_my_api
| _|helper
| __|function_alpha.py
|_my_api.py
|tests
|
|_test_my_api.py

------------------- test_my_api-.py-----------------------

from unittest import mock
from contextlib import contextmanager
from my_api.my_api import app
import json

@contextmanager
def mocked_bad_event_es():
    with mock.patch("my_api.helper.function_alpha.test_func") as mocked_query_single_event:
        def _mock_query_single_event(_host, _index, _sort_field: str, identifier: str, **kwargs):
             json({"applicationId": 'Mock_test',
            "events": []
                })

        mocked_query_single_event.side_effect = _mock_query_single_event

        yield

def test_xyz():

    with mocked_bad_event_es():
        app.test_client
        _, response = app.test_client.get("/protected/20503/events", params=[("applicationId", "sampleApplicationId")])

    #unwrap the response
    response_body = json.loads(response.body.decode('utf-8'))

    import pdb; pdb.set_trace()
    assert response.status == 200
    assert len(response_body['events']) == 0

— my_api.py ----------

@app.route('/protected/20503/events')
async def lookup_application_id(request):

    <removed some code>
 
    api_response = await test_func(app.ctx.httpSession, app.config, application_id, limit)
    return api_response

Someone will hopefully help you out with this, but this is more a unittest problem than a Sanic problem, so it may be some time.

Looking real quick from my phone I notice that your mock func doesn’t look like it returns a response object.

What does this mean? What problem are you facing?

appreciate for taking a look.

I have mock function : mocked_bad_event_es (using side_effect).

API endpoint refers to a function : test_func, which is mocked with mocked_bad_event_es()
I expect the API to return with application id : Mock_test (mocked data)

I am sorry. I am really lost as to what the problem is you are seeing.