Skip to content

[Question] Async Client and testing with RedisContainer #374

Closed

Description

@bearrito

Hello,

I'm attempting to use the RedisContainer inside a IsolatedAsyncioTestCase
It seems like the below works

from redis.asyncio import Redis
from testcontainers.redis import RedisContainer


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(AsyncRedisContainer, self).__init__(
            image, port_to_expose, password, **kwargs
        )

    async def get_async_client(self, **kwargs):
        return await Redis(
            host=self.get_container_host_ip(),
            port=self.get_exposed_port(self.port_to_expose),
            password=self.password,
            **kwargs,
        )


class TestAyncRedisContainerContainer(IsolatedAsyncioTestCase):
    async def test_foo(self):
        with AsyncRedisContainer.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)
            self.assertEqual(int(actual_value), expected_value)


Is there a cleaner way to do this? If so, would there be any interest in a PR?

Activity

  1. bearrito commented on Feb 25, 2024

    @bearrito
    ContributorAuthor

    For grooming purposes re: #405

    Would you like a PR for this? If not I can close.

  2. totallyzen commented on Feb 27, 2024

    @totallyzen
    Contributor

    heyo! thanks for coming back! :)
    It's a worthwhile idea to properly support async for all containers in fact!
    With my current level of energy, I'm not sure I can squeeze it in right now.

    If you want you can contribute just this one, but then the last ask is to make use of AnyIO for testing
    https://anyio.readthedocs.io/en/stable/testing.html

    Reason for that:

    • it integrates with pytest, following the pytest fixtures
    • it is better than pytest-asyncio because of the support for the higher scope fixtures (the default is function level)
  3. bearrito commented on Feb 27, 2024

    @bearrito
    ContributorAuthor

    Okay. That shouldnt be an issue. I had this in a work project and these were our standard libs, but I can split it out and make use of the libs you suggested.

  4. bearrito commented on Feb 29, 2024

    @bearrito
    ContributorAuthor

    Closing. Opened a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions