Spaces:
Sleeping
Sleeping
fix: enhance audio processing tests with improved validation and error handling
Browse files- tests/test_data_processing.py +65 -12
tests/test_data_processing.py
CHANGED
|
@@ -3,37 +3,90 @@
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import io
|
|
|
|
| 6 |
|
| 7 |
from fastapi.testclient import TestClient
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def test_audio_rejects_non_audio(client: TestClient) -> None:
|
| 11 |
-
"""Should reject non-audio content type."""
|
| 12 |
-
fake = io.BytesIO(b"not audio")
|
| 13 |
response = client.post(
|
| 14 |
"/api/process/audio",
|
| 15 |
-
data={"options":
|
| 16 |
-
files={"file": ("test.txt",
|
| 17 |
)
|
| 18 |
-
assert response.status_code
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
def test_audio_rejects_missing_content_type(client: TestClient) -> None:
|
| 22 |
-
"""Should reject file with
|
| 23 |
-
fake = io.BytesIO(b"some bytes")
|
| 24 |
response = client.post(
|
| 25 |
"/api/process/audio",
|
| 26 |
-
data={"options":
|
| 27 |
-
files={"file": ("test.bin",
|
| 28 |
)
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
def test_audio_rejects_missing_file(client: TestClient) -> None:
|
| 34 |
"""Should reject request without file."""
|
| 35 |
response = client.post(
|
| 36 |
"/api/process/audio",
|
| 37 |
-
data={"options":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import io
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
from fastapi.testclient import TestClient
|
| 9 |
|
| 10 |
|
| 11 |
+
def _valid_options(**overrides: object) -> str:
|
| 12 |
+
"""Return a valid JSON options string with optional overrides."""
|
| 13 |
+
defaults = {
|
| 14 |
+
"pitchShift": False,
|
| 15 |
+
"speedChange": False,
|
| 16 |
+
"bassBoost": False,
|
| 17 |
+
"trimSilence": False,
|
| 18 |
+
"mixAudio": False,
|
| 19 |
+
"addNoise": False,
|
| 20 |
+
}
|
| 21 |
+
defaults.update(overrides)
|
| 22 |
+
return json.dumps(defaults)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _fake_audio(content: bytes = b"\x00" * 1024) -> io.BytesIO:
|
| 26 |
+
return io.BytesIO(content)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_audio_valid_request_accepted(client: TestClient) -> None:
|
| 30 |
+
"""Valid audio file + valid options should not return 422."""
|
| 31 |
+
response = client.post(
|
| 32 |
+
"/api/process/audio",
|
| 33 |
+
data={"options": _valid_options()},
|
| 34 |
+
files={"file": ("test.wav", _fake_audio(), "audio/wav")},
|
| 35 |
+
)
|
| 36 |
+
# Should be processed (200) or a processing error (400/500) — never 422
|
| 37 |
+
assert response.status_code != 422
|
| 38 |
+
|
| 39 |
+
|
| 40 |
def test_audio_rejects_non_audio(client: TestClient) -> None:
|
| 41 |
+
"""Should reject non-audio content type with 400."""
|
|
|
|
| 42 |
response = client.post(
|
| 43 |
"/api/process/audio",
|
| 44 |
+
data={"options": _valid_options()},
|
| 45 |
+
files={"file": ("test.txt", _fake_audio(), "text/plain")},
|
| 46 |
)
|
| 47 |
+
assert response.status_code == 400
|
| 48 |
+
detail = response.json()["detail"]
|
| 49 |
+
assert detail["code"] == "invalid_file_type"
|
| 50 |
|
| 51 |
|
| 52 |
def test_audio_rejects_missing_content_type(client: TestClient) -> None:
|
| 53 |
+
"""Should reject file with empty content type as invalid."""
|
|
|
|
| 54 |
response = client.post(
|
| 55 |
"/api/process/audio",
|
| 56 |
+
data={"options": _valid_options()},
|
| 57 |
+
files={"file": ("test.bin", _fake_audio(), "")},
|
| 58 |
)
|
| 59 |
+
assert response.status_code == 400
|
| 60 |
+
detail = response.json()["detail"]
|
| 61 |
+
assert detail["code"] == "invalid_file_type"
|
| 62 |
|
| 63 |
|
| 64 |
def test_audio_rejects_missing_file(client: TestClient) -> None:
|
| 65 |
"""Should reject request without file."""
|
| 66 |
response = client.post(
|
| 67 |
"/api/process/audio",
|
| 68 |
+
data={"options": _valid_options()},
|
| 69 |
+
)
|
| 70 |
+
assert response.status_code == 422
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_audio_rejects_invalid_options_json(client: TestClient) -> None:
|
| 74 |
+
"""Should reject malformed JSON in options with 422 + invalid_options code."""
|
| 75 |
+
response = client.post(
|
| 76 |
+
"/api/process/audio",
|
| 77 |
+
data={"options": "not-valid-json"},
|
| 78 |
+
files={"file": ("test.wav", _fake_audio(), "audio/wav")},
|
| 79 |
)
|
| 80 |
assert response.status_code == 422
|
| 81 |
+
detail = response.json()["detail"]
|
| 82 |
+
assert detail["code"] == "invalid_options"
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def test_audio_accepts_camel_case_options(client: TestClient) -> None:
|
| 86 |
+
"""Frontend sends camelCase keys — should be accepted."""
|
| 87 |
+
response = client.post(
|
| 88 |
+
"/api/process/audio",
|
| 89 |
+
data={"options": _valid_options(pitchShift=True, addNoise=True)},
|
| 90 |
+
files={"file": ("test.wav", _fake_audio(), "audio/wav")},
|
| 91 |
+
)
|
| 92 |
+
assert response.status_code != 422
|