Skip to content

Commit

Permalink
test: switch to async openai client
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedir Zadniprovskyi authored and fedirz committed Sep 21, 2024
1 parent 4cb006f commit 5cbc876
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
17 changes: 10 additions & 7 deletions tests/api_model_test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import openai
from openai import OpenAI
from openai import AsyncOpenAI
import pytest

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


def test_list_models(openai_client: OpenAI) -> None:
models = openai_client.models.list().data
@pytest.mark.asyncio()
async def test_list_models(openai_client: AsyncOpenAI) -> None:
models = (await openai_client.models.list()).data
assert len(models) > MIN_EXPECTED_NUMBER_OF_MODELS


def test_model_exists(openai_client: OpenAI) -> None:
model = openai_client.models.retrieve(MODEL_THAT_EXISTS)
@pytest.mark.asyncio()
async def test_model_exists(openai_client: AsyncOpenAI) -> None:
model = await openai_client.models.retrieve(MODEL_THAT_EXISTS)
assert model.id == MODEL_THAT_EXISTS


def test_model_does_not_exist(openai_client: OpenAI) -> None:
@pytest.mark.asyncio()
async def test_model_does_not_exist(openai_client: AsyncOpenAI) -> None:
with pytest.raises(openai.NotFoundError):
openai_client.models.retrieve(MODEL_THAT_DOES_NOT_EXIST)
await openai_client.models.retrieve(MODEL_THAT_DOES_NOT_EXIST)
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fastapi.testclient import TestClient
from faster_whisper_server.main import create_app
from httpx import ASGITransport, AsyncClient
from openai import AsyncOpenAI, OpenAI
from openai import AsyncOpenAI
import pytest
import pytest_asyncio

Expand All @@ -32,9 +32,9 @@ async def aclient() -> AsyncGenerator[AsyncClient, None]:
yield aclient


@pytest.fixture()
def openai_client(client: TestClient) -> OpenAI:
return OpenAI(api_key="cant-be-empty", http_client=client)
@pytest_asyncio.fixture()
def openai_client(aclient: AsyncClient) -> AsyncOpenAI:
return AsyncOpenAI(api_key="cant-be-empty", http_client=aclient)


@pytest.fixture()
Expand Down

0 comments on commit 5cbc876

Please sign in to comment.