1
1
from dataclasses import dataclass
2
2
3
3
import pytest
4
+ from pydantic import BaseModel
4
5
5
6
from injection import get_instance , injectable
6
7
@@ -65,28 +66,32 @@ class C(B):
65
66
assert isinstance (b , C )
66
67
assert a is not b
67
68
68
- def test_injectable_without_auto_inject_raise_type_error (self ):
69
+ def test_injectable_with_inject (self ):
69
70
@injectable
70
71
class A :
71
72
...
72
73
73
- @injectable ( auto_inject = False )
74
+ @injectable
74
75
class B :
75
- def __init__ (self , a : A ):
76
- raise NotImplementedError
76
+ def __init__ (self , __a : A ):
77
+ self . a = __a
77
78
78
- with pytest .raises (TypeError ):
79
- get_instance (B )
79
+ a = get_instance (A )
80
+ b = get_instance (B )
81
+ assert isinstance (a , A )
82
+ assert isinstance (b , B )
83
+ assert isinstance (b .a , A )
84
+ assert a is not b .a
80
85
81
- def test_injectable_with_auto_inject (self ):
86
+ def test_injectable_with_dataclass_and_inject (self ):
82
87
@injectable
83
88
class A :
84
89
...
85
90
86
- @injectable (auto_inject = True )
91
+ @injectable
92
+ @dataclass (frozen = True , slots = True )
87
93
class B :
88
- def __init__ (self , __a : A ):
89
- self .a = __a
94
+ a : A
90
95
91
96
a = get_instance (A )
92
97
b = get_instance (B )
@@ -95,14 +100,13 @@ def __init__(self, __a: A):
95
100
assert isinstance (b .a , A )
96
101
assert a is not b .a
97
102
98
- def test_injectable_with_dataclass_and_auto_inject (self ):
103
+ def test_injectable_with_pydantic_model_and_inject (self ):
99
104
@injectable
100
- class A :
105
+ class A ( BaseModel ) :
101
106
...
102
107
103
- @injectable (auto_inject = True )
104
- @dataclass (frozen = True , slots = True )
105
- class B :
108
+ @injectable
109
+ class B (BaseModel ):
106
110
a : A
107
111
108
112
a = get_instance (A )
@@ -112,15 +116,15 @@ class B:
112
116
assert isinstance (b .a , A )
113
117
assert a is not b .a
114
118
115
- def test_injectable_with_recipe_and_auto_inject (self ):
119
+ def test_injectable_with_recipe_and_inject (self ):
116
120
@injectable
117
121
class A :
118
122
...
119
123
120
124
class B :
121
125
...
122
126
123
- @injectable ( auto_inject = True )
127
+ @injectable
124
128
def recipe (__a : A ) -> B :
125
129
assert isinstance (__a , A )
126
130
assert __a is not a
0 commit comments