Skip to content

Commit

Permalink
Merge pull request #56 from olxbr/python-3.11
Browse files Browse the repository at this point in the history
Python 3.11
  • Loading branch information
timotta authored Apr 18, 2023
2 parents d7779d9 + ab2c74c commit eb1d9f6
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 83 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup python 3.7
- name: Setup python 3.11
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.11

- name: Install project dependencies
run: make setup
Expand All @@ -42,10 +42,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup python 3.7
- name: Setup python 3.11
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.11

- name: Install project dependencies
run: make install && python3 -m pip install build
Expand All @@ -70,10 +70,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup python 3.7
- name: Setup python 3.11
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.11

- name: Install project dependencies
run: make install && python3 -m pip install build
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test:
@pytest --ignore="tests_integration" --cov=barterdude

integration:
@pytest --ignore="tests_unit"
@pytest --ignore="tests_unit" -vv

all-tests: | test integration lint check-sec

Expand Down
2 changes: 1 addition & 1 deletion barterdude/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from asyncworker.options import Options
from asyncworker.connections import AMQPConnection
from asyncworker.rabbitmq.message import RabbitMQMessage
from collections import MutableMapping
from collections.abc import MutableMapping
from typing import Iterable, Optional, Callable, Any, Tuple
from barterdude.monitor import Monitor
from barterdude.message import MessageValidation, ValidationException
Expand Down
2 changes: 1 addition & 1 deletion barterdude/mocks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
from typing import Dict, Any, Iterable, Tuple
from collections import MutableMapping
from collections.abc import MutableMapping
from aioamqp.properties import Properties


Expand Down
3 changes: 1 addition & 2 deletions requirements/requirements_base.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
async-worker==0.15.1
aioamqp==0.14.0
git+https://github.com/async-worker/async-worker.git@release/0.20.0
python-json-logger==2.0.1
jsonschema==3.2.0
5 changes: 2 additions & 3 deletions requirements/requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pytest>5.0.0,<6.0.0
pytest-cov>2.0.0,<3.0.0
pytest>=7.0.0,<8.0.0
pytest-cov>=4.0.0,<5.0.0
coverage>5.0,<6.0
bandit>1.0,<2.0.0
flake8>3.0.0,<4.0.0
asynctest==0.13.0
freezegun==0.3.14
pydantic==1.7.4
14 changes: 12 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
}

with open("requirements/requirements_base.txt") as reqs:
requirements = reqs.read().split("\n")
raw_requirements = reqs.read().split("\n")

requirements = []
for r in raw_requirements:
if r.startswith("git+"):
name = r.split(".git@")[0].split("/")[-1]
requirements.append(f"{name} @ {r}")
else:
requirements.append(r)

for lib in libs:
with open(f"requirements/requirements_{lib}.txt") as reqs:
Expand All @@ -33,7 +41,9 @@
install_requires=requirements,
extras_require=extra,
classifiers=[
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: Apache Software License",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Software Development :: Libraries :: Application Frameworks"
Expand Down
24 changes: 14 additions & 10 deletions tests_integration/test_barterdude.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from string import ascii_uppercase

import aiohttp
from asynctest import TestCase
from unittest import IsolatedAsyncioTestCase
from asyncworker.connections import AMQPConnection
from barterdude import BarterDude
from barterdude.hooks import logging as hook_logging
Expand All @@ -17,11 +17,9 @@
from tests_integration.helpers import ErrorHook


class TestBarterDude(TestCase):
use_default_loop = True

async def setUp(self):
self.input_queue = "test"
class TestBarterDude(IsolatedAsyncioTestCase):
async def asyncSetUp(self):
self.input_queue = "test_input"
self.output_exchange = "test_exchange"
self.output_queue = "test_output"
self.rabbitmq_host = os.environ.get("RABBITMQ_HOST", "127.0.0.1")
Expand All @@ -36,6 +34,9 @@ async def setUp(self):
)
self.queue_manager = self.connection["/"]
await self.queue_manager.connection._connect()

await self.clear()

await self.queue_manager.connection.channel.queue_declare(
self.input_queue
)
Expand All @@ -58,8 +59,12 @@ async def setUp(self):

self.app = BarterDude(hostname=self.rabbitmq_host)

