From 6bb54ed6dc2fb606b9a1766036ec4b23f2c9c166 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 27 Sep 2022 23:53:45 +0200 Subject: [PATCH] Add integration tests --- .../test_message_queue_service.py | 20 ++++++++ .../test_tournament_service.py | 46 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/integration_tests/test_tournament_service.py diff --git a/tests/integration_tests/test_message_queue_service.py b/tests/integration_tests/test_message_queue_service.py index d14a5b98b..7f64ebaee 100644 --- a/tests/integration_tests/test_message_queue_service.py +++ b/tests/integration_tests/test_message_queue_service.py @@ -1,8 +1,10 @@ import asyncio +import json from unittest import mock import aio_pika import pytest +from aio_pika.abc import AbstractIncomingMessage from server.config import config from server.decorators import with_logger @@ -175,3 +177,21 @@ async def test_declaring_exchange_without_initialization(mq_uninit_service): assert service._is_ready assert service._connection is not None assert service._exchanges.get(exchange_name) is not None + + +async def test_listening_to_incoming_messages(mq_service: MessageQueueService): + callback = Callback() + await mq_service.listen_to_message("test", "routing.test", callback.callback_listening) + await mq_service.declare_exchange(config.MQ_EXCHANGE_NAME) + await mq_service.publish(config.MQ_EXCHANGE_NAME, "routing.test", {"test": "test"}) + await callback.called_future + + +class Callback: + + def __init__(self) -> None: + self.called_future = asyncio.Future() + + def callback_listening(self, message: AbstractIncomingMessage): + assert json.loads(message.body)["test"] == "test" + self.called_future.set_result(None) diff --git a/tests/integration_tests/test_tournament_service.py b/tests/integration_tests/test_tournament_service.py new file mode 100644 index 000000000..82e13a172 --- /dev/null +++ b/tests/integration_tests/test_tournament_service.py @@ -0,0 +1,46 @@ +import pytest + +from server import TournamentService +from server.config import config +from server.message_queue_service import MessageQueueService +from tests.integration_tests.conftest import ( + connect_and_sign_in, + read_until_command +) + +pytestmark = pytest.mark.rabbitmq + + +async def test_create_game_by_message(message_queue_service: MessageQueueService, + tournament_service: TournamentService, lobby_server): + _, _, proto1 = await connect_and_sign_in( + ("test", "test_password"), lobby_server + ) + await message_queue_service.declare_exchange(config.MQ_EXCHANGE_NAME) + await message_queue_service.publish(config.MQ_EXCHANGE_NAME, "request.match.create", + { + "request_id": "9124e8c9-c62f-43c3-bb64-94f3093f2997", + "game_name": "My game name", + "participants": [ + { + "team": 1, + "slot": 1, + "faction": 1, + "player_id": 1 + } + ], + "featured_mod": "faf", + "map_name": "SCMP_001", + "game_options": { + "test": "test" + } + }, correlation_id="9124e8c9-c62f-43c3-bb64-94f3093f2997" + ) + msg = await read_until_command(proto1, "is_ready") + assert msg == { + 'command': 'is_ready', + 'featured_mod': 'faf', + 'game_name': 'My game name', + 'request_id': '9124e8c9-c62f-43c3-bb64-94f3093f2997', + 'response_time_seconds': 30 + }