2016-04-10 01:17:34 +08:00
|
|
|
import os.path
|
|
|
|
|
2017-07-18 04:40:34 +08:00
|
|
|
from django.forms import FilePathField, ValidationError
|
2016-04-10 01:17:34 +08:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
2017-07-18 04:40:34 +08:00
|
|
|
PATH = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
2016-04-10 01:17:34 +08:00
|
|
|
|
|
|
|
def fix_os_paths(x):
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(x, str):
|
2017-07-18 04:40:34 +08:00
|
|
|
if x.startswith(PATH):
|
|
|
|
x = x[len(PATH):]
|
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):
|
2017-07-18 04:40:34 +08:00
|
|
|
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') + '/'
|
2016-04-10 01:17:34 +08:00
|
|
|
|
2017-07-18 04:40:34 +08:00
|
|
|
def assertChoices(self, field, expected_choices):
|
|
|
|
self.assertEqual(fix_os_paths(field.choices), expected_choices)
|
2016-04-10 01:17:34 +08:00
|
|
|
|
2017-07-18 04:40:34 +08:00
|
|
|
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)
|
2016-04-10 01:17:34 +08:00
|
|
|
expected = [
|
2017-07-18 04:40:34 +08:00
|
|
|
('/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.'"
|
2016-04-10 01:17:34 +08:00
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
2017-07-18 04:40:34 +08:00
|
|
|
f.clean('a.py')
|
|
|
|
self.assertEqual(fix_os_paths(f.clean(self.path + 'a.py')), '/filepathfield_test_dir/a.py')
|
2016-04-10 01:17:34 +08:00
|
|
|
|
2017-07-18 04:40:34 +08:00
|
|
|
def test_match(self):
|
|
|
|
f = FilePathField(path=self.path, match=r'^.*?\.py$')
|
|
|
|
self.assertChoices(f, self.expected_choices[:4])
|
2016-04-10 01:17:34 +08:00
|
|
|
|
2017-07-18 04:40:34 +08:00
|
|
|
def test_recursive(self):
|
|
|
|
f = FilePathField(path=self.path, recursive=True, match=r'^.*?\.py$')
|
2016-04-10 01:17:34 +08:00
|
|
|
f.choices.sort()
|
|
|
|
expected = [
|
2017-07-18 04:40:34 +08:00
|
|
|
('/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'),
|
2016-04-10 01:17:34 +08:00
|
|
|
]
|
2017-07-18 04:40:34 +08:00
|
|
|
self.assertChoices(f, expected)
|
2016-04-10 01:17:34 +08:00
|
|
|
|
2017-07-18 04:40:34 +08:00
|
|
|
def test_allow_folders(self):
|
|
|
|
f = FilePathField(path=self.path, allow_folders=True, allow_files=False)
|
|
|
|
self.assertChoices(f, [
|
|
|
|
('/filepathfield_test_dir/c', 'c')],
|
|
|
|
)
|
2016-04-10 01:17:34 +08:00
|
|
|
|
2017-07-18 04:40:34 +08:00
|
|
|
def test_recursive_no_folders_or_files(self):
|
|
|
|
f = FilePathField(path=self.path, recursive=True, allow_folders=False, allow_files=False)
|
|
|
|
self.assertChoices(f, [])
|
2016-04-10 01:17:34 +08:00
|
|
|
|
2017-07-18 04:40:34 +08:00
|
|
|
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'),
|
|
|
|
])
|