File size: 4,648 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 135 136 | import pytest
from fastapi.testclient import TestClient
from app.main import app
from io import BytesIO
client = TestClient(app)
def test_two_object_size_success():
# Create test image 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"
# Test data
files = [
("garment_images", ("test_image1.jpg", test_image1, "image/jpeg")),
("garment_images", ("test_image2.jpg", test_image2, "image/jpeg"))
]
data = {
"product_height1": 10.0,
"product_width1": 15.0,
"product_length1": 2.0,
"product_height2": 12.0,
"product_width2": 18.0,
"product_length2": 2.5
}
response = client.post("/api/v1/walletsize", files=files, data=data)
assert response.status_code == 200
assert response.headers["content-type"] == "image/png"
def test_two_object_size_invalid_dimensions():
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_height1": "invalid", # Invalid height
"product_width1": 15.0,
"product_length1": 2.0,
"product_height2": 12.0,
"product_width2": 18.0,
"product_length2": 2.5
}
response = client.post("/api/v1/walletsize", files=files, data=data)
assert response.status_code == 400
assert "Invalid dimensions" in response.json()["detail"]
def test_two_object_size_invalid_file_type():
test_file1 = BytesIO(b"not an image 1")
test_file1.name = "test1.txt"
test_file2 = BytesIO(b"not an image 2")
test_file2.name = "test2.txt"
files = [
("garment_images", ("test1.txt", test_file1, "text/plain")),
("garment_images", ("test2.txt", test_file2, "text/plain"))
]
data = {
"product_height1": 10.0,
"product_width1": 15.0,
"product_length1": 2.0,
"product_height2": 12.0,
"product_width2": 18.0,
"product_length2": 2.5
}
response = client.post("/api/v1/walletsize", files=files, data=data)
assert response.status_code == 400
assert "Invalid file type" in response.json()["detail"]
def test_two_object_size_missing_file():
data = {
"product_height1": 10.0,
"product_width1": 15.0,
"product_length1": 2.0,
"product_height2": 12.0,
"product_width2": 18.0,
"product_length2": 2.5
}
response = client.post("/api/v1/walletsize", data=data)
assert response.status_code == 422 # FastAPI validation error
def test_two_object_size_single_file():
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_height1": 10.0,
"product_width1": 15.0,
"product_length1": 2.0,
"product_height2": 12.0,
"product_width2": 18.0,
"product_length2": 2.5
}
response = client.post("/api/v1/walletsize", files=files, data=data)
assert response.status_code == 400
assert "Exactly two front shot images of wallet" in response.json()["detail"]
def test_two_object_size_three_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"
test_image3 = BytesIO(b"fake image data 3")
test_image3.name = "test_image3.jpg"
files = [
("garment_images", ("test_image1.jpg", test_image1, "image/jpeg")),
("garment_images", ("test_image2.jpg", test_image2, "image/jpeg")),
("garment_images", ("test_image3.jpg", test_image3, "image/jpeg"))
]
data = {
"product_height1": 10.0,
"product_width1": 15.0,
"product_length1": 2.0,
"product_height2": 12.0,
"product_width2": 18.0,
"product_length2": 2.5
}
response = client.post("/api/v1/walletsize", files=files, data=data)
assert response.status_code == 400
assert "Exactly two front shot images of wallet" in response.json()["detail"] |