File size: 2,524 Bytes
b87deef
 
 
 
 
 
 
 
 
 
 
 
4516ae1
b87deef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd7b3bb
b87deef
 
 
 
 
cd7b3bb
b87deef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e92099
 
 
 
 
 
 
 
b87deef
 
 
0e92099
b87deef
0e92099
 
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
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