summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polyconseil.fr>2015-03-27 17:00:32 +0100
committerRaphaël Barrois <raphael.barrois@polyconseil.fr>2015-03-27 17:46:02 +0100
commit5363951bb62ca90d971bf036851dea564204ed2e (patch)
tree7180115c88a632f9dcfc36ca0846caedbc3ec952
parentbdc1b815cfdf3028379c6c3f18c9c47ee8298a70 (diff)
downloadfactory-boy-5363951bb62ca90d971bf036851dea564204ed2e.tar
factory-boy-5363951bb62ca90d971bf036851dea564204ed2e.tar.gz
Support declarations in FileField/ImageField.
Previously, the declarations (``factory.Sequence`` & co) weren't properly computed.
-rw-r--r--docs/changelog.rst2
-rw-r--r--factory/django.py11
-rw-r--r--tests/djapp/models.py1
-rw-r--r--tests/test_django.py11
4 files changed, 20 insertions, 5 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst
index cc4a1dc..de9778b 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -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:
diff --git a/factory/django.py b/factory/django.py
index ba81f13..cbf7c10 100644
--- a/factory/django.py
+++ b/factory/django.py
@@ -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()
@@ -156,10 +157,8 @@ class FileField(declarations.ParameteredAttribute):
"""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(
@@ -189,10 +188,12 @@ class FileField(declarations.ParameteredAttribute):
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)
diff --git a/tests/djapp/models.py b/tests/djapp/models.py
index 96ee5cf..1c1fd8e 100644
--- a/tests/djapp/models.py
+++ b/tests/djapp/models.py
@@ -79,6 +79,7 @@ if Image is not None: # PIL is available
class WithImage(models.Model):
animage = models.ImageField(upload_to=WITHFILE_UPLOAD_TO)
+ size = models.IntegerField(default=0)
else:
class WithImage(models.Model):
diff --git a/tests/test_django.py b/tests/test_django.py
index 9ac8f5c..ac52769 100644
--- a/tests/test_django.py
+++ b/tests/test_django.py
@@ -497,6 +497,17 @@ class DjangoImageFieldTestCase(unittest.TestCase):
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)