tobymao / saq

Simple Async Queues
https://saq-py.readthedocs.io/en/latest/
MIT License
577 stars 39 forks source link

Test utility #87

Closed grigi closed 1 year ago

grigi commented 1 year ago

This PR contains a simple test double for testing your app that uses SAQ a bit easier. This doesn't require a redis server running, or using a fake redis server to test. It's really only aimed at the more common use cases

Done:

To use you could do, e.g.:

from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch

from saq.testing import TestQueue
from tests.testing.tasks import enqueues_a_job

@patch("tests.testing.tasks.queue", new=TestQueue())
class TestSimple(IsolatedAsyncioTestCase):
    def test_ignore_that_a_job_got_enqueued(self) -> None:
        await enqueues_a_job()
        # It didn't connect to redis

Or more typically:

from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch

from saq.testing import TestQueue
from tests.testing.tasks import enqueues_a_job

class TestTypically(IsolatedAsyncioTestCase):
    async def test_indirect_enqueue(self) -> None:
        with patch("tests.testing.tasks.queue", new=TestQueue()) as testqueue:
            await enqueues_a_job()

            testqueue.assertEnqueuedTimes("add", 1)
            # Verified that it enqueued the task "add"

    async def test_not_enqueue(self) -> None:
        with patch("tests.testing.tasks.queue", new=TestQueue()) as testqueue:
            testqueue.assertNotEnqueued("add")
            # Verified that task "add" was enqueued

If you need to test something that uses queue.apply() or queue.map() you can pass in the worker settings:

from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch

from saq.testing import TestQueue
from tests.testing.tasks import applies_a_job, settings

@patch("tests.testing.tasks.queue", new=TestQueue(settings))
class TestTypically(IsolatedAsyncioTestCase):
    async def test_apply(self) -> None:
        self.assertEqual(
            await applies_a_job(),
            8
        )
        # Task "add" called directly
grigi commented 1 year ago

Re the rest failures: The tests.test_queue.TestQueue.test_dequeue_fifo test is flaky for me locally as well, in fact it has been the whole time I knew this project. I know this one will ALWAYS fail if upgrading redis-py to 5.0.0. I'm pretty certain it will pass on a retry.

grigi commented 1 year ago

Converted back to draft as I'm cleaning this up. Please don't merge as is.

tobymao commented 1 year ago

closing due to inactivity, feel free to reopen when you have time to work on it