Debian Codegraph @

tests/forms_tests/field_tests/test_filepathfield.py

raw
1 import os.path
2
3 from django.core.exceptions import ValidationError
4 from django.forms import FilePathField
5 from django.test import SimpleTestCase
6
7 PATH = os.path.dirname(os.path.abspath(__file__))
8
9
10 def fix_os_paths(x):
11 if isinstance(x, str):
12 return x.removeprefix(PATH).replace("\\", "/")
13 elif isinstance(x, tuple):
14 return tuple(fix_os_paths(list(x)))
15 elif isinstance(x, list):
16 return [fix_os_paths(y) for y in x]
17 else:
18 return x
19
20
21 class FilePathFieldTest(SimpleTestCase):
22 expected_choices = [
23 ("/filepathfield_test_dir/__init__.py", "__init__.py"),
24 ("/filepathfield_test_dir/a.py", "a.py"),
25 ("/filepathfield_test_dir/ab.py", "ab.py"),
26 ("/filepathfield_test_dir/b.py", "b.py"),
27 ("/filepathfield_test_dir/c/__init__.py", "__init__.py"),
28 ("/filepathfield_test_dir/c/d.py", "d.py"),
29 ("/filepathfield_test_dir/c/e.py", "e.py"),
30 ("/filepathfield_test_dir/c/f/__init__.py", "__init__.py"),
31 ("/filepathfield_test_dir/c/f/g.py", "g.py"),
32 ("/filepathfield_test_dir/h/__init__.py", "__init__.py"),
33 ("/filepathfield_test_dir/j/__init__.py", "__init__.py"),
34 ]
35 path = os.path.join(PATH, "filepathfield_test_dir") + "/"
36
37 def assertChoices(self, field, expected_choices):
38 self.assertEqual(fix_os_paths(field.choices), expected_choices)
39
40 def test_fix_os_paths(self):
41 self.assertEqual(fix_os_paths(self.path), ("/filepathfield_test_dir/"))
42
43 def test_nonexistent_path(self):
44 with self.assertRaisesMessage(FileNotFoundError, "nonexistent"):
45 FilePathField(path="nonexistent")
46
47 def test_no_options(self):
48 f = FilePathField(path=self.path)
49 expected = [
50 ("/filepathfield_test_dir/README", "README"),
51 ] + self.expected_choices[:4]
52 self.assertChoices(f, expected)
53
54 def test_clean(self):
55 f = FilePathField(path=self.path)
56 msg = "'Select a valid choice. a.py is not one of the available choices.'"
57 with self.assertRaisesMessage(ValidationError, msg):
58 f.clean("a.py")
59 self.assertEqual(
60 fix_os_paths(f.clean(self.path + "a.py")), "/filepathfield_test_dir/a.py"
61 )
62
63 def test_match(self):
64 f = FilePathField(path=self.path, match=r"^.*?\.py$")
65 self.assertChoices(f, self.expected_choices[:4])
66
67 def test_recursive(self):
68 f = FilePathField(path=self.path, recursive=True, match=r"^.*?\.py$")
69 expected = [
70 ("/filepathfield_test_dir/__init__.py", "__init__.py"),
71 ("/filepathfield_test_dir/a.py", "a.py"),
72 ("/filepathfield_test_dir/ab.py", "ab.py"),
73 ("/filepathfield_test_dir/b.py", "b.py"),
74 ("/filepathfield_test_dir/c/__init__.py", "c/__init__.py"),
75 ("/filepathfield_test_dir/c/d.py", "c/d.py"),
76 ("/filepathfield_test_dir/c/e.py", "c/e.py"),
77 ("/filepathfield_test_dir/c/f/__init__.py", "c/f/__init__.py"),
78 ("/filepathfield_test_dir/c/f/g.py", "c/f/g.py"),
79 ("/filepathfield_test_dir/h/__init__.py", "h/__init__.py"),
80 ("/filepathfield_test_dir/j/__init__.py", "j/__init__.py"),
81 ]
82 self.assertChoices(f, expected)
83
84 def test_allow_folders(self):
85 f = FilePathField(path=self.path, allow_folders=True, allow_files=False)
86 self.assertChoices(
87 f,
88 [
89 ("/filepathfield_test_dir/c", "c"),
90 ("/filepathfield_test_dir/h", "h"),
91 ("/filepathfield_test_dir/j", "j"),
92 ],
93 )
94
95 def test_recursive_no_folders_or_files(self):
96 f = FilePathField(
97 path=self.path, recursive=True, allow_folders=False, allow_files=False
98 )
99 self.assertChoices(f, [])
100
101 def test_recursive_folders_without_files(self):
102 f = FilePathField(
103 path=self.path, recursive=True, allow_folders=True, allow_files=False
104 )
105 self.assertChoices(
106 f,
107 [
108 ("/filepathfield_test_dir/c", "c"),
109 ("/filepathfield_test_dir/h", "h"),
110 ("/filepathfield_test_dir/j", "j"),
111 ("/filepathfield_test_dir/c/f", "c/f"),
112 ],
113 )