Skip to content

Commit

Permalink
Increase coverage by ~0.37% and take a few files to 100% (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmunoz94 authored Oct 17, 2022
1 parent 59c0482 commit 11800d2
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 6 deletions.
2 changes: 1 addition & 1 deletion fpdf/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ def paint_rule(self):
def paint_rule(self, new):
if new is None:
super().__setattr__("_paint_rule", PathPaintRule.DONT_PAINT)
if new is self.INHERIT:
elif new is self.INHERIT:
super().__setattr__("_paint_rule", new)
else:
super().__setattr__("_paint_rule", PathPaintRule.coerce(new))
Expand Down
Binary file not shown.
60 changes: 60 additions & 0 deletions test/drawing/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ def wrapper():
pytest.param(123, TypeError, id="bad type integer"),
)

rgb_colors = (
pytest.param(
"rgb(204, 170, 187)", fpdf.drawing.rgb8(r=204, g=170, b=187, a=None), id="RGB"
),
pytest.param(
"rgb(13, 6, 240, 5)", fpdf.drawing.rgb8(r=13, g=6, b=240, a=5), id="RGBA"
),
pytest.param(
"rgb( 192, 255 , 238 )",
fpdf.drawing.rgb8(r=192, g=255, b=238, a=None),
id="rgb with extra spaces",
),
pytest.param("rgb(a, s, d)", ValueError, id="bad characters"),
pytest.param("(240, 170, 187)", ValueError, id="missing rgb"),
pytest.param("rgb(10, 10, 10", ValueError, id="missing )"),
pytest.param("rgb(13, 6, 240, 5, 200)", ValueError, id="wrong length"),
pytest.param("rgba(13, 6, 240, 5)", ValueError, id="rgba instead of rgb"),
pytest.param(123, TypeError, id="bad type integer"),
)

numbers = (
pytest.param(100, "100", id="integer"),
pytest.param(Decimal("1.1"), "1.1", id="Decimal"),
Expand Down Expand Up @@ -86,6 +106,12 @@ def pdf_repr(self):
),
)

pdf_bad_primitives = (
pytest.param(type("EmptyClass", (), {}), TypeError, id="Class without bdf_repr"),
pytest.param({"element", 1, 2.0}, TypeError, id="unsupported (set)"),
pytest.param({"key": "value", "k2": True}, ValueError, id="dict with bad keys"),
)

T = fpdf.drawing.Transform
P = fpdf.drawing.Point

Expand Down Expand Up @@ -393,6 +419,7 @@ def pdf_repr(self):
pytest.param("stroke_join_style", "bevel", id="bevel stroke join"),
pytest.param("stroke_cap_style", "butt", id="butt stroke cap"),
pytest.param("stroke_cap_style", "square", id="square stroke cap"),
pytest.param("stroke_dash_pattern", 0.5, id="numeric stroke dash pattern"),
pytest.param("stroke_dash_pattern", [0.5, 0.5], id="50-50 stroke dash pattern"),
pytest.param("stroke_dash_pattern", [1, 2, 3, 1], id="complex stroke dash pattern"),
)
Expand Down Expand Up @@ -430,6 +457,24 @@ def pdf_repr(self):
pytest.param("fill_color", 2, TypeError, id="invalid numeric fill_color"),
pytest.param("fill_opacity", "123123", TypeError, id="invalid string fill_opacity"),
pytest.param("fill_opacity", 2, ValueError, id="invalid numeric fill_opacity"),
pytest.param("stroke_color", [2], TypeError, id="invalid stroke_color"),
pytest.param(
"stroke_dash_pattern", "123", TypeError, id="invalid string stroke_dash_pattern"
),
pytest.param(
"stroke_dash_pattern",
[0.5, "0.5"],
TypeError,
id="invalid string in stroke_dash_pattern",
),
pytest.param(
"stroke_dash_phase", "123", TypeError, id="invalid string stroke_dash_phase"
),
pytest.param(
"stroke_miter_limit", "123", TypeError, id="invalid string stroke_miter_limit"
),
pytest.param("stroke_width", [2], TypeError, id="invalid stroke_width"),
pytest.param("invalid_style_name", 2, AttributeError, id="invalid style name"),
)


