Skip to content

Commit

Permalink
Make adjustments for test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
nkiryanov committed Jun 9, 2024
1 parent 80671f7 commit 6b7c323
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ jobs:
key: ${{ runner.os }}-venv-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/*requirements.txt') }}

- name: Run the tests
env:
BROKER_URL: amqp://guest:guest@rabbitmq:5672/
run: |
source .venv/bin/activate
make test
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ lint:
mypy

test:
cd src && pytest -x
cd src && pytest -x -m "not rabbitmq"
cd src && pytest -x -m rabbitmq
cd src && pytest --dead-fixtures
2 changes: 2 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ services:
TZ: UTC
tty: true
command: /bin/bash
volumes:
- ./:/repo
2 changes: 1 addition & 1 deletion env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DEBUG=False

BROKER_URL=amqp://guest:guest@localhost:5672/
BROKER_EXCHANGE=guest-exchange
BROKER_EXCHANGE=websocket-notifications-exchange
BROKER_QUEUE=websockets-notifications-queue
BROKER_ROUTING_KEYS_CONSUME_FROM=["test-event-boobs"]

Expand Down
2 changes: 1 addition & 1 deletion src/app/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Settings(BaseSettings):
BROKER_URL: AmqpDsn
BROKER_EXCHANGE: str
BROKER_QUEUE: str
BROKER_QUEUE: str | None
BROKER_ROUTING_KEYS_CONSUME_FROM: list[str]
WEBSOCKETS_HOST: str
WEBSOCKETS_PORT: int
Expand Down
23 changes: 4 additions & 19 deletions src/conftest.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import pytest

from app.conf import Settings
from app.conf import get_app_settings

pytest_plugins = [
"app.fixtures",
"storage.fixtures",
]


@pytest.fixture(autouse=True)
def settings(mocker):
mock = mocker.patch(
"app.conf.get_app_settings",
return_value=Settings(
BROKER_URL="amqp://guest:guest@localhost/",
BROKER_EXCHANGE="test-exchange",
BROKER_QUEUE="test-queue",
BROKER_ROUTING_KEYS_CONSUME_FROM=["test-routing-key", "ya-test-routing-key"],
WEBSOCKETS_HOST="localhost",
WEBSOCKETS_PORT=50000,
WEBSOCKETS_PATH="/v2/test-subscription-websocket",
AUTH_JWKS_URL="https://auth.test.com/auth/realms/test-realm/protocol/openid-connect/certs",
AUTH_SUPPORTED_SIGNING_ALGORITHMS=["RS256"],
),
)

return mock.return_value
@pytest.fixture
def settings():
return get_app_settings()
2 changes: 1 addition & 1 deletion src/consumer/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __post_init__(self) -> None:

self.broker_url: str = str(settings.BROKER_URL)
self.exchange: str = settings.BROKER_EXCHANGE
self.queue: str = settings.BROKER_QUEUE
self.queue: str | None = settings.BROKER_QUEUE
self.routing_keys_consume_from: list[str] = settings.BROKER_ROUTING_KEYS_CONSUME_FROM

async def consume(self, stop_signal: asyncio.Future) -> None:
Expand Down
8 changes: 1 addition & 7 deletions src/consumer/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@

@pytest.fixture(autouse=True)
def _adjust_settings(settings):
settings.BROKER_URL = "amqp://guest:guest@localhost/"
settings.BROKER_EXCHANGE = "test-exchange"
settings.BROKER_QUEUE = "test-queue"
settings.BROKER_ROUTING_KEYS_CONSUME_FROM = [
"event-routing-key",
"ya-event-routing-key",
]
settings.BROKER_QUEUE = None # force consumer to create a queue with a random name


@pytest.fixture
Expand Down
14 changes: 4 additions & 10 deletions src/consumer/tests/tests_consumer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import asyncio
import pytest

pytestmark = [
pytest.mark.rabbitmq,
]


@pytest.fixture
def run_consume_task(consumer):
Expand All @@ -10,16 +14,6 @@ def run(stop_signal: asyncio.Future):
return run


def test_consumer_attributes(consumer):
assert consumer.broker_url == "amqp://guest:guest@localhost/"
assert consumer.exchange == "test-exchange"
assert consumer.queue == "test-queue"
assert consumer.routing_keys_consume_from == [
"event-routing-key",
"ya-event-routing-key",
]


async def test_consumer_correctly_stopped_on_stop_signal(run_consume_task):
stop_signal = asyncio.get_running_loop().create_future()
consumer_task = run_consume_task(stop_signal)
Expand Down
4 changes: 2 additions & 2 deletions src/tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def force_token_validation(mocker, valid_token):

@pytest.fixture(autouse=True)
def _adjust_settings(settings, unused_tcp_port):
settings.BROKER_QUEUE = None # force consumer to create a queue with a random name
settings.WEBSOCKETS_HOST = "0.0.0.0"
settings.WEBSOCKETS_PORT = unused_tcp_port

Expand Down Expand Up @@ -54,11 +55,10 @@ async def serve_app_runner(settings, websockets_handler, access_guardian, consum
)

await asyncio.sleep(0.1) # give enough time to start the server
assert serve_task.done() is False # be sure server is running
assert serve_task.done() is False, "It's looks like app runner couldn't be started. Check the settings carefully."
yield serve_task

stop_signal.set_result(None)
await asyncio.sleep(0.2) # give enough time to stop the server; it's required to free all the consumed resources


@pytest.fixture
Expand Down

0 comments on commit 6b7c323

Please sign in to comment.