Muestra las diferencias entre dos versiones de la página.
| Próxima revisión | Revisión previa | ||
|
otros:factoryboy [2014/05/22 07:41] alfred creado |
otros:factoryboy [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 37: | Línea 37: | ||
| ==== Installation ==== | ==== Installation ==== | ||
| + | <code> | ||
| + | $ pip install factory_boy | ||
| + | </code> | ||
| + | ==== ORMs ==== | ||
| + | === MongoEngine === | ||
| + | <code python> | ||
| + | from factory.mongoengine import MongoEngineFactory | ||
| + | # ... | ||
| + | class User(Document): | ||
| + | # ... | ||
| + | class UserFactory(MongoEngineFactory): | ||
| + | </code> | ||
| + | ===== First steps ===== | ||
| + | ==== 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'' (:!: In previous versions the class ''Meta'' with its attribute ''model'' it is substituted by ''FACTORY_FOR'' attribute): | ||
| + | <code python> | ||
| + | import factory | ||
| + | import models | ||
| + | |||
| + | class UserFactory(factory.Factory): | ||
| + | class Meta: | ||
| + | model = models.User | ||
| + | |||
| + | first_name = 'John' | ||
| + | last_name = 'Doe' | ||
| + | admin = False | ||
| + | </code> | ||
| + | |||
| + | 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> | ||