Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc94952

Browse files
author
Steve Canny
committed
img: close test coverage gaps in docx.image
1 parent dd8c7ef commit fc94952

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

docx/image/helpers.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def read(self, count):
3131
"""
3232
return self._stream.read(count)
3333

34-
def read_byte(self, base=None, offset=0):
34+
def read_byte(self, base, offset=0):
3535
"""
3636
Return the int value of the byte at the file position defined by
3737
self._base_offset + *base* + *offset*. If *base* is None, the byte is
@@ -40,7 +40,7 @@ def read_byte(self, base=None, offset=0):
4040
fmt = 'B'
4141
return self._read_int(fmt, base, offset)
4242

43-
def read_long(self, base=None, offset=0):
43+
def read_long(self, base, offset=0):
4444
"""
4545
Return the int value of the four bytes at the file position defined by
4646
self._base_offset + *base* + *offset*. If *base* is None, the long is
@@ -50,7 +50,7 @@ def read_long(self, base=None, offset=0):
5050
fmt = '<L' if self._byte_order is LITTLE_ENDIAN else '>L'
5151
return self._read_int(fmt, base, offset)
5252

53-
def read_short(self, base=None, offset=0):
53+
def read_short(self, base, offset=0):
5454
"""
5555
Return the int value of the two bytes at the file position determined
5656
by *base* and *offset*, similarly to ``read_long()`` above.
@@ -90,8 +90,6 @@ def _read_bytes(self, byte_count, base, offset):
9090

9191
def _read_int(self, fmt, base, offset):
9292
struct = Struct(fmt)
93-
if base is None:
94-
base = self._stream.tell() - self._base_offset
9593
return self._unpack_item(struct, base, offset)
9694

9795
def _unpack_item(self, struct, base, offset):

docx/image/jpeg.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __init__(self, markers):
7777
super(_JfifMarkers, self).__init__()
7878
self._markers = list(markers)
7979

80-
def __str__(self):
80+
def __str__(self): # pragma: no cover
8181
"""
8282
Returns a tabular listing of the markers in this instance, which can
8383
be handy for debugging and perhaps other uses.
@@ -243,7 +243,7 @@ def _read_byte(self):
243243
at end of file.
244244
"""
245245
byte_ = self._stream.read(1)
246-
if not byte_:
246+
if not byte_: # pragma: no cover
247247
raise Exception('unexpected end of file')
248248
return byte_
249249

@@ -296,11 +296,11 @@ def marker_code(self):
296296
return self._marker_code
297297

298298
@property
299-
def name(self):
299+
def name(self): # pragma: no cover
300300
return JPEG_MARKER_CODE.marker_names[self._marker_code]
301301

302302
@property
303-
def offset(self):
303+
def offset(self): # pragma: no cover
304304
return self._offset
305305

306306
@property

tests/image/test_image.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ def call_fixture(self, request):
197197

198198
class DescribeBaseImageHeader(object):
199199

200+
def it_defines_content_type_as_an_abstract_property(self):
201+
base_image_header = BaseImageHeader(None, None, None, None)
202+
with pytest.raises(NotImplementedError):
203+
base_image_header.content_type
204+
200205
def it_knows_the_image_dimensions(self):
201206
px_width, px_height = 42, 24
202207
image_header = BaseImageHeader(px_width, px_height, None, None)

tests/image/test_jpeg.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ def it_raises_if_it_cant_find_the_APP0_marker(self, no_app0_fixture):
128128
with pytest.raises(KeyError):
129129
jfif_markers.app0
130130

131+
def it_raises_if_it_cant_find_the_APP1_marker(self, no_app1_fixture):
132+
jfif_markers = no_app1_fixture
133+
with pytest.raises(KeyError):
134+
jfif_markers.app1
135+
131136
def it_can_find_the_SOF_marker(self, sof_fixture):
132137
jfif_markers, sof_ = sof_fixture
133138
sof = jfif_markers.sof
@@ -202,6 +207,11 @@ def no_app0_fixture(self, soi_, eoi_):
202207
markers = (soi_, eoi_)
203208
return _JfifMarkers(markers)
204209

210+
@pytest.fixture
211+
def no_app1_fixture(self, soi_, eoi_):
212+
markers = (soi_, eoi_)
213+
return _JfifMarkers(markers)
214+
205215
@pytest.fixture
206216
def no_sof_fixture(self, soi_, eoi_):
207217
markers = (soi_, eoi_)

tests/image/test_png.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010

1111
from docx.compat import BytesIO
12-
from docx.image.constants import TAG
12+
from docx.image.constants import MIME_TYPE, TAG
1313
from docx.image.exceptions import InvalidImageStreamError
1414
from docx.image.helpers import BIG_ENDIAN, StreamReader
1515
from docx.image.png import Png
@@ -71,6 +71,10 @@ def it_can_parse_an_pHYs_chunk(self, parse_pHYs_fixture):
7171
attrs = Png._parse_pHYs(stream, offset)
7272
assert attrs == expected_attrs
7373

74+
def it_knows_its_content_type(self):
75+
png = Png(None, None, None)
76+
assert png.content_type == MIME_TYPE.PNG
77+
7478
def it_knows_its_dpi(self, dpi_fixture):
7579
png, expected_dpi = dpi_fixture
7680
assert png.horz_dpi == expected_dpi

0 commit comments

Comments
 (0)