File size: 4,649 Bytes
bd55a25 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | 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"] |