Testing dependency call args in FastAPI

I have an api with a route that creates google cloud tasks. I import the task client as dependency into the endpoint. I mocked the task client in my tests with app.dependency_overrides but now I’m not sure how to use that in order to see the call args for the send_task_to_queue function. In my old flask api I could just patch send_task_to_queue and check its call_args but trying that here is giving me None

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># TASK CLIENT
class TasksClient:
def __init__(self) -> None:
# sets up client
def send_task_to_queue(
self,
queue: str,
task_func: str,
payload: Any # payload can be anything
) -> None:
# creates task - want to check the queue, task_func, and payload args
@lru_cache
def get_tasks_client() -> TasksClient:
return TasksClient()
# ENDPOINT
@router.post("/route",
status_code=201,
description="POST to route")
async def post_inspection(
req: Request,
tasks_client: TasksClient = Depends(get_tasks_client)
) -> None:
tasks_client.send_task_to_queue('queue_name', 'task_func_name', 'payload')
# MOCK CLASSES
class MockGoogleTaskClient(object):
""" Mock tasks client from google """
@staticmethod
def queue_path(project_id, loc, queue):
return f'{project_id}.{loc}.{queue}'
@staticmethod
def create_task(path, task):
return
class MockTasksClient(object):
""" mock tasks class """
def __init__(self):
self.credentials = 'creds'
self.client = MockGoogleTaskClient()
@staticmethod
def send_task_to_queue(queue, task_func, payload):
""" mock send task to queue """
return
def mock_get_tasks_client() -> MockTasksClient:
return MockTasksClient()
# CONFTEST
from api.main import app
app.dependency_overrides[get_tasks_client] = mock_get_tasks_client
# TEST
# doesn't work - get None
@patch('evir_api_v2.util.tasks.get_tasks_client', side_effect=mock_get_tasks_client)
def test_post_inspection(
m_tasks_client,
client: TestClient,
engine: Engine
) -> None:
happy_insert_resp = client.post(f'route', json={'hello': 'test'})
assert happy_insert_resp.status_code == 201
# make sure tasks are created
assert m_tasks_client.send_task_to_queue.call_args == ??? # comes in as None
</code>
<code># TASK CLIENT class TasksClient: def __init__(self) -> None: # sets up client def send_task_to_queue( self, queue: str, task_func: str, payload: Any # payload can be anything ) -> None: # creates task - want to check the queue, task_func, and payload args @lru_cache def get_tasks_client() -> TasksClient: return TasksClient() # ENDPOINT @router.post("/route", status_code=201, description="POST to route") async def post_inspection( req: Request, tasks_client: TasksClient = Depends(get_tasks_client) ) -> None: tasks_client.send_task_to_queue('queue_name', 'task_func_name', 'payload') # MOCK CLASSES class MockGoogleTaskClient(object): """ Mock tasks client from google """ @staticmethod def queue_path(project_id, loc, queue): return f'{project_id}.{loc}.{queue}' @staticmethod def create_task(path, task): return class MockTasksClient(object): """ mock tasks class """ def __init__(self): self.credentials = 'creds' self.client = MockGoogleTaskClient() @staticmethod def send_task_to_queue(queue, task_func, payload): """ mock send task to queue """ return def mock_get_tasks_client() -> MockTasksClient: return MockTasksClient() # CONFTEST from api.main import app app.dependency_overrides[get_tasks_client] = mock_get_tasks_client # TEST # doesn't work - get None @patch('evir_api_v2.util.tasks.get_tasks_client', side_effect=mock_get_tasks_client) def test_post_inspection( m_tasks_client, client: TestClient, engine: Engine ) -> None: happy_insert_resp = client.post(f'route', json={'hello': 'test'}) assert happy_insert_resp.status_code == 201 # make sure tasks are created assert m_tasks_client.send_task_to_queue.call_args == ??? # comes in as None </code>
# TASK CLIENT
class TasksClient:
    def __init__(self) -> None:
        # sets up client

    def send_task_to_queue(
            self,
            queue: str,
            task_func: str,
            payload: Any  # payload can be anything
    ) -> None:
        # creates task - want to check the queue, task_func, and payload args


@lru_cache
def get_tasks_client() -> TasksClient:
    return TasksClient()


# ENDPOINT
@router.post("/route",
             status_code=201,
             description="POST to route")
async def post_inspection(
        req: Request,
        tasks_client: TasksClient = Depends(get_tasks_client)
) -> None:
    tasks_client.send_task_to_queue('queue_name', 'task_func_name', 'payload')


# MOCK CLASSES
class MockGoogleTaskClient(object):
    """ Mock tasks client from google """
    @staticmethod
    def queue_path(project_id, loc, queue):
        return f'{project_id}.{loc}.{queue}'

    @staticmethod
    def create_task(path, task):
        return


class MockTasksClient(object):
    """ mock tasks class """
    def __init__(self):
        self.credentials = 'creds'
        self.client = MockGoogleTaskClient()

    @staticmethod
    def send_task_to_queue(queue, task_func, payload):
        """ mock send task to queue """
        return


def mock_get_tasks_client() -> MockTasksClient:
    return MockTasksClient()


# CONFTEST
from api.main import app
app.dependency_overrides[get_tasks_client] = mock_get_tasks_client


# TEST
# doesn't work - get None
@patch('evir_api_v2.util.tasks.get_tasks_client', side_effect=mock_get_tasks_client)
def test_post_inspection(
        m_tasks_client,
        client: TestClient,
        engine: Engine
) -> None:
    happy_insert_resp = client.post(f'route', json={'hello': 'test'})
    assert happy_insert_resp.status_code == 201

    # make sure tasks are created
    assert m_tasks_client.send_task_to_queue.call_args == ???  # comes in as None

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật