| """Provider tests for ImageQualityProvider.""" |
|
|
| 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.image_analysis.image_quality import ImageQualityProvider |
|
|
|
|
| @pytest.fixture |
| def quality_provider(): |
| return ImageQualityProvider(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 TestImageQualityProvider: |
| def test_name(self, quality_provider): |
| assert quality_provider.name == "image_quality" |
|
|
| def test_capability(self, quality_provider): |
| from models.providers import ProviderCapability |
| assert quality_provider.capability == ProviderCapability.IMAGE_ANALYSIS |
|
|
| def test_is_available(self, quality_provider): |
| assert quality_provider.is_available() is True |
|
|
| def test_execute_returns_metrics(self, quality_provider, pipeline_output): |
| result = quality_provider.execute(pipeline_output) |
| assert result.success is True |
| n = result.normalized |
| assert "brightness" in n |
| assert "contrast" in n |
| assert "sharpness" in n |
| assert "noise_level" in n |
| assert "quality_score" in n |
| assert isinstance(n["brightness"], float) |
| assert isinstance(n["quality_score"], float) |
|
|
| def test_quality_score_in_range(self, quality_provider, pipeline_output): |
| result = quality_provider.execute(pipeline_output) |
| q = result.normalized["quality_score"] |
| assert 0.0 <= q <= 1.0 |
|
|
| def test_black_image_low_brightness(self, quality_provider): |
| img = np.zeros((200, 200, 3), dtype=np.uint8) |
| po = PipelineOutput(image=img, image_hash="h", width=200, height=200, source="bytes") |
| result = quality_provider.execute(po) |
| assert result.normalized["brightness"] < 10.0 |
|
|
| def test_white_image_high_brightness(self, quality_provider): |
| img = np.full((200, 200, 3), 255, dtype=np.uint8) |
| po = PipelineOutput(image=img, image_hash="h", width=200, height=200, source="bytes") |
| result = quality_provider.execute(po) |
| assert result.normalized["brightness"] > 240.0 |
|
|