@@ -167,3 +167,86 @@ def test_to_representation__raise_value_error(
167167 serializer .data ["picture" ]
168168
169169 assert str (e .value ) == "Invalid ratio: 21/11. Choices are: 1/1, 3/2, 16/9"
170+
171+ @pytest .mark .django_db
172+ def test_to_representation__with_container (self , rf , image_upload_file , settings ):
173+ settings .PICTURES ["USE_PLACEHOLDERS" ] = False
174+
175+ profile = models .Profile .objects .create (picture = image_upload_file )
176+ request = rf .get ("/" )
177+ request .GET ._mutable = True
178+ request .GET ["picture_ratio" ] = "16/9"
179+ request .GET ["picture_container" ] = "1200"
180+ serializer = ProfileSerializer (profile , context = {"request" : request })
181+ assert serializer .data ["picture" ] == {
182+ "url" : "/media/testapp/profile/image.jpg" ,
183+ "width" : 800 ,
184+ "height" : 800 ,
185+ "ratios" : {
186+ "16/9" : {
187+ "sources" : {
188+ "image/webp" : {
189+ "800" : "/media/testapp/profile/image/16_9/800w.webp" ,
190+ "100" : "/media/testapp/profile/image/16_9/100w.webp" ,
191+ "200" : "/media/testapp/profile/image/16_9/200w.webp" ,
192+ "300" : "/media/testapp/profile/image/16_9/300w.webp" ,
193+ "400" : "/media/testapp/profile/image/16_9/400w.webp" ,
194+ "500" : "/media/testapp/profile/image/16_9/500w.webp" ,
195+ "600" : "/media/testapp/profile/image/16_9/600w.webp" ,
196+ "700" : "/media/testapp/profile/image/16_9/700w.webp" ,
197+ }
198+ },
199+ "media" : "(min-width: 0px) and (max-width: 1199px) 100vw, 1200px" ,
200+ }
201+ },
202+ }
203+
204+ @pytest .mark .django_db
205+ def test_to_representation__without_container (
206+ self , rf , image_upload_file , settings
207+ ):
208+ settings .PICTURES ["USE_PLACEHOLDERS" ] = False
209+
210+ profile = models .Profile .objects .create (picture = image_upload_file )
211+ request = rf .get ("/" )
212+ request .GET ._mutable = True
213+ request .GET ["picture_ratio" ] = "16/9"
214+ serializer = ProfileSerializer (profile , context = {"request" : request })
215+ assert serializer .data ["picture" ] == {
216+ "url" : "/media/testapp/profile/image.jpg" ,
217+ "width" : 800 ,
218+ "height" : 800 ,
219+ "ratios" : {
220+ "16/9" : {
221+ "sources" : {
222+ "image/webp" : {
223+ "800" : "/media/testapp/profile/image/16_9/800w.webp" ,
224+ "100" : "/media/testapp/profile/image/16_9/100w.webp" ,
225+ "200" : "/media/testapp/profile/image/16_9/200w.webp" ,
226+ "300" : "/media/testapp/profile/image/16_9/300w.webp" ,
227+ "400" : "/media/testapp/profile/image/16_9/400w.webp" ,
228+ "500" : "/media/testapp/profile/image/16_9/500w.webp" ,
229+ "600" : "/media/testapp/profile/image/16_9/600w.webp" ,
230+ "700" : "/media/testapp/profile/image/16_9/700w.webp" ,
231+ }
232+ },
233+ "media" : "100vw" ,
234+ }
235+ },
236+ }
237+
238+ @pytest .mark .django_db
239+ def test_to_representation__with_false_str_container (
240+ self , rf , image_upload_file , settings
241+ ):
242+ settings .PICTURES ["USE_PLACEHOLDERS" ] = False
243+
244+ profile = models .Profile .objects .create (picture = image_upload_file )
245+ request = rf .get ("/" )
246+ request .GET ._mutable = True
247+ request .GET ["picture_ratio" ] = "16/9"
248+ request .GET ["picture_container" ] = "not_a_number"
249+ serializer = ProfileSerializer (profile , context = {"request" : request })
250+ with pytest .raises (ValueError ) as e :
251+ serializer .data ["picture" ]
252+ assert str (e .value ) == "Container width is not a number: not_a_number"
0 commit comments