|
33 | 33 |
|
34 | 34 |
|
35 | 35 | from .compat import is_python2, unittest
|
| 36 | +from . import testdata |
36 | 37 | from . import tools
|
37 | 38 |
|
38 | 39 |
|
39 | 40 | if django is not None:
|
40 | 41 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.djapp.settings')
|
41 | 42 |
|
42 | 43 | from django import test as django_test
|
| 44 | + from django.db import models as django_models |
43 | 45 | from django.test import simple as django_test_simple
|
44 | 46 | from django.test import utils as django_test_utils
|
45 | 47 | from .djapp import models
|
@@ -91,6 +93,12 @@ class NonIntegerPkFactory(factory.django.DjangoModelFactory):
|
91 | 93 | bar = ''
|
92 | 94 |
|
93 | 95 |
|
| 96 | +class WithFileFactory(factory.django.DjangoModelFactory): |
| 97 | + FACTORY_FOR = models.WithFile |
| 98 | + |
| 99 | + afile = factory.django.FileField() |
| 100 | + |
| 101 | + |
94 | 102 | @unittest.skipIf(django is None, "Django not installed.")
|
95 | 103 | class DjangoPkSequenceTestCase(django_test.TestCase):
|
96 | 104 | def setUp(self):
|
@@ -163,3 +171,95 @@ def test_force_pk(self):
|
163 | 171 | nonint2 = NonIntegerPkFactory.create()
|
164 | 172 | self.assertEqual('foo1', nonint2.foo)
|
165 | 173 | self.assertEqual('foo1', nonint2.pk)
|
| 174 | + |
| 175 | + |
| 176 | +@unittest.skipIf(django is None, "Django not installed.") |
| 177 | +class DjangoFileFieldTestCase(unittest.TestCase): |
| 178 | + |
| 179 | + def tearDown(self): |
| 180 | + super(DjangoFileFieldTestCase, self).tearDown() |
| 181 | + for path in os.listdir(models.WITHFILE_UPLOAD_DIR): |
| 182 | + # Remove temporary files written during tests. |
| 183 | + os.unlink(os.path.join(models.WITHFILE_UPLOAD_DIR, path)) |
| 184 | + |
| 185 | + def test_default_build(self): |
| 186 | + o = WithFileFactory.build() |
| 187 | + self.assertIsNone(o.pk) |
| 188 | + self.assertEqual('', o.afile.read()) |
| 189 | + self.assertEqual('django/example.dat', o.afile.name) |
| 190 | + |
| 191 | + def test_default_create(self): |
| 192 | + o = WithFileFactory.create() |
| 193 | + self.assertIsNotNone(o.pk) |
| 194 | + self.assertEqual('', o.afile.read()) |
| 195 | + self.assertEqual('django/example.dat', o.afile.name) |
| 196 | + |
| 197 | + def test_with_content(self): |
| 198 | + o = WithFileFactory.build(afile__data='foo') |
| 199 | + self.assertIsNone(o.pk) |
| 200 | + self.assertEqual('foo', o.afile.read()) |
| 201 | + self.assertEqual('django/example.dat', o.afile.name) |
| 202 | + |
| 203 | + def test_with_file(self): |
| 204 | + with open(testdata.TESTFILE_PATH, 'rb') as f: |
| 205 | + o = WithFileFactory.build(afile__from_file=f) |
| 206 | + self.assertIsNone(o.pk) |
| 207 | + self.assertEqual('example_data\n', o.afile.read()) |
| 208 | + self.assertEqual('django/example.data', o.afile.name) |
| 209 | + |
| 210 | + def test_with_path(self): |
| 211 | + o = WithFileFactory.build(afile__from_path=testdata.TESTFILE_PATH) |
| 212 | + self.assertIsNone(o.pk) |
| 213 | + self.assertEqual('example_data\n', o.afile.read()) |
| 214 | + self.assertEqual('django/example.data', o.afile.name) |
| 215 | + |
| 216 | + def test_with_file_empty_path(self): |
| 217 | + with open(testdata.TESTFILE_PATH, 'rb') as f: |
| 218 | + o = WithFileFactory.build( |
| 219 | + afile__from_file=f, |
| 220 | + afile__from_path='' |
| 221 | + ) |
| 222 | + self.assertIsNone(o.pk) |
| 223 | + self.assertEqual('example_data\n', o.afile.read()) |
| 224 | + self.assertEqual('django/example.data', o.afile.name) |
| 225 | + |
| 226 | + def test_with_path_empty_file(self): |
| 227 | + o = WithFileFactory.build( |
| 228 | + afile__from_path=testdata.TESTFILE_PATH, |
| 229 | + afile__from_file=None, |
| 230 | + ) |
| 231 | + self.assertIsNone(o.pk) |
| 232 | + self.assertEqual('example_data\n', o.afile.read()) |
| 233 | + self.assertEqual('django/example.data', o.afile.name) |
| 234 | + |
| 235 | + def test_error_both_file_and_path(self): |
| 236 | + self.assertRaises(ValueError, WithFileFactory.build, |
| 237 | + afile__from_file='fakefile', |
| 238 | + afile__from_path=testdata.TESTFILE_PATH, |
| 239 | + ) |
| 240 | + |
| 241 | + def test_override_filename_with_path(self): |
| 242 | + o = WithFileFactory.build( |
| 243 | + afile__from_path=testdata.TESTFILE_PATH, |
| 244 | + afile__filename='example.foo', |
| 245 | + ) |
| 246 | + self.assertIsNone(o.pk) |
| 247 | + self.assertEqual('example_data\n', o.afile.read()) |
| 248 | + self.assertEqual('django/example.foo', o.afile.name) |
| 249 | + |
| 250 | + def test_existing_file(self): |
| 251 | + o1 = WithFileFactory.build(afile__from_path=testdata.TESTFILE_PATH) |
| 252 | + |
| 253 | + o2 = WithFileFactory.build(afile=o1.afile) |
| 254 | + self.assertIsNone(o2.pk) |
| 255 | + self.assertEqual('example_data\n', o2.afile.read()) |
| 256 | + self.assertEqual('django/example_1.data', o2.afile.name) |
| 257 | + |
| 258 | + def test_no_file(self): |
| 259 | + o = WithFileFactory.build(afile=None) |
| 260 | + self.assertIsNone(o.pk) |
| 261 | + self.assertFalse(o.afile) |
| 262 | + |
| 263 | + |
| 264 | +if __name__ == '__main__': # pragma: no cover |
| 265 | + unittest.main() |
0 commit comments