最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

fastapi - How to mock async GetBalancesRateThrottle.__call__ with Pytest? - Stack Overflow

programmeradmin2浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论