-
-
Notifications
You must be signed in to change notification settings - Fork 67
Simple Documentation
paulocheque edited this page Apr 6, 2012
·
4 revisions
from django_dynamic_fixture import N, G, F, P
#or use old default names:
#from django_dynamic_fixture import new, get, DynamicFixture as F, print_field_values
from django.db import models
class ModelA(models.Model): pass
class ModelY(models.Model):
other_text = models.CharField()
other_list = models.ManyToManyField('ModelA')
class ModelX(models.Model):
some_text = models.CharField(null=True)
parent_left = models.ForeingKey('self')
y_reference = models.ForeingKey('ModelY')
list_a = models.ManyToManyField('ModelA')
- DynamicFixture (F): receive arguments and create model instances.
- new: it is just a wrapper: it creates a F to create a not saved model instance.
- get: basically, call the new method and save the instance. You can set ManyToMany fields only after the instance is saved.
instance_of_modelx = N(ModelX)
assert instance_of_modelx.some_text != None
assert instance_of_modelx.parent_left != None
assert instance_of_modelx.parent_left.parent_left == None
assert instance_of_modelx.id == None # new do not save the instance
assert instance_of_modelx.y_reference.id != None # save dependencies by default
assert len(instance_of_modelx.list_a.all()) == 0 # do not create many2many fields by default
instance_of_modelx = N(ModelX, fill_nullable_fields=False) # default = True
assert instance_of_modelx.some_text == None
instance_of_modelx = G(ModelX, ignore_fields=['some_text'])
assert instance_of_modelx.some_text == None
instance_of_modelx = N(ModelX, number_of_laps=2) # default = 1
assert instance_of_modelx.parent_left != None
assert instance_of_modelx.parent_left.parent_left != None
assert instance_of_modelx.parent_left.parent_left.parent_left == None
instance_of_modelx = N(ModelX, some_text='some fixed data') # attribute accepts static data
assert instance_of_modelx.some_text == 'some fixed data'
instance_of_modelx = N(ModelX, id=99999) # you can define the id too
assert instance_of_modelx.id == 99999
instance_of_modelx = N(ModelX, some_text=lambda field: field.name) # attribute accepts callables
assert instance_of_modelx.some_text == 'some_text'
Use this with attention, you can get an error if you try to save an instance with not saved dependencies
instance_of_modelx = N(ModelX, persist_dependencies=False)
assert instance_of_modelx.y_reference.id == None
instance_of_modelx = G(ModelX) # get save the model instance
assert instance_of_modelx.id != None
instance_of_modelx = G(ModelX, list_a=2) # Many2Many can receive a number of instances to be created
assert len(instance_of_modelx.list_a.all()) == 2
instance_of_modelx = G(ModelX, list_a=[F(), F(), F()]) # Many2Many can receive a list of DynamicFixtures
assert len(instance_of_modelx.list_a.all()) == 3
a = G(ModelA)
instance_of_modelx = G(ModelX, list_a=[F(), a, F()]) # Many2Many can receive a list of instances
assert len(instance_of_modelx.list_a.all()) == 3
You can pass arguments to F (DynamicFixture) recursively. This works for ForeignKey and ManyToMany Fields! Easy and customizable!
instance_of_modelx = G(ModelX, parent_left=F(other_text='wow', other_list=2))
assert len(instance_of_modelx.y_reference.other_list.all()) == 2
# X has a ForeignKey to A
# A has a ForeignKey to B
# B has a ForeignKey to C
# this will create instances of C, B, A and X (in this order). Attribute d of C will be 'some value'
G(X, a=F(b=F(c=F(d='some value'))))
P(instance_of_modelx)
class ModelX(models.Model):
my_file = models.FileField(upload_to='/')
from tempfile import mkstemp
pdf_a = File(open(mkstemp()[1], 'w'), name='a.pdf')
G(ModelX, my_file=pdf_a)
G(ModelX, my_field=C('my_field_y.x'))
G(ModelX, my_field='x', shelve=True)
G(ModelX, use_library=True)
G(ModelX, my_field='x', shelve='some name')
G(ModelX, use_library=True, named_shelve='some name')
def setUpModule():
N(ModelX, my_field='x', shelve=True)
Put the code in the tests.py or tests/__init__.py of specific application.
N(ModelX, my_field='x', shelve=True)
OR
Add in ddf_setup.py:
N(ModelX, my_field='x', shelve=True)
Run with nose plugin:
python manage.py test --with-ddf-setup
from django_dynamic_fixture.decorators import skip_for_database, only_for_database, SQLITE3
@only_for_database(SQLITE3)
def test_something1(self): pass
@skip_for_database(SQLITE3)
def test_something2(self): pass
@only_for_database("some value used in settings.DATABASES['default']['ENGINE']")
def test_something3(self): pass
@skip_for_database("some value used in settings.DATABASES['default']['ENGINE']")
def test_something4(self): pass
python manage.py test --with-queries
python manage.py count_queries_on_save