-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from nonnontrivial/feature/improved-tests
improve tests
- Loading branch information
Showing
23 changed files
with
192 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ jobs: | |
package-dir: | ||
- api | ||
- pp | ||
- pc | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from unittest.mock import AsyncMock, MagicMock, patch | ||
|
||
import pytest | ||
from pc.consumer.consumer import Consumer | ||
|
||
|
||
@pytest.fixture | ||
def mock_connection(): | ||
connection = AsyncMock() | ||
channel = AsyncMock() | ||
connection.channel.return_value = channel | ||
return connection | ||
|
||
@pytest.fixture | ||
def mock_channel(mock_connection): | ||
return mock_connection.channel.return_value | ||
|
||
@pytest.fixture | ||
def mock_queues(): | ||
prediction_queue = AsyncMock() | ||
cycle_queue = AsyncMock() | ||
return {"prediction": prediction_queue, "cycle": cycle_queue} | ||
|
||
@pytest.fixture | ||
def mock_pool(): | ||
return AsyncMock() | ||
|
||
@pytest.fixture | ||
def mock_handler(): | ||
return AsyncMock() | ||
|
||
@pytest.fixture | ||
def mock_shutdown(): | ||
return MagicMock() | ||
|
||
@pytest.fixture | ||
def consumer(mock_connection, mock_pool, mock_handler, mock_shutdown): | ||
consumer = Consumer( | ||
url="amqp://test", | ||
prediction_queue="prediction", | ||
cycle_queue="cycle", | ||
connection_pool=mock_pool, | ||
on_cycle_completion=mock_handler, | ||
) | ||
consumer.connection = mock_connection | ||
return consumer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,23 @@ | ||
import asyncio | ||
from unittest.mock import AsyncMock, patch | ||
|
||
import pytest | ||
import asyncpg | ||
from aio_pika import Message | ||
|
||
from pc.consumer.consumer import Consumer | ||
|
||
@pytest.fixture | ||
async def mock_asyncpg_pool(): | ||
with patch("asyncpg.create_pool") as mock_create_pool: | ||
mock_pool = AsyncMock() | ||
mock_create_pool.return_value = mock_pool | ||
|
||
mock_connection = AsyncMock() | ||
mock_pool.acquire.return_value.__aenter__.return_value = mock_connection | ||
yield mock_pool | ||
|
||
amqp_url="amqp://localhost" | ||
|
||
@pytest.fixture | ||
def consumer(mock_asyncpg_pool): | ||
prediction_queue="prediction" | ||
cycle_queue="cycle" | ||
return Consumer( | ||
url=amqp_url, | ||
prediction_queue=prediction_queue, | ||
cycle_queue=cycle_queue, | ||
connection_pool=mock_asyncpg_pool, | ||
on_cycle_completion=lambda _: None | ||
) | ||
from .fixtures import * | ||
|
||
@pytest.mark.asyncio | ||
async def test_consumer_connection(consumer): | ||
with patch("pc.consumer.consumer.Consumer.connect", new_callable=AsyncMock) as mock_connect: | ||
mock_connection = AsyncMock() | ||
mock_channel = AsyncMock() | ||
mock_connect.return_value = mock_connection | ||
mock_connection.channel.return_value = mock_channel | ||
await consumer.connect() | ||
mock_connect.assert_called_once() | ||
async def test_consumer_can_consume_from_queues(consumer: Consumer, mock_channel, mock_queues): | ||
mock_channel.declare_queue.side_effect = [ | ||
mock_queues["prediction"], | ||
mock_queues["cycle"], | ||
] | ||
task = asyncio.create_task(consumer.consume_from_queues()) | ||
await asyncio.sleep(0.1) | ||
task.cancel() | ||
mock_channel.declare_queue.assert_any_call("prediction") | ||
mock_channel.declare_queue.assert_any_call("cycle") | ||
mock_queues["prediction"].consume.assert_called_once() | ||
mock_queues["cycle"].consume.assert_called_once() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.