diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-06-27 01:19:24 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-06-27 01:19:24 +0200 |
commit | 168ef54e5acfac59f7c625e75a0c7c6d2484cdf0 (patch) | |
tree | 30649368a7fe6db8f1c3c965f598a17ac23a495a /tests | |
parent | 77807b4a6bbec59ea98c9f53557ac96239c6300e (diff) | |
download | factory-boy-168ef54e5acfac59f7c625e75a0c7c6d2484cdf0.tar factory-boy-168ef54e5acfac59f7c625e75a0c7c6d2484cdf0.tar.gz |
Add factory.django.ImageField (Closes #52).
Diffstat (limited to 'tests')
-rw-r--r-- | tests/djapp/models.py | 5 | ||||
-rw-r--r-- | tests/test_django.py | 114 | ||||
-rw-r--r-- | tests/testdata/__init__.py | 1 | ||||
-rw-r--r-- | tests/testdata/example.jpeg | bin | 0 -> 301 bytes |
4 files changed, 120 insertions, 0 deletions
diff --git a/tests/djapp/models.py b/tests/djapp/models.py index 52acebe..7bb5ace 100644 --- a/tests/djapp/models.py +++ b/tests/djapp/models.py @@ -42,3 +42,8 @@ WITHFILE_UPLOAD_DIR = os.path.join(settings.MEDIA_ROOT, WITHFILE_UPLOAD_TO) class WithFile(models.Model): afile = models.FileField(upload_to=WITHFILE_UPLOAD_TO) + + +class WithImage(models.Model): + animage = models.ImageField(upload_to=WITHFILE_UPLOAD_TO) + diff --git a/tests/test_django.py b/tests/test_django.py index a74a7ae..4ae2a58 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -31,6 +31,16 @@ try: except ImportError: # pragma: no cover django = None +try: + from PIL import Image +except ImportError: # pragma: no cover + # Try PIL alternate name + try: + import Image + except ImportError: + # OK, not installed + Image = None + from .compat import is_python2, unittest from . import testdata @@ -99,6 +109,12 @@ class WithFileFactory(factory.django.DjangoModelFactory): afile = factory.django.FileField() +class WithImageFactory(factory.django.DjangoModelFactory): + FACTORY_FOR = models.WithImage + + animage = factory.django.ImageField() + + @unittest.skipIf(django is None, "Django not installed.") class DjangoPkSequenceTestCase(django_test.TestCase): def setUp(self): @@ -261,5 +277,103 @@ class DjangoFileFieldTestCase(unittest.TestCase): self.assertFalse(o.afile) +@unittest.skipIf(django is None, "Django not installed.") +@unittest.skipIf(Image is None, "PIL not installed.") +class DjangoImageFieldTestCase(unittest.TestCase): + + def tearDown(self): + super(DjangoImageFieldTestCase, 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 = WithImageFactory.build() + self.assertIsNone(o.pk) + self.assertEqual(100, o.animage.width) + self.assertEqual(100, o.animage.height) + self.assertEqual('django/example.jpg', o.animage.name) + + def test_default_create(self): + o = WithImageFactory.create() + self.assertIsNotNone(o.pk) + self.assertEqual(100, o.animage.width) + self.assertEqual(100, o.animage.height) + self.assertEqual('django/example.jpg', o.animage.name) + + def test_with_content(self): + o = WithImageFactory.build(animage__width=13, animage__color='blue') + self.assertIsNone(o.pk) + self.assertEqual(13, o.animage.width) + self.assertEqual(13, o.animage.height) + self.assertEqual('django/example.jpg', o.animage.name) + + def test_with_file(self): + with open(testdata.TESTIMAGE_PATH, 'rb') as f: + o = WithImageFactory.build(animage__from_file=f) + self.assertIsNone(o.pk) + # Image file for a 42x42 green jpeg: 301 bytes long. + self.assertEqual(301, len(o.animage.read())) + self.assertEqual('django/example.jpeg', o.animage.name) + + def test_with_path(self): + o = WithImageFactory.build(animage__from_path=testdata.TESTIMAGE_PATH) + self.assertIsNone(o.pk) + # Image file for a 42x42 green jpeg: 301 bytes long. + self.assertEqual(301, len(o.animage.read())) + self.assertEqual('django/example.jpeg', o.animage.name) + + def test_with_file_empty_path(self): + with open(testdata.TESTIMAGE_PATH, 'rb') as f: + o = WithImageFactory.build( + animage__from_file=f, + animage__from_path='' + ) + self.assertIsNone(o.pk) + # Image file for a 42x42 green jpeg: 301 bytes long. + self.assertEqual(301, len(o.animage.read())) + self.assertEqual('django/example.jpeg', o.animage.name) + + def test_with_path_empty_file(self): + o = WithImageFactory.build( + animage__from_path=testdata.TESTIMAGE_PATH, + animage__from_file=None, + ) + self.assertIsNone(o.pk) + # Image file for a 42x42 green jpeg: 301 bytes long. + self.assertEqual(301, len(o.animage.read())) + self.assertEqual('django/example.jpeg', o.animage.name) + + def test_error_both_file_and_path(self): + self.assertRaises(ValueError, WithImageFactory.build, + animage__from_file='fakefile', + animage__from_path=testdata.TESTIMAGE_PATH, + ) + + def test_override_filename_with_path(self): + o = WithImageFactory.build( + animage__from_path=testdata.TESTIMAGE_PATH, + animage__filename='example.foo', + ) + self.assertIsNone(o.pk) + # Image file for a 42x42 green jpeg: 301 bytes long. + self.assertEqual(301, len(o.animage.read())) + self.assertEqual('django/example.foo', o.animage.name) + + def test_existing_file(self): + o1 = WithImageFactory.build(animage__from_path=testdata.TESTIMAGE_PATH) + + o2 = WithImageFactory.build(animage=o1.animage) + self.assertIsNone(o2.pk) + # Image file for a 42x42 green jpeg: 301 bytes long. + self.assertEqual(301, len(o2.animage.read())) + self.assertEqual('django/example_1.jpeg', o2.animage.name) + + def test_no_file(self): + o = WithImageFactory.build(animage=None) + self.assertIsNone(o.pk) + self.assertFalse(o.animage) + + if __name__ == '__main__': # pragma: no cover unittest.main() diff --git a/tests/testdata/__init__.py b/tests/testdata/__init__.py index 3d1d441..9956610 100644 --- a/tests/testdata/__init__.py +++ b/tests/testdata/__init__.py @@ -24,3 +24,4 @@ import os.path TESTDATA_ROOT = os.path.abspath(os.path.dirname(__file__)) TESTFILE_PATH = os.path.join(TESTDATA_ROOT, 'example.data') +TESTIMAGE_PATH = os.path.join(TESTDATA_ROOT, 'example.jpeg') diff --git a/tests/testdata/example.jpeg b/tests/testdata/example.jpeg Binary files differnew file mode 100644 index 0000000..28beea9 --- /dev/null +++ b/tests/testdata/example.jpeg |