2016-04-10 01:17:34 +08:00
|
|
|
import os.path
|
|
|
|
|
|
|
|
from django.forms import FilePathField, ValidationError, forms
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
|
|
|
|
def fix_os_paths(x):
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(x, str):
|
2016-04-10 01:17:34 +08:00
|
|
|
return x.replace('\\', '/')
|
|
|
|
elif isinstance(x, tuple):
|
|
|
|
return tuple(fix_os_paths(list(x)))
|
|
|
|
elif isinstance(x, list):
|
|
|
|
return [fix_os_paths(y) for y in x]
|
|
|
|
else:
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
class FilePathFieldTest(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_filepathfield_1(self):
|
2017-01-20 21:01:02 +08:00
|
|
|
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/'
|
2016-04-10 01:17:34 +08:00
|
|
|
self.assertTrue(fix_os_paths(path).endswith('/django/forms/'))
|
|
|
|
|
|
|
|
def test_filepathfield_2(self):
|
2017-01-20 21:01:02 +08:00
|
|
|
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/'
|
2016-04-10 01:17:34 +08:00
|
|
|
f = FilePathField(path=path)
|
|
|
|
f.choices = [p for p in f.choices if p[0].endswith('.py')]
|
|
|
|
f.choices.sort()
|
|
|
|
expected = [
|
|
|
|
('/django/forms/__init__.py', '__init__.py'),
|
|
|
|
('/django/forms/boundfield.py', 'boundfield.py'),
|
|
|
|
('/django/forms/fields.py', 'fields.py'),
|
|
|
|
('/django/forms/forms.py', 'forms.py'),
|
|
|
|
('/django/forms/formsets.py', 'formsets.py'),
|
|
|
|
('/django/forms/models.py', 'models.py'),
|
2016-12-28 06:00:56 +08:00
|
|
|
('/django/forms/renderers.py', 'renderers.py'),
|
2016-04-10 01:17:34 +08:00
|
|
|
('/django/forms/utils.py', 'utils.py'),
|
|
|
|
('/django/forms/widgets.py', 'widgets.py')
|
|
|
|
]
|
|
|
|
for exp, got in zip(expected, fix_os_paths(f.choices)):
|
2017-03-08 05:00:43 +08:00
|
|
|
with self.subTest(expected=exp):
|
|
|
|
self.assertEqual(exp[1], got[1])
|
|
|
|
self.assertTrue(got[0].endswith(exp[0]))
|
2016-04-10 01:17:34 +08:00
|
|
|
msg = "'Select a valid choice. fields.py is not one of the available choices.'"
|
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
|
|
|
f.clean('fields.py')
|
|
|
|
self.assertTrue(fix_os_paths(f.clean(path + 'fields.py')).endswith('/django/forms/fields.py'))
|
|
|
|
|
|
|
|
def test_filepathfield_3(self):
|
2017-01-20 21:01:02 +08:00
|
|
|
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/'
|
2016-09-17 00:15:00 +08:00
|
|
|
f = FilePathField(path=path, match=r'^.*?\.py$')
|
2016-04-10 01:17:34 +08:00
|
|
|
f.choices.sort()
|
|
|
|
expected = [
|
|
|
|
('/django/forms/__init__.py', '__init__.py'),
|
|
|
|
('/django/forms/boundfield.py', 'boundfield.py'),
|
|
|
|
('/django/forms/fields.py', 'fields.py'),
|
|
|
|
('/django/forms/forms.py', 'forms.py'),
|
|
|
|
('/django/forms/formsets.py', 'formsets.py'),
|
|
|
|
('/django/forms/models.py', 'models.py'),
|
2016-12-28 06:00:56 +08:00
|
|
|
('/django/forms/renderers.py', 'renderers.py'),
|
2016-04-10 01:17:34 +08:00
|
|
|
('/django/forms/utils.py', 'utils.py'),
|
|
|
|
('/django/forms/widgets.py', 'widgets.py')
|
|
|
|
]
|
|
|
|
for exp, got in zip(expected, fix_os_paths(f.choices)):
|
2017-03-08 05:00:43 +08:00
|
|
|
with self.subTest(expected=exp):
|
|
|
|
self.assertEqual(exp[1], got[1])
|
|
|
|
self.assertTrue(got[0].endswith(exp[0]))
|
2016-04-10 01:17:34 +08:00
|
|
|
|
|
|
|
def test_filepathfield_4(self):
|
2017-01-20 21:01:02 +08:00
|
|
|
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/'
|
2016-09-17 00:15:00 +08:00
|
|
|
f = FilePathField(path=path, recursive=True, match=r'^.*?\.py$')
|
2016-04-10 01:17:34 +08:00
|
|
|
f.choices.sort()
|
|
|
|
expected = [
|
|
|
|
('/django/forms/__init__.py', '__init__.py'),
|
|
|
|
('/django/forms/boundfield.py', 'boundfield.py'),
|
|
|
|
('/django/forms/fields.py', 'fields.py'),
|
|
|
|
('/django/forms/forms.py', 'forms.py'),
|
|
|
|
('/django/forms/formsets.py', 'formsets.py'),
|
|
|
|
('/django/forms/models.py', 'models.py'),
|
2016-12-28 06:00:56 +08:00
|
|
|
('/django/forms/renderers.py', 'renderers.py'),
|
2016-04-10 01:17:34 +08:00
|
|
|
('/django/forms/utils.py', 'utils.py'),
|
|
|
|
('/django/forms/widgets.py', 'widgets.py')
|
|
|
|
]
|
|
|
|
for exp, got in zip(expected, fix_os_paths(f.choices)):
|
2017-03-08 05:00:43 +08:00
|
|
|
with self.subTest(expected=exp):
|
|
|
|
self.assertEqual(exp[1], got[1])
|
|
|
|
self.assertTrue(got[0].endswith(exp[0]))
|
2016-04-10 01:17:34 +08:00
|
|
|
|
|
|
|
def test_filepathfield_folders(self):
|
2017-01-20 21:01:02 +08:00
|
|
|
path = os.path.abspath(os.path.join(__file__, '..', '..')) + '/tests/filepath_test_files/'
|
2016-04-10 01:17:34 +08:00
|
|
|
f = FilePathField(path=path, allow_folders=True, allow_files=False)
|
|
|
|
f.choices.sort()
|
|
|
|
expected = [
|
|
|
|
('/forms_tests/tests/filepath_test_files/directory', 'directory'),
|
|
|
|
]
|
|
|
|
actual = fix_os_paths(f.choices)
|
|
|
|
self.assertEqual(len(expected), len(actual))
|
|
|
|
for exp, got in zip(expected, actual):
|
2017-03-08 05:00:43 +08:00
|
|
|
with self.subTest(expected=exp):
|
|
|
|
self.assertEqual(exp[1], got[1])
|
|
|
|
self.assertTrue(got[0].endswith(exp[0]))
|
2016-04-10 01:17:34 +08:00
|
|
|
|
|
|
|
f = FilePathField(path=path, allow_folders=True, allow_files=True)
|
|
|
|
f.choices.sort()
|
|
|
|
expected = [
|
|
|
|
('/forms_tests/tests/filepath_test_files/.dot-file', '.dot-file'),
|
|
|
|
('/forms_tests/tests/filepath_test_files/1x1.bmp', '1x1.bmp'),
|
|
|
|
('/forms_tests/tests/filepath_test_files/1x1.png', '1x1.png'),
|
|
|
|
('/forms_tests/tests/filepath_test_files/directory', 'directory'),
|
|
|
|
('/forms_tests/tests/filepath_test_files/fake-image.jpg', 'fake-image.jpg'),
|
|
|
|
('/forms_tests/tests/filepath_test_files/real-text-file.txt', 'real-text-file.txt'),
|
|
|
|
]
|
|
|
|
|
|
|
|
actual = fix_os_paths(f.choices)
|
|
|
|
self.assertEqual(len(expected), len(actual))
|
|
|
|
for exp, got in zip(expected, actual):
|
2017-03-08 05:00:43 +08:00
|
|
|
with self.subTest(expected=exp):
|
|
|
|
self.assertEqual(exp[1], got[1])
|
|
|
|
self.assertTrue(got[0].endswith(exp[0]))
|