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.00026, id="similar expression", ), pytest.param( "testdata/face_pic.jpeg", "testdata/face_pic2.jpeg", 2, 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) 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"