File: test_filepathfield.py

package info (click to toggle)
python-django 1%3A1.11.29-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,428 kB
  • sloc: python: 220,776; javascript: 13,523; makefile: 209; xml: 201; sh: 64
file content (124 lines) | stat: -rw-r--r-- 5,443 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from __future__ import unicode_literals

import os.path

from django.forms import FilePathField, ValidationError, forms
from django.test import SimpleTestCase
from django.utils import six
from django.utils._os import upath


def fix_os_paths(x):
    if isinstance(x, six.string_types):
        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):

    def test_filepathfield_1(self):
        path = os.path.abspath(upath(forms.__file__))
        path = os.path.dirname(path) + '/'
        self.assertTrue(fix_os_paths(path).endswith('/django/forms/'))

    def test_filepathfield_2(self):
        path = upath(forms.__file__)
        path = os.path.dirname(os.path.abspath(path)) + '/'
        f = FilePathField(path=path)
        f.choices = [p for p in f.choices if p[0].endswith('.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)):
            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):
            f.clean('fields.py')
        self.assertTrue(fix_os_paths(f.clean(path + 'fields.py')).endswith('/django/forms/fields.py'))

    def test_filepathfield_3(self):
        path = upath(forms.__file__)
        path = os.path.dirname(os.path.abspath(path)) + '/'
        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)):
            self.assertEqual(exp[1], got[1])
            self.assertTrue(got[0].endswith(exp[0]))

    def test_filepathfield_4(self):
        path = os.path.abspath(upath(forms.__file__))
        path = os.path.dirname(path) + '/'
        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/extras/__init__.py', 'extras/__init__.py'),
            ('/django/forms/extras/widgets.py', 'extras/widgets.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)):
            self.assertEqual(exp[1], got[1])
            self.assertTrue(got[0].endswith(exp[0]))

    def test_filepathfield_folders(self):
        path = os.path.abspath(os.path.join(upath(__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):
            self.assertEqual(exp[1], got[1])
            self.assertTrue(got[0].endswith(exp[0]))

        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):
            self.assertEqual(exp[1], got[1])
            self.assertTrue(got[0].endswith(exp[0]))