File size: 1,414 Bytes
f5b5ebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Generator

import pytest
from fastapi.testclient import TestClient

from faster_whisper_server.main import app
from faster_whisper_server.server_models import ModelObject

MODEL_THAT_EXISTS = "Systran/faster-whisper-tiny.en"
MODEL_THAT_DOES_NOT_EXIST = "i-do-not-exist"
MIN_EXPECTED_NUMBER_OF_MODELS = (
    200  # At the time of the test creation there are 228 models
)


@pytest.fixture()
def client() -> Generator[TestClient, None, None]:
    with TestClient(app) as client:
        yield client


# HACK: because ModelObject(**data) doesn't work
def model_dict_to_object(model_dict: dict) -> ModelObject:
    return ModelObject(
        id=model_dict["id"],
        created=model_dict["created"],
        object_=model_dict["object"],
        owned_by=model_dict["owned_by"],
    )


def test_list_models(client: TestClient):
    response = client.get("/v1/models")
    data = response.json()
    models = [model_dict_to_object(model_dict) for model_dict in data]
    assert len(models) > MIN_EXPECTED_NUMBER_OF_MODELS


def test_model_exists(client: TestClient):
    response = client.get(f"/v1/model/{MODEL_THAT_EXISTS}")
    data = response.json()
    model = model_dict_to_object(data)
    assert model.id == MODEL_THAT_EXISTS


def test_model_does_not_exist(client: TestClient):
    response = client.get(f"/v1/model/{MODEL_THAT_DOES_NOT_EXIST}")
    assert response.status_code == 404