summaryrefslogtreecommitdiff
path: root/tests/forms_tests/field_tests/test_filepathfield.py
diff options
context:
space:
mode:
authorSrinivas Reddy Thatiparthy <srinivasreddy@users.noreply.github.com>2017-07-18 02:10:34 +0530
committerTim Graham <timograham@gmail.com>2017-07-17 16:40:34 -0400
commit71ff9a74cb5fb8ac2fd8346f711df4bb6e07000f (patch)
treeb29cd436e5d933b6e971f1b1a6db679116f23b39 /tests/forms_tests/field_tests/test_filepathfield.py
parent776cee974933677730aef177eb5b5bdec3749c80 (diff)
Created a dedicated test directory for FilePathField tests.
Diffstat (limited to 'tests/forms_tests/field_tests/test_filepathfield.py')
-rw-r--r--tests/forms_tests/field_tests/test_filepathfield.py152
1 files changed, 62 insertions, 90 deletions
diff --git a/tests/forms_tests/field_tests/test_filepathfield.py b/tests/forms_tests/field_tests/test_filepathfield.py
index 56bf0e9fda..cc1cca3e36 100644
--- a/tests/forms_tests/field_tests/test_filepathfield.py
+++ b/tests/forms_tests/field_tests/test_filepathfield.py
@@ -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$')
- 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_match(self):
+ f = FilePathField(path=self.path, match=r'^.*?\.py$')
+ self.assertChoices(f, self.expected_choices[:4])
- def test_filepathfield_4(self):
- path = os.path.dirname(os.path.abspath(forms.__file__)) + '/'
- f = FilePathField(path=path, recursive=True, match=r'^.*?\.py$')
+ 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_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_allow_folders(self):
+ f = FilePathField(path=self.path, allow_folders=True, allow_files=False)
+ self.assertChoices(f, [
+ ('/filepathfield_test_dir/c', 'c')],
+ )
- 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'),
- ]
+ def test_recursive_no_folders_or_files(self):
+ f = FilePathField(path=self.path, recursive=True, allow_folders=False, allow_files=False)
+ self.assertChoices(f, [])
- 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'),
+ ])