File size: 2,747 Bytes
23d337e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Provider tests for HaarDetector."""

from __future__ import annotations

import cv2
import numpy as np
import pytest

from config.settings import Settings
from pipeline.feature_extraction import PipelineOutput
from providers.detection.haar import HaarDetector


@pytest.fixture
def haar_detector():
    return HaarDetector(settings=Settings(environment="test", db_path=":memory:"))


@pytest.fixture
def pipeline_output(sample_image_bytes):
    """A PipelineOutput wrapping a synthetic image."""
    img = cv2.imdecode(np.frombuffer(sample_image_bytes, np.uint8), cv2.IMREAD_COLOR)
    return PipelineOutput(
        image=img,
        image_hash="test-hash",
        width=img.shape[1],
        height=img.shape[0],
        source="bytes",
    )


class TestHaarDetector:
    def test_provider_name(self, haar_detector):
        assert haar_detector.name == "haar"

    def test_capability(self, haar_detector):
        from models.providers import ProviderCapability
        assert haar_detector.capability == ProviderCapability.DETECTION

    def test_is_available(self, haar_detector):
        assert haar_detector.is_available() is True

    def test_execute_returns_provider_result(self, haar_detector, pipeline_output):
        from providers.base import ProviderResult
        result = haar_detector.execute(pipeline_output)
        assert isinstance(result, ProviderResult)
        assert result.provider == "haar"
        assert result.success is True
        assert result.elapsed_ms > 0

    def test_execute_normalized_has_expected_keys(self, haar_detector, pipeline_output):
        result = haar_detector.execute(pipeline_output)
        assert "boxes" in result.normalized
        assert "num_faces" in result.normalized
        assert "confidences" in result.normalized
        assert "landmarks" in result.normalized

    def test_execute_raw_has_expected_keys(self, haar_detector, pipeline_output):
        result = haar_detector.execute(pipeline_output)
        assert "rectangles" in result.raw
        assert "num_faces" in result.raw

    def test_execute_on_black_image_finds_no_faces(self, haar_detector):
        img = np.zeros((200, 200, 3), dtype=np.uint8)
        po = PipelineOutput(image=img, image_hash="x", width=200, height=200, source="bytes")
        result = haar_detector.execute(po)
        assert result.success is True
        assert result.normalized["num_faces"] == 0

    def test_execute_handles_empty_image_gracefully(self, haar_detector):
        # 1x1 image should not crash
        img = np.zeros((1, 1, 3), dtype=np.uint8)
        po = PipelineOutput(image=img, image_hash="x", width=1, height=1, source="bytes")
        result = haar_detector.execute(po)
        assert result.success is True