mirror of https://github.com/django/django.git
Created a dedicated test directory for FilePathField tests.
This commit is contained in:
parent
776cee9749
commit
71ff9a74cb
|
@ -1,11 +1,15 @@
|
|||
import os.path
|
||||
|
||||
from django.forms import FilePathField, ValidationError, forms
|
||||
from django.forms import FilePathField, ValidationError
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
PATH = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
def fix_os_paths(x):
|
||||
if isinstance(x, str):
|
||||
if x.startswith(PATH):
|
||||
x = x[len(PATH):]
|
||||
return x.replace('\\', '/')
|
||||
elif isinstance(x, tuple):
|
||||
return tuple(fix_os_paths(list(x)))
|
||||
|
@ -16,104 +20,72 @@ def fix_os_paths(x):
|
|||
|
||||
|
||||
class FilePathFieldTest(SimpleTestCase):
|
||||
expected_choices = [
|
||||
('/filepathfield_test_dir/__init__.py', '__init__.py'),
|
||||
('/filepathfield_test_dir/a.py', 'a.py'),
|
||||
('/filepathfield_test_dir/ab.py', 'ab.py'),
|
||||
('/filepathfield_test_dir/b.py', 'b.py'),
|
||||
('/filepathfield_test_dir/c/__init__.py', '__init__.py'),
|
||||
('/filepathfield_test_dir/c/d.py', 'd.py'),
|
||||
('/filepathfield_test_dir/c/e.py', 'e.py'),
|
||||
('/filepathfield_test_dir/c/f/__init__.py', '__init__.py'),
|
||||
('/filepathfield_test_dir/c/f/g.py', 'g.py'),
|
||||
]
|
||||
path = os.path.join(PATH, 'filepathfield_test_dir') + '/'
|
||||
|
||||
def test_filepathfield_1(self):
|
||||
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/'
|
||||
self.assertTrue(fix_os_paths(path).endswith('/django/forms/'))
|
||||
def assertChoices(self, field, expected_choices):
|
||||
self.assertEqual(fix_os_paths(field.choices), expected_choices)
|
||||
|
||||
def test_filepathfield_2(self):
|
||||
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/'
|
||||
f = FilePathField(path=path)
|
||||
f.choices = [p for p in f.choices if p[0].endswith('.py')]
|
||||
f.choices.sort()
|
||||
def test_fix_os_paths(self):
|
||||
self.assertEqual(fix_os_paths(self.path), ('/filepathfield_test_dir/'))
|
||||
|
||||
def test_no_options(self):
|
||||
f = FilePathField(path=self.path)
|
||||
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'),
|
||||
('/django/forms/renderers.py', 'renderers.py'),
|
||||
('/django/forms/utils.py', 'utils.py'),
|
||||
('/django/forms/widgets.py', 'widgets.py')
|
||||
]
|
||||
for exp, got in zip(expected, fix_os_paths(f.choices)):
|
||||
with self.subTest(expected=exp):
|
||||
self.assertEqual(exp[1], got[1])
|
||||
self.assertTrue(got[0].endswith(exp[0]))
|
||||
msg = "'Select a valid choice. fields.py is not one of the available choices.'"
|
||||
('/filepathfield_test_dir/README', 'README'),
|
||||
] + self.expected_choices[:4]
|
||||
self.assertChoices(f, expected)
|
||||
|
||||
def test_clean(self):
|
||||
f = FilePathField(path=self.path)
|
||||
msg = "'Select a valid choice. a.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'))
|
||||
f.clean('a.py')
|
||||
self.assertEqual(fix_os_paths(f.clean(self.path + 'a.py')), '/filepathfield_test_dir/a.py')
|
||||
|
||||
def test_filepathfield_3(self):
|
||||
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/'
|
||||
f = FilePathField(path=path, match=r'^.*?\.py$')
|
||||
def test_match(self):
|
||||
f = FilePathField(path=self.path, match=r'^.*?\.py$')
|
||||
self.assertChoices(f, self.expected_choices[:4])
|
||||
|
||||
def test_recursive(self):
|
||||
f = FilePathField(path=self.path, recursive=True, match=r'^.*?\.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'),
|
||||
('/django/forms/renderers.py', 'renderers.py'),
|
||||
('/django/forms/utils.py', 'utils.py'),
|
||||
('/django/forms/widgets.py', 'widgets.py')
|
||||
('/filepathfield_test_dir/__init__.py', '__init__.py'),
|
||||
('/filepathfield_test_dir/a.py', 'a.py'),
|
||||
('/filepathfield_test_dir/ab.py', 'ab.py'),
|
||||
('/filepathfield_test_dir/b.py', 'b.py'),
|
||||
('/filepathfield_test_dir/c/__init__.py', 'c/__init__.py'),
|
||||
('/filepathfield_test_dir/c/d.py', 'c/d.py'),
|
||||
('/filepathfield_test_dir/c/e.py', 'c/e.py'),
|
||||
('/filepathfield_test_dir/c/f/__init__.py', 'c/f/__init__.py'),
|
||||
('/filepathfield_test_dir/c/f/g.py', 'c/f/g.py'),
|
||||
]
|
||||
for exp, got in zip(expected, fix_os_paths(f.choices)):
|
||||
with self.subTest(expected=exp):
|
||||
self.assertEqual(exp[1], got[1])
|
||||
self.assertTrue(got[0].endswith(exp[0]))
|
||||
self.assertChoices(f, expected)
|
||||
|
||||
def test_filepathfield_4(self):
|
||||
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/'
|
||||
f = FilePathField(path=path, recursive=True, match=r'^.*?\.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'),
|
||||
('/django/forms/renderers.py', 'renderers.py'),
|
||||
('/django/forms/utils.py', 'utils.py'),
|
||||
('/django/forms/widgets.py', 'widgets.py')
|
||||
]
|
||||
for exp, got in zip(expected, fix_os_paths(f.choices)):
|
||||
with self.subTest(expected=exp):
|
||||
self.assertEqual(exp[1], got[1])
|
||||
self.assertTrue(got[0].endswith(exp[0]))
|
||||
def test_allow_folders(self):
|
||||
f = FilePathField(path=self.path, allow_folders=True, allow_files=False)
|
||||
self.assertChoices(f, [
|
||||
('/filepathfield_test_dir/c', 'c')],
|
||||
)
|
||||
|
||||
def test_filepathfield_folders(self):
|
||||
path = os.path.abspath(os.path.join(__file__, '..', '..')) + '/tests/filepath_test_files/'
|
||||
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):
|
||||
with self.subTest(expected=exp):
|
||||
self.assertEqual(exp[1], got[1])
|
||||
self.assertTrue(got[0].endswith(exp[0]))
|
||||
def test_recursive_no_folders_or_files(self):
|
||||
f = FilePathField(path=self.path, recursive=True, allow_folders=False, allow_files=False)
|
||||
self.assertChoices(f, [])
|
||||
|
||||
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):
|
||||
with self.subTest(expected=exp):
|
||||
self.assertEqual(exp[1], got[1])
|
||||
self.assertTrue(got[0].endswith(exp[0]))
|
||||
def test_recursive_folders_without_files(self):
|
||||
f = FilePathField(path=self.path, recursive=True, allow_folders=True, allow_files=False)
|
||||
self.assertChoices(f, [
|
||||
('/filepathfield_test_dir/c', 'c'),
|
||||
('/filepathfield_test_dir/c/f', 'c/f'),
|
||||
])
|
||||
|
|
Loading…
Reference in New Issue