import pytest from fastapi.testclient import TestClient from datetime import datetime from bson import ObjectId from src.models.image import ImageModel from src.db.repositories.image_repository import image_repository # Assuming this exists def test_image_model_properties(): """Test the basic properties of the image model without API dependencies""" team_id = ObjectId() uploader_id = ObjectId() # Create an image model instance 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, description="A test image", tags=["test", "api"], metadata={"width": 800, "height": 600} ) # Check properties 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.description == "A test image" assert "test" in image.tags assert "api" in image.tags assert image.metadata["width"] == 800 assert image.metadata["height"] == 600 assert image.has_embedding is False def test_image_model_embedding_fields(): """Test the embedding-related fields in the image model""" team_id = ObjectId() uploader_id = ObjectId() # Create an image with embedding data 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, embedding_id="embedding123", embedding_model="clip", has_embedding=True ) # Check embedding properties assert image.embedding_id == "embedding123" assert image.embedding_model == "clip" assert image.has_embedding is True # Original API test that will be commented out until we fix the mocking approach """ @pytest.mark.asyncio async def test_list_images(client: TestClient, user_api_key: tuple): # Test the placeholder list images endpoint raw_key, _ = user_api_key # Set up the headers headers = {"X-API-Key": raw_key} # Call the list images endpoint response = client.get( "/api/v1/images", headers=headers ) # This is currently a placeholder endpoint assert response.status_code == 200 data = response.json() assert "message" in data assert "implemented" in data["message"].lower() """ # The following tests are for future implementation of the image API # They are commented out since the endpoints don't exist yet """ @pytest.mark.asyncio async def test_upload_image(client: TestClient, user_api_key: tuple): # Test uploading an image raw_key, _ = user_api_key # Set up the headers headers = {"X-API-Key": raw_key} # Create test image file files = { "file": ("test_image.jpg", open("tests/test_data/test_image.jpg", "rb"), "image/jpeg") } # Upload image response = client.post( "/api/v1/images", headers=headers, files=files, data={ "description": "Test image upload", "tags": "test,upload,image" } ) # Check response assert response.status_code == 201 data = response.json() assert "id" in data assert "filename" in data assert "storage_path" in data assert "team_id" in data assert "uploader_id" in data assert data["description"] == "Test image upload" assert len(data["tags"]) == 3 assert "test" in data["tags"] assert "upload" in data["tags"] assert "image" in data["tags"] @pytest.mark.asyncio async def test_get_image(client: TestClient, user_api_key: tuple): # Test getting image metadata raw_key, api_key = user_api_key # Create a test image in the database 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=api_key.team_id, uploader_id=api_key.user_id, description="A test image", tags=["test", "image"] ) created_image = await image_repository.create(image) # Set up the headers headers = {"X-API-Key": raw_key} # Get the image response = client.get( f"/api/v1/images/{created_image.id}", headers=headers ) # Check response assert response.status_code == 200 data = response.json() assert data["id"] == str(created_image.id) assert data["filename"] == "test-image-123.jpg" assert data["team_id"] == str(api_key.team_id) assert data["uploader_id"] == str(api_key.user_id) assert data["description"] == "A test image" assert "test" in data["tags"] assert "image" in data["tags"] @pytest.mark.asyncio async def test_get_image_download(client: TestClient, user_api_key: tuple): # Test downloading an image raw_key, api_key = user_api_key # Create a test image in the database 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=api_key.team_id, uploader_id=api_key.user_id ) created_image = await image_repository.create(image) # Set up the headers headers = {"X-API-Key": raw_key} # Download the image response = client.get( f"/api/v1/images/{created_image.id}/download", headers=headers ) # Check response assert response.status_code == 200 assert response.headers["Content-Type"] == "image/jpeg" assert response.headers["Content-Disposition"] == f"attachment; filename=test_image.jpg" assert len(response.content) > 0 @pytest.mark.asyncio async def test_delete_image(client: TestClient, user_api_key: tuple): # Test deleting an image raw_key, api_key = user_api_key # Create a test image in the database 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=api_key.team_id, uploader_id=api_key.user_id ) created_image = await image_repository.create(image) # Set up the headers headers = {"X-API-Key": raw_key} # Delete the image response = client.delete( f"/api/v1/images/{created_image.id}", headers=headers ) # Check response assert response.status_code == 204 # Verify the image has been deleted deleted_image = await image_repository.get_by_id(created_image.id) assert deleted_image is None @pytest.mark.asyncio async def test_update_image_metadata(client: TestClient, user_api_key: tuple): # Test updating image metadata raw_key, api_key = user_api_key # Create a test image in the database 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=api_key.team_id, uploader_id=api_key.user_id, description="Original description", tags=["original"] ) created_image = await image_repository.create(image) # Set up the headers headers = {"X-API-Key": raw_key} # Update the image metadata response = client.patch( f"/api/v1/images/{created_image.id}", headers=headers, json={ "description": "Updated description", "tags": ["updated", "metadata"] } ) # Check response assert response.status_code == 200 data = response.json() assert data["id"] == str(created_image.id) assert data["description"] == "Updated description" assert len(data["tags"]) == 2 assert "updated" in data["tags"] assert "metadata" in data["tags"] """