Skip to content

Commit 6fdf325

Browse files
committed
fixed failing tests
1 parent a6f97e9 commit 6fdf325

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

ellar/di/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class DIImproperConfiguration(Exception):
1212
pass
1313

1414

15+
class RequestScopeContextNotFound(Exception):
16+
pass
17+
18+
1519
__all__ = [
1620
"CallError",
1721
"CircularDependency",
@@ -21,4 +25,5 @@ class DIImproperConfiguration(Exception):
2125
"UnknownProvider",
2226
"UnsatisfiedRequirement",
2327
"DIImproperConfiguration",
28+
"RequestScopeContextNotFound",
2429
]

ellar/di/scopes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
UnsatisfiedRequirement,
1919
)
2020

21+
from .exceptions import RequestScopeContextNotFound
2122
from .providers import InstanceProvider, Provider
2223
from .types import T
2324

@@ -34,7 +35,7 @@ def get(self, key: t.Type[T], provider: Provider[T]) -> Provider[T]:
3435
scoped_context = self.get_context()
3536

3637
if scoped_context is None:
37-
raise Exception(
38+
raise RequestScopeContextNotFound(
3839
"RequestScope is not available. Trying to access RequestScope outside request",
3940
UnsatisfiedRequirement(None, key),
4041
)

ellar/testing/module.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,19 @@ def get_test_client(
9494
)
9595

9696
def get(self, interface: t.Type[T]) -> T:
97-
# try to find module to which the interface belongs
98-
module = self.create_application().injector.tree_manager.search_module_tree(
99-
filter_item=lambda data: True,
100-
find_predicate=lambda data: interface in data.exports
101-
or interface in data.providers,
102-
)
103-
if module:
104-
return t.cast(T, module.value.get(interface))
105-
return self.create_application().injector.get(interface) # type: ignore[no-any-return]
97+
try:
98+
return t.cast(T, self.create_application().injector.get(interface))
99+
except Exception as ex:
100+
# try to find module to which the interface belongs
101+
module = self.create_application().injector.tree_manager.search_module_tree(
102+
filter_item=lambda data: True,
103+
find_predicate=lambda data: interface in data.exports
104+
or interface in data.providers,
105+
)
106+
if module:
107+
return t.cast(T, module.value.get(interface))
108+
109+
raise ex
106110

107111

108112
class Test:

tests/test_di/test_injector.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
request_scope,
1111
transient_scope,
1212
)
13+
from ellar.di.exceptions import RequestScopeContextNotFound
1314
from ellar.di.providers import ClassProvider, InstanceProvider
1415
from injector import Binder, Injector, UnsatisfiedRequirement
1516

@@ -119,7 +120,7 @@ async def test_request_service_context():
119120
with pytest.raises(UnsatisfiedRequirement):
120121
injector.get(Foo)
121122

122-
with pytest.raises(UnsatisfiedRequirement):
123+
with pytest.raises(RequestScopeContextNotFound):
123124
injector.get(Foo1)
124125

125126

tests/test_di/test_provider_scopes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from ellar.core import HttpRequestConnectionContext
33
from ellar.core.execution_context import HostContextFactory
44
from ellar.di import EllarInjector, ProviderConfig, has_binding
5-
from ellar.di.exceptions import DIImproperConfiguration
5+
from ellar.di.exceptions import DIImproperConfiguration, RequestScopeContextNotFound
66
from ellar.di.scopes import RequestScope, SingletonScope, TransientScope
77
from ellar.utils.importer import get_class_import
8-
from injector import UnsatisfiedRequirement, inject
8+
from injector import inject
99

1010
from .examples import AnyContext, Foo, IContext, TransientRequestContext
1111

@@ -52,7 +52,7 @@ async def test_request_scope_instance():
5252

5353
# resolving RequestScope Providers outside RequestServiceProvider will behave like TransientScope
5454

55-
with pytest.raises(UnsatisfiedRequirement):
55+
with pytest.raises(RequestScopeContextNotFound):
5656
assert injector.get(IContext)
5757

5858
async with HttpRequestConnectionContext(

0 commit comments

Comments
 (0)