1- import sys , os
1+ import sys
2+ import os
23from contextlib import contextmanager
3- import tinify , unittest , tempfile
4+ import tinify
5+ import pytest
6+ import tempfile
47
58if not os .environ .get ("TINIFY_KEY" ):
69 sys .exit ("Set the TINIFY_KEY environment variable." )
710
11+
812@contextmanager
913def create_named_tmpfile ():
1014 # Due to NamedTemporaryFile requiring to be closed when used on Windows
@@ -19,76 +23,87 @@ def create_named_tmpfile():
1923 os .unlink (tmp .name )
2024
2125
22- class ClientIntegrationTest (unittest .TestCase ):
26+ # Fixture for shared resources
27+ @pytest .fixture (scope = "module" )
28+ def optimized_image ():
2329 tinify .key = os .environ .get ("TINIFY_KEY" )
2430 tinify .proxy = os .environ .get ("TINIFY_PROXY" )
2531
26- unoptimized_path = os .path .join (os .path .dirname (__file__ ), 'examples' , 'voormedia.png' )
27- optimized = tinify .from_file (unoptimized_path )
32+ unoptimized_path = os .path .join (
33+ os .path .dirname (__file__ ), "examples" , "voormedia.png"
34+ )
35+ return tinify .from_file (unoptimized_path )
36+
37+
38+ def test_should_compress_from_file (optimized_image ):
39+ with create_named_tmpfile () as tmp :
40+ optimized_image .to_file (tmp )
41+
42+ size = os .path .getsize (tmp )
43+
44+ with open (tmp , "rb" ) as f :
45+ contents = f .read ()
46+
47+ assert 1000 < size < 1500
2848
29- def test_should_compress_from_file ( self ):
30- with create_named_tmpfile () as tmp :
31- self . optimized . to_file ( tmp )
49+ # width == 137
50+ assert b" \x00 \x00 \x00 \x89 " in contents
51+ assert b"Copyright Voormedia" not in contents
3252
33- size = os .path .getsize (tmp )
3453
35- with open (tmp , 'rb' ) as f :
36- contents = f .read ()
54+ def test_should_compress_from_url ():
55+ source = tinify .from_url (
56+ "https://raw.githubusercontent.com/tinify/tinify-python/master/test/examples/voormedia.png"
57+ )
58+ with create_named_tmpfile () as tmp :
59+ source .to_file (tmp )
3760
38- self .assertTrue (1000 < size < 1500 )
61+ size = os .path .getsize (tmp )
62+ with open (tmp , "rb" ) as f :
63+ contents = f .read ()
3964
40- # width == 137
41- self .assertIn (b'\x00 \x00 \x00 \x89 ' , contents )
42- self .assertNotIn (b'Copyright Voormedia' , contents )
65+ assert 1000 < size < 1500
4366
44- def test_should_compress_from_url (self ):
45- source = tinify .from_url ('https://raw.githubusercontent.com/tinify/tinify-python/master/test/examples/voormedia.png' )
46- with create_named_tmpfile () as tmp :
47- source .to_file (tmp )
67+ # width == 137
68+ assert b"\x00 \x00 \x00 \x89 " in contents
69+ assert b"Copyright Voormedia" not in contents
4870
49- size = os .path .getsize (tmp )
50- with open (tmp , 'rb' ) as f :
51- contents = f .read ()
5271
53- self .assertTrue (1000 < size < 1500 )
72+ def test_should_resize (optimized_image ):
73+ with create_named_tmpfile () as tmp :
74+ optimized_image .resize (method = "fit" , width = 50 , height = 20 ).to_file (tmp )
5475
55- # width == 137
56- self . assertIn ( b' \x00 \x00 \x00 \x89 ' , contents )
57- self . assertNotIn ( b'Copyright Voormedia' , contents )
76+ size = os . path . getsize ( tmp )
77+ with open ( tmp , "rb" ) as f :
78+ contents = f . read ( )
5879
59- def test_should_resize (self ):
60- with create_named_tmpfile () as tmp :
61- self .optimized .resize (method = "fit" , width = 50 , height = 20 ).to_file (tmp )
80+ assert 500 < size < 1000
6281
63- size = os . path . getsize ( tmp )
64- with open ( tmp , 'rb' ) as f :
65- contents = f . read ()
82+ # width == 50
83+ assert b" \x00 \x00 \x00 \x32 " in contents
84+ assert b"Copyright Voormedia" not in contents
6685
67- self .assertTrue (500 < size < 1000 )
6886
69- # width == 50
70- self . assertIn ( b' \x00 \x00 \x00 \x32 ' , contents )
71- self . assertNotIn ( b'Copyright Voormedia' , contents )
87+ def test_should_preserve_metadata ( optimized_image ):
88+ with create_named_tmpfile () as tmp :
89+ optimized_image . preserve ( "copyright" , "creation" ). to_file ( tmp )
7290
73- def test_should_preserve_metadata ( self ):
74- with create_named_tmpfile ( ) as tmp :
75- self . optimized . preserve ( "copyright" , "creation" ). to_file ( tmp )
91+ size = os . path . getsize ( tmp )
92+ with open ( tmp , "rb" ) as f :
93+ contents = f . read ( )
7694
77- size = os .path .getsize (tmp )
78- with open (tmp , 'rb' ) as f :
79- contents = f .read ()
95+ assert 1000 < size < 2000
8096
81- self .assertTrue (1000 < size < 2000 )
97+ # width == 137
98+ assert b"\x00 \x00 \x00 \x89 " in contents
99+ assert b"Copyright Voormedia" in contents
82100
83- # width == 137
84- self .assertIn (b'\x00 \x00 \x00 \x89 ' , contents )
85- self .assertIn (b'Copyright Voormedia' , contents )
86101
87- def test_should_transcode_image (self ):
88- with create_named_tmpfile () as tmp :
89- a = self . optimized .convert (type = ["image/webp" ]).to_file (tmp )
90- with open (tmp , 'rb' ) as f :
91- content = f .read ()
102+ def test_should_transcode_image (optimized_image ):
103+ with create_named_tmpfile () as tmp :
104+ optimized_image .convert (type = ["image/webp" ]).to_file (tmp )
105+ with open (tmp , "rb" ) as f :
106+ content = f .read ()
92107
93- self . assertEqual ( b' RIFF' , content [:4 ])
94- self . assertEqual ( b' WEBP' , content [8 :12 ])
108+ assert b" RIFF" == content [:4 ]
109+ assert b" WEBP" == content [8 :12 ]
0 commit comments