Herramientas de usuario

Herramientas del sitio


otros:factoryboy

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
otros:factoryboy [2014/05/22 08:32]
alfred [Factories]
otros:factoryboy [2020/05/09 09:25] (actual)
Línea 40: Línea 40:
 $ pip install factory_boy $ pip install factory_boy
 </​code>​ </​code>​
 +==== ORMs ==== 
 +=== MongoEngine === 
 +<code python>​ 
 +from factory.mongoengine import MongoEngineFactory 
 +# ... 
 +class User(Document):​ 
 +# ... 
 +class UserFactory(MongoEngineFactory):​ 
 +</​code>​
 ===== First steps ===== ===== First steps =====
 ==== Factories ==== ==== Factories ====
-Factories declare a set of attributes for an object. The class of this object must be defined in the ''​model''​ attribute of a subclass ''​Meta'':​+Factories declare a set of attributes for an object. The class of this object must be defined in the ''​model''​ attribute of a subclass ''​Meta'' ​(:!: In previous versions the class ''​Meta''​ with its attribute ''​model''​ it is substituted by ''​FACTORY_FOR''​ attribute):
 <code python> <code python>
 import factory import factory
Línea 56: Línea 64:
     admin = False     admin = False
 </​code>​ </​code>​
-:!: In previous versions the class ''​Meta'' ​with its attribute ​''​model'' ​it is substituted by ''​FACTORY_FOR'' ​attribute.+ 
 +From here we can: 
 +<code python>​ 
 +# Build a not saved user 
 +user = UserFactory.build() 
 +# Build a saved user 
 +user = UserFactory.create() 
 +# Build a saved user 
 +user = UserFactory() 
 +# Returns a dict of attributes that can be used to build a user 
 +attributes = UserFactory.attributes() 
 +# Build a not saved user overriding its first_name 
 +user = UserFactory.build(first_name='Joe'
 +# Build ten not saved users named Joe 
 +users = USerFactory.build(10,​ first_name="​Joe"​) 
 +</​code>​ 
 + 
 +=== Derived values === 
 +We can create a field which is derived from others (this example will put ''​joe.blow@example.com'' ​in email field): 
 +<code python>​ 
 +class UserFactory(factory.Factory):​ 
 +    FACTORY_FOR = models.User 
 +    first_name = 'Joe' 
 +    last_name = 'Blow' 
 +    email = factory.LazyAttribute(lambda a: '​{0}.{1}@example.com'​.format(a.first_name,​ a.last_name).lower()) 
 +</​code>​ 
 + 
 +=== Unique values in sequence === 
 +We can create a field which is auto-incremented as: 
 +<code python>​ 
 +class UserFactory(factory.Factory):​ 
 +    ​FACTORY_FOR ​= models.User 
 +    email = factory.Sequence(lambda n: 'person{0}@example.com'.format(n)) 
 +</​code>​ 
 + 
 +=== Associate factories === 
 +We can associate a field with other factory to create other sub instance: 
 +<code python>​ 
 +class PostFactory(factory.Factory):​ 
 +    FACTORY_FOR = models.Post 
 +    author = factory.SubFactory(UserFactory) 
 +</​code>​
otros/factoryboy.1400747556.txt.gz · Última modificación: 2020/05/09 09:25 (editor externo)