forked from tinify/tinify-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegration.py
More file actions
109 lines (78 loc) · 2.94 KB
/
integration.py
File metadata and controls
109 lines (78 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import sys
import os
from contextlib import contextmanager
import tinify
import pytest
import tempfile
if not os.environ.get("TINIFY_KEY"):
sys.exit("Set the TINIFY_KEY environment variable.")
@contextmanager
def create_named_tmpfile():
# Due to NamedTemporaryFile requiring to be closed when used on Windows
# we create our own NamedTemporaryFile contextmanager
# See note: https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
tmp = tempfile.NamedTemporaryFile(delete=False)
try:
tmp.close()
yield tmp.name
finally:
os.unlink(tmp.name)
# Fixture for shared resources
@pytest.fixture(scope="module")
def optimized_image():
tinify.key = os.environ.get("TINIFY_KEY")
tinify.proxy = os.environ.get("TINIFY_PROXY")
unoptimized_path = os.path.join(
os.path.dirname(__file__), "examples", "voormedia.png"
)
return tinify.from_file(unoptimized_path)
def test_should_compress_from_file(optimized_image):
with create_named_tmpfile() as tmp:
optimized_image.to_file(tmp)
size = os.path.getsize(tmp)
with open(tmp, "rb") as f:
contents = f.read()
assert 1000 < size < 1500
# width == 137
assert b"\x00\x00\x00\x89" in contents
assert b"Copyright Voormedia" not in contents
def test_should_compress_from_url():
source = tinify.from_url(
"https://raw.githubusercontent.com/tinify/tinify-python/master/test/examples/voormedia.png"
)
with create_named_tmpfile() as tmp:
source.to_file(tmp)
size = os.path.getsize(tmp)
with open(tmp, "rb") as f:
contents = f.read()
assert 1000 < size < 1500
# width == 137
assert b"\x00\x00\x00\x89" in contents
assert b"Copyright Voormedia" not in contents
def test_should_resize(optimized_image):
with create_named_tmpfile() as tmp:
optimized_image.resize(method="fit", width=50, height=20).to_file(tmp)
size = os.path.getsize(tmp)
with open(tmp, "rb") as f:
contents = f.read()
assert 500 < size < 1000
# width == 50
assert b"\x00\x00\x00\x32" in contents
assert b"Copyright Voormedia" not in contents
def test_should_preserve_metadata(optimized_image):
with create_named_tmpfile() as tmp:
optimized_image.preserve("copyright", "creation").to_file(tmp)
size = os.path.getsize(tmp)
with open(tmp, "rb") as f:
contents = f.read()
assert 1000 < size < 2000
# width == 137
assert b"\x00\x00\x00\x89" in contents
assert b"Copyright Voormedia" in contents
def test_should_transcode_image(optimized_image):
with create_named_tmpfile() as tmp:
optimized_image.convert(type=["image/webp"]).to_file(tmp)
with open(tmp, "rb") as f:
content = f.read()
assert b"RIFF" == content[:4]
assert b"WEBP" == content[8:12]