Skip to content

Commit

Permalink
Support declarations in FileField/ImageField.
Browse files Browse the repository at this point in the history
Previously, the declarations (``factory.Sequence`` & co)  weren't properly computed.
  • Loading branch information
rbarrois committed Mar 27, 2015
1 parent bdc1b81 commit 5363951
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ ChangeLog
*Bugfix:*

- Respect custom managers in :class:`~factory.django.DjangoModelFactory` (see :issue:`192`)
- Allow passing declarations (e.g :class:`~factory.Sequence`) as parameters to :class:`~factory.django.FileField`
and :class:`~factory.django.ImageField`.

.. _v2.5.0:

Expand Down
11 changes: 6 additions & 5 deletions factory/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class FileField(declarations.ParameteredAttribute):
"""Helper to fill in django.db.models.FileField from a Factory."""

DEFAULT_FILENAME = 'example.dat'
EXTEND_CONTAINERS = True

def __init__(self, **defaults):
require_django()
Expand All @@ -156,10 +157,8 @@ def _make_data(self, params):
"""Create data for the field."""
return params.get('data', b'')

def _make_content(self, extra):
def _make_content(self, params):
path = ''
params = dict(self.defaults)
params.update(extra)

if params.get('from_path') and params.get('from_file'):
raise ValueError(
Expand Down Expand Up @@ -189,10 +188,12 @@ def _make_content(self, extra):
filename = params.get('filename', default_filename)
return filename, content

def evaluate(self, sequence, obj, create, extra=None, containers=()):
def generate(self, sequence, obj, create, params):
"""Fill in the field."""

filename, content = self._make_content(extra)
params.setdefault('__sequence', sequence)
params = base.DictFactory.simple_generate(create, **params)
filename, content = self._make_content(params)
return django_files.File(content.file, filename)


Expand Down
1 change: 1 addition & 0 deletions tests/djapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class WithFile(models.Model):

class WithImage(models.Model):
animage = models.ImageField(upload_to=WITHFILE_UPLOAD_TO)
size = models.IntegerField(default=0)

else:
class WithImage(models.Model):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,17 @@ def test_default_create(self):
self.assertEqual(100, o.animage.height)
self.assertEqual('django/example.jpg', o.animage.name)

def test_complex_create(self):
o = WithImageFactory.create(
size=10,
animage__filename=factory.Sequence(lambda n: 'img%d.jpg' % n),
__sequence=42,
animage__width=factory.SelfAttribute('..size'),
animage__height=factory.SelfAttribute('width'),
)
self.assertIsNotNone(o.pk)
self.assertEqual('django/img42.jpg', o.animage.name)

def test_with_content(self):
o = WithImageFactory.build(animage__width=13, animage__color='red')
self.assertIsNone(o.pk)
Expand Down

0 comments on commit 5363951

Please sign in to comment.