File size: 2,049 Bytes
7e25f7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Provider tests for QR code + barcode providers."""

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.object_intelligence.qr_code import QRCodeProvider


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


@pytest.fixture
def pipeline_output(sample_image_bytes):
    img = cv2.imdecode(np.frombuffer(sample_image_bytes, np.uint8), cv2.IMREAD_COLOR)
    return PipelineOutput(
        image=img, image_hash="h", width=img.shape[1], height=img.shape[0], source="bytes",
        original_bytes=sample_image_bytes, original_format=".jpg",
    )


class TestQRCodeProvider:
    def test_name(self, qr_provider):
        assert qr_provider.name == "qr_code"

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

    def test_is_available(self, qr_provider):
        # OpenCV has QRCodeDetector since 4.2
        assert qr_provider.is_available() is True

    def test_execute_no_qr(self, qr_provider, pipeline_output):
        """An image without a QR code should return empty results."""
        result = qr_provider.execute(pipeline_output)
        assert result.success is True
        assert len(result.normalized["objects"]) == 0

    def test_execute_synthetic_qr(self, qr_provider):
        """Verify the provider handles an arbitrary image without crashing.

        We don't generate a real QR code (cv2.QRCodeGenerator isn't available
        in all OpenCV builds); we just verify the provider runs.
        """
        img = np.zeros((200, 200, 3), dtype=np.uint8)
        po = PipelineOutput(
            image=img, image_hash="qr", width=200, height=200, source="bytes",
        )
        result = qr_provider.execute(po)
        assert result.success is True
        assert "objects" in result.normalized