Skip to content

Commit

Permalink
refactor: concentrate test methods on logic
Browse files Browse the repository at this point in the history
  • Loading branch information
boidolr committed Oct 14, 2023
1 parent d057005 commit 4e2d4e5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 56 deletions.
40 changes: 20 additions & 20 deletions tests/optimize_avif_test.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import pathlib
import shutil
from pathlib import Path

import pytest

from pre_commit_images.optimize_avif import main


@pytest.fixture
def original_image(tmpdir):
return Path(__file__).parent / "test.avif"
def images(tmpdir):
test_file = pathlib.Path(__file__).parent / "test.avif"
path = pathlib.Path(tmpdir) / "test.avif"
shutil.copy(test_file, path)
return path, test_file


@pytest.fixture
def image(tmpdir, original_image):
path = Path(tmpdir) / "test.avif"
shutil.copy(original_image, path)
return path


def test_qmin_qmax_deprecated(image):
def test_qmin_qmax_deprecated(images):
path, _ = images
with pytest.deprecated_call():
assert main(("-min", "0", str(image))) == 0
assert main(("-min", "0", str(path))) == 0


def test_qmin_qmax_and_quality(image):
def test_qmin_qmax_and_quality(images):
path, _ = images
with pytest.raises(SystemExit) as wrapped_exit:
assert main(("-min", "10", "--quality", "20", str(image))) == 1
assert main(("-min", "10", "--quality", "20", str(path))) == 1
assert wrapped_exit.type == SystemExit
assert wrapped_exit.value.code == 1


def test_compress_avif(original_image, image):
assert main(("--quality", "75", "-e", "0", str(image))) == 0
assert original_image.stat().st_size > image.stat().st_size
def test_compress_avif(images):
path, test_file = images
assert main(("--quality", "75", "-e", "0", str(path))) == 0
assert test_file.stat().st_size > path.stat().st_size


def test_compress_avif_below_threshold(original_image, image):
def test_compress_avif_below_threshold(images):
path, test_file = images
assert (
main(
(
Expand All @@ -45,9 +45,9 @@ def test_compress_avif_below_threshold(original_image, image):
"10",
"-t",
"6144",
str(image),
str(path),
)
)
== 0
)
assert original_image.stat().st_size == image.stat().st_size
assert test_file.stat().st_size == path.stat().st_size
23 changes: 14 additions & 9 deletions tests/optimize_jpg_test.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import pathlib
import shutil
from pathlib import Path

import pytest

from pre_commit_images.optimize_jpg import main


def test_compress_jpg(tmpdir):
@pytest.fixture
def images(tmpdir):
image = "test.jpg"
path = Path(tmpdir) / image
test_file = Path(__file__).parent / image
path = pathlib.Path(tmpdir) / image
test_file = pathlib.Path(__file__).parent / image
shutil.copy(test_file, path)
return path, test_file


def test_compress_jpg(images):
path, test_file = images

assert main(("-q", "50", str(path))) == 0
assert test_file.stat().st_size > path.stat().st_size


def test_compress_jpg_below_threshold(tmpdir):
image = "test.jpg"
path = Path(tmpdir) / image
test_file = Path(__file__).parent / image
shutil.copy(test_file, path)
def test_compress_jpg_below_threshold(images):
path, test_file = images

assert main(("-q", "97", "-t", "15000", str(path))) == 0
assert test_file.stat().st_size == path.stat().st_size
23 changes: 14 additions & 9 deletions tests/optimize_png_test.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import pathlib
import shutil
from pathlib import Path

import pytest

from pre_commit_images.optimize_png import main


def test_compress_png(tmpdir):
@pytest.fixture
def images(tmpdir):
image = "test.png"
path = Path(tmpdir) / image
test_file = Path(__file__).parent / image
path = pathlib.Path(tmpdir) / image
test_file = pathlib.Path(__file__).parent / image
shutil.copy(test_file, path)
return path, test_file


def test_compress_png(images):
path, test_file = images

assert main((str(path),)) == 0
assert test_file.stat().st_size > path.stat().st_size


def test_compress_png_below_threshold(tmpdir):
image = "test.png"
path = Path(tmpdir) / image
test_file = Path(__file__).parent / image
shutil.copy(test_file, path)
def test_compress_png_below_threshold(images):
path, test_file = images

assert (
main(
Expand Down
23 changes: 14 additions & 9 deletions tests/optimize_svg_test.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import pathlib
import shutil
from pathlib import Path

import pytest

from pre_commit_images.optimize_svg import main


def test_compress_svg(tmpdir):
@pytest.fixture
def images(tmpdir):
image = "test.svg"
path = Path(tmpdir) / image
test_file = Path(__file__).parent / image
path = pathlib.Path(tmpdir) / image
test_file = pathlib.Path(__file__).parent / image
shutil.copy(test_file, path)
return path, test_file


def test_compress_svg(images):
path, test_file = images

assert main((str(path),)) == 0
assert test_file.stat().st_size > path.stat().st_size


def test_compress_svg_below_threshold(tmpdir):
image = "test.svg"
path = Path(tmpdir) / image
test_file = Path(__file__).parent / image
shutil.copy(test_file, path)
def test_compress_svg_below_threshold(images):
path, test_file = images

assert (
main(
Expand Down
23 changes: 14 additions & 9 deletions tests/optimize_webp_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import pathlib
import shutil
from pathlib import Path

import pytest

from pre_commit_images.optimize_webp import main


def test_compress_webp(tmpdir):
@pytest.fixture
def images(tmpdir):
image = "test.webp"
path = Path(tmpdir) / image
test_file = Path(__file__).parent / image
path = pathlib.Path(tmpdir) / image
test_file = pathlib.Path(__file__).parent / image
shutil.copy(test_file, path)
return path, test_file


def test_compress_webp(images):
path, test_file = images

assert (
main(
Expand All @@ -23,11 +31,8 @@ def test_compress_webp(tmpdir):
assert test_file.stat().st_size > path.stat().st_size


def test_compress_webp_below_threshold(tmpdir):
image = "test.webp"
path = Path(tmpdir) / image
test_file = Path(__file__).parent / image
shutil.copy(test_file, path)
def test_compress_webp_below_threshold(images):
path, test_file = images

assert (
main(
Expand Down

0 comments on commit 4e2d4e5

Please sign in to comment.