@@ -20,14 +20,21 @@ Nothing could be better than brief example:
2020 from objects.providers import Factory
2121
2222
23+ class User (object ):
24+
25+ """ Example class User."""
26+
27+
2328 # Factory provider creates new instance of specified class on every call.
24- object_factory = Factory(object )
29+ users_factory = Factory(User)
30+
31+ user1 = users_factory()
32+ user2 = users_factory()
33+
34+ assert user1 is not user2
35+ assert isinstance (user1, User) and isinstance (user2, User)
2536
26- object_1 = object_factory()
27- object_2 = object_factory()
2837
29- assert object_1 is not object_2
30- assert isinstance (object_1, object ) and isinstance (object_2, object )
3138
3239 Factory providers and injections
3340~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -74,58 +81,116 @@ provided by another factories:
7481 from objects.injections import KwArg
7582
7683
77- class A (object ):
84+ class User (object ):
7885
79- """ Example class A .
86+ """ Example class User .
8087
81- Class A has dependencies on class B and class C objects, that have to be
82- provided as init arguments.
88+ Class User has dependencies on class Photo and class CreditCard objects,
89+ that have to be provided as init arguments.
8390 """
8491
85- def __init__ (self , object_b , object_c ):
86- self .object_b = object_b
87- self .object_c = object_c
88- super (A, self ).__init__ ()
92+ def __init__ (self , main_photo , credit_card ):
93+ """ Initializer.
8994
95+ :param main_photo: Photo
96+ :param credit_card: CreditCard
97+ :return:
98+ """
99+ self .main_photo = main_photo
100+ self .credit_card = credit_card
101+ super (User, self ).__init__ ()
90102
91- class B (object ):
92103
93- """ Example class B. """
104+ class Photo ( object ):
94105
106+ """ Example class Photo."""
95107
96- class C (object ):
97108
98- """ Example class C. """
109+ class CreditCard ( object ):
99110
111+ """ Example class CreditCard."""
100112
101- # A, B, C factories:
102- c_factory = Factory(C)
103- b_factory = Factory(B)
104- a_factory = Factory(A,
105- KwArg(' object_b' , b_factory),
106- KwArg(' object_c' , c_factory))
107113
108- # Creating several A objects:
109- object_a_1 = a_factory() # Same as: A(object_b=B(), object_c=C())
110- object_a_2 = a_factory() # Same as: A(object_b=B(), object_c=C())
114+ # User, Photo and CreditCard factories:
115+ credit_cards_factory = Factory(CreditCard)
116+ photos_factory = Factory(Photo)
117+ users_factory = Factory(User,
118+ KwArg(' main_photo' , photos_factory),
119+ KwArg(' credit_card' , credit_cards_factory))
120+
121+ # Creating several User objects:
122+ user1 = users_factory() # Same as: User(main_photo=Photo(),
123+ # credit_card=CreditCard())
124+ user2 = users_factory() # Same as: User(main_photo=Photo(),
125+ # credit_card=CreditCard())
111126
112127 # Making some asserts:
113- assert object_a_1 is not object_a_2
114- assert object_a_1.object_b is not object_a_2.object_b
115- assert object_a_1.object_c is not object_a_2.object_c
128+ assert user1 is not user2
129+ assert user1.main_photo is not user2.main_photo
130+ assert user1.credit_card is not user2.credit_card
131+
132+
133+ Next example shows how ``Factory `` provider deals with positional and keyword
134+ ``__init__ `` context arguments. In few words, ``Factory `` provider fully
135+ passes positional context arguments to class's ``__init__ `` method, but
136+ keyword context arguments have priority on ``KwArg `` injections (this could be
137+ useful for testing). So, please, follow the example below:
138+
139+ .. code-block :: python
116140
141+ """ `Factory` providers with init injections and context arguments example."""
117142
118- Need to make examples for:
143+ from objects.providers import Factory
144+ from objects.injections import KwArg
145+
146+
147+ class User (object ):
148+
149+ """ Example class User."""
150+
151+ def __init__ (self , id , main_photo ):
152+ """ Initializer.
153+
154+ :param id: int
155+ :param main_photo: Photo
156+ :return:
157+ """
158+ self .id = id
159+ self .main_photo = main_photo
160+ super (User, self ).__init__ ()
161+
162+
163+ class Photo (object ):
164+
165+ """ Example class Photo."""
166+
167+
168+ # User and Photo factories:
169+ photos_factory = Factory(Photo)
170+ users_factory = Factory(User,
171+ KwArg(' main_photo' , photos_factory))
172+
173+ # Creating several User objects:
174+ user1 = users_factory(1 ) # Same as: User(1, main_photo=Photo())
175+ user2 = users_factory(2 ) # Same as: User(1, main_photo=Photo())
176+
177+ # Making some asserts:
178+ assert user1.id == 1
179+ assert user2.id == 2
180+ assert user1 is not user2
181+ assert isinstance (user1.main_photo, Photo)
182+ assert isinstance (user2.main_photo, Photo)
183+ assert user1.main_photo is not user2.main_photo
119184
120- - Several KwArgs usage. +
121- - Factory depends on another factory. +
185+ # Context keyword arguments have priority on KwArg injections priority:
186+ photo_mock = Photo()
122187
123- - KwArg usage with not provider injectable value.
188+ user3 = users_factory( 3 , main_photo = photo_mock)
124189
125- - Context positional arguments usage with KwArgs.
126- - Context keyword arguments priority on KwArgs.
190+ assert user3.id == 3
191+ assert user3 not in (user2, user1)
192+ assert user3.main_photo is photo_mock
127193
128- - Context keyword arguments usage with KwArgs ???
129194
130195 Factory providers and attribute injections
131196~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments