import pytest from datetime import datetime from bson import ObjectId from pydantic import ValidationError, HttpUrl from src.db.models.image import ImageModel class TestImageModel: def test_create_image(self): """Test creating an image with valid data""" team_id = ObjectId() uploader_id = ObjectId() image_data = { "filename": "test-image-123.jpg", "original_filename": "test_image.jpg", "file_size": 1024, "content_type": "image/jpeg", "storage_path": "images/test-image-123.jpg", "team_id": team_id, "uploader_id": uploader_id } image = ImageModel(**image_data) assert image.filename == "test-image-123.jpg" assert image.original_filename == "test_image.jpg" assert image.file_size == 1024 assert image.content_type == "image/jpeg" assert image.storage_path == "images/test-image-123.jpg" assert image.team_id == team_id assert image.uploader_id == uploader_id assert image.public_url is None assert isinstance(image.upload_date, datetime) assert image.last_accessed is None assert image.description is None assert image.tags == [] assert image.metadata == {} assert image.embedding_id is None assert image.embedding_model is None assert image.has_embedding is False def test_image_with_all_fields(self): """Test creating an image with all fields populated""" team_id = ObjectId() uploader_id = ObjectId() last_accessed = datetime.utcnow() public_url = "https://storage.googleapis.com/bucket/images/test-image-123.jpg" image = ImageModel( filename="test-image-123.jpg", original_filename="test_image.jpg", file_size=1024, content_type="image/jpeg", storage_path="images/test-image-123.jpg", team_id=team_id, uploader_id=uploader_id, public_url=public_url, last_accessed=last_accessed, description="A test image", tags=["test", "image"], metadata={"width": 800, "height": 600}, embedding_id="embedding123", embedding_model="clip", has_embedding=True ) assert str(image.public_url) == public_url assert image.last_accessed == last_accessed assert image.description == "A test image" assert "test" in image.tags assert "image" in image.tags assert image.metadata["width"] == 800 assert image.metadata["height"] == 600 assert image.embedding_id == "embedding123" assert image.embedding_model == "clip" assert image.has_embedding is True def test_image_with_missing_required_fields(self): """Test that required fields must be provided""" team_id = ObjectId() uploader_id = ObjectId() # Missing filename with pytest.raises(ValidationError): ImageModel( original_filename="test_image.jpg", file_size=1024, content_type="image/jpeg", storage_path="images/test-image-123.jpg", team_id=team_id, uploader_id=uploader_id ) # Missing original_filename with pytest.raises(ValidationError): ImageModel( filename="test-image-123.jpg", file_size=1024, content_type="image/jpeg", storage_path="images/test-image-123.jpg", team_id=team_id, uploader_id=uploader_id ) # Other required fields tests... def test_image_with_invalid_public_url(self): """Test that invalid URL is rejected""" with pytest.raises(ValidationError): ImageModel( filename="test-image-123.jpg", original_filename="test_image.jpg", file_size=1024, content_type="image/jpeg", storage_path="images/test-image-123.jpg", team_id=ObjectId(), uploader_id=ObjectId(), public_url="not-a-url" ) def test_image_dict_conversion(self): """Test the image model can be converted to dict with correct field mapping""" team_id = ObjectId() uploader_id = ObjectId() image = ImageModel( filename="test-image-123.jpg", original_filename="test_image.jpg", file_size=1024, content_type="image/jpeg", storage_path="images/test-image-123.jpg", team_id=team_id, uploader_id=uploader_id ) image_dict = image.model_dump(by_alias=True) assert "_id" in image_dict assert "filename" in image_dict assert "original_filename" in image_dict assert "file_size" in image_dict assert "content_type" in image_dict assert "storage_path" in image_dict assert "team_id" in image_dict assert "uploader_id" in image_dict assert str(image_dict["team_id"]) == str(team_id) assert str(image_dict["uploader_id"]) == str(uploader_id)