async def tearDown(self):
async def asyncTearDown(self):
await self.app.shutdown()
await self.clear()
await self.queue_manager.connection.close()

async def clear(self):
await self.queue_manager.connection.channel.queue_delete(
self.input_queue
)
Expand All @@ -69,7 +74,6 @@ async def tearDown(self):
await self.queue_manager.connection.channel.exchange_delete(
self.output_exchange
)
await self.queue_manager.connection.close()

async def test_obtains_healthcheck(self):
monitor = Monitor(Healthcheck(self.app))
Expand Down Expand Up @@ -189,7 +193,7 @@ async def handler(message):
data=self.messages[0]
)
await sync_event.wait()
await asyncio.sleep(1)
await asyncio.sleep(0.05)

key = self.messages[0]["key"]
error_str = repr(error)
Expand Down Expand Up @@ -227,7 +231,7 @@ async def handler(message):
data=self.messages[0]
)
await sync_event.wait()
await asyncio.sleep(1)
await asyncio.sleep(0.05)

key = self.messages[0]["key"]
error_str = repr(error)
Expand Down
30 changes: 18 additions & 12 deletions tests_integration/test_rabbitmq_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from random import choices
from string import ascii_uppercase

from asynctest import TestCase
from unittest import IsolatedAsyncioTestCase
from asyncworker.connections import AMQPConnection
from barterdude import BarterDude
from barterdude.hooks.logging import Logging
Expand All @@ -15,11 +15,10 @@
from tests_integration.helpers import ErrorHook


class RabbitMQConsumerTest(TestCase):
use_default_loop = True
class RabbitMQConsumerTest(IsolatedAsyncioTestCase):

async def setUp(self):
self.input_queue = "test"
async def asyncSetUp(self):
self.input_queue = "test_input"
self.output_exchange = "test_exchange"
self.output_queue = "test_output"
self.rabbitmq_host = os.environ.get("RABBITMQ_HOST", "127.0.0.1")
Expand All @@ -34,6 +33,9 @@ async def setUp(self):
)
self.queue_manager = self.connection["/"]
await self.queue_manager.connection._connect()

await self.clear()

await self.queue_manager.connection.channel.queue_declare(
self.input_queue
)
Expand All @@ -56,8 +58,12 @@ async def setUp(self):

self.app = BarterDude(hostname=self.rabbitmq_host)

async def tearDown(self):
async def asyncTearDown(self):
await self.app.shutdown()
await self.clear()
await self.queue_manager.connection.close()

async def clear(self):
await self.queue_manager.connection.channel.queue_delete(
self.input_queue
)
Expand All @@ -67,7 +73,6 @@ async def tearDown(self):
await self.queue_manager.connection.channel.exchange_delete(
self.output_exchange
)
await self.queue_manager.connection.close()

async def send_all_messages(self):
futures = []
Expand Down Expand Up @@ -111,7 +116,6 @@ async def handler(message):

async def test_process_messages_successfully_even_with_crashed_hook(self):
received_messages = set()
sync_event = Event()

monitor = Monitor(ErrorHook())

Expand All @@ -120,14 +124,16 @@ async def test_process_messages_successfully_even_with_crashed_hook(self):
async def handler(message):
nonlocal received_messages
received_messages.add(message.body["key"])
sync_event.set()

await self.app.startup()
await self.send_all_messages()

await sync_event.wait()
for message in self.messages:
self.assertTrue(message["key"] in received_messages)
expected = set([m["key"] for m in self.messages])

while len(expected - received_messages) > 0:
await asyncio.sleep(0.05)

self.assertEquals(expected, received_messages)

async def test_process_one_message_and_publish(self):
sync_event = Event()
Expand Down
38 changes: 20 additions & 18 deletions tests_unit/test__init__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
from asynctest import Mock, TestCase, CoroutineMock, patch, call
from unittest import IsolatedAsyncioTestCase
from unittest.mock import Mock, AsyncMock, patch, call

from asyncworker import Options, RouteTypes
from barterdude import BarterDude
from barterdude.message import Message
from tests_unit.helpers import load_fixture


