Skip to content

Test redis

test_getting_value(fastapi_app, fake_redis_pool, client) async

Tests that you can get value from redis by key.

Parameters:

Name Type Description Default
fastapi_app FastAPI

current application fixture.

required
fake_redis_pool ConnectionPool

fake redis pool.

required
client AsyncClient

client fixture.

required
Source code in hestia/tests/test_redis.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@pytest.mark.anyio
async def test_getting_value(
    fastapi_app: FastAPI,
    fake_redis_pool: ConnectionPool,
    client: AsyncClient,
) -> None:
    """
    Tests that you can get value from redis by key.

    :param fastapi_app: current application fixture.
    :param fake_redis_pool: fake redis pool.
    :param client: client fixture.
    """
    test_key = uuid.uuid4().hex
    test_val = uuid.uuid4().hex
    async with Redis(connection_pool=fake_redis_pool) as redis:
        await redis.set(test_key, test_val)
    url = fastapi_app.url_path_for("get_redis_value")
    response = await client.get(url, params={"key": test_key})

    assert response.status_code == status.HTTP_200_OK
    assert response.json()["key"] == test_key
    assert response.json()["value"] == test_val

test_setting_value(fastapi_app, fake_redis_pool, client) async

Tests that you can set value in redis.

Parameters:

Name Type Description Default
fastapi_app FastAPI

current application fixture.

required
fake_redis_pool ConnectionPool

fake redis pool.

required
client AsyncClient

client fixture.

required
Source code in hestia/tests/test_redis.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@pytest.mark.anyio
async def test_setting_value(
    fastapi_app: FastAPI,
    fake_redis_pool: ConnectionPool,
    client: AsyncClient,
) -> None:
    """
    Tests that you can set value in redis.

    :param fastapi_app: current application fixture.
    :param fake_redis_pool: fake redis pool.
    :param client: client fixture.
    """
    url = fastapi_app.url_path_for("set_redis_value")

    test_key = uuid.uuid4().hex
    test_val = uuid.uuid4().hex
    response = await client.put(
        url,
        json={
            "key": test_key,
            "value": test_val,
        },
    )

    assert response.status_code == status.HTTP_200_OK
    async with Redis(connection_pool=fake_redis_pool) as redis:
        actual_value = await redis.get(test_key)
    assert actual_value.decode() == test_val