Skip to content

Commit

Permalink
fix bug in define + test
Browse files Browse the repository at this point in the history
  • Loading branch information
biniona committed Oct 3, 2024
1 parent da92ce9 commit 2736a50
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions minject/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ def define(
"""Create a new registry key based on a class and optional bindings."""
meta = _get_meta(base_class)
if meta:
is_async = meta.is_async_context
meta = RegistryMetadata(base_class, bindings=dict(meta.bindings))
meta.update_bindings(**bindings)
meta.is_async_context = is_async
else:
meta = RegistryMetadata(base_class, bindings=bindings)
meta._close = _close
Expand Down
28 changes: 27 additions & 1 deletion tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from minject.inject import async_context, bind, config, nested_config, reference
from minject.inject import async_context, bind, config, define, nested_config, reference
from minject.registry import Registry, RegistryAPIError

TEXT = "we love tests"
Expand Down Expand Up @@ -97,6 +97,15 @@ async def __aexit__(
self.in_context = False


MY_ASYNC_API_DEFINE = define(
MyAsyncApi,
text=TEXT,
dep_async=reference(MyDependencyAsync),
dep_not_specified=reference(MyDependencyNotSpecifiedAsync),
dep_context_counter=reference(MyAsyncAPIContextCounter),
)


@async_context
@bind(nested=nested_config("a.b"))
@bind(flat=config(1))
Expand Down Expand Up @@ -232,3 +241,20 @@ async def test_entering_already_entered_registry_throws(registry: Registry) -> N
with pytest.raises(RegistryAPIError):
async with r:
pass


async def test_define(registry: Registry) -> None:
async with registry as r:
my_api = await r.aget(MY_ASYNC_API_DEFINE)
assert my_api.text == TEXT
assert my_api.in_context == True
assert my_api.dep_async.in_context == True
assert my_api.dep_not_specified.in_context == False
assert my_api.dep_context_counter.in_context == True
assert my_api.dep_context_counter.entered_context_counter == 1

assert my_api.in_context == False
assert my_api.dep_async.in_context == False
assert my_api.dep_not_specified.in_context == False
assert my_api.dep_context_counter.in_context == False
assert my_api.dep_context_counter.exited_context_counter == 1

0 comments on commit 2736a50

Please sign in to comment.