summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-06-15 16:17:01 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-06-15 16:18:10 +0200
commita24e2b945166cdd32ece9bf2cab21280974143cb (patch)
treea5f0539659cb0bd012c386fe7a135fc7d159afce /tests
parentc490870c67116387f116528fc8563b8dcba37b3e (diff)
downloadfactory-boy-a24e2b945166cdd32ece9bf2cab21280974143cb.tar
factory-boy-a24e2b945166cdd32ece9bf2cab21280974143cb.tar.gz
Add factory.django.FileField (See #52)
Diffstat (limited to 'tests')
-rw-r--r--tests/djapp/models.py8
-rw-r--r--tests/djapp/settings.py10
-rw-r--r--tests/test_django.py100
-rw-r--r--tests/testdata/__init__.py26
-rw-r--r--tests/testdata/example.data1
5 files changed, 145 insertions, 0 deletions
diff --git a/tests/djapp/models.py b/tests/djapp/models.py
index c107add..52acebe 100644
--- a/tests/djapp/models.py
+++ b/tests/djapp/models.py
@@ -22,7 +22,9 @@
"""Helpers for testing django apps."""
+import os.path
+from django.conf import settings
from django.db import models
class StandardModel(models.Model):
@@ -34,3 +36,9 @@ class NonIntegerPk(models.Model):
bar = models.CharField(max_length=20, blank=True)
+WITHFILE_UPLOAD_TO = 'django'
+WITHFILE_UPLOAD_DIR = os.path.join(settings.MEDIA_ROOT, WITHFILE_UPLOAD_TO)
+
+class WithFile(models.Model):
+ afile = models.FileField(upload_to=WITHFILE_UPLOAD_TO)
+
diff --git a/tests/djapp/settings.py b/tests/djapp/settings.py
index 787d3f3..c1b79b0 100644
--- a/tests/djapp/settings.py
+++ b/tests/djapp/settings.py
@@ -20,6 +20,16 @@
# THE SOFTWARE.
"""Settings for factory_boy/Django tests."""
+import os
+
+FACTORY_ROOT = os.path.join(
+ os.path.abspath(os.path.dirname(__file__)), # /path/to/fboy/tests/djapp/
+ os.pardir, # /path/to/fboy/tests/
+ os.pardir, # /path/to/fboy
+)
+
+MEDIA_ROOT = os.path.join(FACTORY_ROOT, 'tmp_test')
+
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
diff --git a/tests/test_django.py b/tests/test_django.py
index 70bc376..eb35f7a 100644
--- a/tests/test_django.py
+++ b/tests/test_django.py
@@ -33,6 +33,7 @@ except ImportError: # pragma: no cover
from .compat import is_python2, unittest
+from . import testdata
from . import tools
@@ -40,6 +41,7 @@ if django is not None:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.djapp.settings')
from django import test as django_test
+ from django.db import models as django_models
from django.test import simple as django_test_simple
from django.test import utils as django_test_utils
from .djapp import models
@@ -91,6 +93,12 @@ class NonIntegerPkFactory(factory.django.DjangoModelFactory):
bar = ''
+class WithFileFactory(factory.django.DjangoModelFactory):
+ FACTORY_FOR = models.WithFile
+
+ afile = factory.django.FileField()
+
+
@unittest.skipIf(django is None, "Django not installed.")
class DjangoPkSequenceTestCase(django_test.TestCase):
def setUp(self):
@@ -163,3 +171,95 @@ class DjangoNonIntegerPkTestCase(django_test.TestCase):
nonint2 = NonIntegerPkFactory.create()
self.assertEqual('foo1', nonint2.foo)
self.assertEqual('foo1', nonint2.pk)
+
+
+@unittest.skipIf(django is None, "Django not installed.")
+class DjangoFileFieldTestCase(unittest.TestCase):
+
+ def tearDown(self):
+ super(DjangoFileFieldTestCase, self).tearDown()
+ for path in os.listdir(models.WITHFILE_UPLOAD_DIR):
+ # Remove temporary files written during tests.
+ os.unlink(os.path.join(models.WITHFILE_UPLOAD_DIR, path))
+
+ def test_default_build(self):
+ o = WithFileFactory.build()
+ self.assertIsNone(o.pk)
+ self.assertEqual('', o.afile.read())
+ self.assertEqual('django/example.dat', o.afile.name)
+
+ def test_default_create(self):
+ o = WithFileFactory.create()
+ self.assertIsNotNone(o.pk)
+ self.assertEqual('', o.afile.read())
+ self.assertEqual('django/example.dat', o.afile.name)
+
+ def test_with_content(self):
+ o = WithFileFactory.build(afile__data='foo')
+ self.assertIsNone(o.pk)
+ self.assertEqual('foo', o.afile.read())
+ self.assertEqual('django/example.dat', o.afile.name)
+
+ def test_with_file(self):
+ with open(testdata.TESTFILE_PATH, 'rb') as f:
+ o = WithFileFactory.build(afile__from_file=f)
+ self.assertIsNone(o.pk)
+ self.assertEqual('example_data\n', o.afile.read())
+ self.assertEqual('django/example.data', o.afile.name)
+
+ def test_with_path(self):
+ o = WithFileFactory.build(afile__from_path=testdata.TESTFILE_PATH)
+ self.assertIsNone(o.pk)
+ self.assertEqual('example_data\n', o.afile.read())
+ self.assertEqual('django/example.data', o.afile.name)
+
+ def test_with_file_empty_path(self):
+ with open(testdata.TESTFILE_PATH, 'rb') as f:
+ o = WithFileFactory.build(
+ afile__from_file=f,
+ afile__from_path=''
+ )
+ self.assertIsNone(o.pk)
+ self.assertEqual('example_data\n', o.afile.read())
+ self.assertEqual('django/example.data', o.afile.name)
+
+ def test_with_path_empty_file(self):
+ o = WithFileFactory.build(
+ afile__from_path=testdata.TESTFILE_PATH,
+ afile__from_file=None,
+ )
+ self.assertIsNone(o.pk)
+ self.assertEqual('example_data\n', o.afile.read())
+ self.assertEqual('django/example.data', o.afile.name)
+
+ def test_error_both_file_and_path(self):
+ self.assertRaises(ValueError, WithFileFactory.build,
+ afile__from_file='fakefile',
+ afile__from_path=testdata.TESTFILE_PATH,
+ )
+
+ def test_override_filename_with_path(self):
+ o = WithFileFactory.build(
+ afile__from_path=testdata.TESTFILE_PATH,
+ afile__filename='example.foo',
+ )
+ self.assertIsNone(o.pk)
+ self.assertEqual('example_data\n', o.afile.read())
+ self.assertEqual('django/example.foo', o.afile.name)
+
+ def test_existing_file(self):
+ o1 = WithFileFactory.build(afile__from_path=testdata.TESTFILE_PATH)
+
+ o2 = WithFileFactory.build(afile=o1.afile)
+ self.assertIsNone(o2.pk)
+ self.assertEqual('example_data\n', o2.afile.read())
+ self.assertEqual('django/example_1.data', o2.afile.name)
+
+ def test_no_file(self):
+ o = WithFileFactory.build(afile=None)
+ self.assertIsNone(o.pk)
+ self.assertFalse(o.afile)
+
+
+if __name__ == '__main__': # pragma: no cover
+ unittest.main()
diff --git a/tests/testdata/__init__.py b/tests/testdata/__init__.py
new file mode 100644
index 0000000..3d1d441
--- /dev/null
+++ b/tests/testdata/__init__.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2011-2013 Raphaël Barrois
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+
+import os.path
+
+TESTDATA_ROOT = os.path.abspath(os.path.dirname(__file__))
+TESTFILE_PATH = os.path.join(TESTDATA_ROOT, 'example.data')
diff --git a/tests/testdata/example.data b/tests/testdata/example.data
new file mode 100644
index 0000000..02ff8ec
--- /dev/null
+++ b/tests/testdata/example.data
@@ -0,0 +1 @@
+example_data