VTO / tests /api /v1 /test_tryon.py
Akshajzclap's picture
Upload 89 files
bd55a25 verified
import pytest
from fastapi.testclient import TestClient
from app.main import app
from io import BytesIO
client = TestClient(app)
def test_tryon_success():
# Create a test image file
test_image = BytesIO(b"fake image data")
test_image.name = "test_image.jpg"
# Test data
files = {
"garment_images": ("test_image.jpg", test_image, "image/jpeg")
}
data = {
"garment_type": "dress",
"model_gender": "female",
"model_age_range": "25-35",
"model_body_shape": "athletic",
"model_race_ethnicity": "asian",
"model_pose": "standing",
"camera_view_angle": "front",
"camera_distance_meters": 2.0,
"camera_focal_length_mm": 50.0,
"camera_aperture_f_number": 2.8,
"camera_lighting_condition": "natural",
"camera_background": "studio"
}
response = client.post("/api/v1/tryon", files=files, data=data)
assert response.status_code == 200
assert response.headers["content-type"] == "image/png"
def test_tryon_invalid_model_attributes():
test_image = BytesIO(b"fake image data")
test_image.name = "test_image.jpg"
files = {
"garment_images": ("test_image.jpg", test_image, "image/jpeg")
}
data = {
"garment_type": "invalid_type", # Invalid garment type
"model_gender": "female",
"model_age_range": "25-35",
"model_body_shape": "athletic",
"model_race_ethnicity": "asian",
"model_pose": "standing",
"camera_view_angle": "front",
"camera_distance_meters": 2.0,
"camera_focal_length_mm": 50.0,
"camera_aperture_f_number": 2.8,
"camera_lighting_condition": "natural",
"camera_background": "studio"
}
response = client.post("/api/v1/tryon", files=files, data=data)
assert response.status_code == 400
assert "Invalid model or camera attributes" in response.json()["detail"]
def test_tryon_invalid_file_type():
test_file = BytesIO(b"not an image")
test_file.name = "test.txt"
files = {
"garment_images": ("test.txt", test_file, "text/plain")
}
data = {
"garment_type": "dress",
"model_gender": "female",
"model_age_range": "25-35",
"model_body_shape": "athletic",
"model_race_ethnicity": "asian",
"model_pose": "standing",
"camera_view_angle": "front",
"camera_distance_meters": 2.0,
"camera_focal_length_mm": 50.0,
"camera_aperture_f_number": 2.8,
"camera_lighting_condition": "natural",
"camera_background": "studio"
}
response = client.post("/api/v1/tryon", files=files, data=data)
assert response.status_code == 400
assert "Invalid file type" in response.json()["detail"]
def test_tryon_missing_file():
data = {
"garment_type": "dress",
"model_gender": "female",
"model_age_range": "25-35",
"model_body_shape": "athletic",
"model_race_ethnicity": "asian",
"model_pose": "standing",
"camera_view_angle": "front",
"camera_distance_meters": 2.0,
"camera_focal_length_mm": 50.0,
"camera_aperture_f_number": 2.8,
"camera_lighting_condition": "natural",
"camera_background": "studio"
}
response = client.post("/api/v1/tryon", data=data)
assert response.status_code == 422 # FastAPI validation error
def test_tryon_multiple_files():
test_image1 = BytesIO(b"fake image data 1")
test_image1.name = "test_image1.jpg"
test_image2 = BytesIO(b"fake image data 2")
test_image2.name = "test_image2.jpg"
files = [
("garment_images", ("test_image1.jpg", test_image1, "image/jpeg")),
("garment_images", ("test_image2.jpg", test_image2, "image/jpeg"))
]
data = {
"garment_type": "dress",
"model_gender": "female",
"model_age_range": "25-35",
"model_body_shape": "athletic",
"model_race_ethnicity": "asian",
"model_pose": "standing",
"camera_view_angle": "front",
"camera_distance_meters": 2.0,
"camera_focal_length_mm": 50.0,
"camera_aperture_f_number": 2.8,
"camera_lighting_condition": "natural",
"camera_background": "studio"
}
response = client.post("/api/v1/tryon", files=files, data=data)
assert response.status_code == 400
assert "Exactly one garment images are required" in response.json()["detail"]