Provides tools to auto generate content.
Project description
This app aims to provide a simple way of loading masses of randomly generated test data into your development database. You can use a management command to load test data through command line.
Usually you add test data through the admin to see how your site looks with non static pages. You export data by using dumpdata to send it to your colleagues or to preserve it before you make a manage.py reset app and so on. Your site gets more and more complex and adding test data gets more and more annoying.
This is the usecase where mockups should help you to save time that can actually be spent on hacking.
Installation
You must make the mockups package available on your python path. Either drop it into your project directory or install it from the python package index with pip install django-mockups. You can also use easy_install django-mockups if you don’t have pip available.
To use the management command you must add 'mockups' to the INSTALLED_APPS setting in your django settings file. You don’t need to do this if you want to use the mockups package only as library.
Management command
The mockups accepts the following syntax:
django-admin.py mockups [options] app.Model:# [app.Model:# ...]
Its nearly self explanatory. Supply names of models, prefixed with its app name. After that, place a colon and tell the command how many objects you want to create. Here is an example how to create three categories and twenty entries for you blogging app:
django-admin.py mockups blog.Category:3 blog.Entry:20
Voila! You have ready to use testing data populated to your database. The model fields are filled with data by producing randomly generated values depending on the type of the field. E.g. text fields are filled with lorem ipsum dummies, date fields are populated with random dates from the last years etc.
There are a few command line options available. Mainly to control the behavior of related fields. If foreingkey or many to many fields should be populated with existing data or if the related models are also generated on the fly. Please have a look at the help page of the command for more information:
django-admin.py help mockups
Using mockups as tool for unittests
It has proofed that mockups have a great use for unittests. It has always bugged me that creating complex models for testing their behaviour was complicated. Sometimes models have strict restrictions or many related objects which they depend on. One solution would be to use traditional fixtures dumped from your production database. But while in development when database schemes are changing frequently, its hard to maintain all fixtures and to know exactly which objects are contained in the dumps etc…
ModelMockups to the rescue! It lets you automatically generate models and all of their dependecies on the fly. Have a look at the following examples.
Lets start with the very basics. We create a ModelMockup instance for the Entry model and tell it to create ten model instances:
from mockups import ModelMockup mockup = ModelMockup(Entry) entries = mockup.create(10)
Now you can play around and test your blog entries. By default dependecies of foreignkeys and many to many relations are solved by randomly selecting an already existing object of the related model. What if you don’t have one yet? You can provide the generate_fk attribute which allows the mockup instance to follow foreignkeys by generating new related models:
mockup = ModelMockup(Entry, generate_fk=True)
This generates new instance for all foreignkey fields of Entry. Its possible to limit this behaviour to single fields:
mockup = ModelMockup(Entry, generate_fk=['author'])
This will only create new authors automatically and doesn’t touch other tables. The same is possible with many to many fields. But you need additionally specify how many objects should be created for the m2m relation:
mockup = ModelMockup(Entry, generate_m2m={'categories': (1,3)})
All created entry models get one to three new categories assigned.
Setting custom values for fields
However its often necessary to be sure that a specific field must have a specific value. This is easily achieved with the use of Factory:
class PonyFactory(Factory): pub_date = generators.StaticGenerator(datetime(2010, 2, 1)) class PonyModelMockup(ModelMockup): factory = PonyFactory mockup = PonyModelMockup(Entry)
More
There is so much more to explore which might be useful for you and your projects:
There are ways to register custom ModelMockup subclasses with models that are automatically used when calling mockups on the model.
More control for related models, even with relations of related models… (e.g. by using generate_fk=['author', 'author__user'])
Custom constraints that are used to ensure that created the models are valid (e.g. unique and unique_together constraints which are already handled by default)
I hope to explain this in the future with more details in a documentation. It will be written but is not finished yet. I wanted to get this project out to support you in development. But since its only python code you can easily study the source on your own and see in which ways it can be used. There are already some parts documented with doc strings which might also be helpful for you.
Changelog
0.4.4
Rename and remake ChoicesGenerator -> ChoiceGenerator + ChoiceFieldGenerator
0.4.3
Make generators take empty_value keyword argument
0.4.2
Make ModelMockup take a factory instance as keyword argument for more flexibility
0.4.1
Just a rename of Mockup to ModelMockup
0.4.0
Major rewrite to use Factory
0.2.6
Changing syntax to match Django Models & Forms
Renaming to django-mockups
0.2.5
Fixing issue with --generate-fk option in management command. Thanks Mikko Hellsing for the report and fix.
0.2.4
Using Autofixture.Values for defining initial values in Autofixture subclasses.
Making autodiscover more robust. Don’t break if some module can’t be imported or throws any other exception.
0.2.3
Fixing bug when a CharField with max_length smaller than 15 is used.
Mockup.field_generators accepts callables as values.
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 django-mockups-0.4.4.tar.gz
.
File metadata
- Download URL: django-mockups-0.4.4.tar.gz
- Upload date:
- Size: 20.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 020ce2c7615819c7512c9554fcc8156f5eb808f00b27ee58beb72fc312e2502a |
|
MD5 | 1d1c4d5b7d8d775de7d64ea82bc4b154 |
|
BLAKE2b-256 | 436c7cbae8a2ed19c104061574f196cb4ef1c71ac7f617fe565608c8f05bd730 |