import os from dotenv import load_dotenv load_dotenv() import pytest from fastapi.testclient import TestClient @pytest.fixture def client(): from app import app clnt = TestClient(app) return clnt @pytest.mark.parametrize( "image_path", [ pytest.param("testdata/face_pic.jpeg", id="happy flow"), ], ) 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() @pytest.mark.parametrize( "image1_path, image2_path, exp_score", [ pytest.param( "testdata/face_pic.jpeg", "testdata/face_pic.jpeg", 0, id="same pic", ), pytest.param( "testdata/face_pic.jpeg", "testdata/face_pic3.jpeg", 0.0588636, id="similar expression", ), pytest.param( "testdata/face_pic.jpeg", "testdata/face_pic2.jpeg", 1.413582, id="different expression", ), ], ) 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) @pytest.mark.parametrize( "token, status_code, detail", [ pytest.param(None, 401, "No token provided", id="no token"), pytest.param("wrong_token", 401, "Invalid token", id="wrong token"), ], ) def test_authentication(client: TestClient, token: str, status_code: int, detail: str): response = client.post( "/embed", files=[("image", ("face_pic.jpeg", b""))], params={"token": token} if token else {}, ) assert response.status_code == status_code assert response.json()["detail"] == detail