test_filepathfield.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from __future__ import unicode_literals
  2. import os.path
  3. from django.forms import FilePathField, ValidationError, forms
  4. from django.test import SimpleTestCase
  5. from django.utils import six
  6. from django.utils._os import upath
  7. def fix_os_paths(x):
  8. if isinstance(x, six.string_types):
  9. return x.replace('\\', '/')
  10. elif isinstance(x, tuple):
  11. return tuple(fix_os_paths(list(x)))
  12. elif isinstance(x, list):
  13. return [fix_os_paths(y) for y in x]
  14. else:
  15. return x
  16. class FilePathFieldTest(SimpleTestCase):
  17. def test_filepathfield_1(self):
  18. path = os.path.abspath(upath(forms.__file__))
  19. path = os.path.dirname(path) + '/'
  20. self.assertTrue(fix_os_paths(path).endswith('/django/forms/'))
  21. def test_filepathfield_2(self):
  22. path = upath(forms.__file__)
  23. path = os.path.dirname(os.path.abspath(path)) + '/'
  24. f = FilePathField(path=path)
  25. f.choices = [p for p in f.choices if p[0].endswith('.py')]
  26. f.choices.sort()
  27. expected = [
  28. ('/django/forms/__init__.py', '__init__.py'),
  29. ('/django/forms/boundfield.py', 'boundfield.py'),
  30. ('/django/forms/fields.py', 'fields.py'),
  31. ('/django/forms/forms.py', 'forms.py'),
  32. ('/django/forms/formsets.py', 'formsets.py'),
  33. ('/django/forms/models.py', 'models.py'),
  34. ('/django/forms/renderers.py', 'renderers.py'),
  35. ('/django/forms/utils.py', 'utils.py'),
  36. ('/django/forms/widgets.py', 'widgets.py')
  37. ]
  38. for exp, got in zip(expected, fix_os_paths(f.choices)):
  39. self.assertEqual(exp[1], got[1])
  40. self.assertTrue(got[0].endswith(exp[0]))
  41. msg = "'Select a valid choice. fields.py is not one of the available choices.'"
  42. with self.assertRaisesMessage(ValidationError, msg):
  43. f.clean('fields.py')
  44. self.assertTrue(fix_os_paths(f.clean(path + 'fields.py')).endswith('/django/forms/fields.py'))
  45. def test_filepathfield_3(self):
  46. path = upath(forms.__file__)
  47. path = os.path.dirname(os.path.abspath(path)) + '/'
  48. f = FilePathField(path=path, match=r'^.*?\.py$')
  49. f.choices.sort()
  50. expected = [
  51. ('/django/forms/__init__.py', '__init__.py'),
  52. ('/django/forms/boundfield.py', 'boundfield.py'),
  53. ('/django/forms/fields.py', 'fields.py'),
  54. ('/django/forms/forms.py', 'forms.py'),
  55. ('/django/forms/formsets.py', 'formsets.py'),
  56. ('/django/forms/models.py', 'models.py'),
  57. ('/django/forms/renderers.py', 'renderers.py'),
  58. ('/django/forms/utils.py', 'utils.py'),
  59. ('/django/forms/widgets.py', 'widgets.py')
  60. ]
  61. for exp, got in zip(expected, fix_os_paths(f.choices)):
  62. self.assertEqual(exp[1], got[1])
  63. self.assertTrue(got[0].endswith(exp[0]))
  64. def test_filepathfield_4(self):
  65. path = os.path.abspath(upath(forms.__file__))
  66. path = os.path.dirname(path) + '/'
  67. f = FilePathField(path=path, recursive=True, match=r'^.*?\.py$')
  68. f.choices.sort()
  69. expected = [
  70. ('/django/forms/__init__.py', '__init__.py'),
  71. ('/django/forms/boundfield.py', 'boundfield.py'),
  72. ('/django/forms/extras/__init__.py', 'extras/__init__.py'),
  73. ('/django/forms/extras/widgets.py', 'extras/widgets.py'),
  74. ('/django/forms/fields.py', 'fields.py'),
  75. ('/django/forms/forms.py', 'forms.py'),
  76. ('/django/forms/formsets.py', 'formsets.py'),
  77. ('/django/forms/models.py', 'models.py'),
  78. ('/django/forms/renderers.py', 'renderers.py'),
  79. ('/django/forms/utils.py', 'utils.py'),
  80. ('/django/forms/widgets.py', 'widgets.py')
  81. ]
  82. for exp, got in zip(expected, fix_os_paths(f.choices)):
  83. self.assertEqual(exp[1], got[1])
  84. self.assertTrue(got[0].endswith(exp[0]))
  85. def test_filepathfield_folders(self):
  86. path = os.path.abspath(os.path.join(upath(__file__), '..', '..')) + '/tests/filepath_test_files/'
  87. f = FilePathField(path=path, allow_folders=True, allow_files=False)
  88. f.choices.sort()
  89. expected = [
  90. ('/forms_tests/tests/filepath_test_files/directory', 'directory'),
  91. ]
  92. actual = fix_os_paths(f.choices)
  93. self.assertEqual(len(expected), len(actual))
  94. for exp, got in zip(expected, actual):
  95. self.assertEqual(exp[1], got[1])
  96. self.assertTrue(got[0].endswith(exp[0]))
  97. f = FilePathField(path=path, allow_folders=True, allow_files=True)
  98. f.choices.sort()
  99. expected = [
  100. ('/forms_tests/tests/filepath_test_files/.dot-file', '.dot-file'),
  101. ('/forms_tests/tests/filepath_test_files/1x1.bmp', '1x1.bmp'),
  102. ('/forms_tests/tests/filepath_test_files/1x1.png', '1x1.png'),
  103. ('/forms_tests/tests/filepath_test_files/directory', 'directory'),
  104. ('/forms_tests/tests/filepath_test_files/fake-image.jpg', 'fake-image.jpg'),
  105. ('/forms_tests/tests/filepath_test_files/real-text-file.txt', 'real-text-file.txt'),
  106. ]
  107. actual = fix_os_paths(f.choices)
  108. self.assertEqual(len(expected), len(actual))
  109. for exp, got in zip(expected, actual):
  110. self.assertEqual(exp[1], got[1])
  111. self.assertTrue(got[0].endswith(exp[0]))