class TestBarterDude(TestCase):
class TestBarterDude(IsolatedAsyncioTestCase):
@patch("barterdude.App")
@patch("barterdude.AMQPConnection")
def setUp(self, AMQPConnection, App):
self.monitor = Mock()
self.monitor.dispatch_before_consume = CoroutineMock()
self.monitor.dispatch_on_success = CoroutineMock()
self.monitor.dispatch_on_fail = CoroutineMock()
self.callback = CoroutineMock()
self.monitor.dispatch_before_consume = AsyncMock()
self.monitor.dispatch_on_success = AsyncMock()
self.monitor.dispatch_on_fail = AsyncMock()
self.callback = AsyncMock()
self.messages = [Mock(value=i) for i in range(10)]
self.calls = [call(message) for message in self.messages]

self.AMQPConnection = AMQPConnection
self.connection = self.AMQPConnection.return_value
self.App = App
self.app = self.App.return_value
self.app.startup = CoroutineMock()
self.app.shutdown = CoroutineMock()
self.app.startup = AsyncMock()
self.app.shutdown = AsyncMock()
self.decorator = self.app.route.return_value
self.schema = load_fixture("schema.json")
self.barterdude = BarterDude()
Expand All @@ -41,7 +43,7 @@ def test_should_call_route_when_created(self):
monitor = Mock()
self.barterdude.consume_amqp(
["queue"], monitor=monitor
)(CoroutineMock())
)(AsyncMock())
self.app.route.assert_called_once_with(
["queue"],
type=RouteTypes.AMQP_RABBITMQ,
Expand Down Expand Up @@ -85,10 +87,10 @@ async def mock_hook(message, barterdude):
await barterdude.publish_amqp(data={'a': 1})

request = Mock()
request.json = CoroutineMock(return_value={'body': {}})
request.json = AsyncMock(return_value={'body': {}})
service_mock = Mock()
service_mock.method_one.return_value = 123
service_mock.method_two = CoroutineMock(return_value=234)
service_mock.method_two = AsyncMock(return_value=234)
dependencies = [(service_mock, 'service')]
response = await self.barterdude._call_callback_endpoint(
request, mock_hook, dependencies)
Expand All @@ -111,10 +113,10 @@ async def mock_hook(message, barterdude):
await barterdude.publish_amqp(data={'a': 1})

request = Mock()
request.json = CoroutineMock(return_value={})
request.json = AsyncMock(return_value={})
service_mock = Mock()
service_mock.method_one.return_value = 123
service_mock.method_two = CoroutineMock(return_value=234)
service_mock.method_two = AsyncMock(return_value=234)
dependencies = [(service_mock, 'service')]
response = await self.barterdude._call_callback_endpoint(
request, mock_hook, dependencies)
Expand All @@ -131,7 +133,7 @@ async def mock_hook(message, barterdude):
raise Exception

request = Mock()
request.json = CoroutineMock(return_value={'body': {}})
request.json = AsyncMock(return_value={'body': {}})
service_mock = Mock()
dependencies = [(service_mock, 'service')]
response = await self.barterdude._call_callback_endpoint(
Expand All @@ -151,10 +153,10 @@ async def mock_hook(message, barterdude):
await barterdude.publish_amqp(data={'a': 1})

request = Mock()
request.json = CoroutineMock()
request.json = AsyncMock()
service_mock = Mock()
service_mock.method_one.return_value = 123
service_mock.method_two = CoroutineMock(return_value=234)
service_mock.method_two = AsyncMock(return_value=234)
dependencies = [(service_mock, 'service')]
response = await self.barterdude._call_callback_endpoint(
request, mock_hook, dependencies)
Expand Down Expand Up @@ -241,7 +243,7 @@ async def test_should_call_monitor_for_each_fail_message(self):

async def test_should_call_put_when_publish(self):
data = Mock()
self.connection.put = CoroutineMock()
self.connection.put = AsyncMock()
await self.barterdude.publish_amqp(
'exchange',
data,
Expand All @@ -267,7 +269,7 @@ def test_should_call_run(self):
self.app.run.assert_called_once_with()


class TestAppSharedProperties(TestCase):
class TestAppSharedProperties(IsolatedAsyncioTestCase):
def setUp(self):
self.barterdude = BarterDude()

Expand Down
Loading

0 comments on commit eb1d9f6

Please sign in to comment.