Spaces:
Build error
Build error
import os | |
from dotenv import load_dotenv | |
load_dotenv() | |
import pytest | |
from fastapi.testclient import TestClient | |
def client(): | |
from app import app | |
clnt = TestClient(app) | |
return clnt | |
def test_embed(client: TestClient, image_path): | |
with open(image_path, "rb") as image_file: | |
response = client.post( | |
"/embed", | |
files=[("image", (os.path.basename(image_path), image_file))], | |
params={"token": os.getenv("CLIENT_TOKEN")}, | |
) | |
assert response.status_code == 200, response.text | |
assert "embedding" in response.json() | |
def test_similarity(client: TestClient, image1_path, image2_path, exp_score): | |
with open(image1_path, "rb") as image1_file, open(image2_path, "rb") as image2_file: | |
response = client.post( | |
"/similarity", | |
files=[ | |
("image1", (os.path.basename(image1_path), image1_file)), | |
("image2", (os.path.basename(image2_path), image2_file)), | |
], | |
params={"token": os.getenv("CLIENT_TOKEN")}, | |
) | |
assert response.status_code == 200 | |
assert "score" in response.json() | |
if exp_score is not None: | |
assert response.json()["score"] == pytest.approx(exp_score, abs=1e-4) | |
def test_authentication(client: TestClient): | |
response = client.post( | |
"/embed", | |
files=[("image", ("face_pic.jpeg", b""))], | |
) | |
assert response.status_code == 401 | |
assert response.json()["detail"] == "No token provided" | |
response = client.post( | |
"/embed", | |
files=[("image", ("face_pic.jpeg", b""))], | |
params={"token": "wrong_token"}, | |
) | |
assert response.status_code == 401 | |
assert response.json()["detail"] == "Invalid token" | |