Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: python3.9
python: python3

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down
27 changes: 27 additions & 0 deletions modules/redis/testcontainers/redis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import Optional

import redis
from redis.asyncio import Redis as asyncRedis
from testcontainers.core.container import DockerContainer
from testcontainers.core.utils import raise_for_deprecated_parameter
from testcontainers.core.waiting_utils import wait_container_is_ready
Expand Down Expand Up @@ -69,3 +70,29 @@ def start(self) -> "RedisContainer":
super().start()
self._connect()
return self


class AsyncRedisContainer(RedisContainer):
"""
Redis container.

Example
-------
.. doctest::

>>> from testcontainers.redis import AsyncRedisContainer

>>> with AsyncRedisContainer() as redis_container:
... redis_client =await redis_container.get_async_client()
"""

def __init__(self, image="redis:latest", port_to_expose=6379, password=None, **kwargs):
super().__init__(image, port_to_expose, password, **kwargs)

async def get_async_client(self, **kwargs):
return await asyncRedis(
host=self.get_container_host_ip(),
port=self.get_exposed_port(self.port),
password=self.password,
**kwargs,
)
61 changes: 60 additions & 1 deletion modules/redis/tests/test_redis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import time

from testcontainers.redis import RedisContainer
from testcontainers.redis import RedisContainer, AsyncRedisContainer
import redis
import pytest


def test_docker_run_redis():
Expand All @@ -23,6 +25,63 @@ def test_docker_run_redis_with_password():
assert client.get("hello") == "world"


pytest.mark.usefixtures("anyio_backend")


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
async def test_key_set_in_async_redis(anyio_backend):
with AsyncRedisContainer() as container:
async_redis_client: redis.Redis = await container.get_async_client(decode_responses=True)
key = "key"
expected_value = 1
await async_redis_client.set(key, expected_value)
actual_value = await async_redis_client.get(key)
assert int(actual_value) == expected_value


pytest.mark.usefixtures("anyio_backend")


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
@pytest.mark.skip(reason="Need to sort out async pub/sub")
async def test_docker_run_async_redis(anyio_backend):
config = AsyncRedisContainer()
with config as container:
client: redis.Redis = await container.get_async_client(decode_responses=True)
p = await client.pubsub()
await p.subscribe("test")
await client.publish("test", "new_msg")
msg = wait_for_message(p)
assert "data" in msg
assert b"new_msg", msg["data"]


pytest.mark.usefixtures("anyio_backend")


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
async def test_docker_run_async_redis_with_password(anyio_backend):
config = AsyncRedisContainer(password="mypass")
with config as container:
client: redis.Redis = await container.get_async_client(decode_responses=True)
await client.set("hello", "world")
assert await client.get("hello") == "world"


pytest.mark.usefixtures("anyio_backend")


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
async def test_key_set_in_async(anyio_backend):
with AsyncRedisContainer() as container:
async_redis_client: redis.Redis = await container.get_async_client(decode_responses=True)
key = "key"
expected_value = 1
await async_redis_client.set(key, expected_value)
actual_value = await async_redis_client.get(key)
assert int(actual_value) == expected_value


def wait_for_message(pubsub, timeout=1, ignore_subscribe_messages=True):
now = time.time()
timeout = now + timeout
Expand Down
Loading