Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Async Client and testing with RedisContainer #374

Closed
bearrito opened this issue Jul 26, 2023 · 4 comments
Closed

[Question] Async Client and testing with RedisContainer #374

bearrito opened this issue Jul 26, 2023 · 4 comments

Comments

@bearrito
Copy link
Contributor

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?

@bearrito
Copy link
Contributor Author

For grooming purposes re: #405

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

@totallyzen
Copy link
Collaborator

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)

@bearrito
Copy link
Contributor Author

bearrito commented Feb 27, 2024

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.

@bearrito
Copy link
Contributor Author

Closing. Opened a PR.

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

No branches or pull requests

2 participants