5
5
6
6
import os .path
7
7
8
- from models import TestModel
9
- from forms import TestModelForm
8
+ from models import TestModel , TestModelNoValidate
9
+ from forms import TestModelForm , TestModelNoValidateForm
10
10
11
11
class ValidatedFileFieldTest (TestCase ):
12
12
13
13
SAMPLE_FILES_PATH = 'validatedfile/tests/sample_files'
14
14
15
15
16
- def _get_sample_file (self , filename ):
17
- path = os .path .join (self .SAMPLE_FILES_PATH , filename )
18
- return open (path )
19
-
20
-
21
- def _check_file_url (self , filefield , filename ):
22
- url = os .path .join (settings .MEDIA_URL , filefield .field .upload_to , filename )
23
- self .assertEqual (filefield .url , url )
24
-
25
-
26
- def _get_file_url (self , filename ):
27
- return os .path .join (MEDIA_ROOT , prefix , filename )
28
-
29
-
30
16
def test_create_empty_instance (self ):
31
17
instance = TestModel .objects .create ()
32
18
instance .save ()
@@ -40,11 +26,12 @@ def test_create_instance_with_file(self):
40
26
41
27
self ._check_file_url (instance .the_file , 'the_file.png' )
42
28
29
+ from ipdb import set_trace ; set_trace ()
43
30
instance .the_file .delete ()
44
31
instance .delete ()
45
32
46
33
47
- def test_form (self ):
34
+ def test_form_ok (self ):
48
35
uploaded_file = SimpleUploadedFile (
49
36
name = 'the_file.png' ,
50
37
content = self ._get_sample_file ('image2k.png' ).read (),
@@ -60,26 +47,80 @@ def test_form(self):
60
47
instance .delete ()
61
48
62
49
50
+ def test_form_invalid_size (self ):
51
+ uploaded_file = SimpleUploadedFile (
52
+ name = 'the_file.png' ,
53
+ content = self ._get_sample_file ('image15k.png' ).read (),
54
+ content_type = 'image/png' ,
55
+ )
56
+ form = TestModelForm (data = {}, files = {'the_file' : uploaded_file })
57
+ self .assertFalse (form .is_valid ())
58
+ self .assertEqual (len (form .errors ), 1 )
59
+ self .assertEqual (len (form .errors ['the_file' ]), 1 )
60
+
61
+
63
62
def test_form_invalid_filetype (self ):
64
63
uploaded_file = SimpleUploadedFile (
65
64
name = 'the_file.pdf' ,
66
- content = self ._get_sample_file ('document .pdf' ).read (),
67
- content_type = 'apllication /pdf' ,
65
+ content = self ._get_sample_file ('document1k .pdf' ).read (),
66
+ content_type = 'application /pdf' ,
68
67
)
69
68
form = TestModelForm (data = {}, files = {'the_file' : uploaded_file })
70
69
self .assertFalse (form .is_valid ())
71
70
self .assertEqual (len (form .errors ), 1 )
72
71
self .assertEqual (len (form .errors ['the_file' ]), 1 )
73
72
74
73
75
- def test_form_invalid_size (self ):
74
+ def test_form_invalid_filetype_and_size (self ):
76
75
uploaded_file = SimpleUploadedFile (
77
76
name = 'the_file.pdf' ,
78
- content = self ._get_sample_file ('image15k.png' ).read (),
77
+ content = self ._get_sample_file ('document15k.pdf' ).read (),
78
+ content_type = 'application/pdf' ,
79
+ )
80
+ form = TestModelForm (data = {}, files = {'the_file' : uploaded_file })
81
+ self .assertFalse (form .is_valid ())
82
+ self .assertEqual (len (form .errors ), 1 )
83
+ self .assertEqual (len (form .errors ['the_file' ]), 1 )
84
+
85
+
86
+ def test_form_fake_filetype (self ):
87
+ uploaded_file = SimpleUploadedFile (
88
+ name = 'the_file.png' ,
89
+ content = self ._get_sample_file ('document1k.pdf' ).read (),
79
90
content_type = 'image/png' ,
80
91
)
81
92
form = TestModelForm (data = {}, files = {'the_file' : uploaded_file })
82
93
self .assertFalse (form .is_valid ())
83
94
self .assertEqual (len (form .errors ), 1 )
84
95
self .assertEqual (len (form .errors ['the_file' ]), 1 )
85
96
97
+
98
+ def test_form_no_validate (self ):
99
+ uploaded_file = SimpleUploadedFile (
100
+ name = 'the_file.pdf' ,
101
+ content = self ._get_sample_file ('document15k.pdf' ).read (),
102
+ content_type = 'application/pdf' ,
103
+ )
104
+ form = TestModelNoValidateForm (data = {}, files = {'the_file' : uploaded_file })
105
+ self .assertTrue (form .is_valid ())
106
+ instance = form .save ()
107
+
108
+ self ._check_file_url (instance .the_file , 'the_file.pdf' )
109
+
110
+ instance .the_file .delete ()
111
+ instance .delete ()
112
+
113
+
114
+ def _get_sample_file (self , filename ):
115
+ path = os .path .join (self .SAMPLE_FILES_PATH , filename )
116
+ return open (path )
117
+
118
+
119
+ def _check_file_url (self , filefield , filename ):
120
+ url = os .path .join (settings .MEDIA_URL , filefield .field .upload_to , filename )
121
+ self .assertEqual (filefield .url , url )
122
+
123
+
124
+ def _get_file_url (self , filename ):
125
+ return os .path .join (MEDIA_ROOT , prefix , filename )
126
+
0 commit comments