fix problem from claude
This commit is contained in:
0
ai-moderation/tests/__init__.py
Normal file
0
ai-moderation/tests/__init__.py
Normal file
95
ai-moderation/tests/conftest.py
Normal file
95
ai-moderation/tests/conftest.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import io
|
||||
import struct
|
||||
import zlib
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from PIL import Image
|
||||
|
||||
from app.core.dependencies import (
|
||||
get_image_moderation_service,
|
||||
get_profanity_detector,
|
||||
get_text_moderation_service
|
||||
)
|
||||
from app.main import app
|
||||
from app.ml.image.image_prediction_result import ImagePredictionResult
|
||||
from app.ml.prediction_result import PredictionResult
|
||||
from app.moderation.image.validator import ImageValidator
|
||||
from app.services.text_moderation_service import TextModerationService
|
||||
|
||||
|
||||
def image_bytes(size=(200, 150), fmt="JPEG", mode="RGB", color=(120, 30, 200), **save_kwargs) -> bytes:
|
||||
buffer = io.BytesIO()
|
||||
Image.new(mode, size, color).save(buffer, format=fmt, **save_kwargs)
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def png_header_only(width: int, height: int) -> bytes:
|
||||
"""PNG с огромными размерами в заголовке и пустыми данными — «бомба» для теста."""
|
||||
def chunk(kind: bytes, data: bytes) -> bytes:
|
||||
return (
|
||||
struct.pack(">I", len(data)) + kind + data
|
||||
+ struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF)
|
||||
)
|
||||
|
||||
ihdr = struct.pack(">IIBBBBB", width, height, 8, 0, 0, 0, 0)
|
||||
return (
|
||||
b"\x89PNG\r\n\x1a\n"
|
||||
+ chunk(b"IHDR", ihdr)
|
||||
+ chunk(b"IDAT", zlib.compress(b""))
|
||||
+ chunk(b"IEND", b"")
|
||||
)
|
||||
|
||||
|
||||
class FakeImageService:
|
||||
def __init__(self):
|
||||
self.validator = ImageValidator()
|
||||
self.seen_sizes = []
|
||||
self.error = None
|
||||
|
||||
def moderate(self, image):
|
||||
self.seen_sizes.append(image.size)
|
||||
if self.error:
|
||||
raise self.error
|
||||
return ImagePredictionResult(
|
||||
label="normal",
|
||||
score=1.0,
|
||||
approved=True,
|
||||
reason="OK",
|
||||
detected_labels=[]
|
||||
)
|
||||
|
||||
|
||||
class FakeTextClassifier:
|
||||
def __init__(self, score=0.01):
|
||||
self.score = score
|
||||
|
||||
def predict(self, text):
|
||||
return PredictionResult(
|
||||
label="toxic",
|
||||
score=self.score,
|
||||
approved=False,
|
||||
raw_scores=[1 - self.score, self.score]
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def image_service():
|
||||
return FakeImageService()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def text_classifier():
|
||||
return FakeTextClassifier()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(image_service, text_classifier):
|
||||
app.dependency_overrides[get_image_moderation_service] = lambda: image_service
|
||||
app.dependency_overrides[get_text_moderation_service] = lambda: TextModerationService(
|
||||
classifier=text_classifier,
|
||||
profanity_detector=get_profanity_detector()
|
||||
)
|
||||
# Без «with»: lifespan (загрузка настоящих моделей) не запускается
|
||||
yield TestClient(app, raise_server_exceptions=False)
|
||||
app.dependency_overrides.clear()
|
||||
Reference in New Issue
Block a user