Skip to content

Commit

Permalink
code refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
amenezes committed Feb 19, 2024
1 parent b29a644 commit d52def3
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ docs:

install-deps:
@echo "> installing dependencies..."
pip install -r requirements-dev.txt
uv pip install -r requirements-dev.txt
pre-commit install

tox:
Expand Down
3 changes: 3 additions & 0 deletions rabbit/background_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def discard(self, task: asyncio.Task) -> None:
def tasks_by_name(self) -> List[str]:
return [task_name for task_name in self._tasks.keys()]

def __getitem__(self, name: str) -> asyncio.Task:
return self._tasks[name]

def __iter__(self) -> Generator[asyncio.Task, None, None]:
for _, task in self._tasks.items():
yield task
Expand Down
4 changes: 2 additions & 2 deletions rabbit/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import random
from typing import List, Optional
from typing import List, Union
from uuid import uuid4

import aioamqp
Expand Down Expand Up @@ -40,7 +40,7 @@ def __repr__(self) -> str:
return f"AioRabbitClient(connected={connected}, channels={channels}, max_channels={max_channels}, background_tasks={self._background_tasks})"

@property
def server_properties(self) -> Optional[dict]:
def server_properties(self) -> Union[None, dict]:
"""Get server properties from the current connection."""
try:
return self.protocol.server_properties # type: ignore
Expand Down
5 changes: 2 additions & 3 deletions rabbit/subscribe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import os
from contextlib import suppress
from typing import Callable, Optional
from typing import Callable, Union

from aioamqp.channel import Channel
from aioamqp.envelope import Envelope
Expand Down Expand Up @@ -72,7 +72,6 @@ def __attrs_post_init__(self) -> None:

@property
def name(self) -> str:
"""Object name."""
return "Subscribe"

@property
Expand All @@ -87,7 +86,7 @@ def channel(self, channel: Channel) -> None:
self._dlx.channel = channel
self._channel = channel

async def configure(self, channel: Optional[Channel] = None) -> None:
async def configure(self, channel: Union[None, Channel] = None) -> None:
"""Configure subscriber channel, queues and exchange."""
await self.qos(prefetch_count=self.concurrent)
with suppress(SynchronizationError):
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/test_background_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import asyncio

import pytest

from rabbit.background_tasks import BackgroundTasks
from rabbit.job import async_echo_job


@pytest.fixture
def background_tasks():
return BackgroundTasks()


async def test_background_tasks_add(background_tasks):
background_tasks.add("test-task", async_echo_job, b'{"message": "test"}')
assert len(background_tasks) == 1


async def test_background_tasks_multiple_add(background_tasks):
background_tasks.add("test-task-1", async_echo_job, b'{"message": "test"}')
background_tasks.add("test-task-1", async_echo_job, b'{"message": "test"}')
background_tasks.add("test-task-2", async_echo_job, b'{"message": "test2"}')
assert len(background_tasks) == 2


async def test_background_tasks_by_name(background_tasks):
background_tasks.add("test-task", async_echo_job, b'{"message": "test"}')
for task in background_tasks:
assert task.get_name() == "test-task"


async def test_background_tasks_getitem(background_tasks):
background_tasks.add("test-task", async_echo_job, b'{"message": "test"}')
assert isinstance(background_tasks["test-task"], asyncio.Task)


def test_background_tasks_len(background_tasks):
assert len(background_tasks) == 0


def test_background_tasks_repr(background_tasks):
assert repr(background_tasks) == "BackgroundTasks(tasks=0, tasks_by_name=[])"
7 changes: 6 additions & 1 deletion tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from rabbit.client import aioamqp
from rabbit.client import AioRabbitClient, aioamqp
from rabbit.exceptions import AttributeNotInitialized
from tests.conftest import AioAmqpMock

Expand Down Expand Up @@ -44,3 +44,8 @@ def test_server_properties_with_client_not_connected(client):

def test_server_properties(client_mock):
assert isinstance(client_mock.server_properties, dict)


@pytest.mark.parametrize("attribute", ["transport", "server_properties", "protocol"])
def test_client_attributes(attribute):
assert hasattr(AioRabbitClient, attribute)
8 changes: 8 additions & 0 deletions tests/unit/test_dlx.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from rabbit.dlx import DLX
from rabbit.exceptions import OperationError
from tests.conftest import EnvelopeMock, PropertiesMock

Expand All @@ -20,3 +21,10 @@ async def test_send_event_error_without_client_connection(dlx):

def test_dlx_repr(dlx):
assert isinstance(repr(dlx), str)


@pytest.mark.parametrize(
"attribute", ["exchange", "dlq_exchange", "queue", "delay_strategy", "channel"]
)
def test_dlx_attributes(attribute):
assert hasattr(DLX, attribute)
14 changes: 14 additions & 0 deletions tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from rabbit.exceptions import ClientNotConnectedError, ExchangeNotFound


def test_exchange_not_found_message():
assert (
str(ExchangeNotFound("test-exchange")) == "Exchange 'test-exchange' not found"
)


def test_client_not_connected_error():
assert (
str(ClientNotConnectedError())
== "AioRabbitClient was not connected with RabbitMQ"
)
10 changes: 9 additions & 1 deletion tests/unit/test_jobs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
from rabbit.job import async_echo_job
import pytest

from rabbit.job import async_chaos_job, async_echo_job


async def test_async_echo_job():
resp = await async_echo_job(b'{"process": 123}', skip_wait=True)
assert resp == b'{"process": 123}'


@pytest.mark.xfail(reason="The test may fail due to randomness issue")
async def test_async_chaos_job():
resp = await async_chaos_job(b'{"process": 123}', skip_wait=True)
assert resp == b'{"process": 123}'
7 changes: 7 additions & 0 deletions tests/unit/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ async def test_publish_confirms_disabled(publish):
async def test_publish_confirms_enabled():
publish = Publish(True)
assert publish.publish_confirms is True


@pytest.mark.parametrize(
"attribute", ["publish_confirms", "name", "channel_id", "channel"]
)
def test_publish_attributes(attribute):
assert hasattr(Publish, attribute)
9 changes: 4 additions & 5 deletions tests/unit/test_queue.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import attr
import pytest


def test_attributes(queue):
values = ["queue", True, {}]
for value in values:
assert value in attr.asdict(queue).values()
@pytest.mark.parametrize("attribute", ["name", "durable", "arguments"])
def test_attributes(queue, attribute):
assert hasattr(queue, attribute)
10 changes: 8 additions & 2 deletions tests/unit/test_subscribe.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import pytest

from rabbit.exceptions import ClientNotConnectedError
from rabbit.subscribe import Subscribe
from tests.conftest import EnvelopeMock

PAYLOAD = b'{"a": 1}'


async def test_register_subscribe_without_client_connected(subscribe):
with pytest.raises(ClientNotConnectedError):
Expand All @@ -25,3 +24,10 @@ def test_subscribe_repr(subscribe_mock):

async def test_nack_event(subscribe_mock):
await subscribe_mock.nack_event(EnvelopeMock())


@pytest.mark.parametrize(
"attribute", ["task", "exchange", "queue", "concurrent", "delay_strategy"]
)
def test_subscribe_attributes(attribute):
assert hasattr(Subscribe, attribute)

0 comments on commit d52def3

Please sign in to comment.