-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from reagento/feature/decorator
Feature/decorator
- Loading branch information
Showing
8 changed files
with
173 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from dishka import Provider, Scope, make_container, provide | ||
from dishka.provider import alias, decorate | ||
|
||
|
||
class A: | ||
pass | ||
|
||
|
||
class A1(A): | ||
pass | ||
|
||
|
||
class A2(A1): | ||
pass | ||
|
||
|
||
class ADecorator: | ||
def __init__(self, a: A): | ||
self.a = a | ||
|
||
|
||
def test_simple(): | ||
class MyProvider(Provider): | ||
a = provide(A, scope=Scope.APP) | ||
ad = decorate(ADecorator, provides=A) | ||
|
||
with make_container(MyProvider()) as container: | ||
a = container.get(A) | ||
assert isinstance(a, ADecorator) | ||
assert isinstance(a.a, A) | ||
|
||
|
||
def test_alias(): | ||
class MyProvider(Provider): | ||
a2 = provide(A2, scope=Scope.APP) | ||
a1 = alias(source=A2, provides=A1) | ||
a = alias(source=A1, provides=A) | ||
|
||
@decorate | ||
def decorated(self, a: A1) -> A1: | ||
return ADecorator(a) | ||
|
||
with make_container(MyProvider()) as container: | ||
a1 = container.get(A1) | ||
assert isinstance(a1, ADecorator) | ||
assert isinstance(a1.a, A2) | ||
|
||
a2 = container.get(A2) | ||
assert isinstance(a2, A2) | ||
assert a2 is a1.a | ||
|
||
a = container.get(A) | ||
assert a is a1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters