test_filefield.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import os
  2. import sys
  3. import tempfile
  4. import unittest
  5. from pathlib import Path
  6. from django.core.files import temp
  7. from django.core.files.base import ContentFile
  8. from django.core.files.uploadedfile import TemporaryUploadedFile
  9. from django.db.utils import IntegrityError
  10. from django.test import TestCase, override_settings
  11. from .models import Document
  12. class FileFieldTests(TestCase):
  13. def test_clearable(self):
  14. """
  15. FileField.save_form_data() will clear its instance attribute value if
  16. passed False.
  17. """
  18. d = Document(myfile='something.txt')
  19. self.assertEqual(d.myfile, 'something.txt')
  20. field = d._meta.get_field('myfile')
  21. field.save_form_data(d, False)
  22. self.assertEqual(d.myfile, '')
  23. def test_unchanged(self):
  24. """
  25. FileField.save_form_data() considers None to mean "no change" rather
  26. than "clear".
  27. """
  28. d = Document(myfile='something.txt')
  29. self.assertEqual(d.myfile, 'something.txt')
  30. field = d._meta.get_field('myfile')
  31. field.save_form_data(d, None)
  32. self.assertEqual(d.myfile, 'something.txt')
  33. def test_changed(self):
  34. """
  35. FileField.save_form_data(), if passed a truthy value, updates its
  36. instance attribute.
  37. """
  38. d = Document(myfile='something.txt')
  39. self.assertEqual(d.myfile, 'something.txt')
  40. field = d._meta.get_field('myfile')
  41. field.save_form_data(d, 'else.txt')
  42. self.assertEqual(d.myfile, 'else.txt')
  43. def test_delete_when_file_unset(self):
  44. """
  45. Calling delete on an unset FileField should not call the file deletion
  46. process, but fail silently (#20660).
  47. """
  48. d = Document()
  49. d.myfile.delete()
  50. def test_refresh_from_db(self):
  51. d = Document.objects.create(myfile='something.txt')
  52. d.refresh_from_db()
  53. self.assertIs(d.myfile.instance, d)
  54. def test_defer(self):
  55. Document.objects.create(myfile='something.txt')
  56. self.assertEqual(Document.objects.defer('myfile')[0].myfile, 'something.txt')
  57. def test_unique_when_same_filename(self):
  58. """
  59. A FileField with unique=True shouldn't allow two instances with the
  60. same name to be saved.
  61. """
  62. Document.objects.create(myfile='something.txt')
  63. with self.assertRaises(IntegrityError):
  64. Document.objects.create(myfile='something.txt')
  65. @unittest.skipIf(sys.platform == 'win32', "Windows doesn't support moving open files.")
  66. # The file's source and destination must be on the same filesystem.
  67. @override_settings(MEDIA_ROOT=temp.gettempdir())
  68. def test_move_temporary_file(self):
  69. """
  70. The temporary uploaded file is moved rather than copied to the
  71. destination.
  72. """
  73. with TemporaryUploadedFile('something.txt', 'text/plain', 0, 'UTF-8') as tmp_file:
  74. tmp_file_path = tmp_file.temporary_file_path()
  75. Document.objects.create(myfile=tmp_file)
  76. self.assertFalse(os.path.exists(tmp_file_path), 'Temporary file still exists')
  77. def test_open_returns_self(self):
  78. """
  79. FieldField.open() returns self so it can be used as a context manager.
  80. """
  81. d = Document.objects.create(myfile='something.txt')
  82. # Replace the FileField's file with an in-memory ContentFile, so that
  83. # open() doesn't write to disk.
  84. d.myfile.file = ContentFile(b'', name='bla')
  85. self.assertEqual(d.myfile, d.myfile.open())
  86. def test_media_root_pathlib(self):
  87. with tempfile.TemporaryDirectory() as tmp_dir:
  88. with override_settings(MEDIA_ROOT=Path(tmp_dir)):
  89. with TemporaryUploadedFile('foo.txt', 'text/plain', 1, 'utf-8') as tmp_file:
  90. Document.objects.create(myfile=tmp_file)
  91. self.assertTrue(os.path.exists(os.path.join(tmp_dir, 'unused', 'foo.txt')))