Created a dedicated test directory for FilePathField tests.

This commit is contained in:
Srinivas Reddy Thatiparthy 2017-07-18 02:10:34 +05:30 committed by Tim Graham
parent 776cee9749
commit 71ff9a74cb
11 changed files with 63 additions and 91 deletions

View File

@ -1,11 +1,15 @@
import os.path import os.path
from django.forms import FilePathField, ValidationError, forms from django.forms import FilePathField, ValidationError
from django.test import SimpleTestCase from django.test import SimpleTestCase
PATH = os.path.dirname(os.path.abspath(__file__))
def fix_os_paths(x): def fix_os_paths(x):
if isinstance(x, str): if isinstance(x, str):
if x.startswith(PATH):
x = x[len(PATH):]
return x.replace('\\', '/') return x.replace('\\', '/')
elif isinstance(x, tuple): elif isinstance(x, tuple):
return tuple(fix_os_paths(list(x))) return tuple(fix_os_paths(list(x)))
@ -16,104 +20,72 @@ def fix_os_paths(x):
class FilePathFieldTest(SimpleTestCase): 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): def assertChoices(self, field, expected_choices):
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/' self.assertEqual(fix_os_paths(field.choices), expected_choices)
self.assertTrue(fix_os_paths(path).endswith('/django/forms/'))
def test_filepathfield_2(self): def test_fix_os_paths(self):
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/' self.assertEqual(fix_os_paths(self.path), ('/filepathfield_test_dir/'))
f = FilePathField(path=path)
f.choices = [p for p in f.choices if p[0].endswith('.py')] def test_no_options(self):
f.choices.sort() f = FilePathField(path=self.path)
expected = [ expected = [
('/django/forms/__init__.py', '__init__.py'), ('/filepathfield_test_dir/README', 'README'),
('/django/forms/boundfield.py', 'boundfield.py'), ] + self.expected_choices[:4]
('/django/forms/fields.py', 'fields.py'), self.assertChoices(f, expected)
('/django/forms/forms.py', 'forms.py'),
('/django/forms/formsets.py', 'formsets.py'), def test_clean(self):
('/django/forms/models.py', 'models.py'), f = FilePathField(path=self.path)
('/django/forms/renderers.py', 'renderers.py'), msg = "'Select a valid choice. a.py is not one of the available choices.'"
('/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.'"
with self.assertRaisesMessage(ValidationError, msg): with self.assertRaisesMessage(ValidationError, msg):
f.clean('fields.py') f.clean('a.py')
self.assertTrue(fix_os_paths(f.clean(path + 'fields.py')).endswith('/django/forms/fields.py')) self.assertEqual(fix_os_paths(f.clean(self.path + 'a.py')), '/filepathfield_test_dir/a.py')
def test_filepathfield_3(self): def test_match(self):
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/' f = FilePathField(path=self.path, match=r'^.*?\.py$')
f = FilePathField(path=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() f.choices.sort()
expected = [ expected = [
('/django/forms/__init__.py', '__init__.py'), ('/filepathfield_test_dir/__init__.py', '__init__.py'),
('/django/forms/boundfield.py', 'boundfield.py'), ('/filepathfield_test_dir/a.py', 'a.py'),
('/django/forms/fields.py', 'fields.py'), ('/filepathfield_test_dir/ab.py', 'ab.py'),
('/django/forms/forms.py', 'forms.py'), ('/filepathfield_test_dir/b.py', 'b.py'),
('/django/forms/formsets.py', 'formsets.py'), ('/filepathfield_test_dir/c/__init__.py', 'c/__init__.py'),
('/django/forms/models.py', 'models.py'), ('/filepathfield_test_dir/c/d.py', 'c/d.py'),
('/django/forms/renderers.py', 'renderers.py'), ('/filepathfield_test_dir/c/e.py', 'c/e.py'),
('/django/forms/utils.py', 'utils.py'), ('/filepathfield_test_dir/c/f/__init__.py', 'c/f/__init__.py'),
('/django/forms/widgets.py', 'widgets.py') ('/filepathfield_test_dir/c/f/g.py', 'c/f/g.py'),
] ]
for exp, got in zip(expected, fix_os_paths(f.choices)): self.assertChoices(f, expected)
with self.subTest(expected=exp):
self.assertEqual(exp[1], got[1])
self.assertTrue(got[0].endswith(exp[0]))
def test_filepathfield_4(self): def test_allow_folders(self):
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/' f = FilePathField(path=self.path, allow_folders=True, allow_files=False)
f = FilePathField(path=path, recursive=True, match=r'^.*?\.py$') self.assertChoices(f, [
f.choices.sort() ('/filepathfield_test_dir/c', 'c')],
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_filepathfield_folders(self): def test_recursive_no_folders_or_files(self):
path = os.path.abspath(os.path.join(__file__, '..', '..')) + '/tests/filepath_test_files/' f = FilePathField(path=self.path, recursive=True, allow_folders=False, allow_files=False)
f = FilePathField(path=path, allow_folders=True, allow_files=False) self.assertChoices(f, [])
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]))
f = FilePathField(path=path, allow_folders=True, allow_files=True) def test_recursive_folders_without_files(self):
f.choices.sort() f = FilePathField(path=self.path, recursive=True, allow_folders=True, allow_files=False)
expected = [ self.assertChoices(f, [
('/forms_tests/tests/filepath_test_files/.dot-file', '.dot-file'), ('/filepathfield_test_dir/c', 'c'),
('/forms_tests/tests/filepath_test_files/1x1.bmp', '1x1.bmp'), ('/filepathfield_test_dir/c/f', 'c/f'),
('/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]))