1010
1111from helper import *
1212
13+
1314class TinifySourceWithInvalidApiKey (TestHelper ):
1415 def setUp (self ):
1516 super (type (self ), self ).setUp ()
@@ -55,6 +56,10 @@ def return_file(request, uri, headers):
5556 response = b'copyrighted file'
5657 elif 'resize' in data :
5758 response = b'small file'
59+ elif 'convert' in data :
60+ response = b'converted file'
61+ elif 'transform' in data :
62+ response = b'transformed file'
5863 else :
5964 response = b'compressed file'
6065 return (200 , headers , response )
@@ -124,6 +129,22 @@ def test_resize_should_return_source_with_data(self):
124129 self .assertEqual (b'small file' , Source .from_buffer ('png file' ).resize (width = 400 ).to_buffer ())
125130 self .assertJsonEqual ('{"resize":{"width":400}}' , httpretty .last_request ().body .decode ('utf-8' ))
126131
132+ def test_transform_should_return_source (self ):
133+ self .assertIsInstance (Source .from_buffer ('png file' ).transform (background = 'black' ), Source )
134+ self .assertEqual (b'png file' , httpretty .last_request ().body )
135+
136+ def test_transform_should_return_source_with_data (self ):
137+ self .assertEqual (b'transformed file' , Source .from_buffer ('png file' ).transform (background = 'black' ).to_buffer ())
138+ self .assertJsonEqual ('{"transform":{"background":"black"}}' , httpretty .last_request ().body .decode ('utf-8' ))
139+
140+ def test_convert_should_return_source (self ):
141+ self .assertIsInstance (Source .from_buffer ('png file' ).resize (width = 400 ).convert (type = ['image/webp' ]), Source )
142+ self .assertEqual (b'png file' , httpretty .last_request ().body )
143+
144+ def test_convert_should_return_source_with_data (self ):
145+ self .assertEqual (b'converted file' , Source .from_buffer ('png file' ).convert (type = 'image/jpg' ).to_buffer ())
146+ self .assertJsonEqual ('{"convert": {"type": "image/jpg"}}' , httpretty .last_request ().body .decode ('utf-8' ))
147+
127148 def test_store_should_return_result_meta (self ):
128149 self .assertIsInstance (Source .from_buffer ('png file' ).store (service = 's3' ), ResultMeta )
129150 self .assertJsonEqual ('{"store":{"service":"s3"}}' , httpretty .last_request ().body .decode ('utf-8' ))
@@ -150,4 +171,14 @@ def test_to_file_with_file_object_should_store_image_data(self):
150171 with create_named_tmpfile () as name :
151172 Source .from_buffer ('png file' ).to_file (name )
152173 with open (name , 'rb' ) as f :
153- self .assertEqual (b'compressed file' , f .read ())
174+ self .assertEqual (b'compressed file' , f .read ())
175+
176+ def test_all_options_together (self ):
177+ self .assertEqual ('https://bucket.s3-region.amazonaws.com/some/location' ,
178+ Source .from_buffer ('png file' ).resize (width = 400 )\
179+ .convert (type = ['image/webp' , 'image/png' ])\
180+ .transform (background = "black" )\
181+ .preserve ("copyright" , "location" )\
182+ .store (service = 's3' ).location )
183+ self .assertJsonEqual ('{"store":{"service":"s3"},"resize":{"width":400},"preserve": ["copyright", "location"], "transform": {"background": "black"}, "convert": {"type": ["image/webp", "image/png"]}}' , httpretty .last_request ().body .decode ('utf-8' ))
184+
0 commit comments