admin管理员组

文章数量:1355665

I need in tests GetBalancesRateThrottle.__call__ return None because of redis:

from fastapi import FastAPI, Depends
from redis.asyncio import Redis
from starlette.requests import Request

app = FastAPI()


class PartnerRateThrottle:
    scope = None
    rate = None
    cache_format = 'throttle_%(scope)s_%(ident)s'

    def __init__(self):
        pass

    async def __call__(self, request: Request):
        redis = Redis(host=settings.redis_host, port=settings.redis_port)
        # for simplicity it should return None
        return None


class GetBalancesRateThrottle(PartnerRateThrottle):
    scope = "balances"
    rate = "rate"


@app.get(
    "/balances",
    dependencies=[
        Depends(GetBalancesRateThrottle()),
    ],
)
async def get_balances(product_id: int):
    # logic

But the test does not work:

def test_balances(mocker: MockerFixture):
    mock = AsyncMock(return_value)
    mocker.patch("path.to.GetBalancesRateThrottle.__call__", side_effect=mock)

How to make GetBalancesRateThrottle.__call__ return None?

本文标签: fastapiHow to mock async GetBalancesRateThrottlecall with PytestStack Overflow