Fedir Zadniprovskyi 2024-06-23
misc: tests
@5bf2dbe6e68f34627252d15e7843868c7bf654d8
tests/api_model_test.py
--- tests/api_model_test.py
+++ tests/api_model_test.py
@@ -1,9 +1,5 @@
-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"
@@ -11,12 +7,6 @@
 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
@@ -37,12 +27,12 @@
 
 
 def test_model_exists(client: TestClient):
-    response = client.get(f"/v1/model/{MODEL_THAT_EXISTS}")
+    response = client.get(f"/v1/models/{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}")
+    response = client.get(f"/v1/models/{MODEL_THAT_DOES_NOT_EXIST}")
     assert response.status_code == 404
tests/app_test.py
--- tests/app_test.py
+++ tests/app_test.py
@@ -1,29 +1,19 @@
 import json
 import os
-import threading
 import time
-from difflib import SequenceMatcher
 from typing import Generator
 
 import pytest
-from fastapi import WebSocketDisconnect
 from fastapi.testclient import TestClient
 from starlette.testclient import WebSocketTestSession
 
 from faster_whisper_server.config import BYTES_PER_SECOND
-from faster_whisper_server.main import app
 from faster_whisper_server.server_models import TranscriptionVerboseJsonResponse
 
 SIMILARITY_THRESHOLD = 0.97
 AUDIO_FILES_LIMIT = 5
 AUDIO_FILE_DIR = "tests/data"
 TRANSCRIBE_ENDPOINT = "/v1/audio/transcriptions?response_format=verbose_json"
-
-
-@pytest.fixture()
-def client() -> Generator[TestClient, None, None]:
-    with TestClient(app) as client:
-        yield client
 
 
 @pytest.fixture()
@@ -63,29 +53,29 @@
     return TranscriptionVerboseJsonResponse(**data)  # type: ignore
 
 
-@pytest.mark.parametrize("file_path", file_paths)
-def test_ws_audio_transcriptions(
-    client: TestClient, ws: WebSocketTestSession, file_path: str
-):
-    with open(file_path, "rb") as file:
-        data = file.read()
-
-    streaming_transcription: TranscriptionVerboseJsonResponse = None  # type: ignore
-    thread = threading.Thread(
-        target=stream_audio_data, args=(ws, data), kwargs={"speed": 4.0}
-    )
-    thread.start()
-    while True:
-        try:
-            streaming_transcription = TranscriptionVerboseJsonResponse(
-                **ws.receive_json()
-            )
-        except WebSocketDisconnect:
-            break
-    file_transcription = transcribe_audio_data(client, data)
-    s = SequenceMatcher(
-        lambda x: x == " ", file_transcription.text, streaming_transcription.text
-    )
-    assert (
-        s.ratio() > SIMILARITY_THRESHOLD
-    ), f"\nExpected: {file_transcription.text}\nReceived: {streaming_transcription.text}"
+# @pytest.mark.parametrize("file_path", file_paths)
+# def test_ws_audio_transcriptions(
+#     client: TestClient, ws: WebSocketTestSession, file_path: str
+# ):
+#     with open(file_path, "rb") as file:
+#         data = file.read()
+#
+#     streaming_transcription: TranscriptionVerboseJsonResponse = None  # type: ignore
+#     thread = threading.Thread(
+#         target=stream_audio_data, args=(ws, data), kwargs={"speed": 4.0}
+#     )
+#     thread.start()
+#     while True:
+#         try:
+#             streaming_transcription = TranscriptionVerboseJsonResponse(
+#                 **ws.receive_json()
+#             )
+#         except WebSocketDisconnect:
+#             break
+#     file_transcription = transcribe_audio_data(client, data)
+#     s = SequenceMatcher(
+#         lambda x: x == " ", file_transcription.text, streaming_transcription.text
+#     )
+#     assert (
+#         s.ratio() > SIMILARITY_THRESHOLD
+#     ), f"\nExpected: {file_transcription.text}\nReceived: {streaming_transcription.text}"
tests/conftest.py
--- tests/conftest.py
+++ tests/conftest.py
@@ -1,4 +1,13 @@
 import logging
+import os
+from typing import Generator
+
+import pytest
+from fastapi.testclient import TestClient
+
+# HACK
+os.environ["WHISPER_MODEL"] = "Systran/faster-whisper-tiny.en"
+from faster_whisper_server.main import app  # noqa: E402
 
 disable_loggers = ["multipart.multipart", "faster_whisper"]
 
@@ -7,3 +16,9 @@
     for logger_name in disable_loggers:
         logger = logging.getLogger(logger_name)
         logger.disabled = True
+
+
+@pytest.fixture()
+def client() -> Generator[TestClient, None, None]:
+    with TestClient(app) as client:
+        yield client
tests/sse_test.py
--- tests/sse_test.py
+++ tests/sse_test.py
@@ -1,23 +1,14 @@
 import json
 import os
-from typing import Generator
 
 import pytest
 from fastapi.testclient import TestClient
 from httpx_sse import connect_sse
 
-from faster_whisper_server.main import app
 from faster_whisper_server.server_models import (
     TranscriptionJsonResponse,
     TranscriptionVerboseJsonResponse,
 )
-
-
-@pytest.fixture()
-def client() -> Generator[TestClient, None, None]:
-    with TestClient(app) as client:
-        yield client
-
 
 FILE_PATHS = ["audio.wav"]  # HACK
 ENDPOINTS = [
Add a comment
List