Rthur2003 commited on
Commit
405f6d3
·
1 Parent(s): 2eb5dd7

test: integration tests için audio processing conversion ve organization uç noktaları eklendi

Browse files
Files changed (1) hide show
  1. tests/test_data_processing.py +65 -0
tests/test_data_processing.py CHANGED
@@ -117,6 +117,71 @@ def test_audio_defaults_when_options_missing(client: TestClient) -> None:
117
  assert response.status_code != 422
118
 
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  def test_audio_rejects_oversized_file_with_413_not_500(client: TestClient) -> None:
121
  """Regression test: the 413 raised inside the read loop's try block was
122
  being caught by the bare `except Exception` below it (HTTPException IS
 
117
  assert response.status_code != 422
118
 
119
 
120
+ def _real_tone_wav(freq: float = 440.0, duration: float = 1.0, sr: int = 22050) -> io.BytesIO:
121
+ """A real sine wave WAV — librosa/soundfile reject an all-zero buffer
122
+ as silence, so mix-audio tests need genuine audio content."""
123
+ import numpy as np
124
+ import soundfile as sf
125
+
126
+ t = np.linspace(0, duration, int(sr * duration))
127
+ y = (0.3 * np.sin(2 * np.pi * freq * t)).astype(np.float32)
128
+ buf = io.BytesIO()
129
+ sf.write(buf, y, sr, format="WAV")
130
+ buf.seek(0)
131
+ return buf
132
+
133
+
134
+ def test_audio_mix_requires_second_file_when_enabled(client: TestClient) -> None:
135
+ """mixAudio: true without a mix_file must 400, not silently ignore it."""
136
+ response = client.post(
137
+ "/api/process/audio",
138
+ data={"options": _valid_options(mixAudio=True)},
139
+ files={"file": ("a.wav", _real_tone_wav(440), "audio/wav")},
140
+ )
141
+ assert response.status_code == 400
142
+ detail = response.json()["detail"]
143
+ assert detail["code"] == "missing_mix_file"
144
+
145
+
146
+ def test_audio_mix_rejects_non_audio_second_file(client: TestClient) -> None:
147
+ response = client.post(
148
+ "/api/process/audio",
149
+ data={"options": _valid_options(mixAudio=True)},
150
+ files={
151
+ "file": ("a.wav", _real_tone_wav(440), "audio/wav"),
152
+ "mix_file": ("b.txt", io.BytesIO(b"not audio"), "text/plain"),
153
+ },
154
+ )
155
+ assert response.status_code == 400
156
+ detail = response.json()["detail"]
157
+ assert detail["code"] == "invalid_file_type"
158
+
159
+
160
+ def test_audio_mix_succeeds_and_blends_both_tracks(client: TestClient) -> None:
161
+ """End-to-end: two distinct real tones, mixAudio on, verify the output
162
+ actually contains spectral energy from BOTH sources."""
163
+ import numpy as np
164
+ import librosa
165
+
166
+ response = client.post(
167
+ "/api/process/audio",
168
+ data={"options": _valid_options(mixAudio=True)},
169
+ files={
170
+ "file": ("a.wav", _real_tone_wav(440), "audio/wav"),
171
+ "mix_file": ("b.wav", _real_tone_wav(880), "audio/wav"),
172
+ },
173
+ )
174
+ assert response.status_code == 200
175
+
176
+ y, sr = librosa.load(io.BytesIO(response.content), sr=22050)
177
+ fft = np.abs(np.fft.rfft(y))
178
+ freqs = np.fft.rfftfreq(len(y), 1 / sr)
179
+ energy_440 = fft[(freqs > 430) & (freqs < 450)].max()
180
+ energy_880 = fft[(freqs > 870) & (freqs < 890)].max()
181
+ assert energy_440 > 5
182
+ assert energy_880 > 5
183
+
184
+
185
  def test_audio_rejects_oversized_file_with_413_not_500(client: TestClient) -> None:
186
  """Regression test: the 413 raised inside the read loop's try block was
187
  being caught by the bare `except Exception` below it (HTTPException IS