Skip to content

Commit

Permalink
Merge pull request #58 from olxbr/fix/callback-endpoint-hook
Browse files Browse the repository at this point in the history
FIX: callback endpoint hook
  • Loading branch information
TauannyFurlanetto authored Sep 11, 2023
2 parents 8728957 + e2d0527 commit 6501dbd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions barterdude/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ def add_callback_endpoint(
hook: Callable,
mock_dependencies: Iterable[Tuple[Any, str]] = None,
):
def hook_to_callback(req):
return self._call_callback_endpoint(req, hook, mock_dependencies)
async def hook_to_callback(req: web.Request):
return await self._call_callback_endpoint(
req,
hook,
mock_dependencies
)

self.add_endpoint(
routes=routes,
Expand Down
17 changes: 17 additions & 0 deletions tests_unit/test__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio
import typing
from unittest import IsolatedAsyncioTestCase
from unittest.mock import Mock, AsyncMock, patch, call

Expand Down Expand Up @@ -79,6 +81,21 @@ async def test_should_call_route_when_adding_callback_endpoint(self):
)
self.decorator.assert_called_once()

async def test_hook_to_callback_should_be_async_and_typed(self):
bd = BarterDude()
bd.add_endpoint = Mock()

hook = Mock()
bd.add_callback_endpoint(
['/my_route'],
hook,
[(Mock(), 'service')]
)

internal_hook = bd.add_endpoint.call_args_list[0][1].get('hook')
assert asyncio.iscoroutinefunction(internal_hook) is True
assert 'req' in typing.get_type_hints(internal_hook)

async def test_should_hook_call_on_callback_endpoint(self):
async def mock_hook(message, barterdude):
barterdude['service'].method_one()
Expand Down

0 comments on commit 6501dbd

Please sign in to comment.