小编Mar*_*lle的帖子

如何在 fastapi 中将 httpx AsyncClient 与 pytest 结合使用?

我已经尝试了所有我能找到的东西,但我无法让异步测试工作。

我开始RuntimeError: This event loop is already running运行TestClient(根据文档,这是有意义的),但我开始httpx.ConnectError: [Errno 8] nodename nor servname provided, or not known使用httpx AsyncClient.

我有一个简单的测试:

@pytest.fixture(scope="module")
async def async_client() -> Generator:
    async with AsyncClient(app=app, base_url='http://0.0.0.0') as client:
        yield client

@pytest.mark.asyncio@mock.patch('apps.core.views.requests.get', new=mocked_get_request)
async def test_get_non_json_response(async_client: AsyncClient):
    response = await async_client.get("/mymedia")
    assertEqual(response.json()['error']['message']['message'], 'Not json')
Run Code Online (Sandbox Code Playgroud)

哪里 /media

@app.get('/mymedia')
async def my_media(request: Request, cache: RedisCacheBackend = Depends(redis_cache)):
    return await my_media_ep(request, cache=cache)
Run Code Online (Sandbox Code Playgroud)

my_media_ep是一个包含多个异步 api 调用的长函数。

我也按照异步测试文档中的建议进行了尝试,但得到了相同的错误。

有什么建议或例子吗?

python unit-testing pytest pytest-asyncio fastapi

9
推荐指数
0
解决办法
3114
查看次数

如何在 Wagtail Admin 中添加多对多字段?

我正在尝试为包含在每个页面上的 Wagtail 站点设置页脚,但我想包含一个链接列表(电话、电子邮件、社交媒体)。如果我在没有 的情况下尝试下面的代码,panel = [...]我可以看到它的工作原理,但我无法添加任何项目:

在此处输入图片说明

from wagtail.contrib.settings.models import BaseSetting, register_setting
from django import forms

class ContactInfo(models.Model):
    CONTACT_CHOICES = (
        ('fas fa-phone', 'Phone'),
        ('fas fa-envelope', 'Email'),
        ('fab fa-facebook-f', 'Facebook'),
        ('fa-instagram', 'Instagram'),
        ('fab fa-linkedin', 'LinkedIn'),
        ('fab fa-twitter', 'Twitter'),
        ('fab fa-pinterest', 'Pinterest'),
        ('fab fa-github', 'GitHub'),
        ('fab fa-gitlab', 'GitLab'),
    )

    contact_type = models.CharField(choices=CONTACT_CHOICES, max_length=50)
    contact_info = models.CharField(max_length=50)
    info_prefix = models.CharField(max_length=10, editable=False)

    def save(self, *args, **kwargs):
        if self.contact_type == 'Phone':
            self.info_prefix = 'tel:'
        elif self.contact_type == 'Email':
            self.info_prefix = 'mailto:'
        else:
            self.info_prefix …
Run Code Online (Sandbox Code Playgroud)

django wagtail

1
推荐指数
1
解决办法
1661
查看次数