A model factory for Django that leverages Model Mommy features in FactoryBoy.
Project description
There are two excellent options for createing fixtures for your Django tests: Model Mommy <http://model-mommy.readthedocs.io/> and Factory Boy <http://model-mommy.readthedocs.io/>, (which were inspired in their Ruby counterparts Object Daddy <https://github.com/flogic/object_daddy> and Factory Girl <https://github.com/thoughtbot/factory_girl>).
While Model Mommy feels lighter and requires absolutely no boilerplate for the simple cases, Factory Boy is more flexible and have more advanced features that might be necessary complex use cases. Which one is better for your project? Why not both? ;-)
Enters Mommy’s Boy
Mommy’s Boy uses both Model Mommy and Factory Boy under the hood so you can benefit from both libraries and choose the interface that is more suitable to each sittuation.
For really simple needs, we can use the make() and prepare() functions of Model Mommy’s API:
>>> from mommys_boy import mommy >>> from django.contrib.auth.models import User >>> user = mommy.make(User, first_name='John')
Mommy’s boy leverages both Fake Factory and Model Mommy to automatically fill up your model’s fields. It tries to use meaningful values by matching a field’s name to the corresponding Fake Factory <http://faker.readthedocs.io/> function. You can also pass explicit values such as first_name='John' in our example.
>>> user.first_name 'John' >>> user.last_name # Chosen randomly from the fake.last_name() function 'McLovin'
Model Mommy fills any required field that does not match a function in Fake Factory with random data.
FactoryBoy Integration
We can also use Fake Factory introspection and Model Mommy’s abilities within a Factory Boy factory:
from mommys_boy import DjangoMommyFactory from django.contrib.auth.models import User class UserFactory(DjangoMommyFactory): class Meta: model = User last_name = 'Smith'
Now we can use the .create() and .build() functions to create instances of the User class.
>>> user = UserFactory.create(email='foo@bar.com') >>> user.email 'foo@bar.com' >>> user.first_name # this field were automatically filled by Fake Factory 'Paul' >>> user.last_name 'Smith'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file mommys-boy-0.1.1.tar.gz
.
File metadata
- Download URL: mommys-boy-0.1.1.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d004d0688c928a8dd7e82ca5f8ab8fa3e8b3636a3f3d21e0ed3795047f190037 |
|
MD5 | 90aa72106a886b9943d1567479c3e56c |
|
BLAKE2b-256 | e9bf4385677fda9a2334c4df7a28b5b1b5b1c03b88508d580c2918023ecee64e |