File size: 2,995 Bytes
672c1ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from pathlib import Path

import numpy as np
import pytest

# Tests assume fixture images exist in tests/fixtures/
FIXTURES_DIR = Path(__file__).parent / "fixtures"


def _has_fixture(name: str) -> str:
    """Check if a fixture image exists, return path or empty string."""
    for ext in (".png", ".jpg", ".jpeg", ".bmp"):
        path = FIXTURES_DIR / (name + ext)
        if path.exists():
            return str(path)
    return ""


@pytest.fixture
def sample_image_path():
    """Path to a sample image, or skip if unavailable."""
    path = _has_fixture("sample")
    if not path:
        pytest.skip("No fixture image found in tests/fixtures/")
    return path


class TestCropToSquare:
    def test_returns_numpy_array(self, sample_image_path):
        from roicropper.core import crop_to_square
        result = crop_to_square(sample_image_path, target_size=640, padding_pixels=10)
        assert isinstance(result, np.ndarray)

    def test_output_is_square(self, sample_image_path):
        from roicropper.core import crop_to_square
        result = crop_to_square(sample_image_path, target_size=640, padding_pixels=10)
        assert result.shape[0] == 640
        assert result.shape[1] == 640

    def test_custom_size(self, sample_image_path):
        from roicropper.core import crop_to_square
        result = crop_to_square(sample_image_path, target_size=1280, padding_pixels=5)
        assert result.shape[0] == 1280
        assert result.shape[1] == 1280

    def test_nonexistent_file(self):
        from roicropper.core import crop_to_square
        result = crop_to_square("/nonexistent/path/image.png", target_size=640)
        assert result is None


class TestCropToAspectRatio:
    def test_returns_numpy_array(self, sample_image_path):
        from roicropper.core import crop_to_aspect_ratio
        result = crop_to_aspect_ratio(sample_image_path, aspect_ratio=(16, 9), max_size=1280)
        assert isinstance(result, np.ndarray)

    def test_16by9_ratio(self, sample_image_path):
        from roicropper.core import crop_to_aspect_ratio
        result = crop_to_aspect_ratio(sample_image_path, aspect_ratio=(16, 9), max_size=1280)
        # Check width/height ratio approximates 16:9
        if result.shape[0] > 0 and result.shape[1] > 0:
            ratio = result.shape[1] / result.shape[0]
            assert abs(ratio - 16 / 9) < 0.05  # within 5% tolerance due to integer rounding

    def test_4by3_ratio(self, sample_image_path):
        from roicropper.core import crop_to_aspect_ratio
        result = crop_to_aspect_ratio(sample_image_path, aspect_ratio=(4, 3), max_size=1280)
        if result.shape[0] > 0 and result.shape[1] > 0:
            ratio = result.shape[1] / result.shape[0]
            assert abs(ratio - 4 / 3) < 0.05

    def test_nonexistent_file(self):
        from roicropper.core import crop_to_aspect_ratio
        result = crop_to_aspect_ratio("/nonexistent/path/image.png", aspect_ratio=(16, 9))
        assert result is None