@@ -47,53 +47,123 @@ def test_to_representation(self, image_upload_file, settings):
47
47
48
48
profile = models .Profile .objects .create (picture = image_upload_file )
49
49
serializer = ProfileSerializer (profile )
50
+
50
51
assert serializer .data ["picture" ] == {
51
- "null" : {
52
- "WEBP" : {
53
- "800" : "/media/testapp/profile/image/800w.webp" ,
54
- "100" : "/media/testapp/profile/image/100w.webp" ,
55
- "200" : "/media/testapp/profile/image/200w.webp" ,
56
- "300" : "/media/testapp/profile/image/300w.webp" ,
57
- "400" : "/media/testapp/profile/image/400w.webp" ,
58
- "500" : "/media/testapp/profile/image/500w.webp" ,
59
- "600" : "/media/testapp/profile/image/600w.webp" ,
60
- "700" : "/media/testapp/profile/image/700w.webp" ,
61
- }
62
- },
63
- "1/1" : {
64
- "WEBP" : {
65
- "800" : "/media/testapp/profile/image/1/800w.webp" ,
66
- "100" : "/media/testapp/profile/image/1/100w.webp" ,
67
- "200" : "/media/testapp/profile/image/1/200w.webp" ,
68
- "300" : "/media/testapp/profile/image/1/300w.webp" ,
69
- "400" : "/media/testapp/profile/image/1/400w.webp" ,
70
- "500" : "/media/testapp/profile/image/1/500w.webp" ,
71
- "600" : "/media/testapp/profile/image/1/600w.webp" ,
72
- "700" : "/media/testapp/profile/image/1/700w.webp" ,
73
- }
74
- },
75
- "3/2" : {
76
- "WEBP" : {
77
- "800" : "/media/testapp/profile/image/3_2/800w.webp" ,
78
- "100" : "/media/testapp/profile/image/3_2/100w.webp" ,
79
- "200" : "/media/testapp/profile/image/3_2/200w.webp" ,
80
- "300" : "/media/testapp/profile/image/3_2/300w.webp" ,
81
- "400" : "/media/testapp/profile/image/3_2/400w.webp" ,
82
- "500" : "/media/testapp/profile/image/3_2/500w.webp" ,
83
- "600" : "/media/testapp/profile/image/3_2/600w.webp" ,
84
- "700" : "/media/testapp/profile/image/3_2/700w.webp" ,
85
- }
52
+ "url" : "/media/testapp/profile/image.jpg" ,
53
+ "width" : 800 ,
54
+ "height" : 800 ,
55
+ "ratios" : {
56
+ "null" : {
57
+ "sources" : {
58
+ "image/webp" : {
59
+ "800" : "/media/testapp/profile/image/800w.webp" ,
60
+ "100" : "/media/testapp/profile/image/100w.webp" ,
61
+ "200" : "/media/testapp/profile/image/200w.webp" ,
62
+ "300" : "/media/testapp/profile/image/300w.webp" ,
63
+ "400" : "/media/testapp/profile/image/400w.webp" ,
64
+ "500" : "/media/testapp/profile/image/500w.webp" ,
65
+ "600" : "/media/testapp/profile/image/600w.webp" ,
66
+ "700" : "/media/testapp/profile/image/700w.webp" ,
67
+ }
68
+ }
69
+ },
70
+ "1/1" : {
71
+ "sources" : {
72
+ "image/webp" : {
73
+ "800" : "/media/testapp/profile/image/1/800w.webp" ,
74
+ "100" : "/media/testapp/profile/image/1/100w.webp" ,
75
+ "200" : "/media/testapp/profile/image/1/200w.webp" ,
76
+ "300" : "/media/testapp/profile/image/1/300w.webp" ,
77
+ "400" : "/media/testapp/profile/image/1/400w.webp" ,
78
+ "500" : "/media/testapp/profile/image/1/500w.webp" ,
79
+ "600" : "/media/testapp/profile/image/1/600w.webp" ,
80
+ "700" : "/media/testapp/profile/image/1/700w.webp" ,
81
+ }
82
+ }
83
+ },
84
+ "3/2" : {
85
+ "sources" : {
86
+ "image/webp" : {
87
+ "800" : "/media/testapp/profile/image/3_2/800w.webp" ,
88
+ "100" : "/media/testapp/profile/image/3_2/100w.webp" ,
89
+ "200" : "/media/testapp/profile/image/3_2/200w.webp" ,
90
+ "300" : "/media/testapp/profile/image/3_2/300w.webp" ,
91
+ "400" : "/media/testapp/profile/image/3_2/400w.webp" ,
92
+ "500" : "/media/testapp/profile/image/3_2/500w.webp" ,
93
+ "600" : "/media/testapp/profile/image/3_2/600w.webp" ,
94
+ "700" : "/media/testapp/profile/image/3_2/700w.webp" ,
95
+ }
96
+ }
97
+ },
98
+ "16/9" : {
99
+ "sources" : {
100
+ "image/webp" : {
101
+ "800" : "/media/testapp/profile/image/16_9/800w.webp" ,
102
+ "100" : "/media/testapp/profile/image/16_9/100w.webp" ,
103
+ "200" : "/media/testapp/profile/image/16_9/200w.webp" ,
104
+ "300" : "/media/testapp/profile/image/16_9/300w.webp" ,
105
+ "400" : "/media/testapp/profile/image/16_9/400w.webp" ,
106
+ "500" : "/media/testapp/profile/image/16_9/500w.webp" ,
107
+ "600" : "/media/testapp/profile/image/16_9/600w.webp" ,
108
+ "700" : "/media/testapp/profile/image/16_9/700w.webp" ,
109
+ }
110
+ }
111
+ },
86
112
},
87
- "16/9" : {
88
- "WEBP" : {
89
- "800" : "/media/testapp/profile/image/16_9/800w.webp" ,
90
- "100" : "/media/testapp/profile/image/16_9/100w.webp" ,
91
- "200" : "/media/testapp/profile/image/16_9/200w.webp" ,
92
- "300" : "/media/testapp/profile/image/16_9/300w.webp" ,
93
- "400" : "/media/testapp/profile/image/16_9/400w.webp" ,
94
- "500" : "/media/testapp/profile/image/16_9/500w.webp" ,
95
- "600" : "/media/testapp/profile/image/16_9/600w.webp" ,
96
- "700" : "/media/testapp/profile/image/16_9/700w.webp" ,
113
+ }
114
+
115
+ @pytest .mark .django_db
116
+ def test_to_representation__with_aspect_ratios (
117
+ self , rf , image_upload_file , settings
118
+ ):
119
+ settings .PICTURES ["USE_PLACEHOLDERS" ] = False
120
+
121
+ profile = models .Profile .objects .create (picture = image_upload_file )
122
+ request = rf .get ("/" )
123
+ request .GET ._mutable = True
124
+ request .GET ["picture_ratio" ] = "1/1"
125
+ request .GET ["picture_l" ] = "3"
126
+ request .GET ["picture_m" ] = "4"
127
+ serializer = ProfileSerializer (profile , context = {"request" : request })
128
+
129
+ assert serializer .data ["picture" ] == {
130
+ "url" : "/media/testapp/profile/image.jpg" ,
131
+ "width" : 800 ,
132
+ "height" : 800 ,
133
+ "ratios" : {
134
+ "1/1" : {
135
+ "sources" : {
136
+ "image/webp" : {
137
+ "800" : "/media/testapp/profile/image/1/800w.webp" ,
138
+ "100" : "/media/testapp/profile/image/1/100w.webp" ,
139
+ "200" : "/media/testapp/profile/image/1/200w.webp" ,
140
+ "300" : "/media/testapp/profile/image/1/300w.webp" ,
141
+ "400" : "/media/testapp/profile/image/1/400w.webp" ,
142
+ "500" : "/media/testapp/profile/image/1/500w.webp" ,
143
+ "600" : "/media/testapp/profile/image/1/600w.webp" ,
144
+ "700" : "/media/testapp/profile/image/1/700w.webp" ,
145
+ }
146
+ },
147
+ "media" : "(min-width: 0px) and (max-width: 991px) 100vw, (min-width: 992px) and (max-width: 1199px) 33vw, 25vw" ,
97
148
}
98
149
},
99
150
}
151
+
152
+ @pytest .mark .django_db
153
+ def test_to_representation__raise_value_error (
154
+ self , rf , image_upload_file , settings
155
+ ):
156
+ settings .PICTURES ["USE_PLACEHOLDERS" ] = False
157
+
158
+ profile = models .Profile .objects .create (picture = image_upload_file )
159
+ request = rf .get ("/" )
160
+ request .GET ._mutable = True
161
+ request .GET ["picture_ratio" ] = "21/11"
162
+ request .GET ["picture_l" ] = "3"
163
+ request .GET ["picture_m" ] = "4"
164
+ serializer = ProfileSerializer (profile , context = {"request" : request })
165
+
166
+ with pytest .raises (ValueError ) as e :
167
+ serializer .data ["picture" ]
168
+
169
+ assert str (e .value ) == "Invalid ratio: 21/11. Choices are: 1/1, 3/2, 16/9"
0 commit comments