小编mfo*_*ism的帖子

aiohttp_client - 运行时错误:超时上下文管理器应该在任务中使用

我在做什么

我正在通过构建一个 REST api 来学习 aiohttp,我正在使用 Pytest(及其 async 和 aiohttp 插件)进行测试。

对于我的第一次测试(我从一开始就使用 TDD)我有以下代码:

@pytest.mark.asyncio
async def test_handle_user_create(
    aiohttp_client, init_test_app, create_test_user_table
):
    payload = {
        "email": "tintin@gmail.com",
        "username": "Tintin",
        "password": "y0u != n00b1e",
    }
    client = await aiohttp_client(init_test_app)
    resp = await client.post("/users/", json=payload)
    ...
Run Code Online (Sandbox Code Playgroud)
  • aiohttp_client 是客户端装置来自 pytest-aiohttp
  • init_test_app 是一个固定装置,它基本上反映了我将要构建的应用程序
  • create_test_user_table 是我在测试数据库中为用户创建表的夹具

它有什么问题

我的第一个测试是在上面代码块的最后一行抛出以下运行时错误:

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ …
Run Code Online (Sandbox Code Playgroud)

python python-asyncio aiohttp pytest-aiohttp pytest-asyncio

5
推荐指数
0
解决办法
1145
查看次数

requests-mock:如何匹配模拟端点中发布的有效负载

我做了什么

我编写了一个身份验证类,用于使用应用程序的API 密钥及其API 密钥机密从 Twitter获取应用程序的不记名令牌,如Twitter 开发人员文档中所示。

我使用这种方式模拟了适当的端点requests_mock

@pytest.fixture
def mock_post_bearer_token_endpoint(
    requests_mock, basic_auth_string, bearer_token
):
    requests_mock.post(
        "https://api.twitter.com/oauth2/token",
        request_headers={
            "Authorization": f"Basic {basic_auth_string}",
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        },
        json={"token_type": "bearer", "access_token": f"{bearer_token}"},
    )
Run Code Online (Sandbox Code Playgroud)

我的测试方法是:

@pytest.mark.usefixtures("mock_post_bearer_token_endpoint")
def test_basic_auth(api_key, api_key_secret, bearer_token):
    response = requests.post(
        'https://api.twitter.com/oauth2/token',
        data={"grant_type": "client_credentials"},
        auth=TwitterBasicAuth(api_key, api_key_secret),
    )
    assert response.json()['access_token'] == bearer_token
Run Code Online (Sandbox Code Playgroud)

TwitterBasicAuth我编写的身份验证类在哪里,固定装置basic_auth_string是一个硬编码的字符串,可以通过适当地转换固定装置来获得api_keyapi_key_secret

它有效。

问题

但我确实对模拟端点不检查有效负载这一事实感到困扰。在这种特殊情况下,有效负载对于获取不记名令牌至关重要。

requests_mock我已经梳理了(以及)的文档responses,但还没有弄清楚如何使端点仅在发布正确的有效负载时才使用不记名令牌进行响应。

请帮忙。

python pytest python-requests requests-mock

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

ValueError:不再支持单位“M”和“Y”,因为它们不代表明确的时间增量值持续时间

我最近将我的代码从 Python 3.3 升级到 Python 3.7,它目前抛出一个错误,内容为:

ValueError:不再支持单位“M”和“Y”,因为它们不代表明确的时间增量值持续时间

这令人费解,因为代码在升级前运行良好。

这是代码的违规部分:

df['date_modified'] = (df['date_variable']-pd.to_timedelta(df['years_variable'], unit = 'Y')).dt.date
Run Code Online (Sandbox Code Playgroud)

这是完整的代码:

import pandas as pd
import numpy as np

idx = [np.array(['Jan-18', 'Jan-18', 'Feb-18', 'Mar-18', 'Mar-18', 'Mar-18','Apr-18', 'Apr-18', 'May-18', 'Jun-18', 'Jun-18', 'Jun-18','Jul-18', 'Aug-18', 'Aug-18', 'Sep-18', 'Sep-18', 'Oct-18','Oct-18', 'Oct-18', 'Nov-18', 'Dec-18', 'Dec-18',]),np.array(['A', 'B', 'B', 'A', 'B', 'C', 'A', 'B', 'B', 'A', 'B', 'C','A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'A', 'B', 'C'])]
data = [{'years_variable': 1}, {'years_variable': 5}, {'years_variable': 3}, {'years_variable': 2}, {'years_variable': 7}, {'years_variable': …
Run Code Online (Sandbox Code Playgroud)

python python-3.x pandas

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