Skip to content

Commit 3fa96c8

Browse files
Expand tests to ensure same behaviour as other clients.
1 parent 1c3d4fd commit 3fa96c8

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

test/tinify_result_meta_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,16 @@ def test_height_should_return_image_height(self):
2323

2424
def test_location_should_return_stored_location(self):
2525
self.assertEqual('https://bucket.s3-region.amazonaws.com/some/location', self.result.location)
26+
27+
class TinifyResultMetaWithoutMetaTest(TestHelper):
28+
def setUp(self):
29+
self.result = ResultMeta({})
30+
31+
def test_width_should_return_none(self):
32+
self.assertEqual(None, self.result.width)
33+
34+
def test_height_should_return_none(self):
35+
self.assertEqual(None, self.result.height)
36+
37+
def test_location_should_return_none(self):
38+
self.assertEqual(None, self.result.location)

test/tinify_result_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,28 @@ def test_content_type_should_return_mime_type(self):
3434

3535
def test_to_buffer_should_return_image_data(self):
3636
self.assertEqual(b'image data', self.result.to_buffer())
37+
38+
class TinifyResultWithoutMetaAndDataTest(TestHelper):
39+
def setUp(self):
40+
self.result = Result({}, None)
41+
42+
def test_width_should_return_none(self):
43+
self.assertEqual(None, self.result.width)
44+
45+
def test_height_should_return_none(self):
46+
self.assertEqual(None, self.result.height)
47+
48+
def test_location_should_return_none(self):
49+
self.assertEqual(None, self.result.location)
50+
51+
def test_size_should_return_none(self):
52+
self.assertEqual(None, self.result.size)
53+
54+
def test_len_builtin_should_return_zero(self):
55+
self.assertEqual(0, len(self.result))
56+
57+
def test_content_type_should_return_none(self):
58+
self.assertEqual(None, self.result.content_type)
59+
60+
def test_to_buffer_should_return_none(self):
61+
self.assertEqual(None, self.result.to_buffer())

tinify/result.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ def to_buffer(self):
2020

2121
@property
2222
def size(self):
23-
return int(self._meta['Content-Length'])
23+
value = self._meta.get('Content-Length')
24+
return value and int(value)
2425

2526
@property
2627
def media_type(self):
27-
return self._meta['Content-Type']
28+
return self._meta.get('Content-Type')
2829

2930
@property
3031
def content_type(self):

tinify/result_meta.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ def __init__(self, meta):
77

88
@property
99
def width(self):
10-
return int(self._meta['Image-Width'])
10+
value = self._meta.get('Image-Width')
11+
return value and int(value)
1112

1213
@property
1314
def height(self):
14-
return int(self._meta['Image-Height'])
15+
value = self._meta.get('Image-Height')
16+
return value and int(value)
1517

1618
@property
1719
def location(self):
18-
return self._meta['Location']
20+
return self._meta.get('Location')
1921

2022
def __len__(self):
21-
return self.size
23+
return self.size or 0

0 commit comments

Comments
 (0)