Skip to content

Commit

Permalink
Add tests for rotate FX when Pillow is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Apr 15, 2021
1 parent 7dc2791 commit d7a49ec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ include_trailing_comma = True
py_version = 36
known_tests = tests
sections = STDLIB,THIRDPARTY,FIRSTPARTY,TESTS,LOCALFOLDER

[coverage:report]
exclude_lines =
pragma: no cover
39 changes: 36 additions & 3 deletions tests/test_fx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import decimal
import importlib
import math
import numbers
import os
Expand Down Expand Up @@ -759,6 +760,7 @@ def test_resize(


# Run several times to ensure that adding 360 to rotation angles has no effect
@pytest.mark.parametrize("PIL_installed", (True, False))
@pytest.mark.parametrize("angle_offset", [-360, 0, 360, 720])
@pytest.mark.parametrize("unit", ["deg", "rad"])
@pytest.mark.parametrize("resample", ["bilinear", "nearest", "bicubic", "unknown"])
Expand Down Expand Up @@ -839,6 +841,7 @@ def test_resize(
),
)
def test_rotate(
PIL_installed,
angle_offset,
angle,
unit,
Expand All @@ -847,6 +850,7 @@ def test_rotate(
center,
bg_color,
expected_frames,
monkeypatch,
):
"""Check ``rotate`` FX behaviour against possible combinations of arguments."""
original_frames = [["AAAA", "BBBB", "CCCC"], ["ABCD", "BCDE", "CDEA"]]
Expand Down Expand Up @@ -875,11 +879,40 @@ def test_rotate(
"'resample' argument must be either 'bilinear', 'nearest' or 'bicubic'"
) == str(exc.value)
return

# if the scenario implies that PIL is not installed, monkeypatch the
# module in which 'rotate' function resides
if not PIL_installed:
rotate_module = importlib.import_module("moviepy.video.fx.rotate")
monkeypatch.setattr(rotate_module, "Image", None)
rotate_func = rotate_module.rotate
else:
rotate_func = rotate

# resolve the angle, because if is a multiple of 90 the rotation can be
# computed event without installing PIL
if hasattr(_angle, "__call__"):
_resolved_angle = _angle(0)
else:
_resolved_angle = _angle
if unit == "rad":
_resolved_angle = math.degrees(_resolved_angle)

if not PIL_installed and (
(_resolved_angle % 90 != 0) or center or translate or bg_color
):
with pytest.raises(ValueError) as exc:
rotated_clip = clip.fx(rotate_func, _angle, **kwargs)

assert (
'Without "Pillow" installed, only angles that are a multiple of 90'
) in str(exc.value)

else:
rotated_clip = clip.rotate(_angle, **kwargs)
rotated_clip = clip.fx(rotate_func, _angle, **kwargs)
expected_clip = BitmapClip(expected_frames, fps=1)

expected_clip = BitmapClip(expected_frames, fps=1)
assert rotated_clip.to_bitmap() == expected_clip.to_bitmap()
assert rotated_clip.to_bitmap() == expected_clip.to_bitmap()


def test_rotate_nonstandard_angles():
Expand Down

0 comments on commit d7a49ec

Please sign in to comment.