Skip to content

Commit aaed13a

Browse files
committed
More pytest and replace httpretty with requests-mock
1 parent 504a446 commit aaed13a

File tree

10 files changed

+849
-613
lines changed

10 files changed

+849
-613
lines changed

.github/workflows/ci-cd.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ jobs:
2424
fail-fast: false
2525
matrix:
2626
python-version: [
27-
"2.7",
28-
"3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14-dev",
27+
"3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14-dev",
2928
"pypy-2.7", "pypy-3.10"
3029
]
3130
os: [ubuntu-latest, macOS-latest, windows-latest]

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'tinify'))
1111
from version import __version__
1212

13-
install_require = ['requests >= 2.7.0, < 3.0.0']
14-
tests_require = ['pytest', 'httpretty < 1.1.5']
13+
install_require = ["requests >= 2.7.0, < 3.0.0"]
14+
tests_require = ["pytest", "pytest-xdist", "requests-mock"]
1515

1616
if sys.version_info < (2, 7):
1717
tests_require.append('unittest2')

test/conftest.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
import os
3+
import tinify
4+
import requests_mock
5+
6+
7+
@pytest.fixture
8+
def dummy_file():
9+
return os.path.join(os.path.dirname(__file__), "examples", "dummy.png")
10+
11+
12+
@pytest.fixture(autouse=True)
13+
def reset_tinify():
14+
original_key = tinify.key
15+
original_app_identifier = tinify.app_identifier
16+
original_proxy = tinify.proxy
17+
18+
tinify.key = None
19+
tinify.app_identifier = None
20+
tinify.proxy = None
21+
22+
yield
23+
24+
tinify.key = original_key
25+
tinify.app_identifier = original_app_identifier
26+
tinify.proxy = original_proxy
27+
28+
29+
@pytest.fixture
30+
def mock_requests():
31+
with requests_mock.Mocker(real_http=False) as m:
32+
yield m

test/helper.py

Lines changed: 0 additions & 73 deletions
This file was deleted.

test/integration.py

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import sys, os
1+
import sys
2+
import os
23
from contextlib import contextmanager
3-
import tinify, unittest, tempfile
4+
import tinify
5+
import pytest
6+
import tempfile
47

58
if not os.environ.get("TINIFY_KEY"):
69
sys.exit("Set the TINIFY_KEY environment variable.")
710

11+
812
@contextmanager
913
def 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

Comments
 (0)