Expand Down Expand Up @@ -606,6 +651,21 @@ def pdf_repr(self):
PP = fpdf.drawing.PaintedPath
d = fpdf.drawing

paint_rules = (
pytest.param(
fpdf.drawing.PathPaintRule.STROKE, fpdf.drawing.PathPaintRule.STROKE, id="Enum"
),
pytest.param(
"FILL_EVENODD", fpdf.drawing.PathPaintRule.FILL_EVENODD, id="matching string"
),
pytest.param(
"stroke_fill_nonzero",
fpdf.drawing.PathPaintRule.STROKE_FILL_NONZERO,
id="matching string after uppercasing",
),
pytest.param(None, fpdf.drawing.PathPaintRule.DONT_PAINT, id="None"),
)

clipping_path_result = (
pytest.param(
(
Expand Down
21 changes: 21 additions & 0 deletions test/drawing/test_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def test_render_primitive(self, primitive, result):

# Add check for bad primitives: class without pdf_repr, dict with non-Name
# keys. Check for proper escaping of Name and string edge cases
@pytest.mark.parametrize("primitive, error_type", parameters.pdf_bad_primitives)
def test_error_on_bad_primitive(self, primitive, error_type):
with pytest.raises(error_type):
fpdf.drawing.render_pdf_primitive(primitive)


class TestGraphicsStateDictRegistry:
Expand Down Expand Up @@ -158,6 +162,9 @@ def test_device_cmyk(self):
assert cmyk.colors == (1, 1, 1, 0)
assert cmyk_a.colors == (1, 1, 1, 0)

assert cmyk.pdf_repr() == "1 1 1 0 k"
assert cmyk_a.pdf_repr() == "1 1 1 0 k"

with pytest.raises(ValueError):
fpdf.drawing.DeviceCMYK(c=2, m=1, y=1, k=0)

Expand Down Expand Up @@ -196,6 +203,14 @@ def test_hex_string_parser(self, hex_string, result):
else:
assert fpdf.drawing.color_from_hex_string(hex_string) == result

@pytest.mark.parametrize("rgb_string, result", parameters.rgb_colors)
def test_rgb_string_parser(self, rgb_string, result):
if isinstance(result, type) and issubclass(result, Exception):
with pytest.raises(result):
fpdf.drawing.color_from_rgb_string(rgb_string)
else:
assert fpdf.drawing.color_from_rgb_string(rgb_string) == result


class TestPoint:
def test_render(self):
Expand Down Expand Up @@ -452,6 +467,12 @@ def test_merge(self):
assert merged.stroke_join_style == fpdf.drawing.StrokeJoinStyle.ROUND.value
assert merged.stroke_cap_style == fpdf.drawing.StrokeCapStyle.BUTT.value

@pytest.mark.parametrize("paint_rule, expected", parameters.paint_rules)
def test_paint_rule(self, paint_rule, expected):
style = fpdf.drawing.GraphicsStyle()
style.paint_rule = paint_rule
assert style.paint_rule is expected

def test_paint_rule_resolution(self):
style = fpdf.drawing.GraphicsStyle()

Expand Down
9 changes: 8 additions & 1 deletion test/errors/test_page_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
import fpdf


def test_page_format_error_class():
def test_page_format_error_both():
with pytest.raises(TypeError) as e:
fpdf.errors.FPDFPageFormatException(None, unknown=True, one=True)

expected = "FPDF Page Format Exception cannot be both"
assert expected in str(e.value)


def test_page_format_error_other():
expected = "error message"
error = fpdf.errors.FPDFPageFormatException(expected)

assert expected in str(error)


def test_page_format_error():
with pytest.raises(fpdf.errors.FPDFPageFormatException) as e:
fpdf.fpdf.get_page_format("letter1")
Expand Down
Binary file added test/metadata/setting_non_utc_date.pdf
Binary file not shown.
12 changes: 11 additions & 1 deletion test/metadata/test_set_date.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import datetime, timedelta, timezone
from pathlib import Path

import pytest
Expand All @@ -23,3 +23,13 @@ def test_setting_old_date(tmp_path):
# 2017, April 18th, almost 7:09a
doc.set_creation_date(datetime(2017, 4, 18, 7, 8, 55).replace(tzinfo=timezone.utc))
assert_pdf_equal(doc, HERE / "setting_old_date.pdf", tmp_path, at_epoch=False)


def test_setting_non_utc_date(tmp_path):
doc = fpdf.FPDF()
doc.add_page()
# 2017, April 18th, almost 7:09a
doc.set_creation_date(
datetime(2017, 4, 18, 7, 8, 55).replace(tzinfo=timezone(timedelta(hours=5)))
)
assert_pdf_equal(doc, HERE / "setting_non_utc_date.pdf", tmp_path, at_epoch=False)
10 changes: 10 additions & 0 deletions test/metadata/test_viewer_preferences.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pathlib import Path

import pytest

from fpdf import FPDF, ViewerPreferences
from test.conftest import assert_pdf_equal

Expand Down Expand Up @@ -35,3 +37,11 @@ def test_custom_viewer_preferences(tmp_path):
pdf.add_page()
pdf.cell(txt="page 2")
assert_pdf_equal(pdf, HERE / "custom_viewer_preferences.pdf", tmp_path)


@pytest.mark.parametrize(
"non_full_screen_page_mode", ("FULL_SCREEN", "USE_ATTACHMENTS")
)
def test_invalid_viewer_preferences(non_full_screen_page_mode):
with pytest.raises(ValueError):
ViewerPreferences(non_full_screen_page_mode=non_full_screen_page_mode)
6 changes: 3 additions & 3 deletions test/svg/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ def Gs(**kwargs):
id="skew x-only",
),
pytest.param(
"skew(2, 3)",
Transform.shearing(x=math.tan(math.radians(2)), y=math.tan(math.radians(3))),
"skew(3, 3)",
Transform.shearing(x=math.tan(math.radians(3))),
no_error(),
id="skew x and y",
),
Expand Down Expand Up @@ -583,7 +583,7 @@ def Gs(**kwargs):
'<path stroke-width="bad"/>',
Gs(),
pytest.raises(ValueError),
id="stroke-width number",
id="stroke-width invalid",
),
pytest.param(
'<path stroke-dasharray="1 2 3 4 5"/>',
Expand Down
6 changes: 6 additions & 0 deletions test/test_transitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def test_transition_errors():
with pytest.raises(ValueError):
pdf.add_page(transition=SplitTransition("A", "B"))

with pytest.raises(ValueError):
pdf.add_page(transition=SplitTransition("H", "B"))

with pytest.raises(ValueError):
pdf.add_page(transition=BlindsTransition("A"))

Expand All @@ -75,6 +78,9 @@ def test_transition_errors():
with pytest.raises(ValueError):
pdf.add_page(transition=FlyTransition("A", -1))

with pytest.raises(ValueError):
pdf.add_page(transition=FlyTransition("V", -1))

with pytest.raises(ValueError):
pdf.add_page(transition=PushTransition(-1))

Expand Down
5 changes: 5 additions & 0 deletions test/utils/test_byte_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def test_string_to_bytes():
assert b("foo") == expected


def test_int_to_bytes():
expected = b"J"
assert b(74) == expected


def test_bytes_conversion_error():
with pytest.raises(ValueError) as error:
b([1, 2, 3])
Expand Down

0 comments on commit 11800d2

Please sign in to comment.