summaryrefslogtreecommitdiff
path: root/tests/test_django.py
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/test_django.py
parentc490870c67116387f116528fc8563b8dcba37b3e (diff)
downloadfactory-boy-a24e2b945166cdd32ece9bf2cab21280974143cb.tar
factory-boy-a24e2b945166cdd32ece9bf2cab21280974143cb.tar.gz
Add factory.django.FileField (See #52)
Diffstat (limited to 'tests/test_django.py')
-rw-r--r--tests/test_django.py100
1 files changed, 100 insertions, 0 deletions
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()