test_filepathfield.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import os.path
  2. from django.core.exceptions import ValidationError
  3. from django.forms import FilePathField
  4. from django.test import SimpleTestCase
  5. PATH = os.path.dirname(os.path.abspath(__file__))
  6. def fix_os_paths(x):
  7. if isinstance(x, str):
  8. if x.startswith(PATH):
  9. x = x[len(PATH):]
  10. return x.replace('\\', '/')
  11. elif isinstance(x, tuple):
  12. return tuple(fix_os_paths(list(x)))
  13. elif isinstance(x, list):
  14. return [fix_os_paths(y) for y in x]
  15. else:
  16. return x
  17. class FilePathFieldTest(SimpleTestCase):
  18. expected_choices = [
  19. ('/filepathfield_test_dir/__init__.py', '__init__.py'),
  20. ('/filepathfield_test_dir/a.py', 'a.py'),
  21. ('/filepathfield_test_dir/ab.py', 'ab.py'),
  22. ('/filepathfield_test_dir/b.py', 'b.py'),
  23. ('/filepathfield_test_dir/c/__init__.py', '__init__.py'),
  24. ('/filepathfield_test_dir/c/d.py', 'd.py'),
  25. ('/filepathfield_test_dir/c/e.py', 'e.py'),
  26. ('/filepathfield_test_dir/c/f/__init__.py', '__init__.py'),
  27. ('/filepathfield_test_dir/c/f/g.py', 'g.py'),
  28. ('/filepathfield_test_dir/h/__init__.py', '__init__.py'),
  29. ('/filepathfield_test_dir/j/__init__.py', '__init__.py'),
  30. ]
  31. path = os.path.join(PATH, 'filepathfield_test_dir') + '/'
  32. def assertChoices(self, field, expected_choices):
  33. self.assertEqual(fix_os_paths(field.choices), expected_choices)
  34. def test_fix_os_paths(self):
  35. self.assertEqual(fix_os_paths(self.path), ('/filepathfield_test_dir/'))
  36. def test_nonexistent_path(self):
  37. with self.assertRaisesMessage(FileNotFoundError, 'nonexistent'):
  38. FilePathField(path='nonexistent')
  39. def test_no_options(self):
  40. f = FilePathField(path=self.path)
  41. expected = [
  42. ('/filepathfield_test_dir/README', 'README'),
  43. ] + self.expected_choices[:4]
  44. self.assertChoices(f, expected)
  45. def test_clean(self):
  46. f = FilePathField(path=self.path)
  47. msg = "'Select a valid choice. a.py is not one of the available choices.'"
  48. with self.assertRaisesMessage(ValidationError, msg):
  49. f.clean('a.py')
  50. self.assertEqual(fix_os_paths(f.clean(self.path + 'a.py')), '/filepathfield_test_dir/a.py')
  51. def test_match(self):
  52. f = FilePathField(path=self.path, match=r'^.*?\.py$')
  53. self.assertChoices(f, self.expected_choices[:4])
  54. def test_recursive(self):
  55. f = FilePathField(path=self.path, recursive=True, match=r'^.*?\.py$')
  56. expected = [
  57. ('/filepathfield_test_dir/__init__.py', '__init__.py'),
  58. ('/filepathfield_test_dir/a.py', 'a.py'),
  59. ('/filepathfield_test_dir/ab.py', 'ab.py'),
  60. ('/filepathfield_test_dir/b.py', 'b.py'),
  61. ('/filepathfield_test_dir/c/__init__.py', 'c/__init__.py'),
  62. ('/filepathfield_test_dir/c/d.py', 'c/d.py'),
  63. ('/filepathfield_test_dir/c/e.py', 'c/e.py'),
  64. ('/filepathfield_test_dir/c/f/__init__.py', 'c/f/__init__.py'),
  65. ('/filepathfield_test_dir/c/f/g.py', 'c/f/g.py'),
  66. ('/filepathfield_test_dir/h/__init__.py', 'h/__init__.py'),
  67. ('/filepathfield_test_dir/j/__init__.py', 'j/__init__.py'),
  68. ]
  69. self.assertChoices(f, expected)
  70. def test_allow_folders(self):
  71. f = FilePathField(path=self.path, allow_folders=True, allow_files=False)
  72. self.assertChoices(f, [
  73. ('/filepathfield_test_dir/c', 'c'),
  74. ('/filepathfield_test_dir/h', 'h'),
  75. ('/filepathfield_test_dir/j', 'j'),
  76. ])
  77. def test_recursive_no_folders_or_files(self):
  78. f = FilePathField(path=self.path, recursive=True, allow_folders=False, allow_files=False)
  79. self.assertChoices(f, [])
  80. def test_recursive_folders_without_files(self):
  81. f = FilePathField(path=self.path, recursive=True, allow_folders=True, allow_files=False)
  82. self.assertChoices(f, [
  83. ('/filepathfield_test_dir/c', 'c'),
  84. ('/filepathfield_test_dir/h', 'h'),
  85. ('/filepathfield_test_dir/j', 'j'),
  86. ('/filepathfield_test_dir/c/f', 'c/f'),
  87. ])