Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
otros:factoryboy [2014/05/22 07:45] 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 55: | Línea 63: | ||
| last_name = 'Doe' | last_name = 'Doe' | ||
| admin = False | 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> | </code> | ||