File size: 2,775 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 | import pytest
from fastapi.testclient import TestClient
from app.main import app
from io import BytesIO
client = TestClient(app)
def test_size_comparison_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 = {
"product_height": 30.0,
"product_width": 20.0,
"product_length": 15.0
}
response = client.post("/api/v1/size", files=files, data=data)
assert response.status_code == 200
assert response.headers["content-type"] == "image/png"
def test_size_comparison_invalid_dimensions():
test_image = BytesIO(b"fake image data")
test_image.name = "test_image.jpg"
files = {
"garment_images": ("test_image.jpg", test_image, "image/jpeg")
}
data = {
"product_height": "invalid", # Invalid height
"product_width": 20.0,
"product_length": 15.0
}
response = client.post("/api/v1/size", files=files, data=data)
assert response.status_code == 400
assert "Invalid dimensions" in response.json()["detail"]
def test_size_comparison_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 = {
"product_height": 30.0,
"product_width": 20.0,
"product_length": 15.0
}
response = client.post("/api/v1/size", files=files, data=data)
assert response.status_code == 400
assert "Invalid file type" in response.json()["detail"]
def test_size_comparison_missing_file():
data = {
"product_height": 30.0,
"product_width": 20.0,
"product_length": 15.0
}
response = client.post("/api/v1/size", data=data)
assert response.status_code == 422 # FastAPI validation error
def test_size_comparison_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 = {
"product_height": 30.0,
"product_width": 20.0,
"product_length": 15.0
}
response = client.post("/api/v1/size", files=files, data=data)
assert response.status_code == 400
assert "Exactly one front shot of the image" in response.json()["detail"] |