88if not os .environ .get ("TINIFY_KEY" ):
99 sys .exit ("Set the TINIFY_KEY environment variable." )
1010
11+ try :
12+ from typing import TYPE_CHECKING
13+ if TYPE_CHECKING :
14+ from tinify .source import Source
15+ except ImportError :
16+ pass
17+
1118
1219@contextmanager
1320def create_named_tmpfile ():
@@ -22,20 +29,26 @@ def create_named_tmpfile():
2229 finally :
2330 os .unlink (tmp .name )
2431
32+ @pytest .fixture (scope = "module" , autouse = True )
33+ def tinify_patch ():
34+ tinify .key = os .environ .get ("TINIFY_KEY" )
35+ tinify .proxy = os .environ .get ("TINIFY_PROXY" )
36+
37+ yield
38+
39+ tinify .key = None
40+ tinify .proxy = None
2541
2642# Fixture for shared resources
2743@pytest .fixture (scope = "module" )
2844def optimized_image ():
29- tinify .key = os .environ .get ("TINIFY_KEY" )
30- tinify .proxy = os .environ .get ("TINIFY_PROXY" )
31-
3245 unoptimized_path = os .path .join (
3346 os .path .dirname (__file__ ), "examples" , "voormedia.png"
3447 )
3548 return tinify .from_file (unoptimized_path )
3649
3750
38- def test_should_compress_from_file (optimized_image ):
51+ def test_should_compress_from_file (optimized_image ): # type: (Source) -> None
3952 with create_named_tmpfile () as tmp :
4053 optimized_image .to_file (tmp )
4154
@@ -69,10 +82,9 @@ def test_should_compress_from_url():
6982 assert b"Copyright Voormedia" not in contents
7083
7184
72- def test_should_resize (optimized_image ):
85+ def test_should_resize (optimized_image ): # type: (Source) -> None
7386 with create_named_tmpfile () as tmp :
7487 optimized_image .resize (method = "fit" , width = 50 , height = 20 ).to_file (tmp )
75-
7688 size = os .path .getsize (tmp )
7789 with open (tmp , "rb" ) as f :
7890 contents = f .read ()
@@ -84,7 +96,7 @@ def test_should_resize(optimized_image):
8496 assert b"Copyright Voormedia" not in contents
8597
8698
87- def test_should_preserve_metadata (optimized_image ):
99+ def test_should_preserve_metadata (optimized_image ): # type: (Source) -> None
88100 with create_named_tmpfile () as tmp :
89101 optimized_image .preserve ("copyright" , "creation" ).to_file (tmp )
90102
@@ -99,11 +111,30 @@ def test_should_preserve_metadata(optimized_image):
99111 assert b"Copyright Voormedia" in contents
100112
101113
102- def test_should_transcode_image (optimized_image ):
114+ def test_should_transcode_image (optimized_image ): # type: (Source) -> None
103115 with create_named_tmpfile () as tmp :
104- optimized_image .convert (type = ["image/webp" ]).to_file (tmp )
116+ conv = optimized_image .convert (type = ["image/webp" ])
117+ conv .to_file (tmp )
105118 with open (tmp , "rb" ) as f :
106119 content = f .read ()
107120
108121 assert b"RIFF" == content [:4 ]
109122 assert b"WEBP" == content [8 :12 ]
123+
124+ assert conv .result ().size < optimized_image .result ().size
125+ assert conv .result ().media_type == "image/webp"
126+ assert conv .result ().extension == "webp"
127+
128+
129+ def test_should_handle_invalid_key ():
130+ invalid_key = "invalid_key"
131+ tinify .key = invalid_key
132+ with pytest .raises (tinify .AccountError ):
133+ tinify .from_url (
134+ "https://raw.githubusercontent.com/tinify/tinify-python/master/test/examples/voormedia.png"
135+ )
136+ tinify .key = os .environ .get ("TINIFY_KEY" )
137+
138+ def test_should_handle_invalid_image ():
139+ with pytest .raises (tinify .ClientError ):
140+ tinify .from_buffer ("invalid_image.png" )
0 commit comments